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
69typedef std::function<void( PCB_MARKER* aMarker )> DRC_CUSTOM_MARKER_HANDLER;
70
71typedef std::function<void( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
72 int aLayer, DRC_CUSTOM_MARKER_HANDLER* aCustomHandler )>
74
86{
87 // They need to change / restore the violation handler
90
91public:
92 DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
93 virtual ~DRC_ENGINE();
94
95 void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
96 BOARD* GetBoard() const { return m_board; }
97
98 void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
100
101 void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
103
104 void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
106
107 void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay )
108 {
109 m_debugOverlay = aOverlay;
110 }
111
112 std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
113
118 {
119 m_violationHandler = std::move( aHandler );
120 }
121
123 {
125 }
126
132
133 /*
134 * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
135 * information.
136 *
137 * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
138 * via a thrown PARSE_ERROR exception.
139 */
140 void SetLogReporter( REPORTER* aReporter ) { m_reporter = aReporter; }
141
147 void InitEngine( const wxFileName& aRulePath );
148
152 void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints,
153 BOARD_COMMIT* aCommit = nullptr );
154
155 bool IsErrorLimitExceeded( int error_code );
156
157 DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
158 const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
159 REPORTER* aReporter = nullptr );
160
162 PCB_LAYER_ID aLayer, REPORTER* aReporter = nullptr );
163
164 void ProcessAssertions( const BOARD_ITEM* a,
165 std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
166 REPORTER* aReporter = nullptr );
167
168 bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
169
171 bool GetTestFootprints() const { return m_testFootprints; }
172
173 bool RulesValid() { return m_rulesValid; }
174
175 void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
176 int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER* aCustomHandler = nullptr );
177
178 bool KeepRefreshing( bool aWait = false );
179 void AdvanceProgress();
180 void SetMaxProgress( int aSize );
181 bool ReportProgress( double aProgress );
182 bool ReportPhase( const wxString& aMessage );
183 void ReportAux( const wxString& aStr );
184 bool IsCancelled() const;
185
186 bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
187 std::set<int> QueryDistinctConstraints( DRC_CONSTRAINT_T aConstraintId );
188
189 std::vector<DRC_TEST_PROVIDER*> GetTestProviders() const { return m_testProviders; };
190
191 DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
192
193 static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
194
202 static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
203 wxString& aBaseDpName );
204
209 bool IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
210 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem );
211
212private:
213 void addRule( std::shared_ptr<DRC_RULE>& rule )
214 {
215 m_rules.push_back(rule);
216 }
217
223 void loadRules( const wxFileName& aPath );
224
225 void compileRules();
226
228 {
231 std::shared_ptr<DRC_RULE> parentRule;
233 };
234
235 void loadImplicitRules();
236 std::shared_ptr<DRC_RULE> createImplicitRule( const wxString& name );
237
238protected:
243
244 std::vector<std::shared_ptr<DRC_RULE>> m_rules;
246 std::vector<DRC_TEST_PROVIDER*> m_testProviders;
247
248 std::vector<int> m_errorLimits;
251
252 // constraint -> rule -> provider
253 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
254
258
259 std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
260};
261
262#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:86
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: drc_engine.h:105
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition: drc_engine.h:253
REPORTER * m_reporter
Definition: drc_engine.h:256
void AdvanceProgress()
bool GetTestFootprints() const
Definition: drc_engine.h:171
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
Definition: drc_engine.cpp:608
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
bool m_testFootprints
Definition: drc_engine.h:250
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition: drc_engine.h:213
PROGRESS_REPORTER * m_progressReporter
Definition: drc_engine.h:257
void loadRules(const wxFileName &aPath)
Load and parse a rule set from an sexpr text file.
Definition: drc_engine.cpp:501
std::vector< DRC_TEST_PROVIDER * > m_testProviders
Definition: drc_engine.h:246
BOARD * GetBoard() const
Definition: drc_engine.h:96
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition: drc_engine.h:112
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition: drc_engine.h:98
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition: drc_engine.h:130
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition: drc_engine.h:107
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition: drc_engine.h:104
bool GetReportAllTrackErrors() const
Definition: drc_engine.h:170
void compileRules()
Definition: drc_engine.cpp:524
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition: drc_engine.h:241
NETLIST * m_schematicNetlist
Definition: drc_engine.h:242
bool KeepRefreshing(bool aWait=false)
void SetLogReporter(REPORTER *aReporter)
Definition: drc_engine.h:140
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition: drc_engine.h:117
BOARD * m_board
Definition: drc_engine.h:240
bool m_reportAllTrackErrors
Definition: drc_engine.h:249
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:99
bool RulesValid()
Definition: drc_engine.h:173
void SetMaxProgress(int aSize)
void ReportAux(const wxString &aStr)
std::vector< int > m_errorLimits
Definition: drc_engine.h:248
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:153
DRC_VIOLATION_HANDLER m_violationHandler
Definition: drc_engine.h:255
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:693
std::vector< std::shared_ptr< DRC_RULE > > m_rules
Definition: drc_engine.h:244
std::shared_ptr< DRC_RULE > createImplicitRule(const wxString &name)
Definition: drc_engine.cpp:140
void ClearViolationHandler()
Definition: drc_engine.h:122
void SetSchematicNetlist(NETLIST *aNetlist)
Definition: drc_engine.h:101
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:131
bool IsCancelled() const
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition: drc_engine.h:189
static bool IsNetADiffPair(BOARD *aBoard, NETINFO_ITEM *aNet, int &aNetP, int &aNetN)
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:97
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Definition: drc_engine.h:259
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:102
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
Definition: drc_engine.cpp:555
DRC_CONSTRAINT EvalZoneConnection(const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:659
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:245
void SetBoard(BOARD *aBoard)
Definition: drc_engine.h:95
BOARD_DESIGN_SETTINGS * m_designSettings
Definition: drc_engine.h:239
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
std::function< void(PCB_MARKER *aMarker)> DRC_CUSTOM_MARKER_HANDLER
Definition: drc_engine.h:69
void drcPrintDebugMessage(int level, const wxString &msg, const char *function, int line)
Definition: drc_engine.cpp:64
std::function< void(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aLayer, DRC_CUSTOM_MARKER_HANDLER *aCustomHandler)> DRC_VIOLATION_HANDLER
Definition: drc_engine.h:73
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:231
DRC_RULE_CONDITION * condition
Definition: drc_engine.h:230