kicad/pcbnew/drc/drc_engine.h

227 lines
7.2 KiB
C
Raw Normal View History

2020-06-13 23:28:08 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
2020-06-13 23:28:08 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef DRC_ENGINE_H
#define DRC_ENGINE_H
#include <memory>
#include <vector>
#include <unordered_map>
2020-06-13 23:28:08 +00:00
#include <geometry/shape.h>
#include <drc/drc_rule.h>
2020-06-13 23:28:08 +00:00
class BOARD_DESIGN_SETTINGS;
class DRC_TEST_PROVIDER;
2020-06-13 23:28:08 +00:00
class PCB_EDIT_FRAME;
class BOARD_ITEM;
class BOARD;
2020-11-14 18:11:28 +00:00
class PCB_MARKER;
2020-06-13 23:28:08 +00:00
class NETCLASS;
class NETLIST;
class NETINFO_ITEM;
2020-06-13 23:28:08 +00:00
class PROGRESS_REPORTER;
class REPORTER;
2020-10-24 14:45:37 +00:00
class wxFileName;
2020-06-13 23:28:08 +00:00
2020-09-11 15:40:36 +00:00
namespace KIGFX
{
class WS_PROXY_VIEW_ITEM;
class VIEW_OVERLAY;
};
void drcPrintDebugMessage( int level, const wxString& msg, const char *function, int line );
#define drc_dbg(level, fmt, ...) \
drcPrintDebugMessage(level, wxString::Format( fmt, __VA_ARGS__ ), __FUNCTION__, __LINE__ );
2020-06-13 23:28:08 +00:00
class DRC_RULE_CONDITION;
class DRC_ITEM;
class DRC_RULE;
class DRC_CONSTRAINT;
typedef
std::function<void( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos )> DRC_VIOLATION_HANDLER;
2020-06-13 23:28:08 +00:00
/**
* Design Rule Checker object that performs all the DRC tests.
*
* Optionally reports violations via a DRC_VIOLATION_HANDLER, user-level progress via a
* PROGRESS_REPORTER and rule parse errors via a REPORTER, all set through various setter
* calls.
*
* Note that EvalRules() has yet another optional REPORTER for reporting resolution info to
* the user.
2020-06-13 23:28:08 +00:00
*/
class DRC_ENGINE
{
public:
DRC_ENGINE( BOARD* aBoard = nullptr, BOARD_DESIGN_SETTINGS* aSettings = nullptr );
2020-06-13 23:28:08 +00:00
~DRC_ENGINE();
void SetBoard( BOARD* aBoard ) { m_board = aBoard; }
BOARD* GetBoard() const { return m_board; }
void SetDesignSettings( BOARD_DESIGN_SETTINGS* aSettings ) { m_designSettings = aSettings; }
BOARD_DESIGN_SETTINGS* GetDesignSettings() const { return m_designSettings; }
void SetSchematicNetlist( NETLIST* aNetlist ) { m_schematicNetlist = aNetlist; }
NETLIST* GetSchematicNetlist() const { return m_schematicNetlist; }
void SetWorksheet( KIGFX::WS_PROXY_VIEW_ITEM* aWorksheet ) { m_worksheet = aWorksheet; }
KIGFX::WS_PROXY_VIEW_ITEM* GetWorksheet() const { return m_worksheet; }
void SetDebugOverlay( std::shared_ptr<KIGFX::VIEW_OVERLAY> aOverlay ) { m_debugOverlay = aOverlay; }
std::shared_ptr<KIGFX::VIEW_OVERLAY> GetDebugOverlay() const { return m_debugOverlay; }
/**
* Set an optional DRC violation handler (receives DRC_ITEMs and positions).
2020-09-14 17:54:14 +00:00
*/
void SetViolationHandler( DRC_VIOLATION_HANDLER aHandler )
{
m_violationHandler = std::move( aHandler );
}
void ClearViolationHandler()
{
m_violationHandler = DRC_VIOLATION_HANDLER();
}
/**
* Set an optional reporter for user-level progress info.
2020-09-14 17:54:14 +00:00
*/
void SetProgressReporter( PROGRESS_REPORTER* aProgRep ) { m_progressReporter = aProgRep; }
PROGRESS_REPORTER* GetProgressReporter() const { return m_progressReporter; }
2020-09-14 17:54:14 +00:00
/*
* Set an optional reporter for rule parse/compile/run-time errors and log-level progress
* information.
*
* Note: if no log reporter is installed rule parse/compile/run-time errors are returned
* via a thrown PARSE_ERROR exception.
2020-09-14 17:54:14 +00:00
*/
void SetLogReporter( REPORTER* aReporter ) { m_reporter = aReporter; }
2020-06-13 23:28:08 +00:00
/**
* Initializes the DRC engine.
*
* @throws PARSE_ERROR if the rules file contains errors
*/
void InitEngine( const wxFileName& aRulePath );
/**
* Runs the DRC tests.
*/
void RunTests( EDA_UNITS aUnits, bool aReportAllTrackErrors, bool aTestFootprints );
2020-06-13 23:28:08 +00:00
2020-09-14 17:54:14 +00:00
bool IsErrorLimitExceeded( int error_code );
2020-06-13 23:28:08 +00:00
DRC_CONSTRAINT EvalRules( DRC_CONSTRAINT_T aConstraintId, const BOARD_ITEM* a,
const BOARD_ITEM* b, PCB_LAYER_ID aLayer,
REPORTER* aReporter = nullptr );
2021-01-01 22:29:15 +00:00
bool HasRulesForConstraintType( DRC_CONSTRAINT_T constraintID );
2020-09-14 17:54:14 +00:00
EDA_UNITS UserUnits() const { return m_userUnits; }
bool GetReportAllTrackErrors() const { return m_reportAllTrackErrors; }
bool GetTestFootprints() const { return m_testFootprints; }
2020-09-11 16:24:27 +00:00
bool RulesValid() { return m_rulesValid; }
2020-06-13 23:28:08 +00:00
void ReportViolation( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos );
bool ReportProgress( double aProgress );
bool ReportPhase( const wxString& aMessage );
void ReportAux( const wxString& aStr );
2021-01-01 22:29:15 +00:00
bool QueryWorstConstraint( DRC_CONSTRAINT_T aRuleId, DRC_CONSTRAINT& aConstraint );
std::vector<DRC_TEST_PROVIDER* > GetTestProviders() const { return m_testProviders; };
DRC_TEST_PROVIDER* GetTestProvider( const wxString& name ) const;
2021-02-01 00:38:20 +00:00
static bool IsNetADiffPair( BOARD* aBoard, NETINFO_ITEM* aNet, int& aNetP, int& aNetN );
static bool IsNetTie( BOARD_ITEM* aItem );
static std::shared_ptr<SHAPE> GetShape( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer );
2020-06-13 23:28:08 +00:00
private:
void addRule( DRC_RULE* rule )
{
m_rules.push_back(rule);
}
/**
* Loads and parses a rule set from an sexpr text file.
*
* @throws PARSE_ERROR
*/
void loadRules( const wxFileName& aPath );
void compileRules();
2021-01-01 22:29:15 +00:00
struct DRC_ENGINE_CONSTRAINT
2020-06-13 23:28:08 +00:00
{
LSET layerTest;
DRC_RULE_CONDITION* condition;
DRC_RULE* parentRule;
DRC_CONSTRAINT constraint;
2020-06-13 23:28:08 +00:00
};
void loadImplicitRules();
DRC_RULE* createImplicitRule( const wxString& name );
2020-06-13 23:28:08 +00:00
protected:
BOARD_DESIGN_SETTINGS* m_designSettings;
BOARD* m_board;
KIGFX::WS_PROXY_VIEW_ITEM* m_worksheet;
NETLIST* m_schematicNetlist;
2020-06-13 23:28:08 +00:00
std::vector<DRC_RULE*> m_rules;
bool m_rulesValid;
2020-06-13 23:28:08 +00:00
std::vector<DRC_TEST_PROVIDER*> m_testProviders;
2020-09-14 17:54:14 +00:00
EDA_UNITS m_userUnits;
std::vector<int> m_errorLimits;
2020-09-14 17:54:14 +00:00
bool m_reportAllTrackErrors;
bool m_testFootprints;
// constraint -> rule -> provider
2021-01-01 22:29:15 +00:00
std::unordered_map<DRC_CONSTRAINT_T, std::vector<DRC_ENGINE_CONSTRAINT*>*> m_constraintMap;
DRC_VIOLATION_HANDLER m_violationHandler;
REPORTER* m_reporter;
PROGRESS_REPORTER* m_progressReporter;
2020-06-13 23:28:08 +00:00
2020-09-11 16:24:27 +00:00
wxString m_msg; // Allocating strings gets expensive enough to want to avoid it
std::shared_ptr<KIGFX::VIEW_OVERLAY> m_debugOverlay;
2020-06-13 23:28:08 +00:00
};
#endif // DRC_H