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 ),
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 if( ( footprint->GetAttributes() & FP_ALLOW_MISSING_COURTYARD ) == 0
148 && footprint->GetCourtyard( F_CrtYd ).OutlineCount() == 0
149 && footprint->GetCourtyard( B_CrtYd ).OutlineCount() == 0 )
150 {
151 errorHandler( footprint, nullptr, nullptr, DRCE_MISSING_COURTYARD, wxEmptyString,
152 { 0, 0 } );
153 }
154
155 footprint->CheckFootprintAttributes(
156 [&]( const wxString& aMsg )
157 {
158 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT_TYPE_MISMATCH, aMsg,
159 { 0, 0 } );
160 } );
161
162 footprint->CheckPads( m_frame,
163 [&]( const PAD* aPad, int aErrorCode, const wxString& aMsg )
164 {
165 errorHandler( aPad, nullptr, nullptr, aErrorCode, aMsg, aPad->GetPosition() );
166 } );
167
168 footprint->CheckShortingPads(
169 [&]( const PAD* aPadA, const PAD* aPadB, int aErrorCode, const VECTOR2I& aPosition )
170 {
171 errorHandler( aPadA, aPadB, nullptr, aErrorCode, wxEmptyString, aPosition );
172 } );
173
174 if( footprint->IsNetTie() )
175 {
176 footprint->CheckNetTiePadGroups(
177 [&]( const wxString& aMsg )
178 {
179 errorHandler( footprint, nullptr, nullptr, DRCE_FOOTPRINT, aMsg, { 0, 0 } );
180 } );
181
182 footprint->CheckNetTies(
183 [&]( const BOARD_ITEM* aItemA, const BOARD_ITEM* aItemB, const BOARD_ITEM* aItemC,
184 const VECTOR2I& aPosition )
185 {
186 errorHandler( aItemA, aItemB, aItemC, DRCE_SHORTING_ITEMS, wxEmptyString,
187 aPosition );
188 } );
189 }
190
191 m_checksRun = true;
192
195
197}
198
199
201{
203
204 // wxWidgets on some platforms fails to correctly ensure that a selected item is
205 // visible, so we have to do it in a separate idle event.
206 m_centerMarkerOnIdle = aMarker;
207 Bind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
208}
209
210
212{
214 m_centerMarkerOnIdle = nullptr;
215 Unbind( wxEVT_IDLE, &DIALOG_FOOTPRINT_CHECKER::centerMarkerIdleHandler, this );
216}
217
218
219void DIALOG_FOOTPRINT_CHECKER::OnRunChecksClick( wxCommandEvent& aEvent )
220{
221 m_checksRun = false;
222
223 runChecks();
224}
225
226
227void DIALOG_FOOTPRINT_CHECKER::OnSelectItem( wxDataViewEvent& aEvent )
228{
229 BOARD* board = m_frame->GetBoard();
230 RC_TREE_NODE* node = RC_TREE_MODEL::ToNode( aEvent.GetItem() );
231 const KIID& itemID = node ? RC_TREE_MODEL::ToUUID( aEvent.GetItem() ) : niluuid;
232 BOARD_ITEM* item = board->GetItem( itemID );
233
235 {
236 // we already came from a cross-probe of the marker in the document; don't go
237 // around in circles
238
239 aEvent.Skip();
240 return;
241 }
242
243 if( node && item )
244 {
245 PCB_LAYER_ID principalLayer = UNDEFINED_LAYER;
246 LSET violationLayers;
247 std::shared_ptr<RC_ITEM> rc_item = node->m_RcItem;
248
249 if( item->GetLayerSet().count() > 0 )
250 principalLayer = item->GetLayerSet().Seq().front();
251
252 if( rc_item->GetErrorCode() == DRCE_MALFORMED_COURTYARD )
253 {
254 BOARD_ITEM* a = board->GetItem( rc_item->GetMainItemID() );
255
256 if( a && ( a->GetFlags() & MALFORMED_B_COURTYARD ) > 0
257 && ( a->GetFlags() & MALFORMED_F_COURTYARD ) == 0 )
258 {
259 principalLayer = B_CrtYd;
260 }
261 else
262 {
263 principalLayer = F_CrtYd;
264 }
265 }
266 else if (rc_item->GetErrorCode() == DRCE_INVALID_OUTLINE )
267 {
268 principalLayer = Edge_Cuts;
269 }
270 else
271 {
272 BOARD_ITEM* a = board->GetItem( rc_item->GetMainItemID() );
273 BOARD_ITEM* b = board->GetItem( rc_item->GetAuxItemID() );
274 BOARD_ITEM* c = board->GetItem( rc_item->GetAuxItem2ID() );
275 BOARD_ITEM* d = board->GetItem( rc_item->GetAuxItem3ID() );
276
277 if( a || b || c || d )
278 violationLayers = LSET::AllLayersMask();
279
280 if( a )
281 violationLayers &= a->GetLayerSet();
282
283 if( b )
284 violationLayers &= b->GetLayerSet();
285
286 if( c )
287 violationLayers &= c->GetLayerSet();
288
289 if( d )
290 violationLayers &= d->GetLayerSet();
291 }
292
293 if( violationLayers.count() )
294 principalLayer = violationLayers.Seq().front();
295 else
296 violationLayers.set( principalLayer );
297
298 WINDOW_THAWER thawer( m_frame );
299
300 m_frame->FocusOnItem( item );
302
303 if( ( violationLayers & board->GetVisibleLayers() ).none() )
304 {
305 m_frame->GetAppearancePanel()->SetLayerVisible( principalLayer, true );
307 }
308
309 if( board->GetVisibleLayers().test( principalLayer ) )
310 m_frame->SetActiveLayer( principalLayer );
311 }
312
313 aEvent.Skip();
314}
315
316
318{
319 if( m_markersDataView->GetCurrentItem().IsOk() )
320 {
321 // turn control over to m_frame, hide this DIALOG_FOOTPRINT_CHECKER window,
322 // no destruction so we can preserve listbox cursor
323 if( !IsModal() )
324 Show( false );
325 }
326
327 // Do not skip aVent here: this is not useful, and Pcbnew crashes
328 // if skipped (at least on Windows)
329}
330
331
332void DIALOG_FOOTPRINT_CHECKER::OnSeverity( wxCommandEvent& aEvent )
333{
334 int flag = 0;
335
336 if( aEvent.GetEventObject() == m_showAll )
338 else if( aEvent.GetEventObject() == m_showErrors )
340 else if( aEvent.GetEventObject() == m_showWarnings )
342 else if( aEvent.GetEventObject() == m_showExclusions )
344
345 if( aEvent.IsChecked() )
347 else if( aEvent.GetEventObject() == m_showAll )
349 else
350 m_severities &= ~flag;
351
353
356}
357
358
359void DIALOG_FOOTPRINT_CHECKER::OnCancelClick( wxCommandEvent& aEvent )
360{
361 m_frame->FocusOnItem( nullptr );
362
363 SetReturnCode( wxID_CANCEL );
364
365 // Leave the tool to destroy (or not) the dialog
367 tool->DestroyCheckerDialog();
368}
369
370
371void DIALOG_FOOTPRINT_CHECKER::OnClose( wxCloseEvent& aEvent )
372{
373 wxCommandEvent dummy;
375}
376
377
379{
380 WINDOW_THAWER thawer( m_frame );
381
383}
384
385
386void DIALOG_FOOTPRINT_CHECKER::OnDeleteOneClick( wxCommandEvent& aEvent )
387{
388 // Clear the selection. It may be the selected DRC marker.
390
392
393 // redraw the pcb
395
397}
398
399
401{
403
404 m_checksRun = false;
407}
408
409
411{
412 // Clear current selection list to avoid selection of deleted items
414
415 m_markersTreeModel->DeleteItems( false, true, false );
416 m_frame->GetBoard()->DeleteMARKERs( true, true );
417}
418
419
421{
422 // Collect counts:
423
424 int numErrors = 0;
425 int numWarnings = 0;
426 int numExcluded = 0;
427
429 {
430 numErrors += m_markersProvider->GetCount( RPT_SEVERITY_ERROR );
431 numWarnings += m_markersProvider->GetCount( RPT_SEVERITY_WARNING );
432 numExcluded += m_markersProvider->GetCount( RPT_SEVERITY_EXCLUSION );
433 }
434
435 // Update badges:
436
437 if( !m_checksRun && numErrors == 0 )
438 numErrors = -1;
439
440 if( !m_checksRun && numWarnings == 0 )
441 numWarnings = -1;
442
443 m_errorsBadge->SetMaximumNumber( numErrors );
445
446 m_warningsBadge->SetMaximumNumber( numWarnings );
448
449 m_exclusionsBadge->SetMaximumNumber( numExcluded );
451}
452
void SetLayerVisible(int aLayer, bool isVisible)
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:79
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:259
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:295
LSET GetVisibleLayers() const
A proxy function that calls the correspondent function in m_BoardSettings.
Definition: board.cpp:831
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition: board.cpp:1042
BOARD_ITEM * GetItem(const KIID &aID) const
Definition: board.cpp:1451
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:456
void DeleteMARKERs()
Delete all MARKERS from the board.
Definition: board.cpp:1400
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:395
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:130
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:3345
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:3258
int GetAttributes() const
Definition: footprint.h:288
void BuildCourtyardCaches(OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr)
Build complex polygons of the courtyard areas from graphic items on the courtyard layers.
Definition: footprint.cpp:2987
bool IsNetTie() const
Definition: footprint.h:295
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:3276
void CheckNetTiePadGroups(const std::function< void(const wxString &)> &aErrorHandler)
Sanity check net-tie pad groups.
Definition: footprint.cpp:3458
const SHAPE_POLY_SET & GetCourtyard(PCB_LAYER_ID aLayer) const
Used in DRC to test the courtyard area (a complex polygon).
Definition: footprint.cpp:2964
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:3233
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: lset.h:37
static LSET AllLayersMask()
Definition: lset.cpp:587
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:295
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:54
VECTOR2I GetPosition() const override
Definition: pad.h:206
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:238
void Update(std::shared_ptr< RC_ITEMS_PROVIDER > aProvider, int aSeverities)
Definition: rc_item.cpp:364
void DeleteItems(bool aCurrentOnly, bool aIncludeExclusions, bool aDeep)
Delete 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:223
int OutlineCount() const
Return the number of outlines in the set.
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:72
@ DRCE_MISSING_COURTYARD
Definition: drc_item.h:66
@ DRCE_SHORTING_ITEMS
Definition: drc_item.h:40
@ DRCE_MALFORMED_COURTYARD
Definition: drc_item.h:67
@ DRCE_FOOTPRINT_TYPE_MISMATCH
Definition: drc_item.h:81
@ DRCE_FOOTPRINT
Definition: drc_item.h:85
#define _(s)
#define MALFORMED_F_COURTYARD
#define MALFORMED_B_COURTYARD
@ FP_ALLOW_MISSING_COURTYARD
Definition: footprint.h:86
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
static int RPT_SEVERITY_ALL