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-2020 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 <drc/drc_rule.h>
32 
33 
35 class DRC_TEST_PROVIDER;
36 class PCB_EDIT_FRAME;
37 class BOARD_ITEM;
38 class BOARD;
39 class PCB_MARKER;
40 class NETCLASS;
41 class NETLIST;
42 class NETINFO_ITEM;
43 class PROGRESS_REPORTER;
44 class REPORTER;
45 class wxFileName;
46 
47 namespace KIGFX
48 {
49  class WS_PROXY_VIEW_ITEM;
50  class VIEW_OVERLAY;
51 };
52 
53 void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
54 
55 #define drc_dbg(level, fmt, ...) \
56  drcPrintDebugMessage(level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
57 
58 class DRC_RULE_CONDITION;
59 class DRC_ITEM;
60 class DRC_RULE;
62 
63 
64 typedef
65 std::function<void( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos )> DRC_VIOLATION_HANDLER;
66 
67 
79 {
80 public:
81  DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
82  ~DRC_ENGINE();
83 
84  void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
85  BOARD* GetBoard() const { return m_board; }
86 
87  void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
89 
90  void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
92 
93  void SetWorksheet( KIGFX::WS_PROXY_VIEW_ITEM* aWorksheet ) { m_worksheet = aWorksheet; }
95 
96  void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay ) { m_debugOverlay = aOverlay; }
97  std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
98 
99 
104  {
105  m_violationHandler = std::move( aHandler );
106  }
107 
109  {
111  }
112 
116  void SetProgressReporter( PROGRESS_REPORTER* aProgRep ) { m_progressReporter = aProgRep; }
118 
119  /*
120  * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
121  * information.
122  *
123  * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
124  * via a thrown PARSE_ERROR exception.
125  */
126  void SetLogReporter( REPORTER* aReporter ) { m_reporter = aReporter; }
127 
133  void InitEngine( const wxFileName& aRulePath );
134 
138  void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints );
139 
140 
141  bool IsErrorLimitExceeded( int error_code );
142 
144  const BOARD_ITEM* b = nullptr,
145  PCB_LAYER_ID aLayer = UNDEFINED_LAYER,
146  REPORTER* aReporter = nullptr );
147 
148  bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
149 
150  EDA_UNITS UserUnits() const { return m_userUnits; }
152  bool GetTestFootprints() const { return m_testFootprints; }
153 
154  bool RulesValid() { return m_rulesValid; }
155 
156  void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos );
157  bool ReportProgress( double aProgress );
158  bool ReportPhase( const wxString& aMessage );
159  void ReportAux( const wxString& aStr );
160 
161  bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
162 
163  std::vector<DRC_TEST_PROVIDER* > GetTestProviders() const { return m_testProviders; };
164 
165  DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
166 
167  static int IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
168 
169 private:
170  void addRule( DRC_RULE* rule )
171  {
172  m_rules.push_back(rule);
173  }
174 
180  void loadRules( const wxFileName& aPath );
181 
182  void compileRules();
183 
185  {
190  };
191 
192  void loadImplicitRules();
193  DRC_RULE* createImplicitRule( const wxString& name );
194 
195 protected:
200 
201  std::vector<DRC_RULE*> m_rules;
203  std::vector<DRC_TEST_PROVIDER*> m_testProviders;
204 
206  std::vector<int> m_errorLimits;
209 
210  // constraint -> rule -> provider
211  std::unordered_map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
212 
216 
217  wxString m_msg; // Allocating strings gets expensive enough to want to avoid it
218  std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
219 };
220 
221 #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:78
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition: drc_engine.h:103
EDA_UNITS m_userUnits
Definition: drc_engine.h:205
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Definition: drc_engine.h:218
void loadRules(const wxFileName &aPath)
Loads and parses a rule set from an sexpr text file.
Definition: drc_engine.cpp:495
std::vector< int > m_errorLimits
Definition: drc_engine.h:206
bool ReportPhase(const wxString &aMessage)
std::function< void(const std::shared_ptr< DRC_ITEM > &aItem, wxPoint aPos)> DRC_VIOLATION_HANDLER
Definition: drc_engine.h:61
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
std::vector< DRC_TEST_PROVIDER * > m_testProviders
Definition: drc_engine.h:203
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition: drc_engine.h:97
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
A progress reporter for use in multi-threaded environments.
void compileRules()
Definition: drc_engine.cpp:518
bool IsErrorLimitExceeded(int error_code)
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition: drc_engine.h:87
NETLIST * m_schematicNetlist
Definition: drc_engine.h:199
bool m_testFootprints
Definition: drc_engine.h:208
DRC_CONSTRAINT EvalRulesForItems(DRC_CONSTRAINT_T ruleID, const BOARD_ITEM *a, const BOARD_ITEM *b=nullptr, PCB_LAYER_ID aLayer=UNDEFINED_LAYER, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:709
static int IsNetADiffPair(BOARD *aBoard, NETINFO_ITEM *aNet, int &aNetP, int &aNetN)
bool m_rulesValid
Definition: drc_engine.h:202
void ClearViolationHandler()
Definition: drc_engine.h:108
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:117
DRC_RULE_CONDITION * condition
Definition: drc_engine.h:187
KIGFX::WS_PROXY_VIEW_ITEM * GetWorksheet() const
Definition: drc_engine.h:94
BOARD_DESIGN_SETTINGS * m_designSettings
Definition: drc_engine.h:196
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition: drc_engine.h:163
void SetLogReporter(REPORTER *aReporter)
Definition: drc_engine.h:126
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
DRC_CONSTRAINT_T
Definition: drc_rule.h:41
PCB_LAYER_ID
A quick note on layer IDs:
LSET is a set of PCB_LAYER_IDs.
KIGFX::WS_PROXY_VIEW_ITEM * m_worksheet
Definition: drc_engine.h:198
bool GetTestFootprints() const
Definition: drc_engine.h:152
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
bool RulesValid()
Definition: drc_engine.h:154
void addRule(DRC_RULE *rule)
Definition: drc_engine.h:170
void drcPrintDebugMessage(int level, const wxString &msg, const char *function, int line)
Definition: drc_engine.cpp:36
NETLIST stores all of information read from a netlist along with the flags used to update the NETLIST...
Definition: pcb_netlist.h:200
void SetWorksheet(KIGFX::WS_PROXY_VIEW_ITEM *aWorksheet)
Definition: drc_engine.h:93
std::vector< DRC_RULE * > m_rules
Definition: drc_engine.h:201
BOARD * GetBoard() const
Definition: drc_engine.h:85
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:151
bool ReportProgress(double aProgress)
std::unordered_map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition: drc_engine.h:211
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition: drc_engine.h:116
PROGRESS_REPORTER * m_progressReporter
Definition: drc_engine.h:215
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition: drc_engine.h:96
BOARD_DESIGN_SETTINGS * GetDesignSettings() const
Definition: drc_engine.h:88
REPORTER * m_reporter
Definition: drc_engine.h:214
EDA_UNITS
Definition: eda_units.h:38
DRC_ENGINE(BOARD *aBoard=nullptr, BOARD_DESIGN_SETTINGS *aSettings=nullptr)
Definition: drc_engine.cpp:52
DRC_TEST_PROVIDER is a base class that represents a DRC "provider" which runs some DRC functions over...
DRC_RULE * createImplicitRule(const wxString &name)
Definition: drc_engine.cpp:115
NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:91
const char * name
Definition: DXF_plotter.cpp:59
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:189
void ReportAux(const wxString &aStr)
PCB_EDIT_FRAME is the main frame for Pcbnew.
wxString m_msg
Definition: drc_engine.h:217
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints)
Runs the DRC tests.
Definition: drc_engine.cpp:648
DRC_VIOLATION_HANDLER m_violationHandler
Definition: drc_engine.h:213
BOARD * m_board
Definition: drc_engine.h:197
EDA_UNITS UserUnits() const
Definition: drc_engine.h:150
void SetBoard(BOARD *aBoard)
Definition: drc_engine.h:84
void loadImplicitRules()
Definition: drc_engine.cpp:128
bool m_reportAllTrackErrors
Definition: drc_engine.h:207
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, wxPoint aPos)
void SetSchematicNetlist(NETLIST *aNetlist)
Definition: drc_engine.h:90
void InitEngine(const wxFileName &aRulePath)
Initializes the DRC engine.
Definition: drc_engine.cpp:594
Container for design settings for a BOARD object.