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