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 <mutex>
28#include <shared_mutex>
29#include <vector>
30#include <unordered_map>
31
32#include <kiid.h>
33#include <layer_ids.h>
34#include <units_provider.h>
35#include <lset.h>
36#include <drc/drc_rule.h>
37
38
43{
46
47 bool operator==( const DRC_OWN_CLEARANCE_CACHE_KEY& aOther ) const
48 {
49 return m_uuid == aOther.m_uuid && m_layer == aOther.m_layer;
50 }
51};
52
53
54namespace std
55{
56 template <>
58 {
59 std::size_t operator()( const DRC_OWN_CLEARANCE_CACHE_KEY& aKey ) const
60 {
61 std::size_t seed = 0xa82de1c0;
62 seed ^= std::hash<KIID>{}( aKey.m_uuid ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
63 seed ^= std::hash<int>{}( static_cast<int>( aKey.m_layer ) ) + 0x9e3779b9
64 + ( seed << 6 ) + ( seed >> 2 );
65 return seed;
66 }
67 };
68}
69
70
71class BOARD_COMMIT;
75class PCB_EDIT_FRAME;
77class BOARD_ITEM;
78class BOARD;
79class PCB_MARKER;
80class NETCLASS;
81class NETLIST;
82class NETINFO_ITEM;
84class REPORTER;
85class wxFileName;
86
87namespace KIGFX
88{
89 class VIEW_OVERLAY;
90};
91
92void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
93
94#define drc_dbg(level, fmt, ...) \
95 drcPrintDebugMessage( level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
96
98class DRC_ITEM;
99class DRC_RULE;
100class DRC_CONSTRAINT;
101
102
103typedef std::function<void( const std::shared_ptr<DRC_ITEM>& aItem,
104 const VECTOR2I& aPos, int aLayer,
105 const std::function<void( PCB_MARKER* )>& aPathGenerator )> DRC_VIOLATION_HANDLER;
106
107
119
120
132{
133 // They need to change / restore the violation handler
135
136public:
137 DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
138 virtual ~DRC_ENGINE();
139
140 // We own several lists of raw pointers. Don't let the compiler fill in copy c'tors that
141 // will only land us in trouble.
142 DRC_ENGINE( const DRC_ENGINE& ) = delete;
143 DRC_ENGINE& operator=( const DRC_ENGINE& ) = delete;
144
145 void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
146 BOARD* GetBoard() const { return m_board; }
147
148 void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
150
151 void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
153
154 void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
156
157 void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay )
158 {
159 m_debugOverlay = aOverlay;
160 }
161
162 std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
163
168 {
169 m_violationHandler = std::move( aHandler );
170 }
171
176
182
183 /*
184 * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
185 * information.
186 *
187 * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
188 * via a thrown PARSE_ERROR exception.
189 */
190 void SetLogReporter( REPORTER* aReporter ) { m_logReporter = aReporter; }
191
197 void InitEngine( const wxFileName& aRulePath );
198
199 void InitEngine( const std::shared_ptr<DRC_RULE>& rule );
200
204 void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints,
205 BOARD_COMMIT* aCommit = nullptr );
206
207 bool IsErrorLimitExceeded( int error_code );
208
209 DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
210 const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
211 REPORTER* aReporter = nullptr );
212
214 PCB_LAYER_ID aLayer, REPORTER* aReporter = nullptr );
215
226 PCB_LAYER_ID aLayer );
227
239 int GetCachedOwnClearance( const BOARD_ITEM* aItem, PCB_LAYER_ID aLayer,
240 wxString* aSource = nullptr );
241
249 void InvalidateClearanceCache( const KIID& aUuid );
250
256 void ClearClearanceCache();
257
265
266 void ProcessAssertions( const BOARD_ITEM* a,
267 std::function<void( const DRC_CONSTRAINT* )> aFailureHandler,
268 REPORTER* aReporter = nullptr );
269
270 bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
271
273 bool GetTestFootprints() const { return m_testFootprints; }
274
275 bool RulesValid() { return m_rulesValid; }
276
277 void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos,
278 int aMarkerLayer, const std::function<void( PCB_MARKER* )>& aPathGenerator = {} );
279
280 bool KeepRefreshing( bool aWait = false );
281 void AdvanceProgress();
282 void SetMaxProgress( int aSize );
283 bool ReportProgress( double aProgress );
284 bool ReportPhase( const wxString& aMessage );
285 bool IsCancelled() const;
286
288
289 bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
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 static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
308
316 static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
317 wxString& aBaseDpName );
318
323 bool IsNetTieExclusion( int aTrackNetCode, PCB_LAYER_ID aTrackLayer,
324 const VECTOR2I& aCollisionPos, BOARD_ITEM* aCollidingItem );
325
326private:
327 void addRule( std::shared_ptr<DRC_RULE>& rule )
328 {
329 m_rules.push_back(rule);
330 }
331
337 void loadRules( const wxFileName& aPath );
338
339 void compileRules();
340
348
349 void loadImplicitRules();
350 std::shared_ptr<DRC_RULE> createImplicitRule( const wxString& name, DRC_IMPLICIT_SOURCE aImplicitSource );
351
352protected:
357
358 std::vector<std::shared_ptr<DRC_RULE>> m_rules;
360 std::vector<DRC_TEST_PROVIDER*> m_testProviders;
361
362 std::vector<int> m_errorLimits;
363 mutable std::mutex m_errorLimitsMutex;
366
367 // constraint -> rule -> provider
368 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
369
373
374 std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
375
376 // Cache for GetOwnClearance lookups to improve rendering performance.
377 // Key is (UUID, layer), value is clearance in internal units.
378 // Protected by m_clearanceCacheMutex for thread-safe access during rendering.
379 std::unordered_map<DRC_OWN_CLEARANCE_CACHE_KEY, int> m_ownClearanceCache;
380
381 // Netclass name -> clearance mapping for fast lookup in EvalRules.
382 // Only written during InitEngine(), read during DRC and rendering.
383 // Protected by m_clearanceCacheMutex for thread-safe access.
384 std::unordered_map<wxString, int> m_netclassClearances;
385
386 // Mutex protecting clearance caches for thread-safe access.
387 // Uses shared_mutex for reader-writer pattern (many concurrent reads, exclusive writes).
388 mutable std::shared_mutex m_clearanceCacheMutex;
391 std::map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>> m_explicitConstraints;
392};
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:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition drc_engine.h:155
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition drc_engine.h:368
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:134
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:273
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:379
bool m_hasDiffPairClearanceOverrides
Definition drc_engine.h:390
bool m_testFootprints
Definition drc_engine.h:365
void addRule(std::shared_ptr< DRC_RULE > &rule)
Definition drc_engine.h:327
PROGRESS_REPORTER * m_progressReporter
Definition drc_engine.h:372
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:360
REPORTER * m_logReporter
Definition drc_engine.h:371
BOARD * GetBoard() const
Definition drc_engine.h:146
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition drc_engine.h:162
std::map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > > m_explicitConstraints
Definition drc_engine.h:391
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition drc_engine.h:148
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition drc_engine.h:180
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition drc_engine.h:157
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition drc_engine.h:154
bool GetReportAllTrackErrors() const
Definition drc_engine.h:272
void compileRules()
std::set< int > QueryDistinctConstraints(DRC_CONSTRAINT_T aConstraintId)
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition drc_engine.h:355
NETLIST * m_schematicNetlist
Definition drc_engine.h:356
std::shared_mutex m_clearanceCacheMutex
Definition drc_engine.h:388
bool KeepRefreshing(bool aWait=false)
void SetLogReporter(REPORTER *aReporter)
Definition drc_engine.h:190
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition drc_engine.h:167
BOARD * m_board
Definition drc_engine.h:354
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:364
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:149
bool RulesValid()
Definition drc_engine.h:275
void SetMaxProgress(int aSize)
DRC_ENGINE(BOARD *aBoard=nullptr, BOARD_DESIGN_SETTINGS *aSettings=nullptr)
bool m_hasExplicitClearanceRules
Definition drc_engine.h:389
DRC_ENGINE(const DRC_ENGINE &)=delete
std::vector< int > m_errorLimits
Definition drc_engine.h:362
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:370
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:358
void ClearViolationHandler()
Definition drc_engine.h:172
void SetSchematicNetlist(NETLIST *aNetlist)
Definition drc_engine.h:151
PROGRESS_REPORTER * GetProgressReporter() const
Definition drc_engine.h:181
bool IsCancelled() const
std::unordered_map< wxString, int > m_netclassClearances
Definition drc_engine.h:384
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition drc_engine.h:292
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:374
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
NETLIST * GetSchematicNetlist() const
Definition drc_engine.h:152
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:287
void InitializeClearanceCache()
Initialize the clearance cache for all items on the board.
bool m_rulesValid
Definition drc_engine.h:359
void SetBoard(BOARD *aBoard)
Definition drc_engine.h:145
void InvalidateClearanceCache(const KIID &aUuid)
Invalidate the clearance cache for a specific item.
BOARD_DESIGN_SETTINGS * m_designSettings
Definition drc_engine.h:353
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:363
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:105
DRC_IMPLICIT_SOURCE
Definition drc_rule.h:105
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.
Batch result for clearance-related constraints to reduce per-query overhead during PNS routing.
Definition drc_engine.h:112
std::shared_ptr< DRC_RULE > parentRule
Definition drc_engine.h:345
DRC_RULE_CONDITION * condition
Definition drc_engine.h:344
Cache key for own clearance lookups, combining item UUID and layer.
Definition drc_engine.h:43
bool operator==(const DRC_OWN_CLEARANCE_CACHE_KEY &aOther) const
Definition drc_engine.h:47
std::size_t operator()(const DRC_OWN_CLEARANCE_CACHE_KEY &aKey) const
Definition drc_engine.h:59
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695