KiCad PCB EDA Suite
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 (C) 2019-2021 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 #ifndef DRC_ENGINE_H
25 #define DRC_ENGINE_H
26 
27 #include <memory>
28 #include <vector>
29 #include <unordered_map>
30 
31 #include <geometry/shape.h>
32 
33 #include <drc/drc_rule.h>
34 
35 
37 class DRC_TEST_PROVIDER;
38 class PCB_EDIT_FRAME;
39 class DS_PROXY_VIEW_ITEM;
40 class BOARD_ITEM;
41 class BOARD;
42 class PCB_MARKER;
43 class NETCLASS;
44 class NETLIST;
45 class NETINFO_ITEM;
46 class PROGRESS_REPORTER;
47 class REPORTER;
48 class wxFileName;
49 
50 namespace KIGFX
51 {
52  class VIEW_OVERLAY;
53 };
54 
55 void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
56 
57 #define drc_dbg(level, fmt, ...) \
58  drcPrintDebugMessage(level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
59 
60 class DRC_RULE_CONDITION;
61 class DRC_ITEM;
62 class DRC_RULE;
64 
65 
66 typedef
67 std::function<void( const std::shared_ptr<DRC_ITEM>& aItem,
68  const wxPoint& aPos )> DRC_VIOLATION_HANDLER;
69 
70 
82 {
83 public:
84  DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
85  ~DRC_ENGINE();
86 
87  void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
88  BOARD* GetBoard() const { return m_board; }
89 
90  void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
92 
93  void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
95 
96  void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
98 
99  void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay )
100  {
101  m_debugOverlay = aOverlay;
102  }
103 
104  std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
105 
110  {
111  m_violationHandler = std::move( aHandler );
112  }
113 
115  {
117  }
118 
122  void SetProgressReporter( PROGRESS_REPORTER* aProgRep ) { m_progressReporter = aProgRep; }
124 
125  /*
126  * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
127  * information.
128  *
129  * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
130  * via a thrown PARSE_ERROR exception.
131  */
132  void SetLogReporter( REPORTER* aReporter ) { m_reporter = aReporter; }
133 
139  void InitEngine( const wxFileName& aRulePath );
140 
144  void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints );
145 
146 
147  bool IsErrorLimitExceeded( int error_code );
148 
149  DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM* a,
150  const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
151  REPORTER* aReporter = nullptr );
152 
153  bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
154 
155  EDA_UNITS UserUnits() const { return m_userUnits; }
157  bool GetTestFootprints() const { return m_testFootprints; }
158 
159  bool RulesValid() { return m_rulesValid; }
160 
161  void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, const wxPoint& aPos );
162  bool ReportProgress( double aProgress );
163  bool ReportPhase( const wxString& aMessage );
164  void ReportAux( const wxString& aStr );
165 
166  bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
167 
168  std::vector<DRC_TEST_PROVIDER* > GetTestProviders() const { return m_testProviders; };
169 
170  DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
171 
172  static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
173 
181  static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
182  wxString& aBaseDpName );
183 
184  static bool IsNetTie( BOARD_ITEM* aItem );
185 
186  static std::shared_ptr<SHAPE> GetShape( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer );
187 
188 private:
189  void addRule( DRC_RULE* rule )
190  {
191  m_rules.push_back(rule);
192  }
193 
199  void loadRules( const wxFileName& aPath );
200 
201  void compileRules();
202 
204  {
209  };
210 
211  void loadImplicitRules();
212  DRC_RULE* createImplicitRule( const wxString& name );
213 
214 protected:
219 
220  std::vector<DRC_RULE*> m_rules;
222  std::vector<DRC_TEST_PROVIDER*> m_testProviders;
223 
225  std::vector<int> m_errorLimits;
228 
229  // constraint -> rule -> provider
230  std::unordered_map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
231 
235 
236  wxString m_msg; // Allocating strings gets expensive enough to want to avoid it
237  std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
238 };
239 
240 #endif // DRC_H
DRC_TEST_PROVIDER * GetTestProvider(const wxString &name) const
Design Rule Checker object that performs all the DRC tests.
Definition: drc_engine.h:81
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition: drc_engine.h:109
EDA_UNITS m_userUnits
Definition: drc_engine.h:224
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: drc_engine.h:97
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Definition: drc_engine.h:237
void loadRules(const wxFileName &aPath)
Load and parse a rule set from an sexpr text file.
Definition: drc_engine.cpp:530
std::vector< int > m_errorLimits
Definition: drc_engine.h:225
bool ReportPhase(const wxString &aMessage)
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
std::vector< DRC_TEST_PROVIDER * > m_testProviders
Definition: drc_engine.h:222
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition: drc_engine.h:104
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
static std::shared_ptr< SHAPE > GetShape(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer)
static bool IsNetADiffPair(BOARD *aBoard, NETINFO_ITEM *aNet, int &aNetP, int &aNetN)
A progress reporter interface for use in multi-threaded environments.
void compileRules()
Definition: drc_engine.cpp:553
bool IsErrorLimitExceeded(int error_code)
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition: drc_engine.h:90
NETLIST * m_schematicNetlist
Definition: drc_engine.h:218
bool m_testFootprints
Definition: drc_engine.h:227
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition: drc_engine.h:96
bool m_rulesValid
Definition: drc_engine.h:221
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.
void ClearViolationHandler()
Definition: drc_engine.h:114
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
static bool IsNetTie(BOARD_ITEM *aItem)
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:123
std::function< void(const std::shared_ptr< DRC_ITEM > &aItem, const wxPoint &aPos)> DRC_VIOLATION_HANDLER
Definition: drc_engine.h:63
DRC_RULE_CONDITION * condition
Definition: drc_engine.h:206
BOARD_DESIGN_SETTINGS * m_designSettings
Definition: drc_engine.h:215
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition: drc_engine.h:168
void SetLogReporter(REPORTER *aReporter)
Definition: drc_engine.h:132
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
DRC_CONSTRAINT_T
Definition: drc_rule.h:41
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:504
bool GetTestFootprints() const
Definition: drc_engine.h:157
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
bool RulesValid()
Definition: drc_engine.h:159
void addRule(DRC_RULE *rule)
Definition: drc_engine.h:189
void drcPrintDebugMessage(int level, const wxString &msg, const char *function, int line)
Definition: drc_engine.cpp:45
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:206
std::vector< DRC_RULE * > m_rules
Definition: drc_engine.h:220
BOARD * GetBoard() const
Definition: drc_engine.h:88
A collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:46
bool GetReportAllTrackErrors() const
Definition: drc_engine.h:156
bool ReportProgress(double aProgress)
std::unordered_map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition: drc_engine.h:230
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition: drc_engine.h:122
PROGRESS_REPORTER * m_progressReporter
Definition: drc_engine.h:234
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition: drc_engine.h:99
BOARD_DESIGN_SETTINGS * GetDesignSettings() const
Definition: drc_engine.h:91
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:765
REPORTER * m_reporter
Definition: drc_engine.h:233
EDA_UNITS
Definition: eda_units.h:38
DRC_ENGINE(BOARD *aBoard=nullptr, BOARD_DESIGN_SETTINGS *aSettings=nullptr)
Definition: drc_engine.cpp:61
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out #DRC_ITEMs and po...
DRC_RULE * createImplicitRule(const wxString &name)
Definition: drc_engine.cpp:124
Handle the data for a net.
Definition: netinfo.h:64
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:94
const char * name
Definition: DXF_plotter.cpp:56
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void ReportAux(const wxString &aStr)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
The main frame for Pcbnew.
wxString m_msg
Definition: drc_engine.h:236
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints)
Run the DRC tests.
Definition: drc_engine.cpp:682
DRC_VIOLATION_HANDLER m_violationHandler
Definition: drc_engine.h:232
BOARD * m_board
Definition: drc_engine.h:216
EDA_UNITS UserUnits() const
Definition: drc_engine.h:155
void SetBoard(BOARD *aBoard)
Definition: drc_engine.h:87
void loadImplicitRules()
Definition: drc_engine.cpp:137
bool m_reportAllTrackErrors
Definition: drc_engine.h:226
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, const wxPoint &aPos)
void SetSchematicNetlist(NETLIST *aNetlist)
Definition: drc_engine.h:93
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
Definition: drc_engine.cpp:626
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition: drc_engine.h:217
Container for design settings for a BOARD object.