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, 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
26#include <tool/tool_manager.h>
27#include <tools/pcb_actions.h>
28#include <footprint.h>
29#include <pad.h>
30#include <pcb_marker.h>
31#include <drc/drc_item.h>
35
36
37static FOOTPRINT* g_lastFootprint = nullptr;
38static bool g_lastChecksRun = false;
39
40
43 m_frame( aParent ),
44 m_checksRun( false ),
45 m_centerMarkerOnIdle( nullptr )
46{
47 m_markersProvider = std::make_shared<DRC_ITEMS_PROVIDER>( m_frame->GetBoard(), MARKER_BASE::MARKER_DRC );
48
50 m_markersDataView->AssociateModel( m_markersTreeModel );
51 m_markersDataView->SetLayoutDirection( wxLayout_LeftToRight );
52
53 if( m_frame->GetBoard()->GetFirstFootprint() == g_lastFootprint )
55
56 SetupStandardButtons( { { wxID_OK, _( "Run Checks" ) },
57 { wxID_CANCEL, _( "Close" ) } } );
58
60}
61
62
64{
65 m_frame->ClearFocus();
66
67 g_lastFootprint = m_frame->GetBoard()->GetFirstFootprint();
69
70 m_markersTreeModel->DecRef();
71}
72
73
79
80
86
87
89{
90 BOARD* board = m_frame->GetBoard();
91 FOOTPRINT* footprint = board->GetFirstFootprint();
92
94
95 if( !footprint )
96 return;
97
98 auto errorHandler =
99 [&]( const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB, const BOARD_ITEM* aItemC,
100 int aErrorCode, const wxString& aMsg, const VECTOR2I& aPt )
101 {
102 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( aErrorCode );
103
104 if( !aMsg.IsEmpty() )
105 drcItem->SetErrorDetail( aMsg );
106
107 drcItem->SetItems( aItemA, aItemB, aItemC );
108
109 PCB_MARKER* marker = new PCB_MARKER( drcItem, aPt );
110 board->Add( marker );
111 m_frame->GetCanvas()->GetView()->Add( marker );
112 };
113
114 OUTLINE_ERROR_HANDLER outlineErrorHandler =
115 [&]( const wxString& aMsg, BOARD_ITEM* aItemA, BOARD_ITEM* aItemB, const VECTOR2I& aPt )
116 {
117 if( !aItemA ) // If we only have a single item, make sure it's A
118 std::swap( aItemA, aItemB );
119
120 errorHandler( aItemA, aItemB, nullptr, DRCE_MALFORMED_COURTYARD, aMsg, aPt );
121 };
122
123 footprint->BuildCourtyardCaches( &outlineErrorHandler );
124
125 if( !footprint->AllowMissingCourtyard()
126 && footprint->GetCourtyard( F_CrtYd ).OutlineCount() == 0
127 && footprint->GetCourtyard( B_CrtYd ).OutlineCount() == 0 )
128 {
129 errorHandler( footprint, nullptr, nullptr, DRCE_MISSING_COURTYARD, wxEmptyString, { 0, 0 } );
130 }
131
132 footprint->CheckFootprintAttributes(
133 [&]( const wxString& aMsg )
134 {
135 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT_TYPE_MISMATCH, aMsg, { 0, 0 } );
136 } );
137
138 footprint->CheckPads( m_frame,
139 [&]( const PAD* aPad, int aErrorCode, const wxString& aMsg )
140 {
141 errorHandler( aPad, nullptr, nullptr, aErrorCode, aMsg, aPad->GetPosition() );
142 } );
143
144 footprint->CheckShortingPads(
145 [&]( const PAD* aPadA, const PAD* aPadB, int aErrorCode, const VECTOR2I& aPosition )
146 {
147 errorHandler( aPadA, aPadB, nullptr, aErrorCode, wxEmptyString, aPosition );
148 } );
149
150 if( footprint->IsNetTie() )
151 {
152 footprint->CheckNetTiePadGroups(
153 [&]( const wxString& aMsg )
154 {
155 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT, aMsg, { 0, 0 } );
156 } );
157
158 footprint->CheckNetTies(
159 [&]( const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB, const BOARD_ITEM* aItemC,
160 const VECTOR2I& aPt )
161 {
162 errorHandler( aItemA, aItemB, aItemC, DRCE_SHORTING_ITEMS, wxEmptyString, aPt );
163 } );
164 }
165
166 footprint->CheckClippedSilk(
167 [&]( BOARD_ITEM* aItemA, BOARD_ITEM* aItemB, const VECTOR2I& aPt )
168 {
169 errorHandler( aItemA, aItemB, nullptr, DRCE_SILK_MASK_CLEARANCE, wxEmptyString, aPt );
170 } );
171
172 m_checksRun = true;
173 updateData();
175}
176
177
179{
180 m_markersTreeModel->SelectMarker( aMarker );
181
182 // wxWidgets on some platforms fails to correctly ensure that a selected item is
183 // visible, so we have to do it in a separate idle event.
184 m_centerMarkerOnIdle = aMarker;
185 Bind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
186}
187
188
190{
191 if( m_markersTreeModel->GetView()->IsFrozen() )
192 return;
193
195 m_centerMarkerOnIdle = nullptr;
196 Unbind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
197}
198
199
200void DIALOG_FOOTPRINT_CHECKER::OnRunChecksClick( wxCommandEvent& aEvent )
201{
202 m_checksRun = false;
203 runChecks();
204}
205
206
207void DIALOG_FOOTPRINT_CHECKER::OnSelectItem( wxDataViewEvent& aEvent )
208{
209 BOARD* board = m_frame->GetBoard();
210 RC_TREE_NODE* node = RC_TREE_MODEL::ToNode( aEvent.GetItem() );
211 const KIID& itemID = node ? RC_TREE_MODEL::ToUUID( aEvent.GetItem() ) : niluuid;
212 BOARD_ITEM* item = board->ResolveItem( itemID, true );
213
215 {
216 // we already came from a cross-probe of the marker in the document; don't go around in circles
217 aEvent.Skip();
218 return;
219 }
220
221 if( node && item )
222 {
223 PCB_LAYER_ID principalLayer = UNDEFINED_LAYER;
224 LSET violationLayers;
225 std::shared_ptr<RC_ITEM> rc_item = node->m_RcItem;
226
227 if( item->GetLayerSet().count() > 0 )
228 principalLayer = item->GetLayerSet().Seq().front();
229
230 if( rc_item->GetErrorCode() == DRCE_MALFORMED_COURTYARD )
231 {
232 BOARD_ITEM* a = board->ResolveItem( rc_item->GetMainItemID(), true );
233
234 if( a && ( a->GetFlags() & MALFORMED_B_COURTYARD ) > 0
235 && ( a->GetFlags() & MALFORMED_F_COURTYARD ) == 0 )
236 {
237 principalLayer = B_CrtYd;
238 }
239 else
240 {
241 principalLayer = F_CrtYd;
242 }
243 }
244 else if (rc_item->GetErrorCode() == DRCE_INVALID_OUTLINE )
245 {
246 principalLayer = Edge_Cuts;
247 }
248 else
249 {
250 BOARD_ITEM* a = board->ResolveItem( rc_item->GetMainItemID(), true );
251 BOARD_ITEM* b = board->ResolveItem( rc_item->GetAuxItemID(), true );
252 BOARD_ITEM* c = board->ResolveItem( rc_item->GetAuxItem2ID(), true );
253 BOARD_ITEM* d = board->ResolveItem( rc_item->GetAuxItem3ID(), true );
254
255 if( a || b || c || d )
256 violationLayers = LSET::AllLayersMask();
257
258 if( a )
259 violationLayers &= a->GetLayerSet();
260
261 if( b )
262 violationLayers &= b->GetLayerSet();
263
264 if( c )
265 violationLayers &= c->GetLayerSet();
266
267 if( d )
268 violationLayers &= d->GetLayerSet();
269 }
270
271 if( violationLayers.count() )
272 principalLayer = violationLayers.Seq().front();
273 else if( principalLayer >= 0 )
274 violationLayers.set( principalLayer );
275
276 WINDOW_THAWER thawer( m_frame );
277
278 m_frame->FocusOnItem( item );
279 m_frame->GetCanvas()->Refresh();
280
281 if( ( violationLayers & board->GetVisibleLayers() ).none() )
282 {
283 m_frame->GetAppearancePanel()->SetLayerVisible( principalLayer, true );
284 m_frame->GetCanvas()->Refresh();
285 }
286
287 if( board->GetVisibleLayers().test( principalLayer ) )
288 m_frame->SetActiveLayer( principalLayer );
289 }
290
291 aEvent.Skip();
292}
293
294
296{
297 if( m_markersDataView->GetCurrentItem().IsOk() )
298 {
299 // turn control over to m_frame, hide this DIALOG_FOOTPRINT_CHECKER window,
300 // no destruction so we can preserve listbox cursor
301 if( !IsModal() )
302 Show( false );
303 }
304
305 // Do not skip event here: this is not useful, and Pcbnew crashes if skipped (at least on MSW)
306}
307
308
310{
311 int severities = 0;
312
313 if( m_showErrors->GetValue() )
314 severities |= RPT_SEVERITY_ERROR;
315
316 if( m_showWarnings->GetValue() )
317 severities |= RPT_SEVERITY_WARNING;
318
319 if( m_showExclusions->GetValue() )
320 severities |= RPT_SEVERITY_EXCLUSION;
321
322 return severities;
323}
324
325
326void DIALOG_FOOTPRINT_CHECKER::OnSeverity( wxCommandEvent& aEvent )
327{
328 if( aEvent.GetEventObject() == m_showAll )
329 {
330 m_showErrors->SetValue( true );
331 m_showWarnings->SetValue( aEvent.IsChecked() );
332 m_showExclusions->SetValue( aEvent.IsChecked() );
333 }
334
335 updateData();
336}
337
338
339void DIALOG_FOOTPRINT_CHECKER::OnCancelClick( wxCommandEvent& aEvent )
340{
341 m_frame->ClearFocus();
342
343 SetReturnCode( wxID_CANCEL );
344
345 // Leave the tool to destroy (or not) the dialog
346 FOOTPRINT_EDITOR_CONTROL* tool = m_frame->GetToolManager()->GetTool<FOOTPRINT_EDITOR_CONTROL>();
347 tool->DestroyCheckerDialog();
348}
349
350
351void DIALOG_FOOTPRINT_CHECKER::OnClose( wxCloseEvent& aEvent )
352{
353 wxCommandEvent dummy;
355}
356
357
359{
360 WINDOW_THAWER thawer( m_frame );
361
362 m_frame->GetCanvas()->Refresh();
363}
364
365
366void DIALOG_FOOTPRINT_CHECKER::OnDeleteOneClick( wxCommandEvent& aEvent )
367{
368 // Clear the selection. It may be the selected DRC marker.
369 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
370
371 m_markersTreeModel->DeleteCurrentItem( true );
372
373 // redraw the pcb
375
377}
378
379
381{
383
384 m_checksRun = false;
387}
388
389
391{
392 // Clear current selection list to avoid selection of deleted items
393 m_frame->GetToolManager()->RunAction( ACTIONS::selectionClear );
394
395 m_markersTreeModel->DeleteItems( false, true, false );
396 m_frame->GetBoard()->DeleteMARKERs( true, true );
397}
398
399
401{
402 // Collect counts:
403
404 int numErrors = 0;
405 int numWarnings = 0;
406 int numExcluded = 0;
407
409 {
410 numErrors += m_markersProvider->GetCount( RPT_SEVERITY_ERROR );
411 numWarnings += m_markersProvider->GetCount( RPT_SEVERITY_WARNING );
412 numExcluded += m_markersProvider->GetCount( RPT_SEVERITY_EXCLUSION );
413 }
414
415 // Update badges:
416
417 if( !m_checksRun && numErrors == 0 )
418 numErrors = -1;
419
420 if( !m_checksRun && numWarnings == 0 )
421 numWarnings = -1;
422
423 m_errorsBadge->SetMaximumNumber( numErrors );
424 m_errorsBadge->UpdateNumber( numErrors, RPT_SEVERITY_ERROR );
425
426 m_warningsBadge->SetMaximumNumber( numWarnings );
427 m_warningsBadge->UpdateNumber( numWarnings, RPT_SEVERITY_WARNING );
428
429 m_exclusionsBadge->SetMaximumNumber( numExcluded );
430 m_exclusionsBadge->UpdateNumber( numExcluded, RPT_SEVERITY_EXCLUSION );
431}
432
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:224
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:84
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:257
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1222
const LSET & GetVisibleLayers() const
A proxy function that calls the correspondent function in m_BoardSettings.
Definition board.cpp:981
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition board.h:530
BOARD_ITEM * ResolveItem(const KIID &aID, bool aAllowNullptrReturn=false) const
Definition board.cpp:1779
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:400
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:151
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:420
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:430
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:49
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:313
static const LSET & AllLayersMask()
Definition lset.cpp:641
Definition pad.h:55
VECTOR2I GetPosition() const override
Definition pad.h:209
static RC_TREE_NODE * ToNode(wxDataViewItem aItem)
Definition rc_item.h:250
static KIID ToUUID(wxDataViewItem aItem)
Definition rc_item.cpp:226
std::shared_ptr< RC_ITEM > m_RcItem
Definition rc_item.h:235
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:97
@ DRCE_INVALID_OUTLINE
Definition drc_item.h:73
@ DRCE_MISSING_COURTYARD
Definition drc_item.h:67
@ DRCE_SHORTING_ITEMS
Definition drc_item.h:41
@ DRCE_MALFORMED_COURTYARD
Definition drc_item.h:68
@ DRCE_FOOTPRINT_TYPE_MISMATCH
Definition drc_item.h:82
@ DRCE_FOOTPRINT
Definition drc_item.h:86
#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:60
@ F_CrtYd
Definition layer_ids.h:116
@ Edge_Cuts
Definition layer_ids.h:112
@ B_CrtYd
Definition layer_ids.h:115
@ UNDEFINED_LAYER
Definition layer_ids.h:61
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
std::vector< FAB_LAYER_COLOR > dummy
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695