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