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