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 <kiid.h>
31#include <layer_ids.h>
32#include <units_provider.h>
33#include <pcb_shape.h>
34#include <lset.h>
35#include <drc/drc_rule.h>
36
37
42{
45
46 bool operator==( const DRC_OWN_CLEARANCE_CACHE_KEY& aOther ) const
47 {
48 return m_uuid == aOther.m_uuid && m_layer == aOther.m_layer;
49 }
50};
51
52
53namespace std
54{
55 template <>
57 {
58 std::size_t operator()( const DRC_OWN_CLEARANCE_CACHE_KEY& aKey ) const
59 {
60 std::size_t seed = 0xa82de1c0;
61 seed ^= std::hash<KIID>{}( aKey.m_uuid ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
62 seed ^= std::hash<int>{}( static_cast<int>( aKey.m_layer ) ) + 0x9e3779b9
63 + ( seed << 6 ) + ( seed >> 2 );
64 return seed;
65 }
66 };
67}
68
69
70class BOARD_COMMIT;
74class PCB_EDIT_FRAME;
76class BOARD_ITEM;
77class BOARD;
78class PCB_MARKER;
79class NETCLASS;
80class NETLIST;
81class NETINFO_ITEM;
83class REPORTER;
84class wxFileName;
85
86namespace KIGFX
87{
88 class VIEW_OVERLAY;
89};
90
91void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
92
93#define drc_dbg(level, fmt, ...) \
94 drcPrintDebugMessage( level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
95
97class DRC_ITEM;
98class DRC_RULE;
99class DRC_CONSTRAINT;
100
101
102typedef std::function<void( const std::shared_ptr<DRC_ITEM>& aItem,
103 const VECTOR2I& aPos, int aLayer,
104 const std::function<void( PCB_MARKER* )>& aPathGenerator )> DRC_VIOLATION_HANDLER;
105
117{
118 // They need to change / restore the violation handler
120
121public:
122 DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
123 virtual ~DRC_ENGINE();
124
125 // We own several lists of raw pointers. Don't let the compiler fill in copy c'tors that
126 // will only land us in trouble.
127 DRC_ENGINE( const DRC_ENGINE& ) = delete;
128 DRC_ENGINE& operator=( const DRC_ENGINE& ) = delete;
129
130 void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
131 BOARD* GetBoard() const { return m_board; }
132
133 void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
135
136 void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
138
139 void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
141
142 void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay )
143 {
144 m_debugOverlay = aOverlay;
145 }
146
147 std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
148
153 {
154 m_violationHandler = std::move( aHandler );
155 }
156
161
167
168 /*
169 * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
170 * information.
171 *
172 * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
173 * via a thrown PARSE_ERROR exception.
174 */
175 void SetLogReporter( REPORTER* aReporter ) { m_logReporter = aReporter; }
176
182 void InitEngine( const wxFileName& aRulePath );
183
187 void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints,
188 BOARD_COMMIT* aCommit = nullptr );
189
190 bool IsErrorLimitExceeded( int error_code );
191
192 DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
193 const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
194 REPORTER* aReporter = nullptr );
195
197 PCB_LAYER_ID aLayer, REPORTER* aReporter = nullptr );
198
210 int GetCachedOwnClearance( const BOARD_ITEM* aItem, PCB_LAYER_ID aLayer,
211 wxString* aSource = nullptr );
212
220 void InvalidateClearanceCache( const KIID& aUuid );
221
227 void ClearClearanceCache();
228
236
237 void ProcessAssertions( const BOARD_ITEM* a,
238 std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
239 REPORTER* aReporter = nullptr );
240
241 bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
242
244 bool GetTestFootprints() const { return m_testFootprints; }
245
246 bool RulesValid() { return m_rulesValid; }
247
248 void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
249 int aMarkerLayer, const std::function<void( PCB_MARKER* )>& aPathGenerator = {} );
250
251 bool KeepRefreshing( bool aWait = false );
252 void AdvanceProgress();
253 void SetMaxProgress( int aSize );
254 bool ReportProgress( double aProgress );
255 bool ReportPhase( const wxString& aMessage );
256 bool IsCancelled() const;
257
259
260 bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
261 std::set<int> QueryDistinctConstraints( DRC_CONSTRAINT_T aConstraintId );
262
263 std::vector<DRC_TEST_PROVIDER*> GetTestProviders() const { return m_testProviders; };
264
265 DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
266
274 std::vector<BOARD_ITEM*> GetItemsMatchingCondition( const wxString& aExpression,
276 REPORTER* aReporter = nullptr );
277
278 static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
279
287 static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
288 wxString& aBaseDpName );
289
294 bool IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
295 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem );
296
297private:
298 void addRule( std::shared_ptr<DRC_RULE>& rule )
299 {
300 m_rules.push_back(rule);
301 }
302
308 void loadRules( const wxFileName& aPath );
309
310 void compileRules();
311
319
320 void loadImplicitRules();
321 std::shared_ptr<DRC_RULE> createImplicitRule( const wxString& name, DRC_IMPLICIT_SOURCE aImplicitSource );
322
323protected:
328
329 std::vector<std::shared_ptr<DRC_RULE>> m_rules;
331 std::vector<DRC_TEST_PROVIDER*> m_testProviders;
332
333 std::vector<int> m_errorLimits;
336
337 // constraint -> rule -> provider
338 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
339
343
344 std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
345
346 // Cache for GetOwnClearance lookups to improve rendering performance.
347 // Key is (UUID, layer), value is clearance in internal units.
348 std::unordered_map<DRC_OWN_CLEARANCE_CACHE_KEY, int> m_ownClearanceCache;
349};
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:83
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition drc_engine.h:140
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition drc_engine.h:338
friend class DRC_TEST_PROVIDER_CREEPAGE
Definition drc_engine.h:119
DRC_ENGINE & operator=(const DRC_ENGINE &)=delete
void AdvanceProgress()
std::shared_ptr< DRC_RULE > createImplicitRule(const wxString &name, DRC_IMPLICIT_SOURCE aImplicitSource)
bool GetTestFootprints() const
Definition drc_engine.h:244
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints, BOARD_COMMIT *aCommit=nullptr)
Run the DRC tests.
std::unordered_map< DRC_OWN_CLEARANCE_CACHE_KEY, int > m_ownClearanceCache
Definition drc_engine.h:348
bool m_testFootprints
Definition drc_engine.h:335
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition drc_engine.h:298
PROGRESS_REPORTER * m_progressReporter
Definition drc_engine.h:342
void ClearClearanceCache()
Clear the entire clearance cache.
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:331
REPORTER * m_logReporter
Definition drc_engine.h:341
BOARD * GetBoard() const
Definition drc_engine.h:131
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition drc_engine.h:147
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition drc_engine.h:133
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition drc_engine.h:165
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition drc_engine.h:142
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition drc_engine.h:139
bool GetReportAllTrackErrors() const
Definition drc_engine.h:243
void compileRules()
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition drc_engine.h:326
NETLIST * m_schematicNetlist
Definition drc_engine.h:327
bool KeepRefreshing(bool aWait=false)
void SetLogReporter(REPORTER *aReporter)
Definition drc_engine.h:175
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition drc_engine.h:152
BOARD * m_board
Definition drc_engine.h:325
int GetCachedOwnClearance(const BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, wxString *aSource=nullptr)
Get the cached own clearance for an item on a specific layer.
bool m_reportAllTrackErrors
Definition drc_engine.h:334
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:134
bool RulesValid()
Definition drc_engine.h:246
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:333
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:340
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:329
void ClearViolationHandler()
Definition drc_engine.h:157
void SetSchematicNetlist(NETLIST *aNetlist)
Definition drc_engine.h:136
PROGRESS_REPORTER * GetProgressReporter() const
Definition drc_engine.h:166
bool IsCancelled() const
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition drc_engine.h:263
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:344
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
NETLIST * GetSchematicNetlist() const
Definition drc_engine.h:137
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:258
void InitializeClearanceCache()
Initialize the clearance cache for all items on the board.
bool m_rulesValid
Definition drc_engine.h:330
void SetBoard(BOARD *aBoard)
Definition drc_engine.h:130
void InvalidateClearanceCache(const KIID &aUuid)
Invalidate the clearance cache for a specific item.
BOARD_DESIGN_SETTINGS * m_designSettings
Definition drc_engine.h:324
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator={})
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
Definition kiid.h:49
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::function< void(PCB_MARKER *)> &aPathGenerator)> DRC_VIOLATION_HANDLER
Definition drc_engine.h:104
DRC_IMPLICIT_SOURCE
Definition drc_rule.h:104
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
STL namespace.
std::shared_ptr< DRC_RULE > parentRule
Definition drc_engine.h:316
DRC_RULE_CONDITION * condition
Definition drc_engine.h:315
Cache key for own clearance lookups, combining item UUID and layer.
Definition drc_engine.h:42
bool operator==(const DRC_OWN_CLEARANCE_CACHE_KEY &aOther) const
Definition drc_engine.h:46
std::size_t operator()(const DRC_OWN_CLEARANCE_CACHE_KEY &aKey) const
Definition drc_engine.h:58
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695