KiCad PCB EDA Suite
drc_engine.h
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#ifndef DRC_ENGINE_H
25#define DRC_ENGINE_H
26
27#include <memory>
28#include <vector>
29#include <unordered_map>
30
31#include <units_provider.h>
32#include <geometry/shape.h>
33
34#include <drc/drc_rule.h>
35
36
39class PCB_EDIT_FRAME;
41class BOARD_ITEM;
42class BOARD;
43class PCB_MARKER;
44class NETCLASS;
45class NETLIST;
46class NETINFO_ITEM;
48class REPORTER;
49class wxFileName;
50
51namespace KIGFX
52{
53 class VIEW_OVERLAY;
54};
55
56void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
57
58#define drc_dbg(level, fmt, ...) \
59 drcPrintDebugMessage(level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
60
62class DRC_ITEM;
63class DRC_RULE;
64class DRC_CONSTRAINT;
65
66
67typedef std::function<void( const std::shared_ptr<DRC_ITEM>& aItem,
68 const VECTOR2I& aPos,
69 int aLayer )> DRC_VIOLATION_HANDLER;
70
71
83{
84public:
85 DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
87
88 void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
89 BOARD* GetBoard() const { return m_board; }
90
91 void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
93
94 void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
96
97 void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
99
100 void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay )
101 {
102 m_debugOverlay = aOverlay;
103 }
104
105 std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
106
111 {
112 m_violationHandler = std::move( aHandler );
113 }
114
116 {
118 }
119
125
126 /*
127 * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
128 * information.
129 *
130 * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
131 * via a thrown PARSE_ERROR exception.
132 */
133 void SetLogReporter( REPORTER* aReporter ) { m_reporter = aReporter; }
134
140 void InitEngine( const wxFileName& aRulePath );
141
145 void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints );
146
147 bool IsErrorLimitExceeded( int error_code );
148
149 DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
150 const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
151 REPORTER* aReporter = nullptr );
152
154 PCB_LAYER_ID aLayer, REPORTER* aReporter = nullptr );
155
156 void ProcessAssertions( const BOARD_ITEM* a,
157 std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
158 REPORTER* aReporter = nullptr );
159
160 bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
161
163 bool GetTestFootprints() const { return m_testFootprints; }
164
165 bool RulesValid() { return m_rulesValid; }
166
167 void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
168 int aMarkerLayer );
169
170 bool KeepRefreshing( bool aWait = false );
171 void AdvanceProgress();
172 void SetMaxProgress( int aSize );
173 bool ReportProgress( double aProgress );
174 bool ReportPhase( const wxString& aMessage );
175 void ReportAux( const wxString& aStr );
176 bool IsCancelled() const;
177
178 bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
179 std::set<int> QueryDistinctConstraints( DRC_CONSTRAINT_T aConstraintId );
180
181 std::vector<DRC_TEST_PROVIDER*> GetTestProviders() const { return m_testProviders; };
182
183 DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
184
185 static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
186
194 static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
195 wxString& aBaseDpName );
196
201 static bool IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
202 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem );
203
204private:
205 void addRule( std::shared_ptr<DRC_RULE>& rule )
206 {
207 m_rules.push_back(rule);
208 }
209
215 void loadRules( const wxFileName& aPath );
216
217 void compileRules();
218
220 {
223 std::shared_ptr<DRC_RULE> parentRule;
225 };
226
227 void loadImplicitRules();
228 std::shared_ptr<DRC_RULE> createImplicitRule( const wxString& name );
229
230protected:
235
236 std::vector<std::shared_ptr<DRC_RULE>> m_rules;
238 std::vector<DRC_TEST_PROVIDER*> m_testProviders;
239
240 std::vector<int> m_errorLimits;
243
244 // constraint -> rule -> provider
245 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
246
250
251 std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
252};
253
254#endif // DRC_H
const char * name
Definition: DXF_plotter.cpp:56
Container for design settings for a BOARD object.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:50
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
Design Rule Checker object that performs all the DRC tests.
Definition: drc_engine.h:83
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: drc_engine.h:98
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition: drc_engine.h:245
REPORTER * m_reporter
Definition: drc_engine.h:248
void AdvanceProgress()
bool GetTestFootprints() const
Definition: drc_engine.h:163
bool m_testFootprints
Definition: drc_engine.h:242
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition: drc_engine.h:205
PROGRESS_REPORTER * m_progressReporter
Definition: drc_engine.h:249
void loadRules(const wxFileName &aPath)
Load and parse a rule set from an sexpr text file.
Definition: drc_engine.cpp:458
std::vector< DRC_TEST_PROVIDER * > m_testProviders
Definition: drc_engine.h:238
BOARD * GetBoard() const
Definition: drc_engine.h:89
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition: drc_engine.h:105
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition: drc_engine.h:91
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition: drc_engine.h:123
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition: drc_engine.h:100
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition: drc_engine.h:97
bool GetReportAllTrackErrors() const
Definition: drc_engine.h:162
void compileRules()
Definition: drc_engine.cpp:481
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition: drc_engine.h:233
NETLIST * m_schematicNetlist
Definition: drc_engine.h:234
bool KeepRefreshing(bool aWait=false)
void SetLogReporter(REPORTER *aReporter)
Definition: drc_engine.h:133
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition: drc_engine.h:110
BOARD * m_board
Definition: drc_engine.h:232
bool m_reportAllTrackErrors
Definition: drc_engine.h:241
bool ReportProgress(double aProgress)
DRC_TEST_PROVIDER * GetTestProvider(const wxString &name) const
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
BOARD_DESIGN_SETTINGS * GetDesignSettings() const
Definition: drc_engine.h:92
bool RulesValid()
Definition: drc_engine.h:165
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints)
Run the DRC tests.
Definition: drc_engine.cpp:565
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aMarkerLayer)
void SetMaxProgress(int aSize)
void ReportAux(const wxString &aStr)
DRC_ENGINE(BOARD *aBoard=nullptr, BOARD_DESIGN_SETTINGS *aSettings=nullptr)
Definition: drc_engine.cpp:66
std::vector< int > m_errorLimits
Definition: drc_engine.h:240
bool IsErrorLimitExceeded(int error_code)
void ProcessAssertions(const BOARD_ITEM *a, std::function< void(const DRC_CONSTRAINT *)> aFailureHandler, REPORTER *aReporter=nullptr)
void loadImplicitRules()
Definition: drc_engine.cpp:141
DRC_VIOLATION_HANDLER m_violationHandler
Definition: drc_engine.h:247
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:671
std::vector< std::shared_ptr< DRC_RULE > > m_rules
Definition: drc_engine.h:236
std::shared_ptr< DRC_RULE > createImplicitRule(const wxString &name)
Definition: drc_engine.cpp:128
void ClearViolationHandler()
Definition: drc_engine.h:115
void SetSchematicNetlist(NETLIST *aNetlist)
Definition: drc_engine.h:94
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:124
bool IsCancelled() const
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition: drc_engine.h:181
static bool IsNetADiffPair(BOARD *aBoard, NETINFO_ITEM *aNet, int &aNetP, int &aNetN)
static bool IsNetTieExclusion(int aTrackNetCode, PCB_LAYER_ID aTrackLayer, const VECTOR2I &aCollisionPos, BOARD_ITEM *aCollidingItem)
Check if the given collision between a track and another item occurs during the track's entry into a ...
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Definition: drc_engine.h:251
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:95
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
Definition: drc_engine.cpp:512
DRC_CONSTRAINT EvalZoneConnection(const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:610
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.
bool ReportPhase(const wxString &aMessage)
bool m_rulesValid
Definition: drc_engine.h:237
void SetBoard(BOARD *aBoard)
Definition: drc_engine.h:88
BOARD_DESIGN_SETTINGS * m_designSettings
Definition: drc_engine.h:231
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out #DRC_ITEMs and po...
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:530
A collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:47
Handle the data for a net.
Definition: netinfo.h:66
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:213
The main frame for Pcbnew.
A progress reporter interface for use in multi-threaded environments.
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
void drcPrintDebugMessage(int level, const wxString &msg, const char *function, int line)
Definition: drc_engine.cpp:52
std::function< void(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aLayer)> DRC_VIOLATION_HANDLER
Definition: drc_engine.h:69
DRC_CONSTRAINT_T
Definition: drc_rule.h:44
EDA_UNITS
Definition: eda_units.h:43
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:266
std::shared_ptr< DRC_RULE > parentRule
Definition: drc_engine.h:223
DRC_RULE_CONDITION * condition
Definition: drc_engine.h:222