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 <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, wxPoint aPos )> DRC_VIOLATION_HANDLER;
68 
69 
81 {
82 public:
83  DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
84  ~DRC_ENGINE();
85 
86  void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
87  BOARD* GetBoard() const { return m_board; }
88 
89  void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
91 
92  void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
94 
95  void SetDrawingSheet( DS_PROXY_VIEW_ITEM* aDrawingSheet ) { m_drawingSheet = aDrawingSheet; }
97 
98  void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay ) { m_debugOverlay = aOverlay; }
99  std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
100 
101 
106  {
107  m_violationHandler = std::move( aHandler );
108  }
109 
111  {
113  }
114 
118  void SetProgressReporter( PROGRESS_REPORTER* aProgRep ) { m_progressReporter = aProgRep; }
120 
121  /*
122  * Set an optional reporter for rule parse/compile/run-time errors and log-level progress
123  * information.
124  *
125  * Note: if no log reporter is installed rule parse/compile/run-time errors are returned
126  * via a thrown PARSE_ERROR exception.
127  */
128  void SetLogReporter( REPORTER* aReporter ) { m_reporter = aReporter; }
129 
135  void InitEngine( const wxFileName& aRulePath );
136 
140  void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints );
141 
142 
143  bool IsErrorLimitExceeded( int error_code );
144 
145  DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintId, const BOARD_ITEM* a,
146  const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
147  REPORTER* aReporter = nullptr );
148 
149  bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
150 
151  EDA_UNITS UserUnits() const { return m_userUnits; }
153  bool GetTestFootprints() const { return m_testFootprints; }
154 
155  bool RulesValid() { return m_rulesValid; }
156 
157  void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos );
158  bool ReportProgress( double aProgress );
159  bool ReportPhase( const wxString& aMessage );
160  void ReportAux( const wxString& aStr );
161 
162  bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
163 
164  std::vector<DRC_TEST_PROVIDER* > GetTestProviders() const { return m_testProviders; };
165 
166  DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
167 
168  static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
169 
177  static int MatchDpSuffix( const wxString& aNetName, wxString& aComplementNet,
178  wxString& aBaseDpName );
179 
180  static bool IsNetTie( BOARD_ITEM* aItem );
181 
182  static std::shared_ptr<SHAPE> GetShape( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer );
183 
184 private:
185  void addRule( DRC_RULE* rule )
186  {
187  m_rules.push_back(rule);
188  }
189 
195  void loadRules( const wxFileName& aPath );
196 
197  void compileRules();
198 
200  {
205  };
206 
207  void loadImplicitRules();
208  DRC_RULE* createImplicitRule( const wxString& name );
209 
210 protected:
215 
216  std::vector<DRC_RULE*> m_rules;
218  std::vector<DRC_TEST_PROVIDER*> m_testProviders;
219 
221  std::vector<int> m_errorLimits;
224 
225  // constraint -> rule -> provider
226  std::unordered_map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
227 
231 
232  wxString m_msg; // Allocating strings gets expensive enough to want to avoid it
233  std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
234 };
235 
236 #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:80
void SetViolationHandler(DRC_VIOLATION_HANDLER aHandler)
Set an optional DRC violation handler (receives DRC_ITEMs and positions).
Definition: drc_engine.h:105
EDA_UNITS m_userUnits
Definition: drc_engine.h:220
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
Definition: drc_engine.h:96
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Definition: drc_engine.h:233
void loadRules(const wxFileName &aPath)
Loads and parses a rule set from an sexpr text file.
Definition: drc_engine.cpp:519
std::vector< int > m_errorLimits
Definition: drc_engine.h:221
bool ReportPhase(const wxString &aMessage)
std::function< void(const std::shared_ptr< DRC_ITEM > &aItem, wxPoint aPos)> DRC_VIOLATION_HANDLER
Definition: drc_engine.h:63
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
std::vector< DRC_TEST_PROVIDER * > m_testProviders
Definition: drc_engine.h:218
std::shared_ptr< KIGFX::VIEW_OVERLAY > GetDebugOverlay() const
Definition: drc_engine.h:99
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
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 for use in multi-threaded environments.
void compileRules()
Definition: drc_engine.cpp:542
bool IsErrorLimitExceeded(int error_code)
void SetDesignSettings(BOARD_DESIGN_SETTINGS *aSettings)
Definition: drc_engine.h:89
NETLIST * m_schematicNetlist
Definition: drc_engine.h:214
bool m_testFootprints
Definition: drc_engine.h:223
void SetDrawingSheet(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Definition: drc_engine.h:95
bool m_rulesValid
Definition: drc_engine.h:217
static int MatchDpSuffix(const wxString &aNetName, wxString &aComplementNet, wxString &aBaseDpName)
Checks if the given net is a diff pair, returning its polarity and complement if so.
void ClearViolationHandler()
Definition: drc_engine.h:110
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
static bool IsNetTie(BOARD_ITEM *aItem)
PROGRESS_REPORTER * GetProgressReporter() const
Definition: drc_engine.h:119
DRC_RULE_CONDITION * condition
Definition: drc_engine.h:202
BOARD_DESIGN_SETTINGS * m_designSettings
Definition: drc_engine.h:211
std::vector< DRC_TEST_PROVIDER * > GetTestProviders() const
Definition: drc_engine.h:164
void SetLogReporter(REPORTER *aReporter)
Definition: drc_engine.h:128
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.
bool GetTestFootprints() const
Definition: drc_engine.h:153
bool QueryWorstConstraint(DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT &aConstraint)
bool RulesValid()
Definition: drc_engine.h:155
void addRule(DRC_RULE *rule)
Definition: drc_engine.h:185
void drcPrintDebugMessage(int level, const wxString &msg, const char *function, int line)
Definition: drc_engine.cpp:40
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:205
std::vector< DRC_RULE * > m_rules
Definition: drc_engine.h:216
BOARD * GetBoard() const
Definition: drc_engine.h:87
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:152
bool ReportProgress(double aProgress)
std::unordered_map< DRC_CONSTRAINT_T, std::vector< DRC_ENGINE_CONSTRAINT * > * > m_constraintMap
Definition: drc_engine.h:226
void SetProgressReporter(PROGRESS_REPORTER *aProgRep)
Set an optional reporter for user-level progress info.
Definition: drc_engine.h:118
PROGRESS_REPORTER * m_progressReporter
Definition: drc_engine.h:230
void SetDebugOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition: drc_engine.h:98
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintId, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:758
BOARD_DESIGN_SETTINGS * GetDesignSettings() const
Definition: drc_engine.h:90
REPORTER * m_reporter
Definition: drc_engine.h:229
EDA_UNITS
Definition: eda_units.h:38
DRC_ENGINE(BOARD *aBoard=nullptr, BOARD_DESIGN_SETTINGS *aSettings=nullptr)
Definition: drc_engine.cpp:56
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:119
Handle the data for a net.
Definition: netinfo.h:64
NETLIST * GetSchematicNetlist() const
Definition: drc_engine.h:93
const char * name
Definition: DXF_plotter.cpp:59
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void ReportAux(const wxString &aStr)
The main frame for Pcbnew.
wxString m_msg
Definition: drc_engine.h:232
void RunTests(EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints)
Runs the DRC tests.
Definition: drc_engine.cpp:674
DRC_VIOLATION_HANDLER m_violationHandler
Definition: drc_engine.h:228
BOARD * m_board
Definition: drc_engine.h:212
EDA_UNITS UserUnits() const
Definition: drc_engine.h:151
void SetBoard(BOARD *aBoard)
Definition: drc_engine.h:86
void loadImplicitRules()
Definition: drc_engine.cpp:132
bool m_reportAllTrackErrors
Definition: drc_engine.h:222
void ReportViolation(const std::shared_ptr< DRC_ITEM > &aItem, wxPoint aPos)
void SetSchematicNetlist(NETLIST *aNetlist)
Definition: drc_engine.h:92
void InitEngine(const wxFileName &aRulePath)
Initializes the DRC engine.
Definition: drc_engine.cpp:618
DS_PROXY_VIEW_ITEM * m_drawingSheet
Definition: drc_engine.h:213
Container for design settings for a BOARD object.