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, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <memory>
23#include <mutex>
24#include <shared_mutex>
25#include <vector>
26#include <unordered_map>
27
28#include <kiid.h>
29#include <layer_ids.h>
30#include <units_provider.h>
31#include <lset.h>
32#include <drc/drc_rule.h>
33
34
39{
42
43 bool operator==( const DRC_OWN_CLEARANCE_CACHE_KEY& aOther ) const
44 {
45 return m_uuid == aOther.m_uuid && m_layer == aOther.m_layer;
46 }
47};
48
49
50namespace std
51{
52 template <>
54 {
55 std::size_t operator()( const DRC_OWN_CLEARANCE_CACHE_KEY& aKey ) const
56 {
57 std::size_t seed = 0xa82de1c0;
58 seed ^= std::hash<KIID>{}( aKey.m_uuid ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
59 seed ^= std::hash<int>{}( static_cast<int>( aKey.m_layer ) ) + 0x9e3779b9
60 + ( seed << 6 ) + ( seed >> 2 );
61 return seed;
62 }
63 };
64}
65
66
67class BOARD_COMMIT;
71class PCB_EDIT_FRAME;
73class BOARD_ITEM;
74class BOARD;
75class PCB_MARKER;
76class NETCLASS;
77class NETLIST;
78class NETINFO_ITEM;
79class ZONE;
81class REPORTER;
82class wxFileName;
83
84namespace KIGFX
85{
86 class VIEW_OVERLAY;
87};
88
89void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
90
91#define drc_dbg(level, fmt, ...) \
92 drcPrintDebugMessage( level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
93
95class DRC_ITEM;
96class DRC_RULE;
97class DRC_CONSTRAINT;
98
99
100typedef std::function<void( const std::shared_ptr<DRC_ITEM>& aItem,
101 const VECTOR2I& aPos, int aLayer,
102 const std::function<void( PCB_MARKER* )>& aPathGenerator )> DRC_VIOLATION_HANDLER;
103
104
116
117
129{
130 // They need to change / restore the violation handler
132
133public:
134 DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
135 virtual ~DRC_ENGINE();
136
137 // We own several lists of raw pointers. Don't let the compiler fill in copy c'tors that
138 // will only land us in trouble.
139 DRC_ENGINE( const DRC_ENGINE& ) = delete;
140 DRC_ENGINE& operator=( const DRC_ENGINE& ) = delete;
141
142 void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
143 BOARD* GetBoard() const { return m_board; }
144
145 void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
147
148 void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
150
151 void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
153
154 void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay )
155 {
156 m_debugOverlay = aOverlay;
157 }
158
159 std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
160
165 {
166 m_violationHandler = std::move( aHandler );
167 }
168
173
179
180 /*
181 * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
182 * information.
183 *
184 * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
185 * via a thrown PARSE_ERROR exception.
186 */
187 void SetLogReporter( REPORTER* aReporter ) { m_logReporter = aReporter; }
188
194 void InitEngine( const wxFileName& aRulePath );
195
196 void InitEngine( const std::shared_ptr<DRC_RULE>& rule );
197
201 void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints,
202 BOARD_COMMIT* aCommit = nullptr );
203
204 bool IsErrorLimitExceeded( int error_code );
205
206 DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
207 const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
208 REPORTER* aReporter = nullptr );
209
211 PCB_LAYER_ID aLayer, REPORTER* aReporter = nullptr );
212
223 PCB_LAYER_ID aLayer );
224
236 int GetCachedOwnClearance( const BOARD_ITEM* aItem, PCB_LAYER_ID aLayer,
237 wxString* aSource = nullptr );
238
246 void InvalidateClearanceCache( const KIID& aUuid );
247
253 void ClearClearanceCache();
254
262
263 void ProcessAssertions( const BOARD_ITEM* a,
264 std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
265 REPORTER* aReporter = nullptr );
266
267 bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
268
270
272 bool GetTestFootprints() const { return m_testFootprints; }
273
274 bool RulesValid() { return m_rulesValid; }
275
276 void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
277 int aMarkerLayer, const std::function<void( PCB_MARKER* )>& aPathGenerator = {} );
278
279 bool KeepRefreshing( bool aWait = false );
280 void AdvanceProgress();
281 void SetMaxProgress( int aSize );
282 bool ReportProgress( double aProgress );
283 bool ReportPhase( const wxString& aMessage );
284 bool IsCancelled() const;
285
287
288 bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint, bool aUnconditionalOnly = false );
290 std::set<int> QueryDistinctConstraints( DRC_CONSTRAINT_T aConstraintId );
291
292 std::vector<DRC_TEST_PROVIDER*> GetTestProviders() const { return m_testProviders; };
293
294 DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
295
303 std::vector<BOARD_ITEM*> GetItemsMatchingCondition( const wxString& aExpression,
305 REPORTER* aReporter = nullptr );
306
307 std::vector<BOARD_ITEM*> GetItemsMatchingRule( const std::shared_ptr<DRC_RULE>& aRule,
308 REPORTER* aReporter = nullptr );
309
310 static bool IsNetADiffPair( BOARD* aBoard, const NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
311
319 static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
320 wxString& aBaseDpName );
321
326 bool IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
327 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem );
328
329private:
330 void addRule( std::shared_ptr<DRC_RULE>& rule )
331 {
332 m_rules.push_back(rule);
333 }
334
340 void loadRules( const wxFileName& aPath );
341
342 void compileRules();
343
345 {
348 std::shared_ptr<DRC_RULE> parentRule;
350
351 // Pre-resolved zone pointer for implicit keepout disallow rules. Allows the fast
352 // path in processConstraint to skip UUID parsing and expression evaluation by
353 // doing a direct bounding box pre-filter against the keepout zone.
355 };
356
357 void loadImplicitRules();
358 std::shared_ptr<DRC_RULE> createImplicitRule( const wxString& name, DRC_IMPLICIT_SOURCE aImplicitSource );
359
360protected:
365
366 std::vector<std::shared_ptr<DRC_RULE>> m_rules;
368 std::vector<DRC_TEST_PROVIDER*> m_testProviders;
369
370 std::vector<int> m_errorLimits;
371 mutable std::mutex m_errorLimitsMutex;
374
375 // constraint -> rule -> provider
376 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
377
381
382 std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
383
384 // Cache for GetOwnClearance lookups to improve rendering performance.
385 // Key is (UUID, layer), value is clearance in internal units.
386 // Protected by m_clearanceCacheMutex for thread-safe access during rendering.
387 std::unordered_map<DRC_OWN_CLEARANCE_CACHE_KEY, int> m_ownClearanceCache;
388
389 // Netclass name -> clearance mapping for fast lookup in EvalRules.
390 // Only written during InitEngine(), read during DRC and rendering.
391 // Protected by m_clearanceCacheMutex for thread-safe access.
392 std::unordered_map<wxString, int> m_netclassClearances;
393
394 // Mutex protecting clearance caches for thread-safe access.
395 // Uses shared_mutex for reader-writer pattern (many concurrent reads, exclusive writes).
396 mutable std::shared_mutex m_clearanceCacheMutex;
400 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>> m_explicitConstraints;
401};
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:81
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition drc_engine.h:152
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition drc_engine.h:376
DRC_CLEARANCE_BATCH EvalClearanceBatch(const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer)
Evaluate all clearance-related constraints in a single batch call.
friend class DRC_TEST_PROVIDER_CREEPAGE
Definition drc_engine.h:131
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:272
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:387
bool m_hasDiffPairClearanceOverrides
Definition drc_engine.h:399
bool HasGeometryDependentRules() const
Definition drc_engine.h:269
bool m_testFootprints
Definition drc_engine.h:373
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition drc_engine.h:330
PROGRESS_REPORTER * m_progressReporter
Definition drc_engine.h:380
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:368
REPORTER * m_logReporter
Definition drc_engine.h:379
BOARD * GetBoard() const
Definition drc_engine.h:143
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition drc_engine.h:159
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > > m_explicitConstraints
Definition drc_engine.h:400
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition drc_engine.h:145
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition drc_engine.h:177
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition drc_engine.h:154
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition drc_engine.h:151
bool GetReportAllTrackErrors() const
Definition drc_engine.h:271
void compileRules()
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
bool m_hasGeometryDependentRules
Definition drc_engine.h:398
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition drc_engine.h:363
NETLIST * m_schematicNetlist
Definition drc_engine.h:364
std::shared_mutex m_clearanceCacheMutex
Definition drc_engine.h:396
bool KeepRefreshing(bool aWait=false)
std::vector< BOARD_ITEM * > GetItemsMatchingRule(const std::shared_ptr< DRC_RULE > &aRule, REPORTER *aReporter=nullptr)
void SetLogReporter(REPORTER *aReporter)
Definition drc_engine.h:187
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition drc_engine.h:164
BOARD * m_board
Definition drc_engine.h:362
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:372
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:146
bool RulesValid()
Definition drc_engine.h:274
void SetMaxProgress(int aSize)
DRC_ENGINE(BOARD *aBoard=nullptr, BOARD_DESIGN_SETTINGS *aSettings=nullptr)
bool m_hasExplicitClearanceRules
Definition drc_engine.h:397
DRC_ENGINE(const DRC_ENGINE &)=delete
std::vector< int > m_errorLimits
Definition drc_engine.h:370
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:378
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:366
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint, bool aUnconditionalOnly=false)
void ClearViolationHandler()
Definition drc_engine.h:169
void SetSchematicNetlist(NETLIST *aNetlist)
Definition drc_engine.h:148
PROGRESS_REPORTER * GetProgressReporter() const
Definition drc_engine.h:178
bool IsCancelled() const
std::unordered_map< wxString, int > m_netclassClearances
Definition drc_engine.h:392
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition drc_engine.h:292
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:382
NETLIST * GetSchematicNetlist() const
Definition drc_engine.h:149
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:286
static bool IsNetADiffPair(BOARD *aBoard, const NETINFO_ITEM *aNet, int &aNetP, int &aNetN)
void InitializeClearanceCache()
Initialize the clearance cache for all items on the board.
bool m_rulesValid
Definition drc_engine.h:367
void SetBoard(BOARD *aBoard)
Definition drc_engine.h:142
void InvalidateClearanceCache(const KIID &aUuid)
Invalidate the clearance cache for a specific item.
BOARD_DESIGN_SETTINGS * m_designSettings
Definition drc_engine.h:361
bool HasUserDefinedPhysicalConstraint()
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, const VECTOR2I &aPos, int aMarkerLayer, const std::function< void(PCB_MARKER *)> &aPathGenerator={})
std::mutex m_errorLimitsMutex
Definition drc_engine.h:371
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
Definition kiid.h:44
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:38
Handle the data for a net.
Definition netinfo.h:46
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:71
UNITS_PROVIDER(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits)
Handle a list of polygons defining a copper zone.
Definition zone.h:70
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:102
DRC_IMPLICIT_SOURCE
Definition drc_rule.h:110
DRC_CONSTRAINT_T
Definition drc_rule.h:49
@ ASSERTION_CONSTRAINT
Definition drc_rule.h:84
EDA_UNITS
Definition eda_units.h:44
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
STL namespace.
Batch result for clearance-related constraints to reduce per-query overhead during PNS routing.
Definition drc_engine.h:109
std::shared_ptr< DRC_RULE > parentRule
Definition drc_engine.h:348
DRC_RULE_CONDITION * condition
Definition drc_engine.h:347
Cache key for own clearance lookups, combining item UUID and layer.
Definition drc_engine.h:39
bool operator==(const DRC_OWN_CLEARANCE_CACHE_KEY &aOther) const
Definition drc_engine.h:43
std::size_t operator()(const DRC_OWN_CLEARANCE_CACHE_KEY &aKey) const
Definition drc_engine.h:55
const uint32_t seed
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683