KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbexpr_functions.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <algorithm>
25#include <cstdio>
26#include <memory>
27#include <mutex>
28
29#include <wx/log.h>
30
31#include <board.h>
35#include <drc/drc_rtree.h>
36#include <drc/drc_engine.h>
37#include <footprint.h>
38#include <lset.h>
39#include <pad.h>
40#include <pcb_track.h>
41#include <pcb_group.h>
43#include <pcbexpr_evaluator.h>
47#include <properties/property.h>
49
50
51bool fromToFunc( LIBEVAL::CONTEXT* aCtx, void* self )
52{
53 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
54 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
56 LIBEVAL::VALUE* argTo = aCtx->Pop();
57 LIBEVAL::VALUE* argFrom = aCtx->Pop();
58
59 result->Set(0.0);
60 aCtx->Push( result );
61
62 if(!item)
63 return false;
64
65 auto ftCache = item->GetBoard()->GetConnectivity()->GetFromToCache();
66
67 if( !ftCache )
68 {
69 wxLogWarning( wxT( "Attempting to call fromTo() with non-existent from-to cache." ) );
70 return true;
71 }
72
73 if( ftCache->IsOnFromToPath( static_cast<BOARD_CONNECTED_ITEM*>( item ),
74 argFrom->AsString(), argTo->AsString() ) )
75 {
76 result->Set(1.0);
77 }
78
79 return true;
80}
81
82
83#define MISSING_LAYER_ARG( f ) wxString::Format( _( "Missing layer name argument to %s." ), f )
84
85static void existsOnLayerFunc( LIBEVAL::CONTEXT* aCtx, void *self )
86{
87 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
88 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
89 LIBEVAL::VALUE* arg = aCtx->Pop();
91
92 result->Set( 0.0 );
93 aCtx->Push( result );
94
95 if( !item )
96 return;
97
98 if( !arg || arg->AsString().IsEmpty() )
99 {
100 if( aCtx->HasErrorCallback() )
101 aCtx->ReportError( MISSING_LAYER_ARG( wxT( "existsOnLayer()" ) ) );
102
103 return;
104 }
105
106 result->SetDeferredEval(
107 [item, arg, aCtx]() -> double
108 {
109 const wxString& layerName = arg->AsString();
110 wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
111
112 if( aCtx->HasErrorCallback())
113 {
114 /*
115 * Interpreted version
116 */
117
118 bool anyMatch = false;
119
120 for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
121 {
122 wxPGChoiceEntry& entry = layerMap[ ii ];
123
124 if( entry.GetText().Matches( layerName ))
125 {
126 anyMatch = true;
127
128 if( item->IsOnLayer( ToLAYER_ID( entry.GetValue() ) ) )
129 return 1.0;
130 }
131 }
132
133 if( !anyMatch )
134 {
135 aCtx->ReportError( wxString::Format( _( "Unrecognized layer '%s'" ),
136 layerName ) );
137 }
138
139 return 0.0;
140 }
141 else
142 {
143 /*
144 * Compiled version
145 */
146
147 BOARD* board = item->GetBoard();
148
149 {
150 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
151
152 auto i = board->m_LayerExpressionCache.find( layerName );
153
154 if( i != board->m_LayerExpressionCache.end() )
155 return ( item->GetLayerSet() & i->second ).any() ? 1.0 : 0.0;
156 }
157
158 LSET mask;
159
160 for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
161 {
162 wxPGChoiceEntry& entry = layerMap[ ii ];
163
164 if( entry.GetText().Matches( layerName ) )
165 mask.set( ToLAYER_ID( entry.GetValue() ) );
166 }
167
168 {
169 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
170 board->m_LayerExpressionCache[ layerName ] = mask;
171 }
172
173 return ( item->GetLayerSet() & mask ).any() ? 1.0 : 0.0;
174 }
175 } );
176}
177
178
179static void isPlatedFunc( LIBEVAL::CONTEXT* aCtx, void* self )
180{
182
183 result->Set( 0.0 );
184 aCtx->Push( result );
185
186 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
187 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
188
189 if( !item )
190 return;
191
192 if( item->Type() == PCB_PAD_T && static_cast<PAD*>( item )->GetAttribute() == PAD_ATTRIB::PTH )
193 result->Set( 1.0 );
194 else if( item->Type() == PCB_VIA_T )
195 result->Set( 1.0 );
196}
197
198
199bool collidesWithCourtyard( BOARD_ITEM* aItem, std::shared_ptr<SHAPE>& aItemShape,
200 PCBEXPR_CONTEXT* aCtx, FOOTPRINT* aFootprint, PCB_LAYER_ID aSide )
201{
202 SHAPE_POLY_SET footprintCourtyard;
203
204 footprintCourtyard = aFootprint->GetCourtyard( aSide );
205
206 if( !aItemShape )
207 {
208 // Since rules are used for zone filling we can't rely on the filled shapes.
209 // Use the zone outline instead.
210 if( ZONE* zone = dynamic_cast<ZONE*>( aItem ) )
211 aItemShape.reset( zone->Outline()->Clone() );
212 else
213 aItemShape = aItem->GetEffectiveShape( aCtx->GetLayer() );
214 }
215
216 return footprintCourtyard.Collide( aItemShape.get() );
217};
218
219
220static bool testFootprintSelector( FOOTPRINT* aFp, const wxString& aSelector )
221{
222 // NOTE: This code may want to be somewhat more generalized, but for now it's implemented
223 // here to support functions like insersectsCourtyard where we want multiple ways to search
224 // for the footprints in question.
225 // If support for text variable replacement is added, it should happen before any other
226 // logic here, so that people can use text variables to contain references or LIBIDs.
227 // (see: https://gitlab.com/kicad/code/kicad/-/issues/11231)
228
229 if( aSelector.IsEmpty() )
230 return false;
231
232 // First check if we have a known directive
233 if( aSelector[0] == '$' && aSelector.Last() == '}' && aSelector.Upper().StartsWith( wxT( "${CLASS:" ) ) )
234 {
235 wxString name = aSelector.Mid( 8, aSelector.Length() - 9 );
236
237 const COMPONENT_CLASS* compClass = aFp->GetComponentClass();
238
239 if( compClass && compClass->ContainsClassName( name ) )
240 return true;
241 }
242 else if( aFp->GetReference().Matches( aSelector ) )
243 {
244 return true;
245 }
246 else if( aSelector.Find( ':' ) != wxNOT_FOUND && aFp->GetFPIDAsString().Matches( aSelector ) )
247 {
248 return true;
249 }
250
251 return false;
252}
253
254
255static bool searchFootprints( BOARD* aBoard, const wxString& aArg, PCBEXPR_CONTEXT* aCtx,
256 const std::function<bool( FOOTPRINT* )>& aFunc )
257{
258 if( aArg == wxT( "A" ) )
259 {
260 FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( aCtx->GetItem( 0 ) );
261
262 if( fp && aFunc( fp ) )
263 return true;
264 }
265 else if( aArg == wxT( "B" ) )
266 {
267 FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( aCtx->GetItem( 1 ) );
268
269 if( fp && aFunc( fp ) )
270 return true;
271 }
272 else for( FOOTPRINT* fp : aBoard->Footprints() )
273 {
274 if( testFootprintSelector( fp, aArg ) && aFunc( fp ) )
275 return true;
276 }
277
278 return false;
279}
280
281
282#define MISSING_FP_ARG( f ) \
283 wxString::Format( _( "Missing footprint argument (A, B, or reference designator) to %s." ), f )
284
285static void intersectsCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
286{
287 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
288 LIBEVAL::VALUE* arg = context->Pop();
289 LIBEVAL::VALUE* result = context->AllocValue();
290
291 result->Set( 0.0 );
292 context->Push( result );
293
294 if( !arg || arg->AsString().IsEmpty() )
295 {
296 if( context->HasErrorCallback() )
297 context->ReportError( MISSING_FP_ARG( wxT( "intersectsCourtyard()" ) ) );
298
299 return;
300 }
301
302 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
303 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
304
305 if( !item )
306 return;
307
308 result->SetDeferredEval(
309 [item, arg, context]() -> double
310 {
311 BOARD* board = item->GetBoard();
312 std::shared_ptr<SHAPE> itemShape;
313
314 if( searchFootprints( board, arg->AsString(), context,
315 [&]( FOOTPRINT* fp )
316 {
317 PTR_PTR_CACHE_KEY key = { fp, item };
318
319 if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
320 {
321 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
322
323 auto i = board->m_IntersectsCourtyardCache.find( key );
324
325 if( i != board->m_IntersectsCourtyardCache.end() )
326 return i->second;
327 }
328
329 bool res = collidesWithCourtyard( item, itemShape, context, fp, F_Cu )
330 || collidesWithCourtyard( item, itemShape, context, fp, B_Cu );
331
332 if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
333 {
334 std::unique_lock<std::shared_mutex> cacheLock( board->m_CachesMutex );
335 board->m_IntersectsCourtyardCache[ key ] = res;
336 }
337
338 return res;
339 } ) )
340 {
341 return 1.0;
342 }
343
344 return 0.0;
345 } );
346}
347
348
349static void intersectsFrontCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
350{
351 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
352 LIBEVAL::VALUE* arg = context->Pop();
353 LIBEVAL::VALUE* result = context->AllocValue();
354
355 result->Set( 0.0 );
356 context->Push( result );
357
358 if( !arg || arg->AsString().IsEmpty() )
359 {
360 if( context->HasErrorCallback() )
361 context->ReportError( MISSING_FP_ARG( wxT( "intersectsFrontCourtyard()" ) ) );
362
363 return;
364 }
365
366 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
367 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
368
369 if( !item )
370 return;
371
372 result->SetDeferredEval(
373 [item, arg, context]() -> double
374 {
375 BOARD* board = item->GetBoard();
376 std::shared_ptr<SHAPE> itemShape;
377
378 if( searchFootprints( board, arg->AsString(), context,
379 [&]( FOOTPRINT* fp )
380 {
381 PTR_PTR_CACHE_KEY key = { fp, item };
382
383 if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
384 {
385 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
386
387 auto i = board->m_IntersectsFCourtyardCache.find( key );
388
389 if( i != board->m_IntersectsFCourtyardCache.end() )
390 return i->second;
391 }
392
393 PCB_LAYER_ID layerId = fp->IsFlipped() ? B_Cu : F_Cu;
394
395 bool res = collidesWithCourtyard( item, itemShape, context, fp, layerId );
396
397 if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
398 {
399 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
400 board->m_IntersectsFCourtyardCache[ key ] = res;
401 }
402
403 return res;
404 } ) )
405 {
406 return 1.0;
407 }
408
409 return 0.0;
410 } );
411}
412
413
414static void intersectsBackCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
415{
416 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
417 LIBEVAL::VALUE* arg = context->Pop();
418 LIBEVAL::VALUE* result = context->AllocValue();
419
420 result->Set( 0.0 );
421 context->Push( result );
422
423 if( !arg || arg->AsString().IsEmpty() )
424 {
425 if( context->HasErrorCallback() )
426 context->ReportError( MISSING_FP_ARG( wxT( "intersectsBackCourtyard()" ) ) );
427
428 return;
429 }
430
431 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
432 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
433
434 if( !item )
435 return;
436
437 result->SetDeferredEval(
438 [item, arg, context]() -> double
439 {
440 BOARD* board = item->GetBoard();
441 std::shared_ptr<SHAPE> itemShape;
442
443 if( searchFootprints( board, arg->AsString(), context,
444 [&]( FOOTPRINT* fp )
445 {
446 PTR_PTR_CACHE_KEY key = { fp, item };
447
448 if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
449 {
450 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
451
452 auto i = board->m_IntersectsBCourtyardCache.find( key );
453
454 if( i != board->m_IntersectsBCourtyardCache.end() )
455 return i->second;
456 }
457
458 PCB_LAYER_ID layerId = fp->IsFlipped() ? F_Cu : B_Cu;
459
460 bool res = collidesWithCourtyard( item, itemShape, context, fp, layerId );
461
462 if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
463 {
464 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
465 board->m_IntersectsBCourtyardCache[ key ] = res;
466 }
467
468 return res;
469 } ) )
470 {
471 return 1.0;
472 }
473
474 return 0.0;
475 } );
476}
477
478
480{
481 // Check cache first with read lock
482 {
483 std::shared_lock<std::shared_mutex> readLock( aBoard->m_CachesMutex );
484 auto it = aBoard->m_DeflatedZoneOutlineCache.find( aArea );
485
486 if( it != aBoard->m_DeflatedZoneOutlineCache.end() )
487 return it->second;
488 }
489
490 // Cache miss - compute deflated outline
491 SHAPE_POLY_SET areaOutline = aArea->Outline()->CloneDropTriangulation();
492 areaOutline.ClearArcs();
493 areaOutline.Deflate( aBoard->GetDesignSettings().GetDRCEpsilon(),
495
496 // Store in cache
497 {
498 std::unique_lock<std::shared_mutex> writeLock( aBoard->m_CachesMutex );
499 aBoard->m_DeflatedZoneOutlineCache[aArea] = areaOutline;
500 }
501
502 return areaOutline;
503}
504
505
506bool collidesWithArea( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, PCBEXPR_CONTEXT* aCtx, ZONE* aArea )
507{
508 BOARD* board = aArea->GetBoard();
509 BOX2I areaBBox = aArea->GetBoundingBox();
510
511 // Get cached deflated outline. Collisions include touching, so we need to deflate outline
512 // by enough to exclude it. This is particularly important for detecting copper fills as
513 // they will be exactly touching along the entire exclusion border.
514 SHAPE_POLY_SET areaOutline = getDeflatedZoneOutline( board, aArea );
515
516 if( aItem->GetFlags() & HOLE_PROXY )
517 {
518 if( aItem->Type() == PCB_PAD_T )
519 {
520 return areaOutline.Collide( aItem->GetEffectiveHoleShape().get() );
521 }
522 else if( aItem->Type() == PCB_VIA_T )
523 {
524 LSET overlap = aItem->GetLayerSet() & aArea->GetLayerSet();
525
527 if( overlap.any() )
528 {
529 if( aCtx->GetLayer() == UNDEFINED_LAYER || overlap.Contains( aCtx->GetLayer() ) )
530 return areaOutline.Collide( aItem->GetEffectiveHoleShape().get() );
531 }
532 }
533
534 return false;
535 }
536
537 if( aItem->Type() == PCB_FOOTPRINT_T )
538 {
539 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aItem );
540
541 if( ( footprint->GetFlags() & MALFORMED_COURTYARDS ) != 0 )
542 {
543 if( aCtx->HasErrorCallback() )
544 aCtx->ReportError( _( "Footprint's courtyard is not a single, closed shape." ) );
545
546 return false;
547 }
548
549 if( ( aArea->GetLayerSet() & LSET::FrontMask() ).any() )
550 {
551 const SHAPE_POLY_SET& courtyard = footprint->GetCourtyard( F_CrtYd );
552
553 if( courtyard.OutlineCount() == 0 )
554 {
555 if( aCtx->HasErrorCallback() )
556 aCtx->ReportError( _( "Footprint has no front courtyard." ) );
557 }
558 else if( areaOutline.Collide( &courtyard.Outline( 0 ) ) )
559 {
560 return true;
561 }
562 }
563
564 if( ( aArea->GetLayerSet() & LSET::BackMask() ).any() )
565 {
566 const SHAPE_POLY_SET& courtyard = footprint->GetCourtyard( B_CrtYd );
567
568 if( courtyard.OutlineCount() == 0 )
569 {
570 if( aCtx->HasErrorCallback() )
571 aCtx->ReportError( _( "Footprint has no back courtyard." ) );
572 }
573 else if( areaOutline.Collide( &courtyard.Outline( 0 ) ) )
574 {
575 return true;
576 }
577 }
578
579 return false;
580 }
581
582 if( aItem->Type() == PCB_ZONE_T )
583 {
584 ZONE* zone = static_cast<ZONE*>( aItem );
585
586 if( !zone->IsFilled() )
587 return false;
588
589 DRC_RTREE* zoneRTree = board->m_CopperZoneRTreeCache[ zone ].get();
590
591 if( zoneRTree )
592 {
593 if( zoneRTree->QueryColliding( areaBBox, &areaOutline, aLayer ) )
594 return true;
595 }
596
597 return false;
598 }
599 else
600 {
601 if( !aArea->GetLayerSet().Contains( aLayer ) )
602 return false;
603
604 return areaOutline.Collide( aItem->GetEffectiveShape( aLayer ).get() );
605 }
606}
607
608
609bool searchAreas( BOARD* aBoard, const wxString& aArg, PCBEXPR_CONTEXT* aCtx,
610 const std::function<bool( ZONE* )>& aFunc )
611{
612 if( aArg == wxT( "A" ) )
613 {
614 return aFunc( dynamic_cast<ZONE*>( aCtx->GetItem( 0 ) ) );
615 }
616 else if( aArg == wxT( "B" ) )
617 {
618 return aFunc( dynamic_cast<ZONE*>( aCtx->GetItem( 1 ) ) );
619 }
620 else if( KIID::SniffTest( aArg ) )
621 {
622 KIID target( aArg );
623
624 // Use the board's item-by-ID cache for O(1) lookup instead of O(n) iteration.
625 // The cache includes both board zones and zones inside footprints.
626 const auto& cache = aBoard->GetItemByIdCache();
627 auto it = cache.find( target );
628
629 if( it != cache.end() && it->second->Type() == PCB_ZONE_T )
630 return aFunc( static_cast<ZONE*>( it->second ) );
631
632 return false;
633 }
634 else // Match on zone name
635 {
636 // Use cached zone name lookup to avoid O(n) iteration through all zones for each call.
637 // This is a significant performance improvement for boards with many area-based DRC rules.
638 std::vector<ZONE*> matchingZones;
639 bool cacheHit = false;
640
641 {
642 std::shared_lock<std::shared_mutex> readLock( aBoard->m_CachesMutex );
643 auto it = aBoard->m_ZonesByNameCache.find( aArg );
644
645 if( it != aBoard->m_ZonesByNameCache.end() )
646 {
647 matchingZones = it->second;
648 cacheHit = true;
649 }
650 }
651
652 if( !cacheHit )
653 {
654 for( ZONE* area : aBoard->Zones() )
655 {
656 if( area->GetZoneName().Matches( aArg ) )
657 matchingZones.push_back( area );
658 }
659
660 for( FOOTPRINT* footprint : aBoard->Footprints() )
661 {
662 for( ZONE* area : footprint->Zones() )
663 {
664 if( area->GetZoneName().Matches( aArg ) )
665 matchingZones.push_back( area );
666 }
667 }
668
669 // Store in cache for future lookups
670 {
671 std::unique_lock<std::shared_mutex> writeLock( aBoard->m_CachesMutex );
672 aBoard->m_ZonesByNameCache[aArg] = matchingZones;
673 }
674 }
675
676 for( ZONE* area : matchingZones )
677 {
678 if( aFunc( area ) )
679 return true;
680 }
681
682 return false;
683 }
684}
685
686
688{
689public:
691 {
692 m_item = aItem;
693 m_layers = aItem->GetLayerSet();
694 }
695
697 {
698 m_item->SetLayerSet( m_layers );
699 }
700
701 void Add( PCB_LAYER_ID aLayer )
702 {
703 m_item->SetLayerSet( m_item->GetLayerSet().set( aLayer ) );
704 }
705
706private:
709};
710
711
712#define MISSING_AREA_ARG( f ) \
713 wxString::Format( _( "Missing rule-area argument (A, B, or rule-area name) to %s." ), f )
714
715static void intersectsAreaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
716{
717 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
718 LIBEVAL::VALUE* arg = aCtx->Pop();
720
721 result->Set( 0.0 );
722 aCtx->Push( result );
723
724 if( !arg || arg->AsString().IsEmpty() )
725 {
726 if( aCtx->HasErrorCallback() )
727 aCtx->ReportError( MISSING_AREA_ARG( wxT( "intersectsArea()" ) ) );
728
729 return;
730 }
731
732 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
733 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
734
735 if( !item )
736 return;
737
738 result->SetDeferredEval(
739 [item, arg, context]() -> double
740 {
741 BOARD* board = item->GetBoard();
742 PCB_LAYER_ID aLayer = context->GetLayer();
743 BOX2I itemBBox = item->GetBoundingBox();
744
745 if( searchAreas( board, arg->AsString(), context,
746 [&]( ZONE* aArea )
747 {
748 if( !aArea || aArea == item || aArea->GetParent() == item )
749 return false;
750
751 SCOPED_LAYERSET scopedLayerSet( aArea );
752
753 if( context->GetConstraint() == SILK_CLEARANCE_CONSTRAINT )
754 {
755 // Silk clearance tests are run across layer pairs
756 if( ( aArea->IsOnLayer( F_SilkS ) && IsFrontLayer( aLayer ) )
757 || ( aArea->IsOnLayer( B_SilkS ) && IsBackLayer( aLayer ) ) )
758 {
759 scopedLayerSet.Add( aLayer );
760 }
761 }
762
763 LSET commonLayers = aArea->GetLayerSet() & item->GetLayerSet();
764
765 if( !commonLayers.any() )
766 return false;
767
768 if( !aArea->GetBoundingBox().Intersects( itemBBox ) )
769 return false;
770
771 LSET testLayers;
772
773 if( aLayer != UNDEFINED_LAYER )
774 testLayers.set( aLayer );
775 else
776 testLayers = commonLayers;
777
778 bool isTransient = ( item->GetFlags() & ROUTER_TRANSIENT ) != 0;
779 std::vector<PCB_LAYER_ID> layersToCompute;
780
781 if( !isTransient )
782 {
783 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
784
785 for( PCB_LAYER_ID layer : testLayers.UIOrder() )
786 {
787 PTR_PTR_LAYER_CACHE_KEY key = { aArea, item, layer };
788 auto i = board->m_IntersectsAreaCache.find( key );
789
790 if( i != board->m_IntersectsAreaCache.end() )
791 {
792 if( i->second )
793 return true;
794 }
795 else
796 {
797 layersToCompute.push_back( layer );
798 }
799 }
800 }
801 else
802 {
803 for( PCB_LAYER_ID layer : testLayers.UIOrder() )
804 layersToCompute.push_back( layer );
805 }
806
807 std::vector<std::pair<PTR_PTR_LAYER_CACHE_KEY, bool>> results;
808 bool anyCollision = false;
809
810 for( PCB_LAYER_ID layer : layersToCompute )
811 {
812 bool collides = collidesWithArea( item, layer, context, aArea );
813
814 if( !isTransient )
815 results.push_back( { { aArea, item, layer }, collides } );
816
817 if( collides )
818 anyCollision = true;
819 }
820
821 if( !isTransient && !results.empty() )
822 {
823 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
824
825 for( const auto& [key, collides] : results )
826 board->m_IntersectsAreaCache[key] = collides;
827 }
828
829 return anyCollision;
830 } ) )
831 {
832 return 1.0;
833 }
834
835 return 0.0;
836 } );
837}
838
839
840static void enclosedByAreaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
841{
842 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
843 LIBEVAL::VALUE* arg = aCtx->Pop();
845
846 result->Set( 0.0 );
847 aCtx->Push( result );
848
849 if( !arg || arg->AsString().IsEmpty() )
850 {
851 if( aCtx->HasErrorCallback() )
852 aCtx->ReportError( MISSING_AREA_ARG( wxT( "enclosedByArea()" ) ) );
853
854 return;
855 }
856
857 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
858 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
859
860 if( !item )
861 return;
862
863 result->SetDeferredEval(
864 [item, arg, context]() -> double
865 {
866 BOARD* board = item->GetBoard();
867 int maxError = board->GetDesignSettings().m_MaxError;
868 PCB_LAYER_ID layer = context->GetLayer();
869 BOX2I itemBBox = item->GetBoundingBox();
870
871 if( searchAreas( board, arg->AsString(), context,
872 [&]( ZONE* aArea )
873 {
874 if( !aArea || aArea == item || aArea->GetParent() == item )
875 return false;
876
877 if( item->Type() != PCB_FOOTPRINT_T )
878 {
879 if( !( aArea->GetLayerSet() & item->GetLayerSet() ).any() )
880 return false;
881 }
882
883 if( !aArea->GetBoundingBox().Intersects( itemBBox ) )
884 return false;
885
886 PTR_PTR_LAYER_CACHE_KEY key = { aArea, item, layer };
887
888 if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
889 {
890 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
891
892 auto i = board->m_EnclosedByAreaCache.find( key );
893
894 if( i != board->m_EnclosedByAreaCache.end() )
895 return i->second;
896 }
897
898 SHAPE_POLY_SET itemShape;
899 bool enclosedByArea;
900
901 if( item->Type() == PCB_ZONE_T )
902 {
903 itemShape = *static_cast<ZONE*>( item )->Outline();
904 }
905 else if( item->Type() == PCB_FOOTPRINT_T )
906 {
907 FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
908
909 for( PCB_LAYER_ID testLayer : aArea->GetLayerSet() )
910 {
911 fp->TransformPadsToPolySet( itemShape, testLayer, 0,
912 maxError, ERROR_OUTSIDE );
913 fp->TransformFPShapesToPolySet( itemShape, testLayer, 0,
914 maxError, ERROR_OUTSIDE );
915 }
916 }
917 else
918 {
919 item->TransformShapeToPolygon( itemShape, layer, 0, maxError,
921 }
922
923 if( itemShape.IsEmpty() )
924 {
925 // If it's already empty then our test will have no meaning.
926 enclosedByArea = false;
927 }
928 else
929 {
930 itemShape.BooleanSubtract( *aArea->Outline() );
931
932 enclosedByArea = itemShape.IsEmpty();
933 }
934
935 if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
936 {
937 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
938 board->m_EnclosedByAreaCache[ key ] = enclosedByArea;
939 }
940
941 return enclosedByArea;
942 } ) )
943 {
944 return 1.0;
945 }
946
947 return 0.0;
948 } );
949}
950
951
952#define MISSING_GROUP_ARG( f ) \
953 wxString::Format( _( "Missing group name argument to %s." ), f )
954
955static void memberOfGroupFunc( LIBEVAL::CONTEXT* aCtx, void* self )
956{
957 LIBEVAL::VALUE* arg = aCtx->Pop();
959
960 result->Set( 0.0 );
961 aCtx->Push( result );
962
963 if( !arg || arg->AsString().IsEmpty() )
964 {
965 if( aCtx->HasErrorCallback() )
966 aCtx->ReportError( MISSING_GROUP_ARG( wxT( "memberOfGroup()" ) ) );
967
968 return;
969 }
970
971 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
972 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
973
974 if( !item )
975 return;
976
977 result->SetDeferredEval(
978 [item, arg]() -> double
979 {
980 EDA_GROUP* group = item->GetParentGroup();
981
982 if( !group && item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
983 group = item->GetParent()->GetParentGroup();
984
985 while( group )
986 {
987 if( group->GetName().Matches( arg->AsString() ) )
988 return 1.0;
989
990 group = group->AsEdaItem()->GetParentGroup();
991 }
992
993 return 0.0;
994 } );
995}
996
997
998#define MISSING_SHEET_ARG( f ) \
999 wxString::Format( _( "Missing sheet name argument to %s." ), f )
1000
1001static void memberOfSheetFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1002{
1003 LIBEVAL::VALUE* arg = aCtx->Pop();
1004 LIBEVAL::VALUE* result = aCtx->AllocValue();
1005
1006 result->Set( 0.0 );
1007 aCtx->Push( result );
1008
1009 if( !arg || arg->AsString().IsEmpty() )
1010 {
1011 if( aCtx->HasErrorCallback() )
1012 aCtx->ReportError( MISSING_SHEET_ARG( wxT( "memberOfSheet()" ) ) );
1013
1014 return;
1015 }
1016
1017 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1018 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1019
1020 if( !item )
1021 return;
1022
1023 result->SetDeferredEval(
1024 [item, arg]() -> double
1025 {
1026 FOOTPRINT* fp = item->GetParentFootprint();
1027
1028 if( !fp && item->Type() == PCB_FOOTPRINT_T )
1029 fp = static_cast<FOOTPRINT*>( item );
1030
1031 if( !fp )
1032 return 0.0;
1033
1034 wxString sheetName = fp->GetSheetname();
1035 wxString refName = arg->AsString();
1036
1037 if( sheetName.EndsWith( wxT( "/" ) ) )
1038 sheetName.RemoveLast();
1039 if( refName.EndsWith( wxT( "/" ) ) )
1040 refName.RemoveLast();
1041
1042 if( sheetName.Matches( refName ) )
1043 return 1.0;
1044
1045 if( ( refName.Matches( wxT( "/" ) ) || refName.IsEmpty() )
1046 && sheetName.IsEmpty() )
1047 {
1048 return 1.0;
1049 }
1050
1051 return 0.0;
1052 } );
1053}
1054
1055
1056static void memberOfSheetOrChildrenFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1057{
1058 LIBEVAL::VALUE* arg = aCtx->Pop();
1059 LIBEVAL::VALUE* result = aCtx->AllocValue();
1060
1061 result->Set( 0.0 );
1062 aCtx->Push( result );
1063
1064 if( !arg || arg->AsString().IsEmpty() )
1065 {
1066 if( aCtx->HasErrorCallback() )
1067 aCtx->ReportError( MISSING_SHEET_ARG( wxT( "memberOfSheetOrChildren()" ) ) );
1068
1069 return;
1070 }
1071
1072 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1073 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1074
1075 if( !item )
1076 return;
1077
1078 result->SetDeferredEval(
1079 [item, arg]() -> double
1080 {
1081 FOOTPRINT* fp = item->GetParentFootprint();
1082
1083 if( !fp && item->Type() == PCB_FOOTPRINT_T )
1084 fp = static_cast<FOOTPRINT*>( item );
1085
1086 if( !fp )
1087 return 0.0;
1088
1089 wxString sheetName = fp->GetSheetname();
1090 wxString refName = arg->AsString();
1091
1092 if( sheetName.EndsWith( wxT( "/" ) ) )
1093 sheetName.RemoveLast();
1094 if( refName.EndsWith( wxT( "/" ) ) )
1095 refName.RemoveLast();
1096
1097 wxArrayString sheetPath = wxSplit( sheetName, '/' );
1098 wxArrayString refPath = wxSplit( refName, '/' );
1099
1100 if( refPath.size() > sheetPath.size() )
1101 return 0.0;
1102
1103 if( ( refName.Matches( wxT( "/" ) ) || refName.IsEmpty() ) && sheetName.IsEmpty() )
1104 {
1105 return 1.0;
1106 }
1107
1108 for( size_t i = 0; i < refPath.size(); i++ )
1109 {
1110 if( !sheetPath[i].Matches( refPath[i] ) )
1111 return 0.0;
1112 }
1113
1114 return 1.0;
1115 } );
1116}
1117
1118
1119#define MISSING_REF_ARG( f ) \
1120 wxString::Format( _( "Missing footprint argument (reference designator) to %s." ), f )
1121
1122static void memberOfFootprintFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1123{
1124 LIBEVAL::VALUE* arg = aCtx->Pop();
1125 LIBEVAL::VALUE* result = aCtx->AllocValue();
1126
1127 result->Set( 0.0 );
1128 aCtx->Push( result );
1129
1130 if( !arg || arg->AsString().IsEmpty() )
1131 {
1132 if( aCtx->HasErrorCallback() )
1133 aCtx->ReportError( MISSING_REF_ARG( wxT( "memberOfFootprint()" ) ) );
1134
1135 return;
1136 }
1137
1138 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1139 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1140
1141 if( !item )
1142 return;
1143
1144 result->SetDeferredEval(
1145 [item, arg]() -> double
1146 {
1147 if( FOOTPRINT* parentFP = item->GetParentFootprint() )
1148 {
1149 if( testFootprintSelector( parentFP, arg->AsString() ) )
1150 return 1.0;
1151 }
1152
1153 return 0.0;
1154 } );
1155}
1156
1157
1158static void isMicroVia( LIBEVAL::CONTEXT* aCtx, void* self )
1159{
1160 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1161 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1162 LIBEVAL::VALUE* result = aCtx->AllocValue();
1163
1164 result->Set( 0.0 );
1165 aCtx->Push( result );
1166
1167 if( item && item->Type() == PCB_VIA_T && static_cast<PCB_VIA*>( item )->IsMicroVia() )
1168 result->Set( 1.0 );
1169}
1170
1171static void isBlindVia( LIBEVAL::CONTEXT* aCtx, void* self )
1172{
1173 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1174 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1175 LIBEVAL::VALUE* result = aCtx->AllocValue();
1176
1177 result->Set( 0.0 );
1178 aCtx->Push( result );
1179
1180 if( item && item->Type() == PCB_VIA_T && static_cast<PCB_VIA*>( item )->IsBlindVia() )
1181 result->Set( 1.0 );
1182}
1183
1184static void isBuriedVia( LIBEVAL::CONTEXT* aCtx, void* self )
1185{
1186 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1187 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1188 LIBEVAL::VALUE* result = aCtx->AllocValue();
1189
1190 result->Set( 0.0 );
1191 aCtx->Push( result );
1192
1193 if( item && item->Type() == PCB_VIA_T && static_cast<PCB_VIA*>( item )->IsBuriedVia() )
1194 result->Set( 1.0 );
1195}
1196
1197static void isBlindBuriedViaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1198{
1199 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1200 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1201 LIBEVAL::VALUE* result = aCtx->AllocValue();
1202
1203 result->Set( 0.0 );
1204 aCtx->Push( result );
1205
1206 if( item && item->Type() == PCB_VIA_T )
1207 {
1208 PCB_VIA* via = static_cast<PCB_VIA*>( item );
1209
1210 if( via->IsBlindVia() || via->IsBuriedVia() )
1211 result->Set( 1.0 );
1212 }
1213}
1214
1215
1216static void isCoupledDiffPairFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1217{
1218 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
1219 BOARD_CONNECTED_ITEM* a = dynamic_cast<BOARD_CONNECTED_ITEM*>( context->GetItem( 0 ) );
1220 BOARD_CONNECTED_ITEM* b = dynamic_cast<BOARD_CONNECTED_ITEM*>( context->GetItem( 1 ) );
1221 LIBEVAL::VALUE* result = aCtx->AllocValue();
1222
1223 result->Set( 0.0 );
1224 aCtx->Push( result );
1225
1226 result->SetDeferredEval(
1227 [a, b, context]() -> double
1228 {
1229 NETINFO_ITEM* netinfo = a ? a->GetNet() : nullptr;
1230
1231 if( !netinfo )
1232 return 0.0;
1233
1234 wxString coupledNet;
1235 wxString dummy;
1236
1237 if( !DRC_ENGINE::MatchDpSuffix( netinfo->GetNetname(), coupledNet, dummy ) )
1238 return 0.0;
1239
1244 {
1245 // DRC engine evaluates these only in the context of a diffpair, but doesn't
1246 // always supply the second (B) item.
1247 if( BOARD* board = a->GetBoard() )
1248 {
1249 if( board->FindNet( coupledNet ) )
1250 return 1.0;
1251 }
1252 }
1253
1254 if( b && b->GetNetname() == coupledNet )
1255 return 1.0;
1256
1257 return 0.0;
1258 } );
1259}
1260
1261
1262#define MISSING_DP_ARG( f ) \
1263 wxString::Format( _( "Missing diff-pair name argument to %s." ), f )
1264
1265static void inDiffPairFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1266{
1267 LIBEVAL::VALUE* argv = aCtx->Pop();
1268 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1269 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1270 LIBEVAL::VALUE* result = aCtx->AllocValue();
1271
1272 result->Set( 0.0 );
1273 aCtx->Push( result );
1274
1275 if( !argv || argv->AsString().IsEmpty() )
1276 {
1277 if( aCtx->HasErrorCallback() )
1278 aCtx->ReportError( MISSING_DP_ARG( wxT( "inDiffPair()" ) ) );
1279
1280 return;
1281 }
1282
1283 if( !item || !item->GetBoard() )
1284 return;
1285
1286 result->SetDeferredEval(
1287 [item, argv]() -> double
1288 {
1289 if( item && item->IsConnected() )
1290 {
1291 NETINFO_ITEM* netinfo = static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
1292
1293 if( !netinfo )
1294 return 0.0;
1295
1296 wxString refName = netinfo->GetNetname();
1297 wxString arg = argv->AsString();
1298 wxString baseName, coupledNet;
1299 int polarity = DRC_ENGINE::MatchDpSuffix( refName, coupledNet, baseName );
1300
1301 if( polarity != 0 && item->GetBoard()->FindNet( coupledNet ) )
1302 {
1303 if( baseName.Matches( arg ) )
1304 return 1.0;
1305
1306 if( baseName.EndsWith( "_" ) && baseName.BeforeLast( '_' ).Matches( arg ) )
1307 return 1.0;
1308 }
1309 }
1310
1311 return 0.0;
1312 } );
1313}
1314
1315
1316static void inNetChainFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1317{
1318 LIBEVAL::VALUE* argv = aCtx->Pop();
1319 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1320 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1321 LIBEVAL::VALUE* result = aCtx->AllocValue();
1322
1323 result->Set( 0.0 );
1324 aCtx->Push( result );
1325
1326 if( !argv || argv->AsString().IsEmpty() )
1327 {
1328 if( aCtx->HasErrorCallback() )
1329 aCtx->ReportError( wxString::Format(
1330 _( "Missing argument to '%s'" ), wxT( "inNetChain()" ) ) );
1331
1332 return;
1333 }
1334
1335 if( !item || !item->GetBoard() )
1336 return;
1337
1338 result->SetDeferredEval(
1339 [item, argv]() -> double
1340 {
1341 if( !item || !item->IsConnected() )
1342 return 0.0;
1343
1344 NETINFO_ITEM* netinfo =
1345 static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
1346
1347 if( !netinfo )
1348 return 0.0;
1349
1350 const wxString& chainName = netinfo->GetNetChain();
1351
1352 if( chainName.IsEmpty() )
1353 return 0.0;
1354
1355 wxString arg = argv->AsString();
1356
1357 return chainName.Matches( arg ) ? 1.0 : 0.0;
1358 } );
1359}
1360
1361
1362static void hasNetChainFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1363{
1364 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1365 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1366 LIBEVAL::VALUE* result = aCtx->AllocValue();
1367
1368 result->Set( 0.0 );
1369 aCtx->Push( result );
1370
1371 if( !item || !item->GetBoard() )
1372 return;
1373
1374 result->SetDeferredEval(
1375 [item]() -> double
1376 {
1377 if( !item || !item->IsConnected() )
1378 return 0.0;
1379
1380 NETINFO_ITEM* netinfo =
1381 static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
1382
1383 return ( netinfo && !netinfo->GetNetChain().IsEmpty() ) ? 1.0 : 0.0;
1384 } );
1385}
1386
1387
1388static void inNetChainClassFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1389{
1390 LIBEVAL::VALUE* argv = aCtx->Pop();
1391 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1392 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1393 LIBEVAL::VALUE* result = aCtx->AllocValue();
1394
1395 result->Set( 0.0 );
1396 aCtx->Push( result );
1397
1398 if( !argv || argv->AsString().IsEmpty() )
1399 {
1400 if( aCtx->HasErrorCallback() )
1401 aCtx->ReportError( wxString::Format(
1402 _( "Missing argument to '%s'" ), wxT( "inNetChainClass()" ) ) );
1403
1404 return;
1405 }
1406
1407 if( !item || !item->GetBoard() )
1408 return;
1409
1410 result->SetDeferredEval(
1411 [item, argv]() -> double
1412 {
1413 if( !item || !item->IsConnected() )
1414 return 0.0;
1415
1416 NETINFO_ITEM* netinfo =
1417 static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
1418
1419 if( !netinfo )
1420 return 0.0;
1421
1422 const wxString& chainName = netinfo->GetNetChain();
1423
1424 if( chainName.IsEmpty() )
1425 return 0.0;
1426
1427 if( BOARD* board = item->GetBoard() )
1428 {
1429 std::shared_ptr<NET_SETTINGS> ns = board->GetDesignSettings().m_NetSettings;
1430
1431 if( ns )
1432 {
1433 const wxString& className = ns->GetNetChainClass( chainName );
1434
1435 if( className.IsEmpty() )
1436 return 0.0;
1437
1438 wxString arg = argv->AsString();
1439
1440 return className.Matches( arg ) ? 1.0 : 0.0;
1441 }
1442 }
1443
1444 return 0.0;
1445 } );
1446}
1447
1448
1449static void getFieldFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1450{
1451 LIBEVAL::VALUE* arg = aCtx->Pop();
1452 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1453 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1454 LIBEVAL::VALUE* result = aCtx->AllocValue();
1455
1456 result->Set( "" );
1457 aCtx->Push( result );
1458
1459 if( !arg )
1460 {
1461 if( aCtx->HasErrorCallback() )
1462 {
1463 aCtx->ReportError( wxString::Format( _( "Missing field name argument to %s." ),
1464 wxT( "getField()" ) ) );
1465 }
1466
1467 return;
1468 }
1469
1470 if( !item || !item->GetBoard() )
1471 return;
1472
1473 result->SetDeferredEval(
1474 [item, arg]() -> wxString
1475 {
1476 if( item && item->Type() == PCB_FOOTPRINT_T )
1477 {
1478 FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
1479
1480 PCB_FIELD* field = fp->GetField( arg->AsString() );
1481
1482 if( field )
1483 return field->GetText();
1484 }
1485
1486 return "";
1487 } );
1488}
1489
1490
1491static void hasNetclassFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1492{
1493 LIBEVAL::VALUE* arg = aCtx->Pop();
1494 LIBEVAL::VALUE* result = aCtx->AllocValue();
1495
1496 result->Set( 0.0 );
1497 aCtx->Push( result );
1498
1499 if( !arg || arg->AsString().IsEmpty() )
1500 {
1501 if( aCtx->HasErrorCallback() )
1502 aCtx->ReportError( _( "Missing netclass name argument to hasNetclass()" ) );
1503
1504 return;
1505 }
1506
1507 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1508 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1509
1510 if( !item )
1511 return;
1512
1513 result->SetDeferredEval(
1514 [item, arg]() -> double
1515 {
1516 if( !item->IsConnected() )
1517 return 0.0;
1518
1519 BOARD_CONNECTED_ITEM* bcItem = static_cast<BOARD_CONNECTED_ITEM*>( item );
1520 NETCLASS* netclass = bcItem->GetEffectiveNetClass();
1521
1522 if( netclass && netclass->ContainsNetclassWithName( arg->AsString() ) )
1523 return 1.0;
1524
1525 return 0.0;
1526 } );
1527}
1528
1529
1530static void hasExactNetclassFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1531{
1532 LIBEVAL::VALUE* arg = aCtx->Pop();
1533 LIBEVAL::VALUE* result = aCtx->AllocValue();
1534
1535 result->Set( 0.0 );
1536 aCtx->Push( result );
1537
1538 if( !arg || arg->AsString().IsEmpty() )
1539 {
1540 if( aCtx->HasErrorCallback() )
1541 aCtx->ReportError( _( "Missing netclass name argument to hasExactNetclass()" ) );
1542
1543 return;
1544 }
1545
1546 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1547 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1548
1549 if( !item )
1550 return;
1551
1552 result->SetDeferredEval(
1553 [item, arg]() -> double
1554 {
1555 if( !item->IsConnected() )
1556 return 0.0;
1557
1558 BOARD_CONNECTED_ITEM* bcItem = static_cast<BOARD_CONNECTED_ITEM*>( item );
1559 BOARD* board = bcItem->GetBoard();
1560 wxString netclassName;
1561
1562 if( board && ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
1563 {
1564 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
1565
1566 auto it = board->m_ItemNetclassCache.find( item );
1567
1568 if( it != board->m_ItemNetclassCache.end() )
1569 netclassName = it->second;
1570 }
1571
1572 if( netclassName.empty() )
1573 {
1574 NETCLASS* netclass = bcItem->GetEffectiveNetClass();
1575
1576 if( netclass )
1577 netclassName = netclass->GetName();
1578
1579 if( board && !netclassName.empty() && ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
1580 {
1581 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
1582 board->m_ItemNetclassCache[item] = netclassName;
1583 }
1584 }
1585
1586 return ( netclassName == arg->AsString() ) ? 1.0 : 0.0;
1587 } );
1588}
1589
1590
1591static void hasComponentClassFunc( LIBEVAL::CONTEXT* aCtx, void* self )
1592{
1593 LIBEVAL::VALUE* arg = aCtx->Pop();
1594 LIBEVAL::VALUE* result = aCtx->AllocValue();
1595
1596 result->Set( 0.0 );
1597 aCtx->Push( result );
1598
1599 if( !arg || arg->AsString().IsEmpty() )
1600 {
1601 if( aCtx->HasErrorCallback() )
1602 aCtx->ReportError( _( "Missing component class name argument to hasComponentClass()" ) );
1603
1604 return;
1605 }
1606
1607 PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
1608 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
1609
1610 if( !item )
1611 return;
1612
1613 result->SetDeferredEval(
1614 [item, arg]() -> double
1615 {
1616 FOOTPRINT* footprint = nullptr;
1617
1618 if( item->Type() == PCB_FOOTPRINT_T )
1619 footprint = static_cast<FOOTPRINT*>( item );
1620 else
1621 footprint = item->GetParentFootprint();
1622
1623 if( !footprint )
1624 return 0.0;
1625
1626 const COMPONENT_CLASS* compClass = footprint->GetComponentClass();
1627
1628 if( compClass && compClass->ContainsClassName( arg->AsString() ) )
1629 return 1.0;
1630
1631 return 0.0;
1632 } );
1633}
1634
1635
1640
1641
1643{
1644 m_funcs.clear();
1645
1646 RegisterFunc( wxT( "existsOnLayer('x')" ), existsOnLayerFunc );
1647
1648 RegisterFunc( wxT( "isPlated()" ), isPlatedFunc );
1649
1650 // Geometry-dependent functions depend on item position/shape rather than item properties.
1651 // The third argument marks them so that CreateFuncCall() can detect them automatically.
1652 RegisterFunc( wxT( "insideCourtyard('x') DEPRECATED" ), intersectsCourtyardFunc, true );
1653 RegisterFunc( wxT( "insideFrontCourtyard('x') DEPRECATED" ), intersectsFrontCourtyardFunc, true );
1654 RegisterFunc( wxT( "insideBackCourtyard('x') DEPRECATED" ), intersectsBackCourtyardFunc, true );
1655 RegisterFunc( wxT( "intersectsCourtyard('x')" ), intersectsCourtyardFunc, true );
1656 RegisterFunc( wxT( "intersectsFrontCourtyard('x')" ), intersectsFrontCourtyardFunc, true );
1657 RegisterFunc( wxT( "intersectsBackCourtyard('x')" ), intersectsBackCourtyardFunc, true );
1658
1659 RegisterFunc( wxT( "insideArea('x') DEPRECATED" ), intersectsAreaFunc, true );
1660 RegisterFunc( wxT( "intersectsArea('x')" ), intersectsAreaFunc, true );
1661 RegisterFunc( wxT( "enclosedByArea('x')" ), enclosedByAreaFunc, true );
1662
1663 RegisterFunc( wxT( "isMicroVia()" ), isMicroVia );
1664 RegisterFunc( wxT( "isBlindVia()" ), isBlindVia );
1665 RegisterFunc( wxT( "isBuriedVia()" ), isBuriedVia );
1666 RegisterFunc( wxT( "isBlindBuriedVia()" ), isBlindBuriedViaFunc );
1667
1668 RegisterFunc( wxT( "memberOf('x') DEPRECATED" ), memberOfGroupFunc );
1669 RegisterFunc( wxT( "memberOfGroup('x')" ), memberOfGroupFunc );
1670 RegisterFunc( wxT( "memberOfFootprint('x')" ), memberOfFootprintFunc );
1671 RegisterFunc( wxT( "memberOfSheet('x')" ), memberOfSheetFunc );
1672 RegisterFunc( wxT( "memberOfSheetOrChildren('x')" ), memberOfSheetOrChildrenFunc );
1673
1674 RegisterFunc( wxT( "fromTo('x','y')" ), fromToFunc );
1675 RegisterFunc( wxT( "isCoupledDiffPair()" ), isCoupledDiffPairFunc );
1676 RegisterFunc( wxT( "inDiffPair('x')" ), inDiffPairFunc );
1677 RegisterFunc( wxT( "inNetChain('x')" ), inNetChainFunc );
1678 RegisterFunc( wxT( "hasNetChain()" ), hasNetChainFunc );
1679 RegisterFunc( wxT( "inNetChainClass('x')" ), inNetChainClassFunc );
1680
1681 RegisterFunc( wxT( "getField('x')" ), getFieldFunc );
1682
1683 RegisterFunc( wxT( "hasNetclass('x')" ), hasNetclassFunc );
1684 RegisterFunc( wxT( "hasExactNetclass('x')" ), hasExactNetclassFunc );
1685 RegisterFunc( wxT( "hasComponentClass('x')" ), hasComponentClassFunc );
1686}
const char * name
@ ERROR_OUTSIDE
constexpr int ARC_LOW_DEF
Definition base_units.h:140
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
BASE_SET & set(size_t pos)
Definition base_set.h:116
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
virtual NETCLASS * GetEffectiveNetClass() const
Return the NETCLASS for this item.
NETINFO_ITEM * GetNet() const
Return #NET_INFO object for a given item.
int GetDRCEpsilon() const
Return an epsilon which accounts for rounding errors, etc.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition board_item.h:158
virtual void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, PCB_LAYER_ID aLayer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const
Convert the item shape to a closed polygon.
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition board_item.h:350
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
FOOTPRINT * GetParentFootprint() const
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:288
BOARD_ITEM_CONTAINER * GetParent() const
Definition board_item.h:234
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
std::unordered_map< const BOARD_ITEM *, wxString > m_ItemNetclassCache
Definition board.h:1579
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition board.cpp:2606
const ZONES & Zones() const
Definition board.h:368
const FOOTPRINTS & Footprints() const
Definition board.h:364
std::unordered_map< wxString, LSET > m_LayerExpressionCache
Definition board.h:1573
std::unordered_map< const ZONE *, SHAPE_POLY_SET > m_DeflatedZoneOutlineCache
Definition board.h:1587
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition board.h:1574
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1101
std::shared_mutex m_CachesMutex
Definition board.h:1567
const std::unordered_map< KIID, BOARD_ITEM * > & GetItemByIdCache() const
Definition board.h:1454
std::unordered_map< wxString, std::vector< ZONE * > > m_ZonesByNameCache
Definition board.h:1583
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition board.h:571
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
A lightweight representation of a component class.
bool ContainsClassName(const wxString &className) const
Determines if this (effective) component class contains a specific constituent class.
std::shared_ptr< FROM_TO_CACHE > GetFromToCache()
static int MatchDpSuffix(const wxString &aNetName, wxString &aComplementNet, wxString &aBaseDpName)
Check if the given net is a diff pair, returning its polarity and complement if so.
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition drc_rtree.h:49
int QueryColliding(BOARD_ITEM *aRefItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer, std::function< bool(BOARD_ITEM *)> aFilter=nullptr, std::function< bool(BOARD_ITEM *)> aVisitor=nullptr, int aClearance=0) const
This is a fast test which essentially does bounding-box overlap given a worst-case clearance.
Definition drc_rtree.h:229
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:46
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:120
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:118
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:152
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:114
static ENUM_MAP< T > & Instance()
Definition property.h:721
wxString GetSheetname() const
Definition footprint.h:455
PCB_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this footprint.
const COMPONENT_CLASS * GetComponentClass() const
Returns the component class for this footprint.
wxString GetFPIDAsString() const
Definition footprint.h:435
bool IsFlipped() const
Definition footprint.h:602
const wxString & GetReference() const
Definition footprint.h:829
const SHAPE_POLY_SET & GetCourtyard(PCB_LAYER_ID aLayer) const
Used in DRC to test the courtyard area (a complex polygon).
Definition kiid.h:48
static bool SniffTest(const wxString &aCandidate)
Returns true if a string has the correct formatting to be a KIID.
Definition kiid.cpp:176
void ReportError(const wxString &aErrorMsg)
void Push(VALUE *v)
virtual const wxString & AsString() const
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & FrontMask()
Return a mask holding all technical layers and the external CU layer on front side.
Definition lset.cpp:722
static const LSET & BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition lset.cpp:729
bool Contains(PCB_LAYER_ID aLayer) const
See if the layer set contains a PCB layer.
Definition lset.h:63
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:42
bool ContainsNetclassWithName(const wxString &netclass) const
Determines if the given netclass name is a constituent of this (maybe aggregate) netclass.
Definition netclass.cpp:284
const wxString GetName() const
Gets the name of this (maybe aggregate) netclass in a format for internal usage or for export to exte...
Definition netclass.cpp:328
Handle the data for a net.
Definition netinfo.h:50
const wxString & GetNetChain() const
Definition netinfo.h:115
const wxString & GetNetname() const
Definition netinfo.h:103
Definition pad.h:55
PAD_ATTRIB GetAttribute() const
Definition pad.h:563
std::map< wxString, LIBEVAL::FUNC_CALL_REF > m_funcs
void RegisterFunc(const wxString &funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr, bool aIsGeometryDependent=false)
int GetConstraint() const
PCB_LAYER_ID GetLayer() const
BOARD_ITEM * GetItem(int index) const
BOARD_ITEM * GetObject(const LIBEVAL::CONTEXT *aCtx) const
bool IsBlindVia() const
bool IsBuriedVia() const
bool IsMicroVia() const
void Add(PCB_LAYER_ID aLayer)
SCOPED_LAYERSET(BOARD_ITEM *aItem)
Represent a set of closed polygons.
void ClearArcs()
Removes all arc references from all the outlines and holes in the polyset.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
int OutlineCount() const
Return the number of outlines in the set.
SHAPE_POLY_SET CloneDropTriangulation() const
Handle a list of polygons defining a copper zone.
Definition zone.h:74
const BOX2I GetBoundingBox() const override
Definition zone.cpp:741
bool IsFilled() const
Definition zone.h:310
SHAPE_POLY_SET * Outline()
Definition zone.h:422
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:137
@ ALLOW_ACUTE_CORNERS
just inflate the polygon. Acute angles create spikes
@ DIFF_PAIR_GAP_CONSTRAINT
Definition drc_rule.h:82
@ NET_CHAIN_LENGTH_CONSTRAINT
Definition drc_rule.h:78
@ LENGTH_CONSTRAINT
Definition drc_rule.h:77
@ SKEW_CONSTRAINT
Definition drc_rule.h:81
#define _(s)
#define ROUTER_TRANSIENT
transient items that should NOT be cached
#define HOLE_PROXY
Indicates the BOARD_ITEM is a proxy for its hole.
#define MALFORMED_COURTYARDS
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ F_CrtYd
Definition layer_ids.h:116
@ B_Cu
Definition layer_ids.h:65
@ B_CrtYd
Definition layer_ids.h:115
@ UNDEFINED_LAYER
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:64
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:754
@ PTH
Plated through hole pad.
Definition padstack.h:98
Class to handle a set of BOARD_ITEMs.
static void intersectsFrontCourtyardFunc(LIBEVAL::CONTEXT *aCtx, void *self)
#define MISSING_SHEET_ARG(f)
bool collidesWithCourtyard(BOARD_ITEM *aItem, std::shared_ptr< SHAPE > &aItemShape, PCBEXPR_CONTEXT *aCtx, FOOTPRINT *aFootprint, PCB_LAYER_ID aSide)
#define MISSING_LAYER_ARG(f)
static SHAPE_POLY_SET getDeflatedZoneOutline(BOARD *aBoard, ZONE *aArea)
static void intersectsBackCourtyardFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void memberOfGroupFunc(LIBEVAL::CONTEXT *aCtx, void *self)
#define MISSING_AREA_ARG(f)
static void isCoupledDiffPairFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void isPlatedFunc(LIBEVAL::CONTEXT *aCtx, void *self)
bool searchAreas(BOARD *aBoard, const wxString &aArg, PCBEXPR_CONTEXT *aCtx, const std::function< bool(ZONE *)> &aFunc)
static void isBuriedVia(LIBEVAL::CONTEXT *aCtx, void *self)
static void existsOnLayerFunc(LIBEVAL::CONTEXT *aCtx, void *self)
#define MISSING_GROUP_ARG(f)
static bool testFootprintSelector(FOOTPRINT *aFp, const wxString &aSelector)
static void isBlindBuriedViaFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void memberOfSheetFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void hasComponentClassFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void isBlindVia(LIBEVAL::CONTEXT *aCtx, void *self)
static void hasExactNetclassFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void inNetChainFunc(LIBEVAL::CONTEXT *aCtx, void *self)
#define MISSING_REF_ARG(f)
#define MISSING_DP_ARG(f)
static void enclosedByAreaFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void memberOfSheetOrChildrenFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void memberOfFootprintFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void hasNetChainFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void isMicroVia(LIBEVAL::CONTEXT *aCtx, void *self)
static void getFieldFunc(LIBEVAL::CONTEXT *aCtx, void *self)
bool collidesWithArea(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, PCBEXPR_CONTEXT *aCtx, ZONE *aArea)
static void hasNetclassFunc(LIBEVAL::CONTEXT *aCtx, void *self)
bool fromToFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void intersectsCourtyardFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void inNetChainClassFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static bool searchFootprints(BOARD *aBoard, const wxString &aArg, PCBEXPR_CONTEXT *aCtx, const std::function< bool(FOOTPRINT *)> &aFunc)
static void inDiffPairFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void intersectsAreaFunc(LIBEVAL::CONTEXT *aCtx, void *self)
#define MISSING_FP_ARG(f)
std::vector< FAB_LAYER_COLOR > dummy
VECTOR3I res
wxString result
Test unit parsing edge cases and error handling.
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:94
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:105
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:83
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:84