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 (C) 2020-2022 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 ),
46 m_centerMarkerOnIdle( nullptr )
47{
48 m_markersProvider = std::make_shared<DRC_ITEMS_PROVIDER>( m_frame->GetBoard(),
50
52 m_markersDataView->AssociateModel( m_markersTreeModel );
54
56 {
59 }
60
61 SetupStandardButtons( { { wxID_OK, _( "Run Checks" ) },
62 { wxID_CANCEL, _( "Close" ) } } );
63
65
67}
68
69
71{
72 m_frame->FocusOnItem( nullptr );
73
76
77 m_markersTreeModel->DecRef();
78}
79
80
82{
83 return true;
84}
85
86
88{
89 return true;
90}
91
92
93// Don't globally define this; different facilities use different definitions of "ALL"
95
96
98{
103}
104
105
107{
108 BOARD* board = m_frame->GetBoard();
109 FOOTPRINT* footprint = board->GetFirstFootprint();
110 wxString msg;
111
113
114 if( !footprint )
115 {
116 msg = _( "No footprint loaded." );
117 return;
118 }
119
120 auto errorHandler =
121 [&]( const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB, const BOARD_ITEM* aItemC,
122 int aErrorCode, const wxString& aMsg, const VECTOR2I& aPt )
123 {
124 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( aErrorCode );
125
126 if( !aMsg.IsEmpty() )
127 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + aMsg );
128
129 drcItem->SetItems( aItemA, aItemB, aItemC );
130
131 PCB_MARKER* marker = new PCB_MARKER( drcItem, aPt );
132 board->Add( marker );
133 m_frame->GetCanvas()->GetView()->Add( marker );
134 };
135
136 OUTLINE_ERROR_HANDLER outlineErrorHandler =
137 [&]( const wxString& aMsg, BOARD_ITEM* aItemA, BOARD_ITEM* aItemB, const VECTOR2I& aPt )
138 {
139 if( !aItemA ) // If we only have a single item, make sure it's A
140 std::swap( aItemA, aItemB );
141
142 errorHandler( aItemA, aItemB, nullptr, DRCE_MALFORMED_COURTYARD, aMsg, aPt );
143 };
144
145 footprint->BuildCourtyardCaches( &outlineErrorHandler );
146
147 footprint->CheckFootprintAttributes(
148 [&]( const wxString& aMsg )
149 {
150 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT_TYPE_MISMATCH, aMsg,
151 { 0, 0 } );
152 } );
153
154 footprint->CheckPads(
155 [&]( const PAD* aPad, int aErrorCode, const wxString& aMsg )
156 {
157 errorHandler( aPad, nullptr, nullptr, aErrorCode, aMsg, aPad->GetPosition() );
158 } );
159
160 footprint->CheckShortingPads(
161 [&]( const PAD* aPadA, const PAD* aPadB, const VECTOR2I& aPosition )
162 {
163 errorHandler( aPadA, aPadB, nullptr, DRCE_SHORTING_ITEMS, wxEmptyString,
164 aPosition );
165 } );
166
167 if( footprint->IsNetTie() )
168 {
169 footprint->CheckNetTiePadGroups(
170 [&]( const wxString& aMsg )
171 {
172 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT, aMsg, { 0, 0 } );
173 } );
174
175 footprint->CheckNetTies(
176 [&]( const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB, const BOARD_ITEM* aItemC,
177 const VECTOR2I& aPosition )
178 {
179 errorHandler( aItemA, aItemB, aItemC, DRCE_SHORTING_ITEMS, wxEmptyString,
180 aPosition );
181 } );
182 }
183
184 m_checksRun = true;
185
188
190}
191
192
194{
196
197 // wxWidgets on some platforms fails to correctly ensure that a selected item is
198 // visible, so we have to do it in a separate idle event.
199 m_centerMarkerOnIdle = aMarker;
200 Bind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
201}
202
203
205{
207 m_centerMarkerOnIdle = nullptr;
208 Unbind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
209}
210
211
212void DIALOG_FOOTPRINT_CHECKER::OnRunChecksClick( wxCommandEvent& aEvent )
213{
214 m_checksRun = false;
215
216 runChecks();
217}
218
219
220void DIALOG_FOOTPRINT_CHECKER::OnSelectItem( wxDataViewEvent& aEvent )
221{
222 BOARD* board = m_frame->GetBoard();
223 RC_TREE_NODE* node = RC_TREE_MODEL::ToNode( aEvent.GetItem() );
224 const KIID& itemID = node ? RC_TREE_MODEL::ToUUID( aEvent.GetItem() ) : niluuid;
225 BOARD_ITEM* item = board->GetItem( itemID );
226
228 {
229 // we already came from a cross-probe of the marker in the document; don't go
230 // around in circles
231
232 aEvent.Skip();
233 return;
234 }
235
236 if( node && item )
237 {
238 PCB_LAYER_ID principalLayer = UNDEFINED_LAYER;
239 LSET violationLayers;
240 std::shared_ptr<RC_ITEM> rc_item = node->m_RcItem;
241
242 if( item->GetLayerSet().count() > 0 )
243 principalLayer = item->GetLayerSet().Seq().front();
244
245 if( rc_item->GetErrorCode() == DRCE_MALFORMED_COURTYARD )
246 {
247 BOARD_ITEM* a = board->GetItem( rc_item->GetMainItemID() );
248
249 if( a && ( a->GetFlags() & MALFORMED_B_COURTYARD ) > 0
250 && ( a->GetFlags() & MALFORMED_F_COURTYARD ) == 0 )
251 {
252 principalLayer = B_CrtYd;
253 }
254 else
255 {
256 principalLayer = F_CrtYd;
257 }
258 }
259 else if (rc_item->GetErrorCode() == DRCE_INVALID_OUTLINE )
260 {
261 principalLayer = Edge_Cuts;
262 }
263 else
264 {
265 BOARD_ITEM* a = board->GetItem( rc_item->GetMainItemID() );
266 BOARD_ITEM* b = board->GetItem( rc_item->GetAuxItemID() );
267 BOARD_ITEM* c = board->GetItem( rc_item->GetAuxItem2ID() );
268 BOARD_ITEM* d = board->GetItem( rc_item->GetAuxItem3ID() );
269
270 if( a || b || c || d )
271 violationLayers = LSET::AllLayersMask();
272
273 if( a )
274 violationLayers &= a->GetLayerSet();
275
276 if( b )
277 violationLayers &= b->GetLayerSet();
278
279 if( c )
280 violationLayers &= c->GetLayerSet();
281
282 if( d )
283 violationLayers &= d->GetLayerSet();
284 }
285
286 if( violationLayers.count() )
287 principalLayer = violationLayers.Seq().front();
288 else
289 violationLayers.set( principalLayer );
290
291 WINDOW_THAWER thawer( m_frame );
292
293 m_frame->FocusOnItem( item );
295
296 if( ( violationLayers & board->GetVisibleLayers() ) == 0 )
297 {
298 m_frame->GetAppearancePanel()->SetLayerVisible( principalLayer, true );
300 }
301
302 if( board->GetVisibleLayers().test( principalLayer ) )
303 m_frame->SetActiveLayer( principalLayer );
304 }
305
306 aEvent.Skip();
307}
308
309
311{
312 if( m_markersDataView->GetCurrentItem().IsOk() )
313 {
314 // turn control over to m_frame, hide this DIALOG_FOOTPRINT_CHECKER window,
315 // no destruction so we can preserve listbox cursor
316 if( !IsModal() )
317 Show( false );
318 }
319
320 // Do not skip aVent here: this is not useful, and Pcbnew crashes
321 // if skipped (at least on Windows)
322}
323
324
325void DIALOG_FOOTPRINT_CHECKER::OnSeverity( wxCommandEvent& aEvent )
326{
327 int flag = 0;
328
329 if( aEvent.GetEventObject() == m_showAll )
331 else if( aEvent.GetEventObject() == m_showErrors )
333 else if( aEvent.GetEventObject() == m_showWarnings )
335 else if( aEvent.GetEventObject() == m_showExclusions )
337
338 if( aEvent.IsChecked() )
340 else if( aEvent.GetEventObject() == m_showAll )
342 else
343 m_severities &= ~flag;
344
346
349}
350
351
352void DIALOG_FOOTPRINT_CHECKER::OnCancelClick( wxCommandEvent& aEvent )
353{
354 m_frame->FocusOnItem( nullptr );
355
356 SetReturnCode( wxID_CANCEL );
357
358 // Leave the tool to destroy (or not) the dialog
360 tool->DestroyCheckerDialog();
361}
362
363
364void DIALOG_FOOTPRINT_CHECKER::OnClose( wxCloseEvent& aEvent )
365{
366 wxCommandEvent dummy;
368}
369
370
372{
373 WINDOW_THAWER thawer( m_frame );
374
376}
377
378
379void DIALOG_FOOTPRINT_CHECKER::OnDeleteOneClick( wxCommandEvent& aEvent )
380{
381 // Clear the selection. It may be the selected DRC marker.
383
385
386 // redraw the pcb
388
390}
391
392
394{
396
397 m_checksRun = false;
400}
401
402
404{
405 // Clear current selection list to avoid selection of deleted items
407
408 m_markersTreeModel->DeleteItems( false, true, false );
409 m_frame->GetBoard()->DeleteMARKERs( true, true );
410}
411
412
414{
415 // Collect counts:
416
417 int numErrors = 0;
418 int numWarnings = 0;
419 int numExcluded = 0;
420
422 {
423 numErrors += m_markersProvider->GetCount( RPT_SEVERITY_ERROR );
424 numWarnings += m_markersProvider->GetCount( RPT_SEVERITY_WARNING );
425 numExcluded += m_markersProvider->GetCount( RPT_SEVERITY_EXCLUSION );
426 }
427
428 // Update badges:
429
430 if( !m_checksRun && numErrors == 0 )
431 numErrors = -1;
432
433 if( !m_checksRun && numWarnings == 0 )
434 numWarnings = -1;
435
436 m_errorsBadge->SetMaximumNumber( numErrors );
438
439 m_warningsBadge->SetMaximumNumber( numWarnings );
441
442 m_exclusionsBadge->SetMaximumNumber( numExcluded );
444}
445
void SetLayerVisible(int aLayer, bool isVisible)
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:231
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:276
LSET GetVisibleLayers() const
A proxy function that calls the correspondent function in m_BoardSettings.
Definition: board.cpp:703
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:891
BOARD_ITEM * GetItem(const KIID &aID) const
Definition: board.cpp:1166
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:414
void DeleteMARKERs()
Delete all MARKERS from the board.
Definition: board.cpp:1121
Class DIALOG_FOOTPRINT_CHECKER_BASE.
void centerMarkerIdleHandler(wxIdleEvent &aEvent)
void OnCancelClick(wxCommandEvent &aEvent) override
void OnDeleteAllClick(wxCommandEvent &event) override
void OnClose(wxCloseEvent &event) override
FOOTPRINT_EDIT_FRAME * m_frame
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:331
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:126
Module editor specific tools.
void SetActiveLayer(PCB_LAYER_ID aLayer) override
void CheckShortingPads(const std::function< void(const PAD *, const PAD *, const VECTOR2I &)> &aErrorHandler)
Check for overlapping, different-numbered, non-net-tie pads.
Definition: footprint.cpp:2846
void CheckPads(const std::function< void(const PAD *, int, const wxString &)> &aErrorHandler)
Run non-board-specific DRC checks on footprint's pads.
Definition: footprint.cpp:2767
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.
Definition: footprint.cpp:2892
void BuildCourtyardCaches(OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr)
Build complex polygons of the courtyard areas from graphic items on the courtyard layers.
Definition: footprint.cpp:2566
bool IsNetTie() const
Definition: footprint.h:276
void CheckNetTiePadGroups(const std::function< void(const wxString &)> &aErrorHandler)
Sanity check net-tie pad groups.
Definition: footprint.cpp:3005
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: footprint.cpp:2742
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition: pcb_view.cpp:57
Definition: kiid.h:49
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:573
static LSET AllLayersMask()
Definition: lset.cpp:898
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:418
void SetMaximumNumber(int aMax)
Set the maximum number to be shown on the badge.
void UpdateNumber(int aNumber, SEVERITY aSeverity)
Update the number displayed on the badge.
Definition: pad.h:59
VECTOR2I GetPosition() const override
Definition: pad.h:198
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:68
APPEARANCE_CONTROLS * GetAppearancePanel()
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
BOARD * GetBoard() const
void FocusOnItem(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer=UNDEFINED_LAYER)
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
void SelectMarker(const MARKER_BASE *aMarker)
Definition: rc_item.cpp:738
static RC_TREE_NODE * ToNode(wxDataViewItem aItem)
Definition: rc_item.h:236
void Update(std::shared_ptr< RC_ITEMS_PROVIDER > aProvider, int aSeverities)
Definition: rc_item.cpp:364
void DeleteItems(bool aCurrentOnly, bool aIncludeExclusions, bool aDeep)
Deletes the current item or all items.
Definition: rc_item.cpp:572
void DeleteCurrentItem(bool aDeep)
Definition: rc_item.cpp:566
void CenterMarker(const MARKER_BASE *aMarker)
Definition: rc_item.cpp:751
static KIID ToUUID(wxDataViewItem aItem)
Definition: rc_item.cpp:220
std::shared_ptr< RC_ITEM > m_RcItem
Definition: rc_item.h:221
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:145
const std::function< void(const wxString &msg, BOARD_ITEM *itemA, BOARD_ITEM *itemB, const VECTOR2I &pt)> OUTLINE_ERROR_HANDLER
static int RPT_SEVERITY_ALL
static bool g_lastChecksRun
static FOOTPRINT * g_lastFootprint
@ DRCE_INVALID_OUTLINE
Definition: drc_item.h:68
@ DRCE_SHORTING_ITEMS
Definition: drc_item.h:40
@ DRCE_MALFORMED_COURTYARD
Definition: drc_item.h:63
@ DRCE_FOOTPRINT_TYPE_MISMATCH
Definition: drc_item.h:76
@ DRCE_FOOTPRINT
Definition: drc_item.h:80
#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:118
@ Edge_Cuts
Definition: layer_ids.h:114
@ B_CrtYd
Definition: layer_ids.h:117
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
std::vector< FAB_LAYER_COLOR > dummy
static int RPT_SEVERITY_ALL