KiCad PCB EDA Suite
DRC_RULE_CONDITION Class Reference

#include <drc_rule_condition.h>

Public Member Functions

 DRC_RULE_CONDITION (const wxString &aExpression="")
 
 ~DRC_RULE_CONDITION ()
 
bool EvaluateFor (const BOARD_ITEM *aItemA, const BOARD_ITEM *aItemB, int aConstraint, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
 
bool Compile (REPORTER *aReporter, int aSourceLine=0, int aSourceOffset=0)
 
void SetExpression (const wxString &aExpression)
 
wxString GetExpression () const
 

Private Attributes

wxString m_expression
 
std::unique_ptr< PCB_EXPR_UCODEm_ucode
 

Detailed Description

Definition at line 35 of file drc_rule_condition.h.

Constructor & Destructor Documentation

◆ DRC_RULE_CONDITION()

DRC_RULE_CONDITION::DRC_RULE_CONDITION ( const wxString &  aExpression = "")

Definition at line 31 of file drc_rule_condition.cpp.

31 :
32 m_expression( aExpression ),
33 m_ucode ( nullptr )
34{
35}
std::unique_ptr< PCB_EXPR_UCODE > m_ucode

◆ ~DRC_RULE_CONDITION()

DRC_RULE_CONDITION::~DRC_RULE_CONDITION ( )

Definition at line 38 of file drc_rule_condition.cpp.

39{
40}

Member Function Documentation

◆ Compile()

bool DRC_RULE_CONDITION::Compile ( REPORTER aReporter,
int  aSourceLine = 0,
int  aSourceOffset = 0 
)

Definition at line 89 of file drc_rule_condition.cpp.

90{
91 PCB_EXPR_COMPILER compiler;
92
93 if( aReporter )
94 {
95 compiler.SetErrorCallback(
96 [&]( const wxString& aMessage, int aOffset )
97 {
98 wxString rest;
99 wxString first = aMessage.BeforeFirst( '|', &rest );
100 wxString msg = wxString::Format( _( "ERROR: <a href='%d:%d'>%s</a>%s" ),
101 aSourceLine,
102 aSourceOffset + aOffset,
103 first,
104 rest );
105
106 aReporter->Report( msg, RPT_SEVERITY_ERROR );
107 } );
108 }
109
110 m_ucode = std::make_unique<PCB_EXPR_UCODE>();
111
112 PCB_EXPR_CONTEXT preflightContext( 0, F_Cu );
113
114 bool ok = compiler.Compile( GetExpression().ToUTF8().data(), m_ucode.get(), &preflightContext );
115 return ok;
116}
wxString GetExpression() const
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
#define _(s)
@ F_Cu
Definition: layer_ids.h:64
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
@ RPT_SEVERITY_ERROR

References _, LIBEVAL::COMPILER::Compile(), F_Cu, Format(), GetExpression(), m_ucode, REPORTER::Report(), RPT_SEVERITY_ERROR, and LIBEVAL::COMPILER::SetErrorCallback().

Referenced by DRC_ENGINE::compileRules(), and DRC_RULES_PARSER::parseConstraint().

◆ EvaluateFor()

bool DRC_RULE_CONDITION::EvaluateFor ( const BOARD_ITEM aItemA,
const BOARD_ITEM aItemB,
int  aConstraint,
PCB_LAYER_ID  aLayer,
REPORTER aReporter = nullptr 
)

Definition at line 43 of file drc_rule_condition.cpp.

45{
46 if( GetExpression().IsEmpty() )
47 return true;
48
49 if( !m_ucode )
50 {
51 if( aReporter )
52 aReporter->Report( _( "ERROR in expression." ) );
53
54 return false;
55 }
56
57 PCB_EXPR_CONTEXT ctx( aConstraint, aLayer );
58
59 if( aReporter )
60 {
61 ctx.SetErrorCallback(
62 [&]( const wxString& aMessage, int aOffset )
63 {
64 aReporter->Report( _( "ERROR:" ) + wxS( " " ) + aMessage );
65 } );
66 }
67
68 BOARD_ITEM* a = const_cast<BOARD_ITEM*>( aItemA );
69 BOARD_ITEM* b = const_cast<BOARD_ITEM*>( aItemB );
70
71 ctx.SetItems( a, b );
72
73 if( m_ucode->Run( &ctx )->AsDouble() != 0.0 )
74 {
75 return true;
76 }
77 else if( aItemB ) // Conditions are commutative
78 {
79 ctx.SetItems( b, a );
80
81 if( m_ucode->Run( &ctx )->AsDouble() != 0.0 )
82 return true;
83 }
84
85 return false;
86}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58

References _, GetExpression(), m_ucode, REPORTER::Report(), LIBEVAL::CONTEXT::SetErrorCallback(), and PCB_EXPR_CONTEXT::SetItems().

◆ GetExpression()

wxString DRC_RULE_CONDITION::GetExpression ( ) const
inline

Definition at line 47 of file drc_rule_condition.h.

47{ return m_expression; }

References m_expression.

Referenced by Compile(), and EvaluateFor().

◆ SetExpression()

void DRC_RULE_CONDITION::SetExpression ( const wxString &  aExpression)
inline

Definition at line 46 of file drc_rule_condition.h.

46{ m_expression = aExpression; }

References m_expression.

Member Data Documentation

◆ m_expression

wxString DRC_RULE_CONDITION::m_expression
private

Definition at line 50 of file drc_rule_condition.h.

Referenced by GetExpression(), and SetExpression().

◆ m_ucode

std::unique_ptr<PCB_EXPR_UCODE> DRC_RULE_CONDITION::m_ucode
private

Definition at line 51 of file drc_rule_condition.h.

Referenced by Compile(), and EvaluateFor().


The documentation for this class was generated from the following files: