KiCad PCB EDA Suite
drc_tool.cpp
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) 2020-2021 KiCad Developers, see change_log.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 #include <pcb_edit_frame.h>
25 #include <bitmaps.h>
26 #include <tool/tool_manager.h>
27 #include <tools/pcb_actions.h>
28 #include <tools/pcb_tool_base.h>
29 #include <tools/zone_filler_tool.h>
30 #include <tools/drc_tool.h>
31 #include <kiface_base.h>
32 #include <dialog_drc.h>
33 #include <board_commit.h>
34 #include <board_design_settings.h>
35 #include <progress_reporter.h>
36 #include <drc/drc_engine.h>
39 
41  PCB_TOOL_BASE( "pcbnew.DRCTool" ),
42  m_editFrame( nullptr ),
43  m_pcb( nullptr ),
44  m_drcDialog( nullptr ),
45  m_drcRunning( false )
46 {
47 }
48 
49 
51 {
52 }
53 
54 
56 {
57  m_editFrame = getEditFrame<PCB_EDIT_FRAME>();
58 
59  if( m_pcb != m_editFrame->GetBoard() )
60  {
61  if( m_drcDialog )
63 
66  }
67 }
68 
69 
70 void DRC_TOOL::ShowDRCDialog( wxWindow* aParent )
71 {
72  bool show_dlg_modal = true;
73 
74  // the dialog needs a parent frame. if it is not specified, this is the PCB editor frame
75  // specified in DRC_TOOL class.
76  if( !aParent )
77  {
78  // if any parent is specified, the dialog is modal.
79  // if this is the default PCB editor frame, it is not modal
80  show_dlg_modal = false;
81  aParent = m_editFrame;
82  }
83 
84  Activate();
86 
87  if( !m_drcDialog )
88  {
89  m_drcDialog = new DIALOG_DRC( m_editFrame, aParent );
91 
92  if( show_dlg_modal )
93  m_drcDialog->ShowModal();
94  else
95  m_drcDialog->Show( true );
96  }
97  else // The dialog is just not visible (because the user has double clicked on an error item)
98  {
100  m_drcDialog->Show( true );
101  }
102 }
103 
104 
106 {
107  ShowDRCDialog( nullptr );
108  return 0;
109 }
110 
111 
113 {
114  if( m_drcDialog )
115  return m_drcDialog->IsShown();
116 
117  return false;
118 }
119 
120 
122 {
123  if( m_drcDialog )
124  {
125  m_drcDialog->Destroy();
126  m_drcDialog = nullptr;
127  }
128 }
129 
130 
131 void DRC_TOOL::RunTests( PROGRESS_REPORTER* aProgressReporter, bool aRefillZones,
132  bool aReportAllTrackErrors, bool aTestFootprints )
133 {
134  // One at a time, please.
135  // Note that the main GUI entry points to get here are blocked, so this is really an
136  // insurance policy and as such we make no attempts to queue up the DRC run or anything.
137  if( m_drcRunning )
138  return;
139 
141  BOARD_COMMIT commit( m_editFrame );
143  bool netlistFetched = false;
144  wxWindowDisabler disabler( /* disable everything except: */ m_drcDialog );
145 
146  m_drcRunning = true;
147 
148  if( aRefillZones )
149  {
150  aProgressReporter->AdvancePhase( _( "Refilling all zones..." ) );
151 
152  zoneFiller->FillAllZones( m_drcDialog, aProgressReporter );
153  }
154 
155  m_drcEngine->SetDrawingSheet( m_editFrame->GetCanvas()->GetDrawingSheet());
156 
157  if( aTestFootprints && !Kiface().IsSingle() )
158  {
159  if( m_editFrame->FetchNetlistFromSchematic( netlist, _( "Schematic parity tests require a "
160  "fully annotated schematic." ) ) )
161  {
162  netlistFetched = true;
163  }
164 
165  if( m_drcDialog )
166  m_drcDialog->Raise();
167 
168  m_drcEngine->SetSchematicNetlist( &netlist );
169  }
170 
171  m_drcEngine->SetProgressReporter( aProgressReporter );
172 
173  m_drcEngine->SetViolationHandler(
174  [&]( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos )
175  {
176  if( aItem->GetErrorCode() == DRCE_MISSING_FOOTPRINT
177  || aItem->GetErrorCode() == DRCE_DUPLICATE_FOOTPRINT
178  || aItem->GetErrorCode() == DRCE_EXTRA_FOOTPRINT
179  || aItem->GetErrorCode() == DRCE_NET_CONFLICT )
180  {
181  m_footprints.push_back( aItem );
182  }
183  else if( aItem->GetErrorCode() == DRCE_UNCONNECTED_ITEMS )
184  {
185  m_unconnected.push_back( aItem );
186  }
187  else
188  {
189  PCB_MARKER* marker = new PCB_MARKER( aItem, aPos );
190  commit.Add( marker );
191  }
192  } );
193 
194  m_drcEngine->RunTests( m_editFrame->GetUserUnits(), aReportAllTrackErrors, aTestFootprints );
195 
196  m_drcEngine->SetProgressReporter( nullptr );
197  m_drcEngine->ClearViolationHandler();
198 
199  if( m_drcDialog )
200  {
202 
203  if( aTestFootprints && netlistFetched )
205  }
206 
207  commit.Push( _( "DRC" ), false );
208 
209  m_drcRunning = false;
210 
211  // update the m_drcDialog listboxes
212  updatePointers();
213 }
214 
215 
217 {
218  // update my pointers, m_editFrame is the only unchangeable one
220 
222 
223  if( m_drcDialog ) // Use diag list boxes only in DRC_TOOL dialog
224  {
227  &m_unconnected ) );
229  &m_footprints ) );
230  }
231 }
232 
233 
234 int DRC_TOOL::PrevMarker( const TOOL_EVENT& aEvent )
235 {
236  if( m_drcDialog )
237  {
238  m_drcDialog->Show( true );
239  m_drcDialog->Raise();
241  }
242  else
243  {
244  ShowDRCDialog( nullptr );
245  }
246 
247  return 0;
248 }
249 
250 
251 int DRC_TOOL::NextMarker( const TOOL_EVENT& aEvent )
252 {
253  if( m_drcDialog )
254  {
255  m_drcDialog->Show( true );
256  m_drcDialog->Raise();
258  }
259  else
260  {
261  ShowDRCDialog( nullptr );
262  }
263 
264  return 0;
265 }
266 
267 
269 {
270  if( m_drcDialog )
272 
273  return 0;
274 }
275 
276 
278 {
283 }
284 
285 
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:59
int NextMarker(const TOOL_EVENT &aEvent)
Definition: drc_tool.cpp:251
void NextMarker()
Definition: dialog_drc.cpp:736
RATSNEST_DRC_ITEMS_PROVIDER.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
A progress reporter interface for use in multi-threaded environments.
DRC_TOOL()
Definition: drc_tool.cpp:40
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void ResolveDRCExclusions()
Update markers to match recorded exclusions.
int ExcludeMarker(const TOOL_EVENT &aEvent)
Definition: drc_tool.cpp:268
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
PCB_EDIT_FRAME * m_editFrame
Definition: drc_tool.h:112
BOARD * m_pcb
Definition: drc_tool.h:113
void PrevMarker()
Definition: dialog_drc.cpp:722
static TOOL_ACTION nextMarker
Definition: actions.h:86
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
std::shared_ptr< DRC_ENGINE > m_drcEngine
Definition: drc_tool.h:117
bool m_drcRunning
Definition: drc_tool.h:115
void SetDrcRun()
Definition: dialog_drc.h:51
void ShowDRCDialog(wxWindow *aParent)
Open a dialog and prompts the user, then if a test run button is clicked, runs the test(s) and create...
Definition: drc_tool.cpp:70
bool FetchNetlistFromSchematic(NETLIST &aNetlist, const wxString &aAnnotateMessage)
void ExcludeMarker()
Definition: dialog_drc.cpp:750
void SetFootprintTestsRun()
Definition: dialog_drc.h:52
static TOOL_ACTION excludeMarker
Definition: actions.h:87
void setTransitions() override
< Set up handlers for various events.
Definition: drc_tool.cpp:277
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:589
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
DS_PROXY_VIEW_ITEM * GetDrawingSheet() const
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: drc_tool.cpp:55
void FillAllZones(wxWindow *aCaller, PROGRESS_REPORTER *aReporter=nullptr)
Handle actions specific to filling copper zones.
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:206
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
BOARD_DRC_ITEMS_PROVIDER is an implementation of the RC_ITEMS_PROVIDER interface which uses a BOARD i...
Generic, UI-independent tool event.
Definition: tool_event.h:152
void SetFootprintsProvider(RC_ITEMS_PROVIDER *aProvider)
Definition: dialog_drc.cpp:301
void updatePointers()
Update needed pointers from the one pointer which is known not to change.
Definition: drc_tool.cpp:216
#define _(s)
DIALOG_DRC * m_drcDialog
Definition: drc_tool.h:114
bool Show(bool show) override
void RunTests(PROGRESS_REPORTER *aProgressReporter, bool aRefillZones, bool aReportAllTrackErrors, bool aTestFootprints)
Run the DRC tests.
Definition: drc_tool.cpp:131
static TOOL_ACTION prevMarker
Definition: actions.h:85
bool IsDRCDialogShown()
Check to see if the DRC_TOOL dialog is currently shown.
Definition: drc_tool.cpp:112
void SetMarkersProvider(RC_ITEMS_PROVIDER *aProvider)
Definition: dialog_drc.cpp:285
std::vector< std::shared_ptr< DRC_ITEM > > m_footprints
Definition: drc_tool.h:120
static TOOL_ACTION runDRC
Definition: pcb_actions.h:342
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
void Activate()
Run the tool.
int PrevMarker(const TOOL_EVENT &aEvent)
Definition: drc_tool.cpp:234
BOARD * GetBoard() const
void DestroyDRCDialog()
Close and free the DRC dialog.
Definition: drc_tool.cpp:121
std::vector< std::shared_ptr< DRC_ITEM > > m_unconnected
Definition: drc_tool.h:119
~DRC_TOOL()
Definition: drc_tool.cpp:50
VECTOR_DRC_ITEMS_PROVIDER is an implementation of the interface named DRC_ITEMS_PROVIDER which uses a...
void SetUnconnectedProvider(RC_ITEMS_PROVIDER *aProvider)
Definition: dialog_drc.cpp:293
bool IsSingle() const
Is this KIFACE running under single_top?
Definition: kiface_base.h:104
std::shared_ptr< DRC_ENGINE > m_DRCEngine
EDA_UNITS GetUserUnits() const
Return the user units currently in use.