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