KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_footprint_checker.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, see <https://www.gnu.org/licenses/>.
18 */
19
22#include <tool/tool_manager.h>
23#include <tools/pcb_actions.h>
24#include <footprint.h>
25#include <pad.h>
26#include <pcb_marker.h>
27#include <drc/drc_item.h>
31
32
33static FOOTPRINT* g_lastFootprint = nullptr;
34static bool g_lastChecksRun = false;
35
36
39 m_frame( aParent ),
40 m_checksRun( false ),
41 m_centerMarkerOnIdle( nullptr )
42{
43 m_markersProvider = std::make_shared<DRC_ITEMS_PROVIDER>( m_frame->GetBoard(), MARKER_BASE::MARKER_DRC );
44
46 m_markersDataView->AssociateModel( m_markersTreeModel );
47 m_markersDataView->SetLayoutDirection( wxLayout_LeftToRight );
48
49 if( m_frame->GetBoard()->GetFirstFootprint() == g_lastFootprint )
51
52 SetupStandardButtons( { { wxID_OK, _( "Run Checks" ) },
53 { wxID_CANCEL, _( "Close" ) } } );
54
56}
57
58
60{
61 m_frame->ClearFocus();
62
63 g_lastFootprint = m_frame->GetBoard()->GetFirstFootprint();
65
66 m_markersTreeModel->DecRef();
67}
68
69
75
76
82
83
85{
86 BOARD* board = m_frame->GetBoard();
87 FOOTPRINT* footprint = board->GetFirstFootprint();
88
90
91 if( !footprint )
92 return;
93
94 auto errorHandler =
95 [&]( const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB, const BOARD_ITEM* aItemC,
96 int aErrorCode, const wxString& aMsg, const VECTOR2I& aPt )
97 {
98 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( aErrorCode );
99
100 if( !aMsg.IsEmpty() )
101 drcItem->SetErrorDetail( aMsg );
102
103 drcItem->SetItems( aItemA, aItemB, aItemC );
104
105 PCB_MARKER* marker = new PCB_MARKER( drcItem, aPt );
106 board->Add( marker );
107 m_frame->GetCanvas()->GetView()->Add( marker );
108 };
109
110 OUTLINE_ERROR_HANDLER outlineErrorHandler =
111 [&]( const wxString& aMsg, BOARD_ITEM* aItemA, BOARD_ITEM* aItemB, const VECTOR2I& aPt )
112 {
113 if( !aItemA ) // If we only have a single item, make sure it's A
114 std::swap( aItemA, aItemB );
115
116 errorHandler( aItemA, aItemB, nullptr, DRCE_MALFORMED_COURTYARD, aMsg, aPt );
117 };
118
119 footprint->BuildCourtyardCaches( &outlineErrorHandler );
120
121 if( !footprint->AllowMissingCourtyard()
122 && footprint->GetCourtyard( F_CrtYd ).OutlineCount() == 0
123 && footprint->GetCourtyard( B_CrtYd ).OutlineCount() == 0 )
124 {
125 errorHandler( footprint, nullptr, nullptr, DRCE_MISSING_COURTYARD, wxEmptyString, { 0, 0 } );
126 }
127
128 footprint->CheckFootprintAttributes(
129 [&]( const wxString& aMsg )
130 {
131 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT_TYPE_MISMATCH, aMsg, { 0, 0 } );
132 } );
133
134 footprint->CheckPads( m_frame,
135 [&]( const PAD* aPad, int aErrorCode, const wxString& aMsg )
136 {
137 errorHandler( aPad, nullptr, nullptr, aErrorCode, aMsg, aPad->GetPosition() );
138 } );
139
140 footprint->CheckShortingPads(
141 [&]( const PAD* aPadA, const PAD* aPadB, int aErrorCode, const VECTOR2I& aPosition )
142 {
143 errorHandler( aPadA, aPadB, nullptr, aErrorCode, wxEmptyString, aPosition );
144 } );
145
146 if( footprint->IsNetTie() )
147 {
148 footprint->CheckNetTiePadGroups(
149 [&]( const wxString& aMsg )
150 {
151 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT, aMsg, { 0, 0 } );
152 } );
153
154 footprint->CheckNetTies(
155 [&]( const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB, const BOARD_ITEM* aItemC,
156 const VECTOR2I& aPt )
157 {
158 errorHandler( aItemA, aItemB, aItemC, DRCE_SHORTING_ITEMS, wxEmptyString, aPt );
159 } );
160 }
161
162 footprint->CheckClippedSilk(
163 [&]( BOARD_ITEM* aItemA, BOARD_ITEM* aItemB, const VECTOR2I& aPt )
164 {
165 errorHandler( aItemA, aItemB, nullptr, DRCE_SILK_MASK_CLEARANCE, wxEmptyString, aPt );
166 } );
167
168 m_checksRun = true;
169 updateData();
171}
172
173
175{
176 m_markersTreeModel->SelectMarker( aMarker );
177
178 // wxWidgets on some platforms fails to correctly ensure that a selected item is
179 // visible, so we have to do it in a separate idle event.
180 m_centerMarkerOnIdle = aMarker;
181 Bind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
182}
183
184
186{
187 if( m_markersTreeModel->GetView()->IsFrozen() )
188 return;
189
191 m_centerMarkerOnIdle = nullptr;
192 Unbind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
193}
194
195
196void DIALOG_FOOTPRINT_CHECKER::OnRunChecksClick( wxCommandEvent& aEvent )
197{
198 m_checksRun = false;
199 runChecks();
200}
201
202
203void DIALOG_FOOTPRINT_CHECKER::OnSelectItem( wxDataViewEvent& aEvent )
204{
205 BOARD* board = m_frame->GetBoard();
206 RC_TREE_NODE* node = RC_TREE_MODEL::ToNode( aEvent.GetItem() );
207 const KIID& itemID = node ? RC_TREE_MODEL::ToUUID( aEvent.GetItem() ) : niluuid;
208 BOARD_ITEM* item = board->ResolveItem( itemID, true );
209
211 {
212 // we already came from a cross-probe of the marker in the document; don't go around in circles
213 aEvent.Skip();
214 return;
215 }
216
217 if( node && item )
218 {
219 PCB_LAYER_ID principalLayer = UNDEFINED_LAYER;
220 LSET violationLayers;
221 std::shared_ptr<RC_ITEM> rc_item = node->m_RcItem;
222
223 if( item->GetLayerSet().count() > 0 )
224 principalLayer = item->GetLayerSet().Seq().front();
225
226 if( rc_item->GetErrorCode() == DRCE_MALFORMED_COURTYARD )
227 {
228 BOARD_ITEM* a = board->ResolveItem( rc_item->GetMainItemID(), true );
229
230 if( a && ( a->GetFlags() & MALFORMED_B_COURTYARD ) > 0
231 && ( a->GetFlags() & MALFORMED_F_COURTYARD ) == 0 )
232 {
233 principalLayer = B_CrtYd;
234 }
235 else
236 {
237 principalLayer = F_CrtYd;
238 }
239 }
240 else if (rc_item->GetErrorCode() == DRCE_INVALID_OUTLINE )
241 {
242 principalLayer = Edge_Cuts;
243 }
244 else
245 {
246 BOARD_ITEM* a = board->ResolveItem( rc_item->GetMainItemID(), true );
247 BOARD_ITEM* b = board->ResolveItem( rc_item->GetAuxItemID(), true );
248 BOARD_ITEM* c = board->ResolveItem( rc_item->GetAuxItem2ID(), true );
249 BOARD_ITEM* d = board->ResolveItem( rc_item->GetAuxItem3ID(), true );
250
251 if( a || b || c || d )
252 violationLayers = LSET::AllLayersMask();
253
254 if( a )
255 violationLayers &= a->GetLayerSet();
256
257 if( b )
258 violationLayers &= b->GetLayerSet();
259
260 if( c )
261 violationLayers &= c->GetLayerSet();
262
263 if( d )
264 violationLayers &= d->GetLayerSet();
265 }
266
267 if( violationLayers.count() )
268 principalLayer = violationLayers.Seq().front();
269 else if( principalLayer >= 0 )
270 violationLayers.set( principalLayer );
271
272 WINDOW_THAWER thawer( m_frame );
273
274 m_frame->FocusOnItem( item );
275 m_frame->GetCanvas()->Refresh();
276
277 if( ( violationLayers & board->GetVisibleLayers() ).none() )
278 {
279 m_frame->GetAppearancePanel()->SetLayerVisible( principalLayer, true );
280 m_frame->GetCanvas()->Refresh();
281 }
282
283 if( board->GetVisibleLayers().test( principalLayer ) )
284 m_frame->SetActiveLayer( principalLayer );
285 }
286
287 aEvent.Skip();
288}
289
290
292{
293 if( m_markersDataView->GetCurrentItem().IsOk() )
294 {
295 // turn control over to m_frame, hide this DIALOG_FOOTPRINT_CHECKER window,
296 // no destruction so we can preserve listbox cursor
297 if( !IsModal() )
298 Show( false );
299 }
300
301 // Do not skip event here: this is not useful, and Pcbnew crashes if skipped (at least on MSW)
302}
303
304
306{
307 int severities = 0;
308
309 if( m_showErrors->GetValue() )
310 severities |= RPT_SEVERITY_ERROR;
311
312 if( m_showWarnings->GetValue() )
313 severities |= RPT_SEVERITY_WARNING;
314
315 if( m_showExclusions->GetValue() )
316 severities |= RPT_SEVERITY_EXCLUSION;
317
318 return severities;
319}
320
321
322void DIALOG_FOOTPRINT_CHECKER::OnSeverity( wxCommandEvent& aEvent )
323{
324 if( aEvent.GetEventObject() == m_showAll )
325 {
326 m_showErrors->SetValue( true );
327 m_showWarnings->SetValue( aEvent.IsChecked() );
328 m_showExclusions->SetValue( aEvent.IsChecked() );
329 }
330
331 updateData();
332}
333
334
335void DIALOG_FOOTPRINT_CHECKER::OnCancelClick( wxCommandEvent& aEvent )
336{
337 m_frame->ClearFocus();
338
339 SetReturnCode( wxID_CANCEL );
340
341 // Leave the tool to destroy (or not) the dialog
342 FOOTPRINT_EDITOR_CONTROL* tool = m_frame->GetToolManager()->GetTool<FOOTPRINT_EDITOR_CONTROL>();
343 tool->DestroyCheckerDialog();
344}
345
346
347void DIALOG_FOOTPRINT_CHECKER::OnClose( wxCloseEvent& aEvent )
348{
349 wxCommandEvent dummy;
351}
352
353
355{
356 WINDOW_THAWER thawer( m_frame );
357
358 m_frame->GetCanvas()->Refresh();
359}
360
361
362void DIALOG_FOOTPRINT_CHECKER::OnDeleteOneClick( wxCommandEvent& aEvent )
363{
364 // Clear the selection. It may be the selected DRC marker.
365 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
366
367 m_markersTreeModel->DeleteCurrentItem( true );
368
369 // redraw the pcb
371
373}
374
375
377{
379
380 m_checksRun = false;
383}
384
385
387{
388 // Clear current selection list to avoid selection of deleted items
389 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
390
391 m_markersTreeModel->DeleteItems( false, true, false );
392 m_frame->GetBoard()->DeleteMARKERs( true, true );
393}
394
395
397{
398 // Collect counts:
399
400 int numErrors = 0;
401 int numWarnings = 0;
402 int numExcluded = 0;
403
405 {
406 numErrors += m_markersProvider->GetCount( RPT_SEVERITY_ERROR );
407 numWarnings += m_markersProvider->GetCount( RPT_SEVERITY_WARNING );
408 numExcluded += m_markersProvider->GetCount( RPT_SEVERITY_EXCLUSION );
409 }
410
411 // Update badges:
412
413 if( !m_checksRun && numErrors == 0 )
414 numErrors = -1;
415
416 if( !m_checksRun && numWarnings == 0 )
417 numWarnings = -1;
418
419 m_errorsBadge->SetMaximumNumber( numErrors );
420 m_errorsBadge->UpdateNumber( numErrors, RPT_SEVERITY_ERROR );
421
422 m_warningsBadge->SetMaximumNumber( numWarnings );
423 m_warningsBadge->UpdateNumber( numWarnings, RPT_SEVERITY_WARNING );
424
425 m_exclusionsBadge->SetMaximumNumber( numExcluded );
426 m_exclusionsBadge->UpdateNumber( numExcluded, RPT_SEVERITY_EXCLUSION );
427}
428
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
BASE_SET & set(size_t pos)
Definition base_set.h:116
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:285
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1295
const LSET & GetVisibleLayers() const
A proxy function that calls the correspondent function in m_BoardSettings.
Definition board.cpp:1048
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition board.h:587
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
Definition board.cpp:1846
DIALOG_FOOTPRINT_CHECKER_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Footprint Checker"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void centerMarkerIdleHandler(wxIdleEvent &aEvent)
void OnCancelClick(wxCommandEvent &aEvent) override
void OnDeleteAllClick(wxCommandEvent &event) override
void OnClose(wxCloseEvent &event) override
std::shared_ptr< RC_ITEMS_PROVIDER > m_markersProvider
void OnSeverity(wxCommandEvent &aEvent) override
DIALOG_FOOTPRINT_CHECKER(FOOTPRINT_EDIT_FRAME *aParent)
void OnRunChecksClick(wxCommandEvent &aEvent) override
void OnSelectItem(wxDataViewEvent &event) override
void SelectMarker(const PCB_MARKER *aMarker)
void OnDeleteOneClick(wxCommandEvent &event) override
void OnLeftDClickItem(wxMouseEvent &event) override
const PCB_MARKER * m_centerMarkerOnIdle
bool Show(bool show) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:417
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:155
Module editor specific tools.
void CheckClippedSilk(const std::function< void(BOARD_ITEM *aItemA, BOARD_ITEM *aItemB, const VECTOR2I &aPt)> &aErrorHandler)
void CheckNetTies(const std::function< void(const BOARD_ITEM *aItem, const BOARD_ITEM *bItem, const BOARD_ITEM *cItem, const VECTOR2I &)> &aErrorHandler)
Check for un-allowed shorting of pads in net-tie footprints.
void CheckPads(UNITS_PROVIDER *aUnitsProvider, const std::function< void(const PAD *, int, const wxString &)> &aErrorHandler)
Run non-board-specific DRC checks on footprint's pads.
bool AllowMissingCourtyard() const
Definition footprint.h:510
void BuildCourtyardCaches(OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr)
Build complex polygons of the courtyard areas from graphic items on the courtyard layers.
bool IsNetTie() const
Definition footprint.h:520
void CheckShortingPads(const std::function< void(const PAD *, const PAD *, int aErrorCode, const VECTOR2I &)> &aErrorHandler)
Check for overlapping, different-numbered, non-net-tie pads.
void CheckNetTiePadGroups(const std::function< void(const wxString &)> &aErrorHandler)
Sanity check net-tie pad groups.
const SHAPE_POLY_SET & GetCourtyard(PCB_LAYER_ID aLayer) const
Used in DRC to test the courtyard area (a complex polygon).
void CheckFootprintAttributes(const std::function< void(const wxString &)> &aErrorHandler)
Test if footprint attributes for type (SMD/Through hole/Other) match the expected type based on the p...
Definition kiid.h:44
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:309
static const LSET & AllLayersMask()
Definition lset.cpp:637
Definition pad.h:61
VECTOR2I GetPosition() const override
Definition pad.cpp:245
static RC_TREE_NODE * ToNode(wxDataViewItem aItem)
Definition rc_item.h:267
static KIID ToUUID(wxDataViewItem aItem)
Definition rc_item.cpp:223
std::shared_ptr< RC_ITEM > m_RcItem
Definition rc_item.h:246
int OutlineCount() const
Return the number of outlines in the set.
const std::function< void(const wxString &msg, BOARD_ITEM *itemA, BOARD_ITEM *itemB, const VECTOR2I &pt)> OUTLINE_ERROR_HANDLER
static bool g_lastChecksRun
static FOOTPRINT * g_lastFootprint
@ DRCE_SILK_MASK_CLEARANCE
Definition drc_item.h:94
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:69
@ DRCE_MISSING_COURTYARD
Definition drc_item.h:63
@ DRCE_SHORTING_ITEMS
Definition drc_item.h:37
@ DRCE_MALFORMED_COURTYARD
Definition drc_item.h:64
@ DRCE_FOOTPRINT_TYPE_MISMATCH
Definition drc_item.h:78
@ DRCE_FOOTPRINT
Definition drc_item.h:82
#define _(s)
#define MALFORMED_F_COURTYARD
#define MALFORMED_B_COURTYARD
KIID niluuid(0)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_CrtYd
Definition layer_ids.h:112
@ Edge_Cuts
Definition layer_ids.h:108
@ B_CrtYd
Definition layer_ids.h:111
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
std::vector< FAB_LAYER_COLOR > dummy
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683