KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_rule_condition.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
25
#include <
board_item.h
>
26
#include <
reporter.h
>
27
#include <
drc/drc_rule_condition.h
>
28
#include <
pcbexpr_evaluator.h
>
29
30
31
DRC_RULE_CONDITION::DRC_RULE_CONDITION
(
const
wxString& aExpression ) :
32
m_expression
( aExpression ),
33
m_ucode
( nullptr )
34
{
35
}
36
37
38
DRC_RULE_CONDITION::~DRC_RULE_CONDITION
()
39
{
40
}
41
42
43
bool
DRC_RULE_CONDITION::EvaluateFor
(
const
BOARD_ITEM
* aItemA,
const
BOARD_ITEM
* aItemB,
44
int
aConstraint,
PCB_LAYER_ID
aLayer,
REPORTER
* aReporter )
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
PCBEXPR_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
}
87
88
89
bool
DRC_RULE_CONDITION::Compile
(
REPORTER
* aReporter,
int
aSourceLine,
int
aSourceOffset )
90
{
91
PCBEXPR_COMPILER
compiler(
new
PCBEXPR_UNIT_RESOLVER
() );
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<PCBEXPR_UCODE>();
111
112
PCBEXPR_CONTEXT
preflightContext( 0,
F_Cu
);
113
114
bool
ok = compiler.
Compile
(
GetExpression
().ToUTF8().data(),
m_ucode
.get(), &preflightContext );
115
return
ok;
116
}
117
118
board_item.h
BOARD_ITEM
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition
board_item.h:79
DRC_RULE_CONDITION::m_ucode
std::unique_ptr< PCBEXPR_UCODE > m_ucode
Definition
drc_rule_condition.h:51
DRC_RULE_CONDITION::DRC_RULE_CONDITION
DRC_RULE_CONDITION(const wxString &aExpression="")
Definition
drc_rule_condition.cpp:31
DRC_RULE_CONDITION::Compile
bool Compile(REPORTER *aReporter, int aSourceLine=0, int aSourceOffset=0)
Definition
drc_rule_condition.cpp:89
DRC_RULE_CONDITION::m_expression
wxString m_expression
Definition
drc_rule_condition.h:50
DRC_RULE_CONDITION::~DRC_RULE_CONDITION
~DRC_RULE_CONDITION()
Definition
drc_rule_condition.cpp:38
DRC_RULE_CONDITION::GetExpression
wxString GetExpression() const
Definition
drc_rule_condition.h:47
DRC_RULE_CONDITION::EvaluateFor
bool EvaluateFor(const BOARD_ITEM *aItemA, const BOARD_ITEM *aItemB, int aConstraint, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition
drc_rule_condition.cpp:43
LIBEVAL::COMPILER::SetErrorCallback
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
Definition
libeval_compiler.h:555
LIBEVAL::COMPILER::Compile
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
Definition
libeval_compiler.cpp:326
LIBEVAL::CONTEXT::SetErrorCallback
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
Definition
libeval_compiler.h:386
PCBEXPR_COMPILER
Definition
pcbexpr_evaluator.h:237
PCBEXPR_CONTEXT
Definition
pcbexpr_evaluator.h:55
PCBEXPR_CONTEXT::SetItems
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
Definition
pcbexpr_evaluator.h:65
PCBEXPR_UNIT_RESOLVER
Definition
pcbexpr_evaluator.h:215
REPORTER
A pure virtual class used to derive REPORTER objects from.
Definition
reporter.h:73
REPORTER::Report
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition
reporter.h:102
drc_rule_condition.h
_
#define _(s)
Definition
eda_3d_actions.cpp:36
PCB_LAYER_ID
PCB_LAYER_ID
A quick note on layer IDs:
Definition
layer_ids.h:60
F_Cu
@ F_Cu
Definition
layer_ids.h:64
pcbexpr_evaluator.h
RPT_SEVERITY_ERROR
@ RPT_SEVERITY_ERROR
Definition
report_severity.h:32
reporter.h
src
pcbnew
drc
drc_rule_condition.cpp
Generated on Sun Sep 21 2025 01:05:28 for KiCad PCB EDA Suite by
1.13.2