Add field access to custom DRC rules.

ADDED getField() method to custom DRC rules.
This commit is contained in:
Jeff Young 2021-11-08 14:22:18 +00:00
parent 390ca93388
commit 6cec58cf75
3 changed files with 86 additions and 22 deletions

View File

@ -116,14 +116,14 @@ bool VALUE::EqualTo( CONTEXT* aCtx, const VALUE* b ) const
if( m_type == VT_NUMERIC && b->m_type == VT_NUMERIC ) if( m_type == VT_NUMERIC && b->m_type == VT_NUMERIC )
{ {
return m_valueDbl == b->m_valueDbl; return AsDouble() == b->AsDouble();
} }
else if( m_type == VT_STRING && b->m_type == VT_STRING ) else if( m_type == VT_STRING && b->m_type == VT_STRING )
{ {
if( b->m_stringIsWildcard ) if( b->m_stringIsWildcard )
return WildCompareString( b->m_valueStr, m_valueStr, false ); return WildCompareString( b->AsString(), AsString(), false );
else else
return !m_valueStr.CmpNoCase( b->m_valueStr ); return !AsString().CmpNoCase( b->AsString() );
} }
return false; return false;

View File

@ -188,7 +188,8 @@ public:
m_type( VT_UNDEFINED ), m_type( VT_UNDEFINED ),
m_valueDbl( 0 ), m_valueDbl( 0 ),
m_stringIsWildcard( false ), m_stringIsWildcard( false ),
m_isDeferredDbl( false ) m_isDeferredDbl( false ),
m_isDeferredStr( false )
{}; {};
VALUE( const wxString& aStr, bool aIsWildcard = false ) : VALUE( const wxString& aStr, bool aIsWildcard = false ) :
@ -196,14 +197,16 @@ public:
m_valueDbl( 0 ), m_valueDbl( 0 ),
m_valueStr( aStr ), m_valueStr( aStr ),
m_stringIsWildcard( aIsWildcard ), m_stringIsWildcard( aIsWildcard ),
m_isDeferredDbl( false ) m_isDeferredDbl( false ),
m_isDeferredStr( false )
{}; {};
VALUE( const double aVal ) : VALUE( const double aVal ) :
m_type( VT_NUMERIC ), m_type( VT_NUMERIC ),
m_valueDbl( aVal ), m_valueDbl( aVal ),
m_stringIsWildcard( false ), m_stringIsWildcard( false ),
m_isDeferredDbl( false ) m_isDeferredDbl( false ),
m_isDeferredStr( false )
{}; {};
virtual ~VALUE() virtual ~VALUE()
@ -222,6 +225,12 @@ public:
virtual const wxString& AsString() const virtual const wxString& AsString() const
{ {
if( m_isDeferredStr )
{
m_valueStr = m_lambdaStr();
m_isDeferredStr = false;
}
return m_valueStr; return m_valueStr;
} }
@ -245,6 +254,13 @@ public:
m_isDeferredDbl = true; m_isDeferredDbl = true;
} }
void SetDeferredEval( std::function<wxString()> aLambda )
{
m_type = VT_STRING;
m_lambdaStr = aLambda;
m_isDeferredStr = true;
}
void Set( const wxString& aValue ) void Set( const wxString& aValue )
{ {
m_type = VT_STRING; m_type = VT_STRING;
@ -261,13 +277,16 @@ public:
} }
private: private:
VAR_TYPE_T m_type; VAR_TYPE_T m_type;
mutable double m_valueDbl; mutable double m_valueDbl; // mutable to support deferred evaluation
wxString m_valueStr; mutable wxString m_valueStr; // mutable to support deferred evaluation
bool m_stringIsWildcard; bool m_stringIsWildcard;
mutable bool m_isDeferredDbl; mutable bool m_isDeferredDbl;
std::function<double()> m_lambdaDbl; std::function<double()> m_lambdaDbl;
mutable bool m_isDeferredStr;
std::function<wxString()> m_lambdaStr;
}; };
class VAR_REF class VAR_REF

View File

