KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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#include <pcb_edit_frame.h>
25#include <tool/tool_manager.h>
26#include <tools/pcb_actions.h>
27#include <tools/pcb_tool_base.h>
30#include <tools/drc_tool.h>
31#include <kiface_base.h>
32#include <dialog_drc.h>
33#include <board_commit.h>
35#include <progress_reporter.h>
36#include <drc/drc_engine.h>
37#include <drc/drc_item.h>
39#include <macros.h>
41
43 PCB_TOOL_BASE( "pcbnew.DRCTool" ),
44 m_editFrame( nullptr ),
45 m_pcb( nullptr ),
46 m_drcDialog( nullptr ),
47 m_drcRunning( false )
48{
49}
50
51
55
56
58{
60
61 if( m_pcb != m_editFrame->GetBoard() )
62 {
63 if( m_drcDialog )
65
66 m_pcb = m_editFrame->GetBoard();
67 m_drcEngine = m_pcb->GetDesignSettings().m_DRCEngine;
68 }
69}
70
71
72void DRC_TOOL::ShowDRCDialog( wxWindow* aParent )
73{
74 bool show_dlg_modal = true;
75
76 // the dialog needs a parent frame. if it is not specified, this is the PCB editor frame
77 // specified in DRC_TOOL class.
78 if( !aParent )
79 {
80 // if any parent is specified, the dialog is modal.
81 // if this is the default PCB editor frame, it is not modal
82 show_dlg_modal = false;
83 aParent = m_editFrame;
84 }
85
86 Activate();
88
89 if( !m_drcDialog )
90 {
91 m_drcDialog = new DIALOG_DRC( m_editFrame, aParent );
92 updatePointers( false );
93
94 if( show_dlg_modal )
95 m_drcDialog->ShowModal();
96 else
97 m_drcDialog->Show( true );
98 }
99 else // The dialog is just not visible (because the user has double clicked on an error item)
100 {
101 updatePointers( false );
102 m_drcDialog->Show( true );
103 }
104}
105
106
108{
109 ShowDRCDialog( nullptr );
110 return 0;
111}
112
113
115{
116 if( m_drcDialog )
117 return m_drcDialog->IsShownOnScreen();
118
119 return false;
120}
121
122
124{
125 if( m_drcDialog )
126 {
127 m_drcDialog->Destroy();
128 m_drcDialog = nullptr;
129 }
130}
131
132
133void DRC_TOOL::RunTests( PROGRESS_REPORTER* aProgressReporter, bool aRefillZones,
134 bool aReportAllTrackErrors, bool aTestFootprints )
135{
136 // One at a time, please.
137 // Note that the main GUI entry points to get here are blocked, so this is really an
138 // insurance policy and as such we make no attempts to queue up the DRC run or anything.
139 if( m_drcRunning )
140 return;
141
142 ZONE_FILLER_TOOL* zoneFiller = m_toolMgr->GetTool<ZONE_FILLER_TOOL>();
143 BOARD_COMMIT commit( m_editFrame );
145 bool netlistFetched = false;
146 wxWindowDisabler disabler( /* disable everything except: */ m_drcDialog );
147
148 m_drcRunning = true;
149
150 if( aRefillZones )
151 {
152 aProgressReporter->AdvancePhase( _( "Refilling all zones..." ) );
153
154 zoneFiller->FillAllZones( m_drcDialog, aProgressReporter );
155 }
156
157 m_drcEngine->SetDrawingSheet( m_editFrame->GetCanvas()->GetDrawingSheet() );
158
159 if( aTestFootprints && !Kiface().IsSingle() )
160 {
161 if( m_editFrame->FetchNetlistFromSchematic( netlist, _( "Schematic parity tests require a "
162 "fully annotated schematic." ) ) )
163 {
164 netlistFetched = true;
165 }
166
167 if( m_drcDialog )
168 m_drcDialog->Raise();
169
170 m_drcEngine->SetSchematicNetlist( &netlist );
171 }
172
173 m_drcEngine->SetProgressReporter( aProgressReporter );
174
175 m_drcEngine->SetViolationHandler(
176 [&]( const std::shared_ptr<DRC_ITEM>& aItem, const VECTOR2I& aPos, int aLayer,
177 const std::function<void( PCB_MARKER* )>& aPathGenerator )
178 {
179 PCB_MARKER* marker = new PCB_MARKER( aItem, aPos, aLayer );
180 aPathGenerator( marker );
181 commit.Add( marker );
182 } );
183
184 m_drcEngine->RunTests( m_editFrame->GetUserUnits(), aReportAllTrackErrors, aTestFootprints,
185 &commit );
186
187 m_drcEngine->SetProgressReporter( nullptr );
188 m_drcEngine->ClearViolationHandler();
189
190 if( m_drcDialog )
191 {
192 m_drcDialog->SetDrcRun();
193
194 if( aTestFootprints && netlistFetched )
195 m_drcDialog->SetFootprintTestsRun();
196 }
197
198 commit.Push( _( "DRC" ), SKIP_UNDO | SKIP_SET_DIRTY );
199
200 m_drcRunning = false;
201
202 m_editFrame->ShowSolderMask();
203
204 // update the m_drcDialog listboxes
205 updatePointers( aProgressReporter->IsCancelled() );
206}
207
208
209void DRC_TOOL::updatePointers( bool aDRCWasCancelled )
210{
211 // update my pointers, m_editFrame is the only unchangeable one
212 m_pcb = m_editFrame->GetBoard();
213
214 m_editFrame->ResolveDRCExclusions( aDRCWasCancelled );
215
216 if( m_drcDialog )
217 m_drcDialog->UpdateData();
218}
219
220
222{
223 if( m_drcDialog )
224 {
225 m_drcDialog->Show( true );
226 m_drcDialog->Raise();
227 m_drcDialog->PrevMarker();
228 }
229 else
230 {
231 ShowDRCDialog( nullptr );
232 }
233
234 return 0;
235}
236
237
239{
240 if( m_drcDialog )
241 {
242 m_drcDialog->Show( true );
243 m_drcDialog->Raise();
244 m_drcDialog->NextMarker();
245 }
246 else
247 {
248 ShowDRCDialog( nullptr );
249 }
250
251 return 0;
252}
253
254
256{
257 if( m_drcDialog && m_drcDialog->IsShownOnScreen() )
258 {
259 PCB_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
260 PCB_SELECTION& selection = selectionTool->GetSelection();
261
262 if( selection.GetSize() == 1 && selection.Front()->Type() == PCB_MARKER_T )
263 m_drcDialog->SelectMarker( static_cast<PCB_MARKER*>( selection.Front() ) );
264 }
265
266 return 0;
267}
268
269
270void DRC_TOOL::CrossProbe( const PCB_MARKER* aMarker )
271{
272 if( !IsDRCDialogShown() )
273 ShowDRCDialog( nullptr );
274
275 m_drcDialog->SelectMarker( aMarker );
276}
277
278
280{
281 if( m_drcDialog )
282 m_drcDialog->ExcludeMarker();
283
284 return 0;
285}
286
287
288wxString DRC_TOOL::FixDRCErrorMenuText( const std::shared_ptr<RC_ITEM>& aDRCItem )
289{
290 if( aDRCItem->GetErrorCode() == DRCE_LIB_FOOTPRINT_ISSUES )
291 {
292 return frame()->GetRunMenuCommandDescription( PCB_ACTIONS::showFootprintLibTable );
293 }
294 else if( aDRCItem->GetErrorCode() == DRCE_LIB_FOOTPRINT_MISMATCH )
295 {
296 return frame()->GetRunMenuCommandDescription( PCB_ACTIONS::updateFootprint );
297 }
298 else if( aDRCItem->GetErrorCode() == DRCE_FOOTPRINT_FILTERS )
299 {
300 return frame()->GetRunMenuCommandDescription( PCB_ACTIONS::changeFootprint );
301 }
302 else if( aDRCItem->GetErrorCode() == DRCE_SCHEMATIC_PARITY
303 || aDRCItem->GetErrorCode() == DRCE_MISSING_FOOTPRINT
304 || aDRCItem->GetErrorCode() == DRCE_DUPLICATE_FOOTPRINT
305 || aDRCItem->GetErrorCode() == DRCE_EXTRA_FOOTPRINT )
306 {
307 return frame()->GetRunMenuCommandDescription( PCB_ACTIONS::updatePcbFromSchematic );
308 }
309 else if( aDRCItem->GetErrorCode() == DRCE_FOOTPRINT_TYPE_MISMATCH
310 || aDRCItem->GetErrorCode() == DRCE_FOOTPRINT )
311 {
312 return _( "Edit Footprint Properties..." );
313 }
314 else if( aDRCItem->GetErrorCode() == DRCE_PADSTACK
315 || aDRCItem->GetErrorCode() == DRCE_PADSTACK_INVALID )
316 {
317 return _( "Edit Pad Properties..." );
318 }
319 else if( aDRCItem->GetErrorCode() == DRCE_TEXT_HEIGHT
320 || aDRCItem->GetErrorCode() == DRCE_TEXT_THICKNESS
321 || aDRCItem->GetErrorCode() == DRCE_MIRRORED_TEXT_ON_FRONT_LAYER
322 || aDRCItem->GetErrorCode() == DRCE_NONMIRRORED_TEXT_ON_BACK_LAYER )
323 {
324 BOARD_ITEM* item = m_pcb->ResolveItem( aDRCItem->GetMainItemID() );
325
326 if( item && BaseType( item->Type() ) == PCB_DIMENSION_T )
327 return _( "Edit Dimension Properties..." );
328 else if( item && item->Type() == PCB_FIELD_T )
329 return _( "Edit Field Properties..." );
330 else
331 return _( "Edit Text Properties..." );
332 }
333 else if( aDRCItem->GetErrorCode() == DRCE_DANGLING_TRACK
334 || aDRCItem->GetErrorCode() == DRCE_DANGLING_VIA )
335 {
336 return frame()->GetRunMenuCommandDescription( PCB_ACTIONS::cleanupTracksAndVias );
337 }
338
339 return wxEmptyString;
340}
341
342
343void DRC_TOOL::FixDRCError( const std::shared_ptr<RC_ITEM>& aDRCItem )
344{
345 if( aDRCItem->GetErrorCode() == DRCE_LIB_FOOTPRINT_ISSUES )
346 {
348 }
349 else if( aDRCItem->GetErrorCode() == DRCE_LIB_FOOTPRINT_MISMATCH
350 || aDRCItem->GetErrorCode() == DRCE_FOOTPRINT_FILTERS )
351 {
352 bool updateMode = aDRCItem->GetErrorCode() == DRCE_LIB_FOOTPRINT_MISMATCH;
353 BOARD_ITEM* item = m_pcb->ResolveItem( aDRCItem->GetMainItemID() );
354
355 if( FOOTPRINT* footprint = dynamic_cast<FOOTPRINT*>( item ) )
356 {
357 DIALOG_EXCHANGE_FOOTPRINTS dialog( m_editFrame, footprint, updateMode, true );
358 dialog.ShowQuasiModal();
359 }
360 }
361 else if( aDRCItem->GetErrorCode() == DRCE_SCHEMATIC_PARITY
362 || aDRCItem->GetErrorCode() == DRCE_MISSING_FOOTPRINT
363 || aDRCItem->GetErrorCode() == DRCE_DUPLICATE_FOOTPRINT
364 || aDRCItem->GetErrorCode() == DRCE_EXTRA_FOOTPRINT )
365 {
367 }
368 else if( aDRCItem->GetErrorCode() == DRCE_FOOTPRINT_TYPE_MISMATCH
369 || aDRCItem->GetErrorCode() == DRCE_FOOTPRINT
370 || aDRCItem->GetErrorCode() == DRCE_PADSTACK
371 || aDRCItem->GetErrorCode() == DRCE_PADSTACK_INVALID
372 || aDRCItem->GetErrorCode() == DRCE_TEXT_HEIGHT
373 || aDRCItem->GetErrorCode() == DRCE_TEXT_THICKNESS
374 || aDRCItem->GetErrorCode() == DRCE_MIRRORED_TEXT_ON_FRONT_LAYER
375 || aDRCItem->GetErrorCode() == DRCE_NONMIRRORED_TEXT_ON_BACK_LAYER)
376
377 {
378 BOARD_ITEM* item = m_pcb->ResolveItem( aDRCItem->GetMainItemID() );
379
380 m_editFrame->OnEditItemRequest( item );
381 }
382 else if( aDRCItem->GetErrorCode() == DRCE_DANGLING_TRACK
383 || aDRCItem->GetErrorCode() == DRCE_DANGLING_VIA )
384 {
386 }
387}
388
389
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
static TOOL_ACTION updatePcbFromSchematic
Definition actions.h:263
static TOOL_ACTION excludeMarker
Definition actions.h:128
static TOOL_ACTION nextMarker
Definition actions.h:127
static TOOL_ACTION showFootprintLibTable
Definition actions.h:284
static TOOL_ACTION prevMarker
Definition actions.h:126
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:223
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition drc_tool.cpp:57
void updatePointers(bool aDRCWasCancelled)
Update needed pointers from the one pointer which is known not to change.
Definition drc_tool.cpp:209
void ShowDRCDialog(wxWindow *aParent)
Opens the DRC dialog.
Definition drc_tool.cpp:72
void FixDRCError(const std::shared_ptr< RC_ITEM > &aDRCItem)
Definition drc_tool.cpp:343
int NextMarker(const TOOL_EVENT &aEvent)
Definition drc_tool.cpp:238
wxString FixDRCErrorMenuText(const std::shared_ptr< RC_ITEM > &aDRCItem)
Definition drc_tool.cpp:288
void DestroyDRCDialog()
Close and free the DRC dialog.
Definition drc_tool.cpp:123
DIALOG_DRC * m_drcDialog
Definition drc_tool.h:117
PCB_EDIT_FRAME * m_editFrame
Definition drc_tool.h:115
bool IsDRCDialogShown()
Check to see if the DRC_TOOL dialog is currently shown.
Definition drc_tool.cpp:114
int CrossProbe(const TOOL_EVENT &aEvent)
Definition drc_tool.cpp:255
int ExcludeMarker(const TOOL_EVENT &aEvent)
Definition drc_tool.cpp:279
void setTransitions() override
< Set up handlers for various events.
Definition drc_tool.cpp:390
BOARD * m_pcb
Definition drc_tool.h:116
int PrevMarker(const TOOL_EVENT &aEvent)
Definition drc_tool.cpp:221
bool m_drcRunning
Definition drc_tool.h:118
void RunTests(PROGRESS_REPORTER *aProgressReporter, bool aRefillZones, bool aReportAllTrackErrors, bool aTestFootprints)
Run the DRC tests.
Definition drc_tool.cpp:133
std::shared_ptr< DRC_ENGINE > m_drcEngine
Definition drc_tool.h:119
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
static const TOOL_EVENT SelectedEvent
Definition actions.h:346
static const TOOL_EVENT PointSelectedEvent
Definition actions.h:345
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
static TOOL_ACTION updateFootprint
static TOOL_ACTION cleanupTracksAndVias
static TOOL_ACTION runDRC
static TOOL_ACTION changeFootprint
The selection tool: currently supports:
PCB_SELECTION & GetSelection()
T * frame() const
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
const PCB_SELECTION & selection() const
FOOTPRINT * footprint() const
A progress reporter interface for use in multi-threaded environments.
virtual bool IsCancelled() const =0
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:186
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:78
Generic, UI-independent tool event.
Definition tool_event.h:171
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).
void Activate()
Run the tool.
Handle actions specific to filling copper zones.
void FillAllZones(wxWindow *aCaller, PROGRESS_REPORTER *aReporter=nullptr, bool aHeadless=false)
@ DRCE_FOOTPRINT_FILTERS
Definition drc_item.h:79
@ DRCE_PADSTACK
Definition drc_item.h:62
@ DRCE_MIRRORED_TEXT_ON_FRONT_LAYER
Definition drc_item.h:109
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition drc_item.h:82
@ DRCE_DANGLING_VIA
Definition drc_item.h:50
@ DRCE_PADSTACK_INVALID
Definition drc_item.h:63
@ DRCE_FOOTPRINT_TYPE_MISMATCH
Definition drc_item.h:81
@ DRCE_NONMIRRORED_TEXT_ON_BACK_LAYER
Definition drc_item.h:110
@ DRCE_DUPLICATE_FOOTPRINT
Definition drc_item.h:75
@ DRCE_DANGLING_TRACK
Definition drc_item.h:51
@ DRCE_TEXT_HEIGHT
Definition drc_item.h:99
@ DRCE_EXTRA_FOOTPRINT
Definition drc_item.h:76
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition drc_item.h:83
@ DRCE_MISSING_FOOTPRINT
Definition drc_item.h:74
@ DRCE_FOOTPRINT
Definition drc_item.h:85
@ DRCE_TEXT_THICKNESS
Definition drc_item.h:100
@ DRCE_SCHEMATIC_PARITY
Definition drc_item.h:78
#define _(s)
This file contains miscellaneous commonly used macros and functions.
#define SKIP_SET_DIRTY
Definition sch_commit.h:42
#define SKIP_UNDO
Definition sch_commit.h:40
constexpr KICAD_T BaseType(const KICAD_T aType)
Return the underlying type of the given type.
Definition typeinfo.h:254
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:90
@ PCB_MARKER_T
class PCB_MARKER, a marker used to show something
Definition typeinfo.h:99
@ PCB_DIMENSION_T
class PCB_DIMENSION_BASE: abstract dimension meta-type
Definition typeinfo.h:100
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695