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