KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbexpr_evaluator.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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "pcbexpr_evaluator.h"
21
22#include <inspectable_impl.h>
23
24#include <cstdio>
25#include <memory>
26#include <mutex>
27
28#include <board.h>
29#include <footprint.h>
30#include <gal/color4d.h>
31#include <geometry/eda_angle.h>
32#include <lset.h>
34#include <drc/drc_engine.h>
36#include <string_utils.h>
37
38
39/* --------------------------------------------------------------------------------------------
40 * Specialized Expression References
41 */
42
44{
45 wxASSERT( dynamic_cast<const PCBEXPR_CONTEXT*>( aCtx ) );
46
47 const PCBEXPR_CONTEXT* ctx = static_cast<const PCBEXPR_CONTEXT*>( aCtx );
48 BOARD_ITEM* item = ctx->GetItem( m_itemIndex );
49
50 for( PCBEXPR_NAV_STEP step : m_navigation )
51 {
52 if( !item )
53 break;
54
55 switch( step )
56 {
57 // The direct parent, matching the semantics of the "Parent" string property so that
58 // "A.Parent" used as an object refers to the same item as "A.Parent" used as a string.
59 case PCBEXPR_NAV_STEP::PARENT: item = item->GetParent(); break;
60 }
61 }
62
63 return item;
64}
65
66
68{
69public:
71 LIBEVAL::VALUE( LayerName( aLayer ) ),
72 m_layer( aLayer )
73 {};
74
75 virtual bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
76 {
77 // For boards with user-defined layer names there will be 2 entries for each layer
78 // in the ENUM_MAP: one for the canonical layer name and one for the user layer name.
79 // We need to check against both.
80
81 wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
82 const wxString& layerName = b->AsString();
83 BOARD* board = static_cast<PCBEXPR_CONTEXT*>( aCtx )->GetBoard();
84
85 {
86 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
87
88 auto i = board->m_LayerExpressionCache.find( layerName );
89
90 if( i != board->m_LayerExpressionCache.end() )
91 return i->second.Contains( m_layer );
92 }
93
94 LSET mask;
95
96 for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
97 {
98 wxPGChoiceEntry& entry = layerMap[ii];
99
100 if( entry.GetText().Matches( layerName ) )
101 mask.set( ToLAYER_ID( entry.GetValue() ) );
102 }
103
104 {
105 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
106 board->m_LayerExpressionCache[ layerName ] = mask;
107 }
108
109 return mask.Contains( m_layer );
110 }
111
112protected:
114};
115
116
118{
119public:
120 PCBEXPR_PINTYPE_VALUE( const wxString& aPinTypeName ) :
121 LIBEVAL::VALUE( aPinTypeName )
122 {};
123
124 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
125 {
126 const wxString& thisStr = AsString();
127 const wxString& otherStr = b->AsString();
128
129 // Case insensitive
130 if( thisStr.IsSameAs( otherStr, false ) )
131 return true;
132
133 // Wildcards
134 if( thisStr.Matches( otherStr ) )
135 return true;
136
137 // Handle cases where the netlist token is different from the EEschema token
138 wxString altStr;
139
140 if( thisStr == wxT( "tri_state" ) )
141 altStr = wxT( "Tri-state" );
142 else if( thisStr == wxT( "power_in" ) )
143 altStr = wxT( "Power input" );
144 else if( thisStr == wxT( "power_out" ) )
145 altStr = wxT( "Power output" );
146 else if( thisStr == wxT( "no_connect" ) )
147 altStr = wxT( "Unconnected" );
148
149 if( !altStr.IsEmpty() )
150 {
151 // Case insensitive
152 if( altStr.IsSameAs( otherStr, false ) )
153 return true;
154
155 // Wildcards
156 if( altStr.Matches( otherStr ) )
157 return true;
158 }
159
160 return false;
161 }
162};
163
164
166{
167public:
169 LIBEVAL::VALUE( wxEmptyString ),
170 m_item( aItem )
171 {};
172
173 const wxString& AsString() const override
174 {
175 const_cast<PCBEXPR_NETCLASS_VALUE*>( this )->Set( m_item->GetEffectiveNetClass()->GetName() );
177 }
178
179 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
180 {
181 if( const PCBEXPR_NETCLASS_VALUE* bValue = dynamic_cast<const PCBEXPR_NETCLASS_VALUE*>( b ) )
182 return *( m_item->GetEffectiveNetClass() ) == *( bValue->m_item->GetEffectiveNetClass() );
183
184 if( b->GetType() == LIBEVAL::VT_STRING )
185 {
186 // Test constituent net class names. The effective net class name (e.g. CLASS1,CLASS2,OTHER_CLASS) is
187 // tested in the fallthrough condition.
188 for( const NETCLASS* nc : m_item->GetEffectiveNetClass()->GetConstituentNetclasses() )
189 {
190 const wxString& ncName = nc->GetName();
191
192 if( b->StringIsWildcard() )
193 {
194 if( WildCompareString( b->AsString(), ncName, false ) )
195 return true;
196 }
197 else
198 {
199 if( ncName.IsSameAs( b->AsString(), false ) )
200 return true;
201 }
202 }
203 }
204
205 return LIBEVAL::VALUE::EqualTo( aCtx, b );
206 }
207
208 bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override
209 {
210 if( const PCBEXPR_NETCLASS_VALUE* bValue = dynamic_cast<const PCBEXPR_NETCLASS_VALUE*>( b ) )
211 return *( m_item->GetEffectiveNetClass() ) != *( bValue->m_item->GetEffectiveNetClass() );
212
213 if( b->GetType() == LIBEVAL::VT_STRING )
214 {
215 // Test constituent net class names
216 bool isInConstituents = false;
217
218 for( const NETCLASS* nc : m_item->GetEffectiveNetClass()->GetConstituentNetclasses() )
219 {
220 const wxString& ncName = nc->GetName();
221
222 if( b->StringIsWildcard() )
223 {
224 if( WildCompareString( b->AsString(), ncName, false ) )
225 {
226 isInConstituents = true;
227 break;
228 }
229 }
230 else
231 {
232 if( ncName.IsSameAs( b->AsString(), false ) )
233 {
234 isInConstituents = true;
235 break;
236 }
237 }
238 }
239
240 // Test effective net class name
241 const bool isFullName = LIBEVAL::VALUE::EqualTo( aCtx, b );
242
243 return !isInConstituents && !isFullName;
244 }
245
246 return LIBEVAL::VALUE::NotEqualTo( aCtx, b );
247 }
248
249protected:
251};
252
253
255{
256public:
258 LIBEVAL::VALUE( wxEmptyString ),
259 m_item( dynamic_cast<FOOTPRINT*>( aItem ) )
260 {};
261
262 const wxString& AsString() const override
263 {
264 if( !m_item )
266
267 if( const COMPONENT_CLASS* compClass = m_item->GetComponentClass() )
268 const_cast<PCBEXPR_COMPONENT_CLASS_VALUE*>( this )->Set( compClass->GetName() );
269
271 }
272
273 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
274 {
275 if( const PCBEXPR_COMPONENT_CLASS_VALUE* bValue = dynamic_cast<const PCBEXPR_COMPONENT_CLASS_VALUE*>( b ) )
276 {
277 if( !m_item || !bValue->m_item )
278 return LIBEVAL::VALUE::EqualTo( aCtx, b );
279
280 const COMPONENT_CLASS* aClass = m_item->GetComponentClass();
281 const COMPONENT_CLASS* bClass = bValue->m_item->GetComponentClass();
282
283 return *aClass == *bClass;
284 }
285
286 if( b->GetType() == LIBEVAL::VT_STRING )
287 {
288 // Test constituent component class names. The effective component class name
289 // (e.g. CLASS1,CLASS2,OTHER_CLASS) is tested in the fallthrough condition.
290 for( const COMPONENT_CLASS* cc : m_item->GetComponentClass()->GetConstituentClasses() )
291 {
292 const wxString& ccName = cc->GetName();
293
294 if( b->StringIsWildcard() )
295 {
296 if( WildCompareString( b->AsString(), ccName, false ) )
297 return true;
298 }
299 else
300 {
301 if( ccName.IsSameAs( b->AsString(), false ) )
302 return true;
303 }
304 }
305 }
306
307 return LIBEVAL::VALUE::EqualTo( aCtx, b );
308 }
309
310 bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override
311 {
312 if( const PCBEXPR_COMPONENT_CLASS_VALUE* bValue = dynamic_cast<const PCBEXPR_COMPONENT_CLASS_VALUE*>( b ) )
313 {
314 if( !m_item || !bValue->m_item )
315 return LIBEVAL::VALUE::NotEqualTo( aCtx, b );
316
317 const COMPONENT_CLASS* aClass = m_item->GetComponentClass();
318 const COMPONENT_CLASS* bClass = bValue->m_item->GetComponentClass();
319
320 return *aClass != *bClass;
321 }
322
323 if( b->GetType() == LIBEVAL::VT_STRING )
324 {
325 // Test constituent component class names
326 bool isInConstituents = false;
327
328 for( const COMPONENT_CLASS* cc : m_item->GetComponentClass()->GetConstituentClasses() )
329 {
330 const wxString& ccName = cc->GetName();
331
332 if( b->StringIsWildcard() )
333 {
334 if( WildCompareString( b->AsString(), ccName, false ) )
335 {
336 isInConstituents = true;
337 break;
338 }
339 }
340 else
341 {
342 if( ccName.IsSameAs( b->AsString(), false ) )
343 {
344 isInConstituents = true;
345 break;
346 }
347 }
348 }
349
350 // Test effective component class name
351 const bool isFullName = LIBEVAL::VALUE::EqualTo( aCtx, b );
352
353 return !isInConstituents && !isFullName;
354 }
355
356 return LIBEVAL::VALUE::NotEqualTo( aCtx, b );
357 }
358
359protected:
361};
362
363
365{
366public:
368 LIBEVAL::VALUE( wxEmptyString ),
369 m_item( aItem )
370 {};
371
372 const wxString& AsString() const override
373 {
374 const_cast<PCBEXPR_NET_VALUE*>( this )->Set( m_item->GetNetname() );
376 }
377
378 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
379 {
380 if( const PCBEXPR_NET_VALUE* bValue = dynamic_cast<const PCBEXPR_NET_VALUE*>( b ) )
381 return m_item->GetNetCode() == bValue->m_item->GetNetCode();
382 else
383 return LIBEVAL::VALUE::EqualTo( aCtx, b );
384 }
385
386 bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override
387 {
388 if( const PCBEXPR_NET_VALUE* bValue = dynamic_cast<const PCBEXPR_NET_VALUE*>( b ) )
389 return m_item->GetNetCode() != bValue->m_item->GetNetCode();
390 else
391 return LIBEVAL::VALUE::NotEqualTo( aCtx, b );
392 }
393
394protected:
396};
397
398
400{
401 auto it = m_typeOverrides.find( aItem );
402 return it != m_typeOverrides.end() ? it->second : aItem->Type();
403}
404
405
407{
408 const TYPE_ID type = aProperty->TypeHash();
409
410 if( type == TYPE_HASH( int ) )
412 else if( type == TYPE_HASH( std::optional<int> ) )
414 else if( type == TYPE_HASH( unsigned ) )
416 else if( type == TYPE_HASH( long long int ) )
418 else if( type == TYPE_HASH( double ) )
420 else if( type == TYPE_HASH( std::optional<double> ) )
422 else if( type == TYPE_HASH( bool ) )
424 else if( type == TYPE_HASH( wxString ) )
426 else if( aProperty->HasChoices() )
428 else if( type == TYPE_HASH( EDA_ANGLE ) )
430 else if( type == TYPE_HASH( COLOR4D ) )
432
434}
435
436
460
461
463{
464 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
465
466 if( m_type == LIBEVAL::VT_NULL )
468
469 if( m_itemIndex == 2 )
470 return new PCBEXPR_LAYER_VALUE( context->GetLayer() );
471
472 BOARD_ITEM* item = GetObject( aCtx );
473
474 if( !item )
475 return new LIBEVAL::VALUE();
476
477 auto it = m_matchingTypes.find( TYPE_HASH( *item ) );
478
479 if( it == m_matchingTypes.end() )
480 {
481 // If the property isn't defined on the item itself but is defined on its parent
482 // footprint (e.g. Reference, Value), resolve against the parent so that conditions
483 // like "A.Reference == 'J1'" match pads and graphics belonging to J1.
484 if( FOOTPRINT* parentFp = item->GetParentFootprint() )
485 {
486 auto parentIt = m_matchingTypes.find( TYPE_HASH( *parentFp ) );
487
488 if( parentIt != m_matchingTypes.end() )
489 {
490 item = parentFp;
491 it = parentIt;
492 }
493 }
494 }
495
496 if( it == m_matchingTypes.end() )
497 {
498 // Don't force user to type "A.Type == 'via' && A.Via_Type == 'buried'" when the
499 // simpler "A.Via_Type == 'buried'" is perfectly clear. Instead, return an undefined
500 // value when the property doesn't appear on a particular object.
501
502 return new LIBEVAL::VALUE();
503 }
504 else
505 {
506 if( !PROPERTY_MANAGER::Instance().IsAvailableFor( TYPE_HASH( *item ), it->second.property, item ) )
507 {
508 return new LIBEVAL::VALUE();
509 }
510
511 switch( it->second.kind )
512 {
514 return new LIBEVAL::VALUE( static_cast<double>( item->Get<int>( it->second.property ) ) );
515
517 {
518 std::optional<int> val = item->Get<std::optional<int>>( it->second.property );
519
520 if( val )
521 return new LIBEVAL::VALUE( static_cast<double>( *val ) );
522
524 }
525
527 return new LIBEVAL::VALUE( static_cast<double>( item->Get<unsigned>( it->second.property ) ) );
528
530 return new LIBEVAL::VALUE( static_cast<double>( item->Get<long long int>( it->second.property ) ) );
531
532 case PCBEXPR_PROPERTY_KIND::DOUBLE: return new LIBEVAL::VALUE( item->Get<double>( it->second.property ) );
533
535 {
536 std::optional<double> val = item->Get<std::optional<double>>( it->second.property );
537
538 if( val )
539 return new LIBEVAL::VALUE( *val );
540
542 }
543
545 return new LIBEVAL::VALUE( static_cast<double>( item->Get<bool>( it->second.property ) ) );
546
548 {
549 wxString str = item->Get<wxString>( it->second.property );
550
551 if( it->second.property->Name() == wxT( "Pin Type" ) )
552 return new PCBEXPR_PINTYPE_VALUE( str );
553
554 // If it quacks like a duck, it is a duck
555 double doubleVal;
556
558 return new LIBEVAL::VALUE( doubleVal );
559
560 return new LIBEVAL::VALUE( str );
561 }
562
564 {
565 wxString str;
566
567 if( it->second.property->Name() == wxT( "Layer" ) || it->second.property->Name() == wxT( "Layer Top" )
568 || it->second.property->Name() == wxT( "Layer Bottom" ) )
569 {
570 const wxAny& any = item->Get( it->second.property );
571 PCB_LAYER_ID layer;
572
573 if( any.GetAs<PCB_LAYER_ID>( &layer ) )
574 return new PCBEXPR_LAYER_VALUE( layer );
575 else if( any.GetAs<wxString>( &str ) )
576 return new PCBEXPR_LAYER_VALUE( context->GetBoard()->GetLayerID( str ) );
577 }
578 else
579 {
580 const wxAny& any = item->Get( it->second.property );
581
582 if( any.GetAs<wxString>( &str ) )
583 return new LIBEVAL::VALUE( str );
584 }
585
586 return new LIBEVAL::VALUE();
587 }
588
590 return new LIBEVAL::VALUE( item->Get<EDA_ANGLE>( it->second.property ).AsDegrees() );
591
593 return new LIBEVAL::VALUE( item->Get<COLOR4D>( it->second.property ).ToCSSString() );
594
596 }
597 }
598
599 return new LIBEVAL::VALUE();
600}
601
602
604{
605 BOARD_CONNECTED_ITEM* item = dynamic_cast<BOARD_CONNECTED_ITEM*>( GetObject( aCtx ) );
606
607 if( !item )
608 return new LIBEVAL::VALUE();
609
610 return new PCBEXPR_NETCLASS_VALUE( item );
611}
612
613
615{
616 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( GetObject( aCtx ) );
617
618 if( !item )
619 return new LIBEVAL::VALUE();
620
621 // Resolve component class via the parent footprint so that conditions like
622 // "A.ComponentClass == 'X'" match pads and graphics inside the footprint.
623 if( item->Type() != PCB_FOOTPRINT_T )
624 item = item->GetParentFootprint();
625
626 if( !item )
627 return new LIBEVAL::VALUE();
628
629 return new PCBEXPR_COMPONENT_CLASS_VALUE( item );
630}
631
632
634{
635 BOARD_CONNECTED_ITEM* item = dynamic_cast<BOARD_CONNECTED_ITEM*>( GetObject( aCtx ) );
636
637 if( !item )
638 return new LIBEVAL::VALUE();
639
640 return new PCBEXPR_NET_VALUE( item );
641}
642
643
645{
646 BOARD_ITEM* item = GetObject( aCtx );
647
648 if( !item )
649 return new LIBEVAL::VALUE();
650
651 PCBEXPR_CONTEXT* ctx = static_cast<PCBEXPR_CONTEXT*>( aCtx );
652 KICAD_T type = ctx->GetEffectiveType( item );
653
654 return new LIBEVAL::VALUE( ENUM_MAP<KICAD_T>::Instance().ToString( type ) );
655}
656
657
659{
660 wxString nameLower = aName.Lower();
661
663
664 if( registry.IsGeometryDependent( nameLower ) )
666
667 return registry.Get( nameLower );
668}
669
670
671std::unique_ptr<LIBEVAL::VAR_REF> PCBEXPR_UCODE::CreateVarRef( const wxString& aVar,
672 const wxString& aField )
673{
675 std::unique_ptr<PCBEXPR_VAR_REF> vref;
676
677 if( aVar.IsSameAs( wxT( "null" ), false ) )
678 {
679 vref = std::make_unique<PCBEXPR_VAR_REF>( 0 );
680 vref->SetType( LIBEVAL::VT_NULL );
681 return vref;
682 }
683
684 // The receiver may be a navigation chain such as "A.Parent". Split off the base variable
685 // and translate each remaining segment into a navigation step. Only "Parent" is supported.
686
687 wxString baseVar = aVar;
688 std::vector<PCBEXPR_NAV_STEP> navigation;
689
690 if( aVar.Contains( wxT( "." ) ) )
691 {
692 wxArrayString tokens = wxSplit( aVar, '.' );
693 baseVar = tokens.IsEmpty() ? wxString() : tokens[0];
694
695 for( size_t i = 1; i < tokens.GetCount(); ++i )
696 {
697 if( tokens[i].CmpNoCase( wxT( "Parent" ) ) == 0 )
698 navigation.push_back( PCBEXPR_NAV_STEP::PARENT );
699 else
700 return nullptr;
701 }
702
703 // Navigation requires one concrete item; combined-item "AB" and layer "L" have no parent.
704 if( baseVar != wxT( "A" ) && baseVar != wxT( "B" ) )
705 return nullptr;
706 }
707
708 if( !aField.IsEmpty() && ( baseVar == wxT( "AB" ) || baseVar == wxT( "L" ) ) )
709 return nullptr;
710
711 if( baseVar == wxT( "B" ) || baseVar == wxT( "AB" ) )
712 m_requiresPairItems = true;
713
714 auto withNav =
715 [&navigation]( std::unique_ptr<PCBEXPR_VAR_REF> aRef ) -> std::unique_ptr<PCBEXPR_VAR_REF>
716 {
717 if( aRef && !navigation.empty() )
718 aRef->SetNavigation( navigation );
719
720 return aRef;
721 };
722
723 // Check for a couple of very common cases and compile them straight to "object code".
724
725 if( aField.CmpNoCase( wxT( "NetClass" ) ) == 0 )
726 {
727 if( baseVar == wxT( "A" ) )
728 return withNav( std::make_unique<PCBEXPR_NETCLASS_REF>( 0 ) );
729 else if( baseVar == wxT( "B" ) )
730 return withNav( std::make_unique<PCBEXPR_NETCLASS_REF>( 1 ) );
731 else
732 return nullptr;
733 }
734 else if( aField.CmpNoCase( wxT( "ComponentClass" ) ) == 0 )
735 {
736 if( baseVar == wxT( "A" ) )
737 return withNav( std::make_unique<PCBEXPR_COMPONENT_CLASS_REF>( 0 ) );
738 else if( baseVar == wxT( "B" ) )
739 return withNav( std::make_unique<PCBEXPR_COMPONENT_CLASS_REF>( 1 ) );
740 else
741 return nullptr;
742 }
743 else if( aField.CmpNoCase( wxT( "NetName" ) ) == 0 )
744 {
745 if( baseVar == wxT( "A" ) )
746 return withNav( std::make_unique<PCBEXPR_NETNAME_REF>( 0 ) );
747 else if( baseVar == wxT( "B" ) )
748 return withNav( std::make_unique<PCBEXPR_NETNAME_REF>( 1 ) );
749 else
750 return nullptr;
751 }
752 else if( aField.CmpNoCase( wxT( "Type" ) ) == 0 )
753 {
754 if( baseVar == wxT( "A" ) )
755 return withNav( std::make_unique<PCBEXPR_TYPE_REF>( 0 ) );
756 else if( baseVar == wxT( "B" ) )
757 return withNav( std::make_unique<PCBEXPR_TYPE_REF>( 1 ) );
758 else
759 return nullptr;
760 }
761
762 if( baseVar == wxT( "A" ) || baseVar == wxT( "AB" ) )
763 vref = std::make_unique<PCBEXPR_VAR_REF>( 0 );
764 else if( baseVar == wxT( "B" ) )
765 vref = std::make_unique<PCBEXPR_VAR_REF>( 1 );
766 else if( baseVar == wxT( "L" ) )
767 vref = std::make_unique<PCBEXPR_VAR_REF>( 2 );
768 else
769 return nullptr;
770
771 vref->SetNavigation( navigation );
772
773 if( aField.length() == 0 ) // return reference to base object
774 return vref;
775
776 wxString field( aField );
777 field.Replace( wxT( "_" ), wxT( " " ) );
778
779 for( const PROPERTY_MANAGER::CLASS_INFO& cls : propMgr.GetAllClasses() )
780 {
781 if( propMgr.IsOfType( cls.type, TYPE_HASH( BOARD_ITEM ) ) )
782 {
783 PROPERTY_BASE* prop = propMgr.GetProperty( cls.type, field );
784
785 if( prop )
786 {
789
790 if( expressionType == LIBEVAL::VT_PARSE_ERROR
791 || ( vref->GetType() != LIBEVAL::VT_UNDEFINED && vref->GetType() != expressionType ) )
792 {
793 vref->SetType( LIBEVAL::VT_PARSE_ERROR );
794 return vref;
795 }
796
797 vref->AddAllowedClass( cls.type, prop, kind );
798 vref->SetType( expressionType );
799 }
800 }
801 }
802
803 if( vref->GetType() == LIBEVAL::VT_UNDEFINED )
804 vref->SetType( LIBEVAL::VT_PARSE_ERROR );
805
806 return vref;
807}
808
809
811{
812 if( m_items[0] )
813 return m_items[0]->GetBoard();
814
815 return nullptr;
816}
817
818
819/* --------------------------------------------------------------------------------------------
820 * Unit Resolvers
821 */
822
823const std::vector<wxString>& PCBEXPR_UNIT_RESOLVER::GetSupportedUnits() const
824{
825 static const std::vector<wxString> pcbUnits = { wxT( "mil" ), wxT( "mm" ), wxT( "in" ),
826 wxT( "deg" ), wxT( "fs" ), wxT( "ps" ) };
827
828
829 return pcbUnits;
830}
831
832
834{
835 return _( "must be mm, in, mil, deg, fs, or ps" );
836}
837
838
839const std::vector<EDA_UNITS>& PCBEXPR_UNIT_RESOLVER::GetSupportedUnitsTypes() const
840{
841 static const std::vector<EDA_UNITS> pcbUnits = { EDA_UNITS::MILS, EDA_UNITS::MM, EDA_UNITS::INCH,
843
844 return pcbUnits;
845}
846
847
848double PCBEXPR_UNIT_RESOLVER::Convert( const wxString& aString, int unitId ) const
849{
850 double v = wxAtof( aString );
851
852 switch( unitId )
853 {
857 case 3: return v;
860 default: return v;
861 }
862};
863
864
865const std::vector<wxString>& PCBEXPR_UNITLESS_RESOLVER::GetSupportedUnits() const
866{
867 static const std::vector<wxString> emptyUnits;
868
869 return emptyUnits;
870}
871
872
873const std::vector<EDA_UNITS>& PCBEXPR_UNITLESS_RESOLVER::GetSupportedUnitsTypes() const
874{
875 static const std::vector<EDA_UNITS> emptyUnits;
876
877 return emptyUnits;
878}
879
880
881double PCBEXPR_UNITLESS_RESOLVER::Convert( const wxString& aString, int unitId ) const
882{
883 return wxAtof( aString );
884};
885
886
888{
889 m_unitResolver.reset( aUnitResolver );
890}
891
892
893/* --------------------------------------------------------------------------------------------
894 * PCB Expression Evaluator
895 */
896
898 m_result( 0 ),
899 m_units( EDA_UNITS::MM ),
900 m_compiler( aUnitResolver ),
901 m_ucode(),
903{
904}
905
906
910
911
912bool PCBEXPR_EVALUATOR::Evaluate( const wxString& aExpr )
913{
914 PCBEXPR_UCODE ucode;
915 PCBEXPR_CONTEXT preflightContext( NULL_CONSTRAINT, F_Cu );
916
917 if( !m_compiler.Compile( aExpr.ToUTF8().data(), &ucode, &preflightContext ) )
918 return false;
919
920 PCBEXPR_CONTEXT evaluationContext( NULL_CONSTRAINT, F_Cu );
921 LIBEVAL::VALUE* result = ucode.Run( &evaluationContext );
922
923 if( result->GetType() == LIBEVAL::VT_NUMERIC )
924 {
925 m_result = KiROUND( result->AsDouble() );
926 m_units = result->GetUnits();
927 }
928
929 return true;
930}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
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,...
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
FOOTPRINT * GetParentFootprint() const
BOARD_ITEM_CONTAINER * GetParent() const
Definition board_item.h:235
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
PCB_LAYER_ID GetLayerID(const wxString &aLayerName) const
Return the ID of a layer.
Definition board.cpp:782
std::unordered_map< wxString, LSET > m_LayerExpressionCache
Definition board.h:1704
std::shared_mutex m_CachesMutex
Definition board.h:1690
A lightweight representation of a component class.
double AsDegrees() const
Definition eda_angle.h:116
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
static ENUM_MAP< T > & Instance()
Definition property.h:721
wxAny Get(PROPERTY_BASE *aProperty) const
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
wxString ToCSSString() const
Definition color4d.cpp:146
std::unique_ptr< UNIT_RESOLVER > m_unitResolver
VALUE * Run(CONTEXT *ctx)
void Set(double aValue)
virtual const wxString & AsString() const
bool StringIsWildcard() const
static VALUE * MakeNullValue()
virtual bool NotEqualTo(CONTEXT *aCtx, const VALUE *b) const
VAR_TYPE_T GetType() const
virtual bool EqualTo(CONTEXT *aCtx, const VALUE *b) const
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
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:38
bool IsGeometryDependent(const wxString &name) const
LIBEVAL::FUNC_CALL_REF Get(const wxString &name)
static PCBEXPR_BUILTIN_FUNCTIONS & Instance()
PCBEXPR_COMPILER(LIBEVAL::UNIT_RESOLVER *aUnitResolver)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
bool NotEqualTo(LIBEVAL::CONTEXT *aCtx, const LIBEVAL::VALUE *b) const override
const wxString & AsString() const override
PCBEXPR_COMPONENT_CLASS_VALUE(BOARD_ITEM *aItem)
bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
BOARD_ITEM * m_items[2]
BOARD * GetBoard() const
KICAD_T GetEffectiveType(const BOARD_ITEM *aItem) const
PCB_LAYER_ID GetLayer() const
std::map< const BOARD_ITEM *, KICAD_T > m_typeOverrides
BOARD_ITEM * GetItem(int index) const
LIBEVAL::ERROR_STATUS m_errorStatus
PCBEXPR_EVALUATOR(LIBEVAL::UNIT_RESOLVER *aUnitResolver)
PCBEXPR_COMPILER m_compiler
bool Evaluate(const wxString &aExpr)
PCBEXPR_LAYER_VALUE(PCB_LAYER_ID aLayer)
virtual bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
BOARD_CONNECTED_ITEM * m_item
PCBEXPR_NETCLASS_VALUE(BOARD_CONNECTED_ITEM *aItem)
const wxString & AsString() const override
bool NotEqualTo(LIBEVAL::CONTEXT *aCtx, const LIBEVAL::VALUE *b) const override
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
BOARD_CONNECTED_ITEM * m_item
PCBEXPR_NET_VALUE(BOARD_CONNECTED_ITEM *aItem)
bool NotEqualTo(LIBEVAL::CONTEXT *aCtx, const LIBEVAL::VALUE *b) const override
const wxString & AsString() const override
bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
PCBEXPR_PINTYPE_VALUE(const wxString &aPinTypeName)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
virtual std::unique_ptr< LIBEVAL::VAR_REF > CreateVarRef(const wxString &aVar, const wxString &aField) override
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall(const wxString &aName) override
bool m_hasGeometryDependentFunctions
const std::vector< wxString > & GetSupportedUnits() const override
const std::vector< EDA_UNITS > & GetSupportedUnitsTypes() const override
double Convert(const wxString &aString, int unitId) const override
double Convert(const wxString &aString, int unitId) const override
const std::vector< EDA_UNITS > & GetSupportedUnitsTypes() const override
wxString GetSupportedUnitsMessage() const override
const std::vector< wxString > & GetSupportedUnits() const override
std::vector< PCBEXPR_NAV_STEP > m_navigation
static PCBEXPR_PROPERTY_KIND ClassifyProperty(const PROPERTY_BASE *aProperty)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
std::unordered_map< TYPE_ID, MATCHED_PROPERTY > m_matchingTypes
static LIBEVAL::VAR_TYPE_T ExpressionType(PCBEXPR_PROPERTY_KIND aKind)
LIBEVAL::VAR_TYPE_T m_type
BOARD_ITEM * GetObject(const LIBEVAL::CONTEXT *aCtx) const
virtual size_t TypeHash() const =0
Return type-id of the property type.
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
Definition property.h:246
Provide class metadata.Helper macro to map type hashes to names.
CLASSES_INFO GetAllClasses()
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.
A type-safe container of any type.
Definition ki_any.h:92
@ NULL_CONSTRAINT
Definition drc_rule.h:50
#define _(s)
EDA_UNITS
Definition eda_units.h:44
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition layer_id.cpp:31
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_Cu
Definition layer_ids.h:60
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:750
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue to a double.
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
PCBEXPR_PROPERTY_KIND
PCBEXPR_NAV_STEP
#define TYPE_HASH(x)
Definition property.h:74
size_t TYPE_ID
Unique type identifier.
bool WildCompareString(const wxString &pattern, const wxString &string_to_tst, bool case_sensitive)
Compare a string against wild card (* and ?) pattern using the usual rules.
static const long long MM
wxString result
Test unit parsing edge cases and error handling.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:79