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