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 <pcb_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
68
69typedef std::function<void( const std::shared_ptr<DRC_ITEM>& aItem,
70 const VECTOR2I& aPos, int aLayer,
71 const std::vector<PCB_SHAPE>& aShapes )> DRC_VIOLATION_HANDLER;
72
84{
85 // They need to change / restore the violation handler
88
89public:
90 DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
91 virtual ~DRC_ENGINE();
92
93 // We own several lists of raw pointers. Don't let the compiler fill in copy c'tors that
94 // will only land us in trouble.
95 DRC_ENGINE( const DRC_ENGINE& ) = delete;
96 DRC_ENGINE& operator=( const DRC_ENGINE& ) = delete;
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
129
135
136 /*
137 * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
138 * information.
139 *
140 * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
141 * via a thrown PARSE_ERROR exception.
142 */
143 void SetLogReporter( REPORTER* aReporter ) { m_logReporter = aReporter; }
144
150 void InitEngine( const wxFileName& aRulePath );
151
155 void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints,
156 BOARD_COMMIT* aCommit = nullptr );
157
158 bool IsErrorLimitExceeded( int error_code );
159
160 DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
161 const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
162 REPORTER* aReporter = nullptr );
163
165 PCB_LAYER_ID aLayer, REPORTER* aReporter = nullptr );
166
167 void ProcessAssertions( const BOARD_ITEM* a,
168 std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
169 REPORTER* aReporter = nullptr );
170
171 bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
172
174 bool GetTestFootprints() const { return m_testFootprints; }
175
176 bool RulesValid() { return m_rulesValid; }
177
178 void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
179 int aMarkerLayer, const std::vector<PCB_SHAPE>& aShapes = {} );
180
181 bool KeepRefreshing( bool aWait = false );
182 void AdvanceProgress();
183 void SetMaxProgress( int aSize );
184 bool ReportProgress( double aProgress );
185 bool ReportPhase( const wxString& aMessage );
186 bool IsCancelled() const;
187
189
190 bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
191 std::set<int> QueryDistinctConstraints( DRC_CONSTRAINT_T aConstraintId );
192
193 std::vector<DRC_TEST_PROVIDER*> GetTestProviders() const { return m_testProviders; };
194
195 DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
196
204 std::vector<BOARD_ITEM*> GetItemsMatchingCondition( const wxString& aExpression,
206 REPORTER* aReporter = nullptr );
207
208 static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
209
217 static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
218 wxString& aBaseDpName );
219
224 bool IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
225 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem );
226
227private:
228 void addRule( std::shared_ptr<DRC_RULE>& rule )
229 {
230 m_rules.push_back(rule);
231 }
232
238 void loadRules( const wxFileName& aPath );
239
240 void compileRules();
241
249
250 void loadImplicitRules();
251 std::shared_ptr<DRC_RULE> createImplicitRule( const wxString& name );
252
253protected:
258
259 std::vector<std::shared_ptr<DRC_RULE>> m_rules;
261 std::vector<DRC_TEST_PROVIDER*> m_testProviders;
262
263 std::vector<int> m_errorLimits;
266
267 // constraint -> rule -> provider
268 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
269
273
274 std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
275};
const char * name
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
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:268
friend class DRC_TEST_PROVIDER_CREEPAGE
Definition drc_engine.h:87
DRC_ENGINE & operator=(const DRC_ENGINE &)=delete
void AdvanceProgress()
bool GetTestFootprints() const
Definition drc_engine.h:174
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
bool m_testFootprints
Definition drc_engine.h:265
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition drc_engine.h:228
PROGRESS_REPORTER * m_progressReporter
Definition drc_engine.h:272
void loadRules(const wxFileName &aPath)
Load and parse a rule set from an sexpr text file.
std::vector< DRC_TEST_PROVIDER * > m_testProviders
Definition drc_engine.h:261
REPORTER * m_logReporter
Definition drc_engine.h:271
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:133
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:173
void compileRules()
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition drc_engine.h:256
NETLIST * m_schematicNetlist
Definition drc_engine.h:257
bool KeepRefreshing(bool aWait=false)
void SetLogReporter(REPORTER *aReporter)
Definition drc_engine.h:143
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:255
bool m_reportAllTrackErrors
Definition drc_engine.h:264
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:176
void SetMaxProgress(int aSize)
DRC_ENGINE(BOARD *aBoard=nullptr, BOARD_DESIGN_SETTINGS *aSettings=nullptr)
DRC_ENGINE(const DRC_ENGINE &)=delete
std::vector< int > m_errorLimits
Definition drc_engine.h:263
bool IsErrorLimitExceeded(int error_code)
void ProcessAssertions(const BOARD_ITEM *a, std::function< void(const DRC_CONSTRAINT *)> aFailureHandler, REPORTER *aReporter=nullptr)
void loadImplicitRules()
DRC_VIOLATION_HANDLER m_violationHandler
Definition drc_engine.h:270
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
std::vector< std::shared_ptr< DRC_RULE > > m_rules
Definition drc_engine.h:259
friend class DRC_TEST_PROVIDER_CLEARANCE_BASE
Definition drc_engine.h:86
std::shared_ptr< DRC_RULE > createImplicitRule(const wxString &name)
void ClearViolationHandler()
Definition drc_engine.h:125
void SetSchematicNetlist(NETLIST *aNetlist)
Definition drc_engine.h:104
PROGRESS_REPORTER * GetProgressReporter() const
Definition drc_engine.h:134
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aMarkerLayer, const std::vector< PCB_SHAPE > &aShapes={})
bool IsCancelled() const
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition drc_engine.h:193
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()
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Definition drc_engine.h:274
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
NETLIST * GetSchematicNetlist() const
Definition drc_engine.h:105
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.
DRC_CONSTRAINT EvalZoneConnection(const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
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:188
bool m_rulesValid
Definition drc_engine.h:260
void SetBoard(BOARD *aBoard)
Definition drc_engine.h:98
BOARD_DESIGN_SETTINGS * m_designSettings
Definition drc_engine.h:254
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:54
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
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
UNITS_PROVIDER(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits)
void drcPrintDebugMessage(int level, const wxString &msg, const char *function, int line)
std::function< void(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aLayer, const std::vector< PCB_SHAPE > &aShapes)> DRC_VIOLATION_HANDLER
Definition drc_engine.h:71
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:246
DRC_RULE_CONDITION * condition
Definition drc_engine.h:245
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695