@ -91,7 +91,7 @@ static void existsOnLayer( LIBEVAL::CONTEXT* aCtx, void *self )
{ {
if( aCtx->HasErrorCallback() ) if( aCtx->HasErrorCallback() )
{ {
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ), aCtx->ReportError( wxString::Format( _( "Missing layer name argument to %s." ),
wxT( "existsOnLayer()" ) ) ); wxT( "existsOnLayer()" ) ) );
} }
@ -260,7 +260,8 @@ static void insideCourtyard( LIBEVAL::CONTEXT* aCtx, void* self )
{ {
if( aCtx->HasErrorCallback() ) if( aCtx->HasErrorCallback() )
{ {
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ), aCtx->ReportError( wxString::Format( _( "Missing footprint identifier argument "
"(A, B, or reference designator) to %s." ),
wxT( "insideCourtyard()" ) ) ); wxT( "insideCourtyard()" ) ) );
} }
@ -326,7 +327,8 @@ static void insideFrontCourtyard( LIBEVAL::CONTEXT* aCtx, void* self )
{ {
if( aCtx->HasErrorCallback() ) if( aCtx->HasErrorCallback() )
{ {
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ), aCtx->ReportError( wxString::Format( _( "Missing footprint identifier argument "
"(A, B, or reference designator) to %s." ),
wxT( "insideFrontCourtyard()" ) ) ); wxT( "insideFrontCourtyard()" ) ) );
} }
@ -392,7 +394,8 @@ static void insideBackCourtyard( LIBEVAL::CONTEXT* aCtx, void* self )
{ {
if( aCtx->HasErrorCallback() ) if( aCtx->HasErrorCallback() )
{ {
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ), aCtx->ReportError( wxString::Format( _( "Missing footprint identifier argument "
"(A, B, or reference designator) to %s." ),
wxT( "insideBackCourtyard()" ) ) ); wxT( "insideBackCourtyard()" ) ) );
} }
return; return;
@ -610,7 +613,8 @@ static void insideArea( LIBEVAL::CONTEXT* aCtx, void* self )
{ {
if( aCtx->HasErrorCallback() ) if( aCtx->HasErrorCallback() )
{ {
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ), aCtx->ReportError( wxString::Format( _( "Missing rule-area identifier argument "
"(A, B, or rule-area name) to %s." ),
wxT( "insideArea()" ) ) ); wxT( "insideArea()" ) ) );
} }
@ -712,7 +716,7 @@ static void memberOf( LIBEVAL::CONTEXT* aCtx, void* self )
{ {
if( aCtx->HasErrorCallback() ) if( aCtx->HasErrorCallback() )
{ {
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ), aCtx->ReportError( wxString::Format( _( "Missing group name argument to %s." ),
wxT( "memberOf()" ) ) ); wxT( "memberOf()" ) ) );
} }
return; return;
@ -818,20 +822,20 @@ static void inDiffPair( LIBEVAL::CONTEXT* aCtx, void* self )
result->Set( 0.0 ); result->Set( 0.0 );
aCtx->Push( result ); aCtx->Push( result );
if( !item || !item->GetBoard() )
return;
if( !arg ) if( !arg )
{ {
if( aCtx->HasErrorCallback() ) if( aCtx->HasErrorCallback() )
{ {
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ), aCtx->ReportError( wxString::Format( _( "Missing diff-pair name argument to %s." ),
wxT( "inDiffPair()" ) ) ); wxT( "inDiffPair()" ) ) );
} }
return; return;
} }
if( !item || !item->GetBoard() )
return;
result->SetDeferredEval( result->SetDeferredEval(
[item, arg]() -> double [item, arg]() -> double
{ {
@ -856,6 +860,46 @@ static void inDiffPair( LIBEVAL::CONTEXT* aCtx, void* self )
} }
static void getField( LIBEVAL::CONTEXT* aCtx, void* self )
{
LIBEVAL::VALUE* arg = aCtx->Pop();
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( "" );
aCtx->Push( result );
if( !arg )
{
if( aCtx->HasErrorCallback() )
{
aCtx->ReportError( wxString::Format( _( "Missing field name argument to %s." ),
wxT( "getField()" ) ) );
}
return;
}
if( !item || !item->GetBoard() )
return;
result->SetDeferredEval(
[item, arg]() -> wxString
{
if( item && item->Type() == PCB_FOOTPRINT_T )
{
FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
if( fp->HasProperty( arg->AsString() ) )
return fp->GetProperty( arg->AsString() );
}
return "";
} );
}
PCB_EXPR_BUILTIN_FUNCTIONS::PCB_EXPR_BUILTIN_FUNCTIONS() PCB_EXPR_BUILTIN_FUNCTIONS::PCB_EXPR_BUILTIN_FUNCTIONS()
{ {
RegisterAllFunctions(); RegisterAllFunctions();
@ -877,6 +921,7 @@ void PCB_EXPR_BUILTIN_FUNCTIONS::RegisterAllFunctions()
RegisterFunc( "fromTo('x','y')", exprFromTo ); RegisterFunc( "fromTo('x','y')", exprFromTo );
RegisterFunc( "isCoupledDiffPair()", isCoupledDiffPair ); RegisterFunc( "isCoupledDiffPair()", isCoupledDiffPair );
RegisterFunc( "inDiffPair('x')", inDiffPair ); RegisterFunc( "inDiffPair('x')", inDiffPair );
RegisterFunc( "getField('x')", getField );
} }