KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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#include <lset.h>
34#include <drc/drc_rule.h>
35
36
37class BOARD_COMMIT;
42class PCB_EDIT_FRAME;
44class BOARD_ITEM;
45class BOARD;
46class PCB_MARKER;
47class NETCLASS;
48class NETLIST;
49class NETINFO_ITEM;
51class REPORTER;
52class wxFileName;
53
54namespace KIGFX
55{
56 class VIEW_OVERLAY;
57};
58
59void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
60
61#define drc_dbg(level, fmt, ...) \
62 drcPrintDebugMessage(level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
63
65class DRC_ITEM;
66class DRC_RULE;
67class DRC_CONSTRAINT;
68
69
70typedef std::function<void( const std::shared_ptr<DRC_ITEM>& aItem,
71 const VECTOR2I& aPos,
72 int aLayer )> DRC_VIOLATION_HANDLER;
73
74
75typedef std::function<void( PCB_MARKER* aMarker )> DRC_GRAPHICS_HANDLER;
76
77
89{
90 // They need to change / restore the violation handler
93
94public:
95 DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
96 virtual ~DRC_ENGINE();
97
98 void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
99 BOARD* GetBoard() const { return m_board; }
100
101 void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
103
104 void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
106
107 void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
109
110 void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay )
111 {
112 m_debugOverlay = aOverlay;
113 }
114
115 std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
116
121 {
122 m_violationHandler = std::move( aHandler );
123 }
124
126 {
128 }
129
130
135 {
136 m_graphicsHandler = std::move( aHandler );
137 }
138
140
142 {
144 m_graphicsHandler( aMarker );
145 }
146
152
153 /*
154 * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
155 * information.
156 *
157 * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
158 * via a thrown PARSE_ERROR exception.
159 */
160 void SetLogReporter( REPORTER* aReporter ) { m_reporter = aReporter; }
161
167 void InitEngine( const wxFileName& aRulePath );
168
172 void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints,
173 BOARD_COMMIT* aCommit = nullptr );
174
175 bool IsErrorLimitExceeded( int error_code );
176
177 DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
178 const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
179 REPORTER* aReporter = nullptr );
180
182 PCB_LAYER_ID aLayer, REPORTER* aReporter = nullptr );
183
184 void ProcessAssertions( const BOARD_ITEM* a,
185 std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
186 REPORTER* aReporter = nullptr );
187
188 bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
189
191 bool GetTestFootprints() const { return m_testFootprints; }
192
193 bool RulesValid() { return m_rulesValid; }
194
195 void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
196 int aMarkerLayer );
197
198 bool KeepRefreshing( bool aWait = false );
199 void AdvanceProgress();
200 void SetMaxProgress( int aSize );
201 bool ReportProgress( double aProgress );
202 bool ReportPhase( const wxString& aMessage );
203 void ReportAux( const wxString& aStr );
204 bool IsCancelled() const;
205
206 bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
207 std::set<int> QueryDistinctConstraints( DRC_CONSTRAINT_T aConstraintId );
208
209 std::vector<DRC_TEST_PROVIDER*> GetTestProviders() const { return m_testProviders; };
210
211 DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
212
213 static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
214
222 static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
223 wxString& aBaseDpName );
224
229 bool IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
230 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem );
231
232private:
233 void addRule( std::shared_ptr<DRC_RULE>& rule )
234 {
235 m_rules.push_back(rule);
236 }
237
243 void loadRules( const wxFileName& aPath );
244
245 void compileRules();
246
248 {
251 std::shared_ptr<DRC_RULE> parentRule;
253 };
254
255 void loadImplicitRules();
256 std::shared_ptr<DRC_RULE> createImplicitRule( const wxString& name );
257
258protected:
263
264 std::vector<std::shared_ptr<DRC_RULE>> m_rules;
266 std::vector<DRC_TEST_PROVIDER*> m_testProviders;
267
268 std::vector<int> m_errorLimits;
271
272 // constraint -> rule -> provider
273 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
274
275
280
281 std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
282};
283
284#endif // DRC_H
const char * name
Definition: DXF_plotter.cpp:57
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:79
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
Design Rule Checker object that performs all the DRC tests.
Definition: drc_engine.h:89
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: drc_engine.h:108
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition: drc_engine.h:273
REPORTER * m_reporter
Definition: drc_engine.h:278
void SetGraphicsHandler(DRC_GRAPHICS_HANDLER aHandler)
Set an optional DRC graphics handler (receives a PCB_MARKER).
Definition: drc_engine.h:134
void AdvanceProgress()
bool GetTestFootprints() const
Definition: drc_engine.h:191
void ClearGraphicsHandler()
Definition: drc_engine.h:139
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
Definition: drc_engine.cpp:598
bool m_testFootprints
Definition: drc_engine.h:270
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition: drc_engine.h:233
PROGRESS_REPORTER * m_progressReporter
Definition: drc_engine.h:279
void loadRules(const wxFileName &aPath)
Load and parse a rule set from an sexpr text file.
Definition: drc_engine.cpp:491
std::vector< DRC_TEST_PROVIDER * > m_testProviders
Definition: drc_engine.h:266
BOARD * GetBoard() const
Definition: drc_engine.h:99
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition: drc_engine.h:115
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition: drc_engine.h:101
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition: drc_engine.h:150
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition: drc_engine.h:110
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition: drc_engine.h:107
bool GetReportAllTrackErrors() const
Definition: drc_engine.h:190
void compileRules()
Definition: drc_engine.cpp:514
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition: drc_engine.h:261
NETLIST * m_schematicNetlist
Definition: drc_engine.h:262
bool KeepRefreshing(bool aWait=false)
void GraphicsHandler(PCB_MARKER *aMarker)
Definition: drc_engine.h:141
void SetLogReporter(REPORTER *aReporter)
Definition: drc_engine.h:160
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition: drc_engine.h:120
BOARD * m_board
Definition: drc_engine.h:260
bool m_reportAllTrackErrors
Definition: drc_engine.h:269
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:102
bool RulesValid()
Definition: drc_engine.h:193
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aMarkerLayer)
void SetMaxProgress(int aSize)
void ReportAux(const wxString &aStr)
std::vector< int > m_errorLimits
Definition: drc_engine.h:268
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:143
DRC_VIOLATION_HANDLER m_violationHandler
Definition: drc_engine.h:277
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:679
std::vector< std::shared_ptr< DRC_RULE > > m_rules
Definition: drc_engine.h:264
std::shared_ptr< DRC_RULE > createImplicitRule(const wxString &name)
Definition: drc_engine.cpp:130
void ClearViolationHandler()
Definition: drc_engine.h:125
void SetSchematicNetlist(NETLIST *aNetlist)
Definition: drc_engine.h:104
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:151
bool IsCancelled() const
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition: drc_engine.h:209
static bool IsNetADiffPair(BOARD *aBoard, NETINFO_ITEM *aNet, int &aNetP, int &aNetN)
DRC_GRAPHICS_HANDLER m_graphicsHandler
Definition: drc_engine.h:276
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 ...
virtual ~DRC_ENGINE()
Definition: drc_engine.cpp:87
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Definition: drc_engine.h:281
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:105
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
Definition: drc_engine.cpp:545
DRC_CONSTRAINT EvalZoneConnection(const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:645
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:265
void SetBoard(BOARD *aBoard)
Definition: drc_engine.h:98
BOARD_DESIGN_SETTINGS * m_designSettings
Definition: drc_engine.h:259
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:36
A collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:44
Handle the data for a net.
Definition: netinfo.h:56
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:241
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:72
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:72
std::function< void(PCB_MARKER *aMarker)> DRC_GRAPHICS_HANDLER
Definition: drc_engine.h:75
DRC_CONSTRAINT_T
Definition: drc_rule.h:47
EDA_UNITS
Definition: eda_units.h:46
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
std::shared_ptr< DRC_RULE > parentRule
Definition: drc_engine.h:251
DRC_RULE_CONDITION * condition
Definition: drc_engine.h:250