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( m_frame,
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, int aErrorCode, const VECTOR2I& aPosition )
162 {
163 errorHandler( aPadA, aPadB, nullptr, aErrorCode, wxEmptyString, aPosition );
164 } );
165
166 if( footprint->IsNetTie() )
167 {
168 footprint->CheckNetTiePadGroups(
169 [&]( const wxString& aMsg )
170 {
171 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT, aMsg, { 0, 0 } );
172 } );
173
174 footprint->CheckNetTies(
175 [&]( const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB, const BOARD_ITEM* aItemC,
176 const VECTOR2I& aPosition )
177 {
178 errorHandler( aItemA, aItemB, aItemC, DRCE_SHORTING_ITEMS, wxEmptyString,
179 aPosition );
180 } );
181 }
182
183 m_checksRun = true;
184
187
189}
190
191
193{
195
196 // wxWidgets on some platforms fails to correctly ensure that a selected item is
197 // visible, so we have to do it in a separate idle event.
198 m_centerMarkerOnIdle = aMarker;
199 Bind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
200}
201
202
204{
206 m_centerMarkerOnIdle = nullptr;
207 Unbind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
208}
209
210
211void DIALOG_FOOTPRINT_CHECKER::OnRunChecksClick( wxCommandEvent& aEvent )
212{
213 m_checksRun = false;
214
215 runChecks();
216}
217
218
219void DIALOG_FOOTPRINT_CHECKER::OnSelectItem( wxDataViewEvent& aEvent )
220{
221 BOARD* board = m_frame->GetBoard();
222 RC_TREE_NODE* node = RC_TREE_MODEL::ToNode( aEvent.GetItem() );
223 const KIID& itemID = node ? RC_TREE_MODEL::ToUUID( aEvent.GetItem() ) : niluuid;
224 BOARD_ITEM* item = board->GetItem( itemID );
225
227 {
228 // we already came from a cross-probe of the marker in the document; don't go
229 // around in circles
230
231 aEvent.Skip();
232 return;
233 }
234
235 if( node && item )
236 {
237 PCB_LAYER_ID principalLayer = UNDEFINED_LAYER;
238 LSET violationLayers;
239 std::shared_ptr<RC_ITEM> rc_item = node->m_RcItem;
240
241 if( item->GetLayerSet().count() > 0 )
242 principalLayer = item->GetLayerSet().Seq().front();
243
244 if( rc_item->GetErrorCode() == DRCE_MALFORMED_COURTYARD )
245 {
246 BOARD_ITEM* a = board->GetItem( rc_item->GetMainItemID() );
247
248 if( a && ( a->GetFlags() & MALFORMED_B_COURTYARD ) > 0
249 && ( a->GetFlags() & MALFORMED_F_COURTYARD ) == 0 )
250 {
251 principalLayer = B_CrtYd;
252 }
253 else
254 {
255 principalLayer = F_CrtYd;
256 }
257 }
258 else if (rc_item->GetErrorCode() == DRCE_INVALID_OUTLINE )
259 {
260 principalLayer = Edge_Cuts;
261 }
262 else
263 {
264 BOARD_ITEM* a = board->GetItem( rc_item->GetMainItemID() );
265 BOARD_ITEM* b = board->GetItem( rc_item->GetAuxItemID() );
266 BOARD_ITEM* c = board->GetItem( rc_item->GetAuxItem2ID() );
267 BOARD_ITEM* d = board->GetItem( rc_item->GetAuxItem3ID() );
268
269 if( a || b || c || d )
270 violationLayers = LSET::AllLayersMask();
271
272 if( a )
273 violationLayers &= a->GetLayerSet();
274
275 if( b )
276 violationLayers &= b->GetLayerSet();
277
278 if( c )
279 violationLayers &= c->GetLayerSet();
280
281 if( d )
282 violationLayers &= d->GetLayerSet();
283 }
284
285 if( violationLayers.count() )
286 principalLayer = violationLayers.Seq().front();
287 else
288 violationLayers.set( principalLayer );
289
290 WINDOW_THAWER thawer( m_frame );
291
292 m_frame->FocusOnItem( item );
294
295 if( ( violationLayers & board->GetVisibleLayers() ) == 0 )
296 {
297 m_frame->GetAppearancePanel()->SetLayerVisible( principalLayer, true );
299 }
300
301 if( board->GetVisibleLayers().test( principalLayer ) )
302 m_frame->SetActiveLayer( principalLayer );
303 }
304
305 aEvent.Skip();
306}
307
308
310{
311 if( m_markersDataView->GetCurrentItem().IsOk() )
312 {
313 // turn control over to m_frame, hide this DIALOG_FOOTPRINT_CHECKER window,
314 // no destruction so we can preserve listbox cursor
315 if( !IsModal() )
316 Show( false );
317 }
318
319 // Do not skip aVent here: this is not useful, and Pcbnew crashes
320 // if skipped (at least on Windows)
321}
322
323
324void DIALOG_FOOTPRINT_CHECKER::OnSeverity( wxCommandEvent& aEvent )
325{
326 int flag = 0;
327
328 if( aEvent.GetEventObject() == m_showAll )
330 else if( aEvent.GetEventObject() == m_showErrors )
332 else if( aEvent.GetEventObject() == m_showWarnings )
334 else if( aEvent.GetEventObject() == m_showExclusions )
336
337 if( aEvent.IsChecked() )
339 else if( aEvent.GetEventObject() == m_showAll )
341 else
342 m_severities &= ~flag;
343
345
348}
349
350
351void DIALOG_FOOTPRINT_CHECKER::OnCancelClick( wxCommandEvent& aEvent )
352{
353 m_frame->FocusOnItem( nullptr );
354
355 SetReturnCode( wxID_CANCEL );
356
357 // Leave the tool to destroy (or not) the dialog
359 tool->DestroyCheckerDialog();
360}
361
362
363void DIALOG_FOOTPRINT_CHECKER::OnClose( wxCloseEvent& aEvent )
364{
365 wxCommandEvent dummy;
367}
368
369
371{
372 WINDOW_THAWER thawer( m_frame );
373
375}
376
377
378void DIALOG_FOOTPRINT_CHECKER::OnDeleteOneClick( wxCommandEvent& aEvent )
379{
380 // Clear the selection. It may be the selected DRC marker.
382
384
385 // redraw the pcb
387
389}
390
391
393{
395
396 m_checksRun = false;
399}
400
401
403{
404 // Clear current selection list to avoid selection of deleted items
406
407 m_markersTreeModel->DeleteItems( false, true, false );
408 m_frame->GetBoard()->DeleteMARKERs( true, true );
409}
410
411
413{
414 // Collect counts:
415
416 int numErrors = 0;
417 int numWarnings = 0;
418 int numExcluded = 0;
419
421 {
422 numErrors += m_markersProvider->GetCount( RPT_SEVERITY_ERROR );
423 numWarnings += m_markersProvider->GetCount( RPT_SEVERITY_WARNING );
424 numExcluded += m_markersProvider->GetCount( RPT_SEVERITY_EXCLUSION );
425 }
426
427 // Update badges:
428
429 if( !m_checksRun && numErrors == 0 )
430 numErrors = -1;
431
432 if( !m_checksRun && numWarnings == 0 )
433 numWarnings = -1;
434
435 m_errorsBadge->SetMaximumNumber( numErrors );
437
438 m_warningsBadge->SetMaximumNumber( numWarnings );
440
441 m_exclusionsBadge->SetMaximumNumber( numExcluded );
443}
444
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:243
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
LSET GetVisibleLayers() const
A proxy function that calls the correspondent function in m_BoardSettings.
Definition: board.cpp:694
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:885
BOARD_ITEM * GetItem(const KIID &aID) const
Definition: board.cpp:1293
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:433
void DeleteMARKERs()
Delete all MARKERS from the board.
Definition: board.cpp:1242
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:338
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:129
Module editor specific tools.
void SetActiveLayer(PCB_LAYER_ID aLayer) override
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:3220
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.
Definition: footprint.cpp:3050
void BuildCourtyardCaches(OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr)
Build complex polygons of the courtyard areas from graphic items on the courtyard layers.
Definition: footprint.cpp:2848
bool IsNetTie() const
Definition: footprint.h:284
void CheckShortingPads(const std::function< void(const PAD *, const PAD *, int aErrorCode, const VECTOR2I &)> &aErrorHandler)
Check for overlapping, different-numbered, non-net-tie pads.
Definition: footprint.cpp:3155
void CheckNetTiePadGroups(const std::function< void(const wxString &)> &aErrorHandler)
Sanity check net-tie pad groups.
Definition: footprint.cpp:3333
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:3025
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:576
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:53
VECTOR2I GetPosition() const override
Definition: pad.h:200
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:150
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:69
@ DRCE_SHORTING_ITEMS
Definition: drc_item.h:40
@ DRCE_MALFORMED_COURTYARD
Definition: drc_item.h:64
@ DRCE_FOOTPRINT_TYPE_MISMATCH
Definition: drc_item.h:77
@ DRCE_FOOTPRINT
Definition: drc_item.h:81
#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:117
@ Edge_Cuts
Definition: layer_ids.h:113
@ B_CrtYd
Definition: layer_ids.h:116
@ 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