KiCad PCB EDA Suite
pcb_expr_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 (C) 2019-2022 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
25#include <cstdio>
26#include <memory>
27#include <board.h>
29#include <drc/drc_rtree.h>
30#include <drc/drc_engine.h>
31#include <pcb_track.h>
32#include <pcb_group.h>
34#include <pcb_expr_evaluator.h>
35
39
40
41bool fromToFunc( LIBEVAL::CONTEXT* aCtx, void* self )
42{
43 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
44 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
45 LIBEVAL::VALUE* result = aCtx->AllocValue();
46
47 LIBEVAL::VALUE* argTo = aCtx->Pop();
48 LIBEVAL::VALUE* argFrom = aCtx->Pop();
49
50 result->Set(0.0);
51 aCtx->Push( result );
52
53 if(!item)
54 return false;
55
56 auto ftCache = item->GetBoard()->GetConnectivity()->GetFromToCache();
57
58 if( !ftCache )
59 {
60 wxLogWarning( wxT( "Attempting to call fromTo() with non-existent from-to cache." ) );
61 return true;
62 }
63
64 if( ftCache->IsOnFromToPath( static_cast<BOARD_CONNECTED_ITEM*>( item ),
65 argFrom->AsString(), argTo->AsString() ) )
66 {
67 result->Set(1.0);
68 }
69
70 return true;
71}
72
73
74#define MISSING_LAYER_ARG( f ) wxString::Format( _( "Missing layer name argument to %s." ), f )
75
76static void existsOnLayerFunc( LIBEVAL::CONTEXT* aCtx, void *self )
77{
78 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
79 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
80
81 LIBEVAL::VALUE* arg = aCtx->Pop();
82 LIBEVAL::VALUE* result = aCtx->AllocValue();
83
84 result->Set( 0.0 );
85 aCtx->Push( result );
86
87 if( !item )
88 return;
89
90 if( !arg )
91 {
92 if( aCtx->HasErrorCallback() )
93 aCtx->ReportError( MISSING_LAYER_ARG( wxT( "existsOnLayer()" ) ) );
94
95 return;
96 }
97
98 result->SetDeferredEval(
99 [item, arg, aCtx]() -> double
100 {
101 const wxString& layerName = arg->AsString();
102 wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
103
104 if( aCtx->HasErrorCallback())
105 {
106 /*
107 * Interpreted version
108 */
109
110 bool anyMatch = false;
111
112 for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
113 {
114 wxPGChoiceEntry& entry = layerMap[ ii ];
115
116 if( entry.GetText().Matches( layerName ))
117 {
118 anyMatch = true;
119
120 if( item->IsOnLayer( ToLAYER_ID( entry.GetValue() ), true ) )
121 return 1.0;
122 }
123 }
124
125 if( !anyMatch )
126 {
127 aCtx->ReportError( wxString::Format( _( "Unrecognized layer '%s'" ),
128 layerName ) );
129 }
130 }
131 else
132 {
133 /*
134 * Compiled version
135 */
136
137 BOARD* board = item->GetBoard();
138 std::unique_lock<std::mutex> cacheLock( board->m_CachesMutex );
139 auto i = board->m_LayerExpressionCache.find( layerName );
140 LSET mask;
141
142 if( i == board->m_LayerExpressionCache.end() )
143 {
144 for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
145 {
146 wxPGChoiceEntry& entry = layerMap[ ii ];
147
148 if( entry.GetText().Matches( layerName ) )
149 mask.set( ToLAYER_ID( entry.GetValue() ) );
150 }
151
152 board->m_LayerExpressionCache[ layerName ] = mask;
153 }
154 else
155 {
156 mask = i->second;
157 }
158
159 if( ( item->GetLayerSet() & mask ).any() )
160 return 1.0;
161 }
162
163 return 0.0;
164 } );
165}
166
167
168static void isPlatedFunc( LIBEVAL::CONTEXT* aCtx, void* self )
169{
170 LIBEVAL::VALUE* result = aCtx->AllocValue();
171
172 result->Set( 0.0 );
173 aCtx->Push( result );
174
175 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
176 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
177
178 if( !item )
179 return;
180
181 if( item->Type() == PCB_PAD_T && static_cast<PAD*>( item )->GetAttribute() == PAD_ATTRIB::PTH )
182 result->Set( 1.0 );
183 else if( item->Type() == PCB_VIA_T )
184 result->Set( 1.0 );
185}
186
187
188bool collidesWithCourtyard( BOARD_ITEM* aItem, std::shared_ptr<SHAPE>& aItemShape,
189 PCB_EXPR_CONTEXT* aCtx, FOOTPRINT* aFootprint, PCB_LAYER_ID aSide )
190{
191 SHAPE_POLY_SET footprintCourtyard;
192
193 footprintCourtyard = aFootprint->GetCourtyard( aSide );
194
195 if( !aItemShape )
196 {
197 // Since rules are used for zone filling we can't rely on the filled shapes.
198 // Use the zone outline instead.
199 if( ZONE* zone = dynamic_cast<ZONE*>( aItem ) )
200 aItemShape.reset( zone->Outline()->Clone() );
201 else
202 aItemShape = aItem->GetEffectiveShape( aCtx->GetLayer() );
203 }
204
205 return footprintCourtyard.Collide( aItemShape.get() );
206};
207
208
209static bool searchFootprints( BOARD* aBoard, const wxString& aArg, PCB_EXPR_CONTEXT* aCtx,
210 std::function<bool( FOOTPRINT* )> aFunc )
211{
212 if( aArg == wxT( "A" ) )
213 {
214 FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( aCtx->GetItem( 0 ) );
215
216 if( fp && aFunc( fp ) )
217 return 1.0;
218 }
219 else if( aArg == wxT( "B" ) )
220 {
221 FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( aCtx->GetItem( 1 ) );
222
223 if( fp && aFunc( fp ) )
224 return 1.0;
225 }
226 else for( FOOTPRINT* fp : aBoard->Footprints() )
227 {
228 if( fp->GetReference().Matches( aArg ) )
229 {
230 if( aFunc( fp ) )
231 return 1.0;
232 }
233 }
234
235 return 0.0;
236}
237
238
239#define MISSING_FP_ARG( f ) \
240 wxString::Format( _( "Missing footprint argument (A, B, or reference designator) to %s." ), f )
241
242static void intersectsCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
243{
244 PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
245 LIBEVAL::VALUE* arg = context->Pop();
246 LIBEVAL::VALUE* result = context->AllocValue();
247
248 result->Set( 0.0 );
249 context->Push( result );
250
251 if( !arg )
252 {
253 if( context->HasErrorCallback() )
254 context->ReportError( MISSING_FP_ARG( wxT( "intersectsCourtyard()" ) ) );
255
256 return;
257 }
258
259 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
260 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
261
262 if( !item )
263 return;
264
265 result->SetDeferredEval(
266 [item, arg, context]() -> double
267 {
268 BOARD* board = item->GetBoard();
269 std::shared_ptr<SHAPE> itemShape;
270
271 if( searchFootprints( board, arg->AsString(), context,
272 [&]( FOOTPRINT* fp )
273 {
274 PTR_PTR_CACHE_KEY key = { fp, item };
275 std::unique_lock<std::mutex> cacheLock( board->m_CachesMutex );
276
277 auto i = board->m_IntersectsCourtyardCache.find( key );
278
279 if( i != board->m_IntersectsCourtyardCache.end() )
280 return i->second;
281
282 bool res = collidesWithCourtyard( item, itemShape, context, fp, F_Cu )
283 || collidesWithCourtyard( item, itemShape, context, fp, B_Cu );
284
285 board->m_IntersectsCourtyardCache[ key ] = res;
286 return res;
287 } ) )
288 {
289 return 1.0;
290 }
291
292 return 0.0;
293 } );
294}
295
296
297static void intersectsFrontCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
298{
299 PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
300 LIBEVAL::VALUE* arg = context->Pop();
301 LIBEVAL::VALUE* result = context->AllocValue();
302
303 result->Set( 0.0 );
304 context->Push( result );
305
306 if( !arg )
307 {
308 if( context->HasErrorCallback() )
309 context->ReportError( MISSING_FP_ARG( wxT( "intersectsFrontCourtyard()" ) ) );
310
311 return;
312 }
313
314 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
315 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
316
317 if( !item )
318 return;
319
320 result->SetDeferredEval(
321 [item, arg, context]() -> double
322 {
323 BOARD* board = item->GetBoard();
324 std::shared_ptr<SHAPE> itemShape;
325
326 if( searchFootprints( board, arg->AsString(), context,
327 [&]( FOOTPRINT* fp )
328 {
329 PTR_PTR_CACHE_KEY key = { fp, item };
330 std::unique_lock<std::mutex> cacheLock( board->m_CachesMutex );
331
332 auto i = board->m_IntersectsFCourtyardCache.find( key );
333
334 if( i != board->m_IntersectsFCourtyardCache.end() )
335 return i->second;
336
337 bool res = collidesWithCourtyard( item, itemShape, context, fp, F_Cu );
338
339 board->m_IntersectsFCourtyardCache[ key ] = res;
340 return res;
341 } ) )
342 {
343 return 1.0;
344 }
345
346 return 0.0;
347 } );
348}
349
350
351static void intersectsBackCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
352{
353 PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
354 LIBEVAL::VALUE* arg = context->Pop();
355 LIBEVAL::VALUE* result = context->AllocValue();
356
357 result->Set( 0.0 );
358 context->Push( result );
359
360 if( !arg )
361 {
362 if( context->HasErrorCallback() )
363 context->ReportError( MISSING_FP_ARG( wxT( "intersectsBackCourtyard()" ) ) );
364
365 return;
366 }
367
368 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
369 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
370
371 if( !item )
372 return;
373
374 result->SetDeferredEval(
375 [item, arg, context]() -> double
376 {
377 BOARD* board = item->GetBoard();
378 std::shared_ptr<SHAPE> itemShape;
379
380 if( searchFootprints( board, arg->AsString(), context,
381 [&]( FOOTPRINT* fp )
382 {
383 PTR_PTR_CACHE_KEY key = { fp, item };
384 std::unique_lock<std::mutex> cacheLock( board->m_CachesMutex );
385
386 auto i = board->m_IntersectsBCourtyardCache.find( key );
387
388 if( i != board->m_IntersectsBCourtyardCache.end() )
389 return i->second;
390
391 bool res = collidesWithCourtyard( item, itemShape, context, fp, B_Cu );
392
393 board->m_IntersectsBCourtyardCache[ key ] = res;
394 return res;
395 } ) )
396 {
397 return 1.0;
398 }
399
400 return 0.0;
401 } );
402}
403
404
406{
407 BOARD* board = aArea->GetBoard();
408 BOX2I areaBBox = aArea->GetBoundingBox();
409 std::shared_ptr<SHAPE> shape;
410
411 // Collisions include touching, so we need to deflate outline by enough to exclude it.
412 // This is particularly important for detecting copper fills as they will be exactly
413 // touching along the entire exclusion border.
414 SHAPE_POLY_SET areaOutline = aArea->Outline()->CloneDropTriangulation();
415 areaOutline.Deflate( board->GetDesignSettings().GetDRCEpsilon(), 0,
417
418 if( aItem->GetFlags() & HOLE_PROXY )
419 {
420 if( aItem->Type() == PCB_PAD_T )
421 {
422 return areaOutline.Collide( aItem->GetEffectiveHoleShape().get() );
423 }
424 else if( aItem->Type() == PCB_VIA_T )
425 {
426 LSET overlap = aItem->GetLayerSet() & aArea->GetLayerSet();
427
429 if( overlap.any() )
430 {
431 if( aCtx->GetLayer() == UNDEFINED_LAYER || overlap.Contains( aCtx->GetLayer() ) )
432 return areaOutline.Collide( aItem->GetEffectiveHoleShape().get() );
433 }
434 }
435
436 return false;
437 }
438
439 if( aItem->Type() == PCB_FOOTPRINT_T )
440 {
441 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aItem );
442
443 if( ( footprint->GetFlags() & MALFORMED_COURTYARDS ) != 0 )
444 {
445 if( aCtx->HasErrorCallback() )
446 aCtx->ReportError( _( "Footprint's courtyard is not a single, closed shape." ) );
447
448 return false;
449 }
450
451 if( ( aArea->GetLayerSet() & LSET::FrontMask() ).any() )
452 {
453 const SHAPE_POLY_SET& courtyard = footprint->GetCourtyard( F_CrtYd );
454
455 if( courtyard.OutlineCount() == 0 )
456 {
457 if( aCtx->HasErrorCallback() )
458 aCtx->ReportError( _( "Footprint has no front courtyard." ) );
459 }
460 else if( areaOutline.Collide( &courtyard.Outline( 0 ) ) )
461 {
462 return true;
463 }
464 }
465
466 if( ( aArea->GetLayerSet() & LSET::BackMask() ).any() )
467 {
468 const SHAPE_POLY_SET& courtyard = footprint->GetCourtyard( B_CrtYd );
469
470 if( courtyard.OutlineCount() == 0 )
471 {
472 if( aCtx->HasErrorCallback() )
473 aCtx->ReportError( _( "Footprint has no back courtyard." ) );
474 }
475 else if( areaOutline.Collide( &courtyard.Outline( 0 ) ) )
476 {
477 return true;
478 }
479 }
480
481 return false;
482 }
483
484 if( aItem->Type() == PCB_ZONE_T || aItem->Type() == PCB_FP_ZONE_T )
485 {
486 ZONE* zone = static_cast<ZONE*>( aItem );
487
488 if( !zone->IsFilled() )
489 return false;
490
491 DRC_RTREE* zoneRTree = board->m_CopperZoneRTreeCache[ zone ].get();
492
493 if( zoneRTree )
494 {
495 for( PCB_LAYER_ID layer : aArea->GetLayerSet().Seq() )
496 {
497 if( aCtx->GetLayer() == layer || aCtx->GetLayer() == UNDEFINED_LAYER )
498 {
499 if( zoneRTree->QueryColliding( areaBBox, &areaOutline, layer ) )
500 return true;
501 }
502 }
503 }
504
505 return false;
506 }
507 else
508 {
509 PCB_LAYER_ID layer = aCtx->GetLayer();
510
511 if( layer != UNDEFINED_LAYER && !( aArea->GetLayerSet().Contains( layer ) ) )
512 return false;
513
514 if( !shape )
515 shape = aItem->GetEffectiveShape( layer );
516
517 return areaOutline.Collide( shape.get() );
518 }
519}
520
521
522bool searchAreas( BOARD* aBoard, const wxString& aArg, PCB_EXPR_CONTEXT* aCtx,
523 std::function<bool( ZONE* )> aFunc )
524{
525 if( aArg == wxT( "A" ) )
526 {
527 return aFunc( dynamic_cast<ZONE*>( aCtx->GetItem( 0 ) ) );
528 }
529 else if( aArg == wxT( "B" ) )
530 {
531 return aFunc( dynamic_cast<ZONE*>( aCtx->GetItem( 1 ) ) );
532 }
533 else if( KIID::SniffTest( aArg ) )
534 {
535 KIID target( aArg );
536
537 for( ZONE* area : aBoard->Zones() )
538 {
539 // Only a single zone can match the UUID; exit once we find a match whether
540 // "inside" or not
541 if( area->m_Uuid == target )
542 return aFunc( area );
543 }
544
545 for( FOOTPRINT* footprint : aBoard->Footprints() )
546 {
547 for( ZONE* area : footprint->Zones() )
548 {
549 // Only a single zone can match the UUID; exit once we find a match
550 // whether "inside" or not
551 if( area->m_Uuid == target )
552 return aFunc( area );
553 }
554 }
555
556 return 0.0;
557 }
558 else // Match on zone name
559 {
560 for( ZONE* area : aBoard->Zones() )
561 {
562 if( area->GetZoneName().Matches( aArg ) )
563 {
564 // Many zones can match the name; exit only when we find an "inside"
565 if( aFunc( area ) )
566 return true;
567 }
568 }
569
570 for( FOOTPRINT* footprint : aBoard->Footprints() )
571 {
572 for( ZONE* area : footprint->Zones() )
573 {
574 // Many zones can match the name; exit only when we find an "inside"
575 if( area->GetZoneName().Matches( aArg ) )
576 {
577 if( aFunc( area ) )
578 return true;
579 }
580 }
581 }
582
583 return false;
584 }
585}
586
587
588#define MISSING_AREA_ARG( f ) \
589 wxString::Format( _( "Missing rule-area argument (A, B, or rule-area name) to %s." ), f )
590
591static void intersectsAreaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
592{
593 PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
594 LIBEVAL::VALUE* arg = aCtx->Pop();
595 LIBEVAL::VALUE* result = aCtx->AllocValue();
596
597 result->Set( 0.0 );
598 aCtx->Push( result );
599
600 if( !arg )
601 {
602 if( aCtx->HasErrorCallback() )
603 aCtx->ReportError( MISSING_AREA_ARG( wxT( "intersectsArea()" ) ) );
604
605 return;
606 }
607
608 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
609 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
610
611 if( !item )
612 return;
613
614 result->SetDeferredEval(
615 [item, arg, context]() -> double
616 {
617 BOARD* board = item->GetBoard();
618 PCB_LAYER_ID aLayer = context->GetLayer();
619 BOX2I itemBBox = item->GetBoundingBox();
620
621 if( searchAreas( board, arg->AsString(), context,
622 [&]( ZONE* aArea )
623 {
624 if( !aArea || aArea == item || aArea->GetParent() == item )
625 return false;
626
627 LSET commonLayers = aArea->GetLayerSet() & item->GetLayerSet();
628
629 if( !commonLayers.any() )
630 return false;
631
632 if( !aArea->GetBoundingBox().Intersects( itemBBox ) )
633 return false;
634
635 std::unique_lock<std::mutex> cacheLock( board->m_CachesMutex );
636 LSET testLayers;
637
638 if( aLayer != UNDEFINED_LAYER )
639 testLayers.set( aLayer );
640 else
641 testLayers = commonLayers;
642
643 for( PCB_LAYER_ID layer : testLayers.UIOrder() )
644 {
645 PTR_PTR_LAYER_CACHE_KEY key = { aArea, item, layer };
646
647 auto i = board->m_IntersectsAreaCache.find( key );
648
649 if( i != board->m_IntersectsAreaCache.end() && i->second )
650 return true;
651
652 bool collides = collidesWithArea( item, context, aArea );
653
654 board->m_IntersectsAreaCache[ key ] = collides;
655
656 if( collides )
657 return true;
658 }
659
660 return false;
661 } ) )
662 {
663 return 1.0;
664 }
665
666 return 0.0;
667 } );
668}
669
670
671static void enclosedByAreaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
672{
673 PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
674 LIBEVAL::VALUE* arg = aCtx->Pop();
675 LIBEVAL::VALUE* result = aCtx->AllocValue();
676
677 result->Set( 0.0 );
678 aCtx->Push( result );
679
680 if( !arg )
681 {
682 if( aCtx->HasErrorCallback() )
683 aCtx->ReportError( MISSING_AREA_ARG( wxT( "enclosedByArea()" ) ) );
684
685 return;
686 }
687
688 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
689 BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
690
691 if( !item )
692 return;
693
694 result->SetDeferredEval(
695 [item, arg, context]() -> double
696 {
697 BOARD* board = item->GetBoard();
698 int maxError = board->GetDesignSettings().m_MaxError;
699 PCB_LAYER_ID layer = context->GetLayer();
700 BOX2I itemBBox = item->GetBoundingBox();
701
702 if( searchAreas( board, arg->AsString(), context,
703 [&]( ZONE* aArea )
704 {
705 if( !aArea || aArea == item || aArea->GetParent() == item )
706 return false;
707
708 if( !( aArea->GetLayerSet() & item->GetLayerSet() ).any() )
709 return false;
710
711 if( !aArea->GetBoundingBox().Intersects( itemBBox ) )
712 return false;
713
714 std::unique_lock<std::mutex> cacheLock( board->m_CachesMutex );
715 PTR_PTR_LAYER_CACHE_KEY key = { aArea, item, layer };
716
717 auto i = board->m_EnclosedByAreaCache.find( key );
718
719 if( i != board->m_EnclosedByAreaCache.end() )
720 return i->second;
721
722 SHAPE_POLY_SET itemShape;
723 bool enclosedByArea;
724
725 item->TransformShapeToPolygon( itemShape, layer, 0, maxError,
727
728 if( itemShape.IsEmpty() )
729 {
730 // If it's already empty then our test will have no meaning.
731 enclosedByArea = false;
732 }
733 else
734 {
735 itemShape.BooleanSubtract( *aArea->Outline(),
736 SHAPE_POLY_SET::PM_FAST );
737
738 enclosedByArea = itemShape.IsEmpty();
739 }
740
741 board->m_EnclosedByAreaCache[ key ] = enclosedByArea;
742
743 return enclosedByArea;
744 } ) )
745 {
746 return 1.0;
747 }
748
749 return 0.0;
750 } );
751}
752
753
754#define MISSING_GROUP_ARG( f ) \
755 wxString::Format( _( "Missing group name argument to %s." ), f )
756
757static void memberOfFunc( LIBEVAL::CONTEXT* aCtx, void* self )
758{
759 LIBEVAL::VALUE* arg = aCtx->Pop();
760 LIBEVAL::VALUE* result = aCtx->AllocValue();
761
762 result->Set( 0.0 );
763 aCtx->Push( result );
764
765 if( !arg )
766 {
767 if( aCtx->HasErrorCallback() )
768 aCtx->ReportError( MISSING_GROUP_ARG( wxT( "memberOf()" ) ) );
769
770 return;
771 }
772
773 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
774 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
775
776 if( !item )
777 return;
778
779 result->SetDeferredEval(
780 [item, arg]() -> double
781 {
782 PCB_GROUP* group = item->GetParentGroup();
783
784 if( !group && item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
785 group = item->GetParent()->GetParentGroup();
786
787 while( group )
788 {
789 if( group->GetName().Matches( arg->AsString() ) )
790 return 1.0;
791
792 group = group->GetParentGroup();
793 }
794
795 return 0.0;
796 } );
797}
798
799
800static void isMicroVia( LIBEVAL::CONTEXT* aCtx, void* self )
801{
802 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
803 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
804 LIBEVAL::VALUE* result = aCtx->AllocValue();
805
806 result->Set( 0.0 );
807 aCtx->Push( result );
808
809 PCB_VIA* via = dyn_cast<PCB_VIA*>( item );
810
811 if( via && via->GetViaType() == VIATYPE::MICROVIA )
812 result->Set ( 1.0 );
813}
814
815
816static void isBlindBuriedViaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
817{
818 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
819 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
820 LIBEVAL::VALUE* result = aCtx->AllocValue();
821
822 result->Set( 0.0 );
823 aCtx->Push( result );
824
825 PCB_VIA* via = dyn_cast<PCB_VIA*>( item );
826
827 if( via && via->GetViaType() == VIATYPE::BLIND_BURIED )
828 result->Set ( 1.0 );
829}
830
831
832static void isCoupledDiffPairFunc( LIBEVAL::CONTEXT* aCtx, void* self )
833{
834 PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
835 BOARD_CONNECTED_ITEM* a = dynamic_cast<BOARD_CONNECTED_ITEM*>( context->GetItem( 0 ) );
836 BOARD_CONNECTED_ITEM* b = dynamic_cast<BOARD_CONNECTED_ITEM*>( context->GetItem( 1 ) );
837 LIBEVAL::VALUE* result = aCtx->AllocValue();
838
839 result->Set( 0.0 );
840 aCtx->Push( result );
841
842 result->SetDeferredEval(
843 [a, b, context]() -> double
844 {
845 NETINFO_ITEM* netinfo = a ? a->GetNet() : nullptr;
846
847 if( !netinfo )
848 return 0.0;
849
850 wxString coupledNet;
851 wxString dummy;
852
853 if( !DRC_ENGINE::MatchDpSuffix( netinfo->GetNetname(), coupledNet, dummy ) )
854 return 0.0;
855
858 {
859 // DRC engine evaluates these singly, so we won't have a B item
860 return 1.0;
861 }
862
863 return b && b->GetNetname() == coupledNet;
864 } );
865}
866
867
868#define MISSING_DP_ARG( f ) \
869 wxString::Format( _( "Missing diff-pair name argument to %s." ), f )
870
871static void inDiffPairFunc( LIBEVAL::CONTEXT* aCtx, void* self )
872{
873 LIBEVAL::VALUE* argv = aCtx->Pop();
874 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
875 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
876 LIBEVAL::VALUE* result = aCtx->AllocValue();
877
878 result->Set( 0.0 );
879 aCtx->Push( result );
880
881 if( !argv )
882 {
883 if( aCtx->HasErrorCallback() )
884 aCtx->ReportError( MISSING_DP_ARG( wxT( "inDiffPair()" ) ) );
885
886 return;
887 }
888
889 if( !item || !item->GetBoard() )
890 return;
891
892 result->SetDeferredEval(
893 [item, argv]() -> double
894 {
895 if( item && item->IsConnected() )
896 {
897 NETINFO_ITEM* netinfo = static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
898
899 if( !netinfo )
900 return 0.0;
901
902 wxString refName = netinfo->GetNetname();
903 wxString arg = argv->AsString();
904 wxString baseName, coupledNet;
905 int polarity = DRC_ENGINE::MatchDpSuffix( refName, coupledNet, baseName );
906
907 if( polarity != 0 && item->GetBoard()->FindNet( coupledNet ) )
908 {
909 if( baseName.Matches( arg ) )
910 return 1.0;
911
912 if( baseName.EndsWith( "_" ) && baseName.BeforeLast( '_' ).Matches( arg ) )
913 return 1.0;
914 }
915 }
916
917 return 0.0;
918 } );
919}
920
921
922static void getFieldFunc( LIBEVAL::CONTEXT* aCtx, void* self )
923{
924 LIBEVAL::VALUE* arg = aCtx->Pop();
925 PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
926 BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
927 LIBEVAL::VALUE* result = aCtx->AllocValue();
928
929 result->Set( "" );
930 aCtx->Push( result );
931
932 if( !arg )
933 {
934 if( aCtx->HasErrorCallback() )
935 {
936 aCtx->ReportError( wxString::Format( _( "Missing field name argument to %s." ),
937 wxT( "getField()" ) ) );
938 }
939
940 return;
941 }
942
943 if( !item || !item->GetBoard() )
944 return;
945
946 result->SetDeferredEval(
947 [item, arg]() -> wxString
948 {
949 if( item && item->Type() == PCB_FOOTPRINT_T )
950 {
951 FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
952
953 if( fp->HasProperty( arg->AsString() ) )
954 return fp->GetProperty( arg->AsString() );
955 }
956
957 return "";
958 } );
959}
960
961
963{
965}
966
967
969{
970 m_funcs.clear();
971
972 RegisterFunc( wxT( "existsOnLayer('x')" ), existsOnLayerFunc );
973
974 RegisterFunc( wxT( "isPlated()" ), isPlatedFunc );
975
976 RegisterFunc( wxT( "insideCourtyard('x') DEPRECATED" ), intersectsCourtyardFunc );
977 RegisterFunc( wxT( "insideFrontCourtyard('x') DEPRECATED" ), intersectsFrontCourtyardFunc );
978 RegisterFunc( wxT( "insideBackCourtyard('x') DEPRECATED" ), intersectsBackCourtyardFunc );
979 RegisterFunc( wxT( "intersectsCourtyard('x')" ), intersectsCourtyardFunc );
980 RegisterFunc( wxT( "intersectsFrontCourtyard('x')" ), intersectsFrontCourtyardFunc );
981 RegisterFunc( wxT( "intersectsBackCourtyard('x')" ), intersectsBackCourtyardFunc );
982
983 RegisterFunc( wxT( "insideArea('x') DEPRECATED" ), intersectsAreaFunc );
984 RegisterFunc( wxT( "intersectsArea('x')" ), intersectsAreaFunc );
985 RegisterFunc( wxT( "enclosedByArea('x')" ), enclosedByAreaFunc );
986
987 RegisterFunc( wxT( "isMicroVia()" ), isMicroVia );
988 RegisterFunc( wxT( "isBlindBuriedVia()" ), isBlindBuriedViaFunc );
989
990 RegisterFunc( wxT( "memberOf('x')" ), memberOfFunc );
991
992 RegisterFunc( wxT( "fromTo('x','y')" ), fromToFunc );
993 RegisterFunc( wxT( "isCoupledDiffPair()" ), isCoupledDiffPairFunc );
994 RegisterFunc( wxT( "inDiffPair('x')" ), inDiffPairFunc );
995
996 RegisterFunc( wxT( "getField('x')" ), getFieldFunc );
997}
998
999
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
NETINFO_ITEM * GetNet() const
Return #NET_INFO object for a given item.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:70
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition: board_item.h:127
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:84
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.
Definition: board_item.cpp:196
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.
Definition: board_item.cpp:219
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:43
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:197
virtual bool IsOnLayer(PCB_LAYER_ID aLayer, bool aIncludeCourtyards=false) const
Test to see if this object is on the given layer.
Definition: board_item.h:257
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:175
virtual std::shared_ptr< SHAPE_SEGMENT > GetEffectiveHoleShape() const
Definition: board_item.cpp:229
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:269
std::unordered_map< PTR_PTR_LAYER_CACHE_KEY, bool > m_EnclosedByAreaCache
Definition: board.h:1162
ZONES & Zones()
Definition: board.h:317
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1478
FOOTPRINTS & Footprints()
Definition: board.h:311
std::unordered_map< PTR_PTR_CACHE_KEY, bool > m_IntersectsCourtyardCache
Definition: board.h:1158
std::unordered_map< PTR_PTR_CACHE_KEY, bool > m_IntersectsFCourtyardCache
Definition: board.h:1159
std::unordered_map< wxString, LSET > m_LayerExpressionCache
Definition: board.h:1163
std::unordered_map< PTR_PTR_CACHE_KEY, bool > m_IntersectsBCourtyardCache
Definition: board.h:1160
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition: board.h:1164
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:704
std::mutex m_CachesMutex
Definition: board.h:1157
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:430
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:48
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:211
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:74
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:142
static ENUM_MAP< T > & Instance()
Definition: property.h:623
bool HasProperty(const wxString &aKey)
Definition: footprint.h:577
const SHAPE_POLY_SET & GetCourtyard(PCB_LAYER_ID aLayer) const
Used in DRC to test the courtyard area (a complex polygon).
Definition: footprint.cpp:2233
const wxString & GetProperty(const wxString &aKey)
Definition: footprint.h:576
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:179
void ReportError(const wxString &aErrorMsg)
void Push(VALUE *v)
void Set(double aValue)
virtual const wxString & AsString() const
void SetDeferredEval(std::function< double()> aLambda)
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:532
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:411
bool Contains(PCB_LAYER_ID aLayer)
See if the layer set contains a PCB layer.
Definition: layer_ids.h:602
static LSET FrontMask()
Return a mask holding all technical layers and the external CU layer on front side.
Definition: lset.cpp:895
static LSET BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition: lset.cpp:902
Handle the data for a net.
Definition: netinfo.h:67
const wxString & GetNetname() const
Definition: netinfo.h:125
Definition: pad.h:60
void RegisterFunc(const wxString &funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr)
std::map< wxString, LIBEVAL::FUNC_CALL_REF > m_funcs
BOARD_ITEM * GetItem(int index) const
PCB_LAYER_ID GetLayer() const
int GetConstraint() const
BOARD_ITEM * GetObject(const LIBEVAL::CONTEXT *aCtx) const
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:51
Represent a set of closed polygons.
@ ALLOW_ACUTE_CORNERS
just inflate the polygon. Acute angles create spikes
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,...
void Deflate(int aAmount, int aCircleSegmentsCount, CORNER_STRATEGY aCornerStrategy=CHAMFER_ALL_CORNERS)
SHAPE_LINE_CHAIN & Outline(int aIndex)
int OutlineCount() const
Return the number of vertices in a given outline/hole.
SHAPE_POLY_SET CloneDropTriangulation() const
Creates a new empty polygon in the set and returns its index.
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
const BOX2I GetBoundingBox() const override
Definition: zone.cpp:329
bool IsFilled() const
Definition: zone.h:242
SHAPE_POLY_SET * Outline()
Definition: zone.h:318
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:122
@ LENGTH_CONSTRAINT
Definition: drc_rule.h:64
@ SKEW_CONSTRAINT
Definition: drc_rule.h:65
#define _(s)
#define HOLE_PROXY
Indicates the BOARD_ITEM is a proxy for its hole.
#define MALFORMED_COURTYARDS
@ ERROR_OUTSIDE
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ F_CrtYd
Definition: layer_ids.h:117
@ B_Cu
Definition: layer_ids.h:95
@ B_CrtYd
Definition: layer_ids.h:116
@ UNDEFINED_LAYER
Definition: layer_ids.h:60
@ F_Cu
Definition: layer_ids.h:64
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:932
@ PTH
Plated through hole pad.
static void intersectsFrontCourtyardFunc(LIBEVAL::CONTEXT *aCtx, void *self)
#define MISSING_LAYER_ARG(f)
bool collidesWithCourtyard(BOARD_ITEM *aItem, std::shared_ptr< SHAPE > &aItemShape, PCB_EXPR_CONTEXT *aCtx, FOOTPRINT *aFootprint, PCB_LAYER_ID aSide)
static void intersectsBackCourtyardFunc(LIBEVAL::CONTEXT *aCtx, void *self)
bool collidesWithArea(BOARD_ITEM *aItem, PCB_EXPR_CONTEXT *aCtx, ZONE *aArea)
#define MISSING_AREA_ARG(f)
static void isCoupledDiffPairFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void isPlatedFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void existsOnLayerFunc(LIBEVAL::CONTEXT *aCtx, void *self)
#define MISSING_GROUP_ARG(f)
static void isBlindBuriedViaFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static bool searchFootprints(BOARD *aBoard, const wxString &aArg, PCB_EXPR_CONTEXT *aCtx, std::function< bool(FOOTPRINT *)> aFunc)
#define MISSING_DP_ARG(f)
static void enclosedByAreaFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void memberOfFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void isMicroVia(LIBEVAL::CONTEXT *aCtx, void *self)
static void getFieldFunc(LIBEVAL::CONTEXT *aCtx, void *self)
bool fromToFunc(LIBEVAL::CONTEXT *aCtx, void *self)
bool searchAreas(BOARD *aBoard, const wxString &aArg, PCB_EXPR_CONTEXT *aCtx, std::function< bool(ZONE *)> aFunc)
static void intersectsCourtyardFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void inDiffPairFunc(LIBEVAL::CONTEXT *aCtx, void *self)
static void intersectsAreaFunc(LIBEVAL::CONTEXT *aCtx, void *self)
#define MISSING_FP_ARG(f)
@ BLIND_BURIED
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
std::vector< FAB_LAYER_COLOR > dummy
VECTOR3I res
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:102
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:112
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_FP_ZONE_T
class ZONE, managed by a footprint
Definition: typeinfo.h:100
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87