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 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#pragma once
25
26#include <memory>
27#include <vector>
28#include <unordered_map>
29
30#include <units_provider.h>
31#include <geometry/shape.h>
32#include <lset.h>
33#include <drc/drc_rule.h>
34
35
36class BOARD_COMMIT;
41class PCB_EDIT_FRAME;
43class BOARD_ITEM;
44class BOARD;
45class PCB_MARKER;
46class NETCLASS;
47class NETLIST;
48class NETINFO_ITEM;
50class REPORTER;
51class wxFileName;
52
53namespace KIGFX
54{
55 class VIEW_OVERLAY;
56};
57
58void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
59
60#define drc_dbg(level, fmt, ...) \
61 drcPrintDebugMessage( level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
62
64class DRC_ITEM;
65class DRC_RULE;
66class DRC_CONSTRAINT;
67
68typedef std::function<void( PCB_MARKER* aMarker )> DRC_CUSTOM_MARKER_HANDLER;
69
70typedef std::function<void( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
71 int aLayer, DRC_CUSTOM_MARKER_HANDLER* aCustomHandler )>
73
85{
86 // They need to change / restore the violation handler
89
90public:
91 DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
92 virtual ~DRC_ENGINE();
93
94 // We own several lists of raw pointers. Don't let the compiler fill in copy c'tors that
95 // will only land us in trouble.
96 DRC_ENGINE( const DRC_ENGINE& ) = delete;
97 DRC_ENGINE& operator=( const DRC_ENGINE& ) = delete;
98
99 void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
100 BOARD* GetBoard() const { return m_board; }
101
102 void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
104
105 void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
107
108 void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
110
111 void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay )
112 {
113 m_debugOverlay = aOverlay;
114 }
115
116 std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
117
122 {
123 m_violationHandler = std::move( aHandler );
124 }
125
127 {
129 }
130
136
137 /*
138 * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
139 * information.
140 *
141 * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
142 * via a thrown PARSE_ERROR exception.
143 */
144 void SetLogReporter( REPORTER* aReporter ) { m_logReporter = aReporter; }
145
151 void InitEngine( const wxFileName& aRulePath );
152
156 void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints,
157 BOARD_COMMIT* aCommit = nullptr );
158
159 bool IsErrorLimitExceeded( int error_code );
160
161 DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
162 const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
163 REPORTER* aReporter = nullptr );
164
166 PCB_LAYER_ID aLayer, REPORTER* aReporter = nullptr );
167
168 void ProcessAssertions( const BOARD_ITEM* a,
169 std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
170 REPORTER* aReporter = nullptr );
171
172 bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
173
175 bool GetTestFootprints() const { return m_testFootprints; }
176
177 bool RulesValid() { return m_rulesValid; }
178
179 void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
180 int aMarkerLayer, DRC_CUSTOM_MARKER_HANDLER* aCustomHandler = nullptr );
181
182 bool KeepRefreshing( bool aWait = false );
183 void AdvanceProgress();
184 void SetMaxProgress( int aSize );
185 bool ReportProgress( double aProgress );
186 bool ReportPhase( const wxString& aMessage );
187 bool IsCancelled() const;
188
190
191 bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
192 std::set<int> QueryDistinctConstraints( DRC_CONSTRAINT_T aConstraintId );
193
194 std::vector<DRC_TEST_PROVIDER*> GetTestProviders() const { return m_testProviders; };
195
196 DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
197
205 std::vector<BOARD_ITEM*> GetItemsMatchingCondition( const wxString& aExpression,
207 REPORTER* aReporter = nullptr );
208
209 static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
210
218 static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
219 wxString& aBaseDpName );
220
225 bool IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
226 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem );
227
228private:
229 void addRule( std::shared_ptr<DRC_RULE>& rule )
230 {
231 m_rules.push_back(rule);
232 }
233
239 void loadRules( const wxFileName& aPath );
240
241 void compileRules();
242
244 {
247 std::shared_ptr<DRC_RULE> parentRule;
249 };
250
251 void loadImplicitRules();
252 std::shared_ptr<DRC_RULE> createImplicitRule( const wxString& name );
253
254protected:
259
260 std::vector<std::shared_ptr<DRC_RULE>> m_rules;
262 std::vector<DRC_TEST_PROVIDER*> m_testProviders;
263
264 std::vector<int> m_errorLimits;
267
268 // constraint -> rule -> provider
269 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
270
274
275 std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
276};
const char * name
Definition: DXF_plotter.cpp:62
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:317
Design Rule Checker object that performs all the DRC tests.
Definition: drc_engine.h:85
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: drc_engine.h:109
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition: drc_engine.h:269
DRC_ENGINE & operator=(const DRC_ENGINE &)=delete
void AdvanceProgress()
bool GetTestFootprints() const
Definition: drc_engine.h:175
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
Definition: drc_engine.cpp:617
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:266
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition: drc_engine.h:229
PROGRESS_REPORTER * m_progressReporter
Definition: drc_engine.h:273
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:262
REPORTER * m_logReporter
Definition: drc_engine.h:272
BOARD * GetBoard() const
Definition: drc_engine.h:100
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition: drc_engine.h:116
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition: drc_engine.h:102
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition: drc_engine.h:134
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition: drc_engine.h:111
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition: drc_engine.h:108
bool GetReportAllTrackErrors() const
Definition: drc_engine.h:174
void compileRules()
Definition: drc_engine.cpp:527
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition: drc_engine.h:257
NETLIST * m_schematicNetlist
Definition: drc_engine.h:258
bool KeepRefreshing(bool aWait=false)
void SetLogReporter(REPORTER *aReporter)
Definition: drc_engine.h:144
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition: drc_engine.h:121
BOARD * m_board
Definition: drc_engine.h:256
bool m_reportAllTrackErrors
Definition: drc_engine.h:265
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:103
bool RulesValid()
Definition: drc_engine.h:177
void SetMaxProgress(int aSize)
DRC_ENGINE(const DRC_ENGINE &)=delete
std::vector< int > m_errorLimits
Definition: drc_engine.h:264
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:154
DRC_VIOLATION_HANDLER m_violationHandler
Definition: drc_engine.h:271
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:706
std::vector< std::shared_ptr< DRC_RULE > > m_rules
Definition: drc_engine.h:260
std::shared_ptr< DRC_RULE > createImplicitRule(const wxString &name)
Definition: drc_engine.cpp:141
void ClearViolationHandler()
Definition: drc_engine.h:126
void SetSchematicNetlist(NETLIST *aNetlist)
Definition: drc_engine.h:105
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:135
bool IsCancelled() const
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition: drc_engine.h:194
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:98
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Definition: drc_engine.h:275
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:106
std::vector< BOARD_ITEM * > GetItemsMatchingCondition(const wxString &aExpression, DRC_CONSTRAINT_T aConstraint=ASSERTION_CONSTRAINT, REPORTER *aReporter=nullptr)
Evaluate a DRC condition against all board items and return matches.
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
Definition: drc_engine.cpp:562
DRC_CONSTRAINT EvalZoneConnection(const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:672
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)
REPORTER * GetLogReporter() const
Definition: drc_engine.h:189
bool m_rulesValid
Definition: drc_engine.h:261
void SetBoard(BOARD *aBoard)
Definition: drc_engine.h:99
BOARD_DESIGN_SETTINGS * m_designSettings
Definition: drc_engine.h:255
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:37
A collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:45
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:274
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:73
std::function< void(PCB_MARKER *aMarker)> DRC_CUSTOM_MARKER_HANDLER
Definition: drc_engine.h:68
void drcPrintDebugMessage(int level, const wxString &msg, const char *function, int line)
Definition: drc_engine.cpp:65
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:72
DRC_CONSTRAINT_T
Definition: drc_rule.h:47
@ ASSERTION_CONSTRAINT
Definition: drc_rule.h:79
EDA_UNITS
Definition: eda_units.h:48
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
The Cairo implementation of the graphics abstraction layer.
Definition: eda_group.h:33
std::shared_ptr< DRC_RULE > parentRule
Definition: drc_engine.h:247
DRC_RULE_CONDITION * condition
Definition: drc_engine.h:246