1 /*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 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
24 #include <dialog_footprint_checker.h>
25 #include <widgets/appearance_controls.h>
26 #include <tool/tool_manager.h>
27 #include <tools/pcb_actions.h>
28 #include <footprint.h>
29 #include <pcb_marker.h>
30 #include <drc/drc_results_provider.h>
31 #include <footprint_edit_frame.h>
32 #include <convert_shape_list_to_polygon.h>
33 #include <tools/footprint_editor_control.h>
34
35
DIALOG_FOOTPRINT_CHECKER(FOOTPRINT_EDIT_FRAME * aParent)36 DIALOG_FOOTPRINT_CHECKER::DIALOG_FOOTPRINT_CHECKER( FOOTPRINT_EDIT_FRAME* aParent ) :
37 DIALOG_FOOTPRINT_CHECKER_BASE( aParent ),
38 m_frame( aParent ),
39 m_checksRun( false ),
40 m_markersProvider( nullptr ),
41 m_severities( RPT_SEVERITY_ERROR | RPT_SEVERITY_WARNING )
42 {
43 m_markersTreeModel = new RC_TREE_MODEL( m_frame, m_markersDataView );
44 m_markersDataView->AssociateModel( m_markersTreeModel );
45
46 m_markersTreeModel->SetSeverities( -1 );
47
48 // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
49 // that requires us to correct the button labels here.
50 m_sdbSizerOK->SetLabel( _( "Run Checks" ) );
51 m_sdbSizerCancel->SetLabel( _( "Close" ) );
52
53 m_sdbSizerOK->SetDefault();
54 m_sdbSizer->Layout();
55
56 syncCheckboxes();
57
58 finishDialogSettings();
59 }
60
61
~DIALOG_FOOTPRINT_CHECKER()62 DIALOG_FOOTPRINT_CHECKER::~DIALOG_FOOTPRINT_CHECKER()
63 {
64 m_markersTreeModel->DecRef();
65 }
66
67
TransferDataToWindow()68 bool DIALOG_FOOTPRINT_CHECKER::TransferDataToWindow()
69 {
70 return true;
71 }
72
73
TransferDataFromWindow()74 bool DIALOG_FOOTPRINT_CHECKER::TransferDataFromWindow()
75 {
76 return true;
77 }
78
79
80 // Don't globally define this; different facilities use different definitions of "ALL"
81 static int RPT_SEVERITY_ALL = RPT_SEVERITY_WARNING | RPT_SEVERITY_ERROR | RPT_SEVERITY_EXCLUSION;
82
83
syncCheckboxes()84 void DIALOG_FOOTPRINT_CHECKER::syncCheckboxes()
85 {
86 m_showAll->SetValue( m_severities == RPT_SEVERITY_ALL );
87 m_showErrors->SetValue( m_severities & RPT_SEVERITY_ERROR );
88 m_showWarnings->SetValue( m_severities & RPT_SEVERITY_WARNING );
89 m_showExclusions->SetValue( m_severities & RPT_SEVERITY_EXCLUSION );
90 }
91
92
runChecks()93 void DIALOG_FOOTPRINT_CHECKER::runChecks()
94 {
95 BOARD* board = m_frame->GetBoard();
96 FOOTPRINT* footprint = board->GetFirstFootprint();
97 wxString msg;
98
99 SetMarkersProvider( new BOARD_DRC_ITEMS_PROVIDER( board ) );
100
101 deleteAllMarkers();
102
103 if( !footprint )
104 {
105 msg = _( "No footprint loaded." );
106 return;
107 }
108
109 OUTLINE_ERROR_HANDLER errorHandler =
110 [&]( const wxString& aMsg, BOARD_ITEM* aItemA, BOARD_ITEM* aItemB, const wxPoint& aPt )
111 {
112 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_MALFORMED_COURTYARD );
113
114 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + aMsg );
115 drcItem->SetItems( aItemA, aItemB );
116
117 PCB_MARKER* marker = new PCB_MARKER( drcItem, aPt );
118 board->Add( marker );
119 m_frame->GetCanvas()->GetView()->Add( marker );
120 };
121
122 footprint->BuildPolyCourtyards( &errorHandler );
123
124
125 const std::function<void( const wxString& msg )> typeWarning =
126 [&]( const wxString& aMsg )
127 {
128 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_FOOTPRINT_TYPE_MISMATCH );
129
130 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + aMsg );
131 drcItem->SetItems( footprint );
132
133 PCB_MARKER* marker = new PCB_MARKER( drcItem, wxPoint( 0, 0 ) );
134 board->Add( marker );
135 m_frame->GetCanvas()->GetView()->Add( marker );
136 };
137
138 const std::function<void( const wxString& msg, const wxPoint& position )> tstHoleInTHPad =
139 [&]( const wxString& aMsg, const wxPoint& aPosition )
140 {
141 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_PAD_TH_WITH_NO_HOLE );
142
143 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + aMsg );
144 drcItem->SetItems( footprint );
145
146 PCB_MARKER* marker = new PCB_MARKER( drcItem, aPosition );
147 board->Add( marker );
148 m_frame->GetCanvas()->GetView()->Add( marker );
149 };
150
151 footprint->CheckFootprintAttributes( &typeWarning );
152 footprint->CheckFootprintTHPadNoHoles( &tstHoleInTHPad );
153 m_checksRun = true;
154
155 SetMarkersProvider( new BOARD_DRC_ITEMS_PROVIDER( board ) );
156
157 refreshEditor();
158 }
159
160
SetMarkersProvider(RC_ITEMS_PROVIDER * aProvider)161 void DIALOG_FOOTPRINT_CHECKER::SetMarkersProvider( RC_ITEMS_PROVIDER* aProvider )
162 {
163 m_markersTreeModel->SetProvider( aProvider );
164 updateDisplayedCounts();
165 }
166
167
OnRunChecksClick(wxCommandEvent & aEvent)168 void DIALOG_FOOTPRINT_CHECKER::OnRunChecksClick( wxCommandEvent& aEvent )
169 {
170 m_checksRun = false;
171
172 runChecks();
173 }
174
175
OnSelectItem(wxDataViewEvent & aEvent)176 void DIALOG_FOOTPRINT_CHECKER::OnSelectItem( wxDataViewEvent& aEvent )
177 {
178 BOARD* board = m_frame->GetBoard();
179 RC_TREE_NODE* node = RC_TREE_MODEL::ToNode( aEvent.GetItem() );
180 const KIID& itemID = node ? RC_TREE_MODEL::ToUUID( aEvent.GetItem() ) : niluuid;
181 BOARD_ITEM* item = board->GetItem( itemID );
182
183 if( node && item )
184 {
185 PCB_LAYER_ID principalLayer = item->GetLayer();
186 LSET violationLayers;
187 std::shared_ptr<RC_ITEM> rc_item = node->m_RcItem;
188
189 if( rc_item->GetErrorCode() == DRCE_MALFORMED_COURTYARD )
190 {
191 BOARD_ITEM* a = board->GetItem( rc_item->GetMainItemID() );
192
193 if( a && ( a->GetFlags() & MALFORMED_B_COURTYARD ) > 0
194 && ( a->GetFlags() & MALFORMED_F_COURTYARD ) == 0 )
195 {
196 principalLayer = B_CrtYd;
197 }
198 else
199 {
200 principalLayer = F_CrtYd;
201 }
202 }
203 else if (rc_item->GetErrorCode() == DRCE_INVALID_OUTLINE )
204 {
205 principalLayer = Edge_Cuts;
206 }
207 else
208 {
209 BOARD_ITEM* a = board->GetItem( rc_item->GetMainItemID() );
210 BOARD_ITEM* b = board->GetItem( rc_item->GetAuxItemID() );
211 BOARD_ITEM* c = board->GetItem( rc_item->GetAuxItem2ID() );
212 BOARD_ITEM* d = board->GetItem( rc_item->GetAuxItem3ID() );
213
214 if( a || b || c || d )
215 violationLayers = LSET::AllLayersMask();
216
217 if( a )
218 violationLayers &= a->GetLayerSet();
219
220 if( b )
221 violationLayers &= b->GetLayerSet();
222
223 if( c )
224 violationLayers &= c->GetLayerSet();
225
226 if( d )
227 violationLayers &= d->GetLayerSet();
228 }
229
230 if( violationLayers.count() )
231 principalLayer = violationLayers.Seq().front();
232 else
233 violationLayers.set( principalLayer );
234
235 WINDOW_THAWER thawer( m_frame );
236
237 m_frame->FocusOnItem( item );
238 m_frame->GetCanvas()->Refresh();
239
240 if( ( violationLayers & board->GetVisibleLayers() ) == 0 )
241 {
242 m_frame->GetAppearancePanel()->SetLayerVisible( principalLayer, true );
243 m_frame->GetCanvas()->Refresh();
244 }
245
246 if( board->GetVisibleLayers().test( principalLayer ) )
247 m_frame->SetActiveLayer( principalLayer );
248 }
249
250 aEvent.Skip();
251 }
252
253
OnLeftDClickItem(wxMouseEvent & event)254 void DIALOG_FOOTPRINT_CHECKER::OnLeftDClickItem( wxMouseEvent& event )
255 {
256 if( m_markersDataView->GetCurrentItem().IsOk() )
257 {
258 // turn control over to m_frame, hide this DIALOG_FOOTPRINT_CHECKER window,
259 // no destruction so we can preserve listbox cursor
260 if( !IsModal() )
261 Show( false );
262 }
263
264 // Do not skip aVent here: this is not useful, and Pcbnew crashes
265 // if skipped (at least on Windows)
266 }
267
268
OnSeverity(wxCommandEvent & aEvent)269 void DIALOG_FOOTPRINT_CHECKER::OnSeverity( wxCommandEvent& aEvent )
270 {
271 int flag = 0;
272
273 if( aEvent.GetEventObject() == m_showAll )
274 flag = RPT_SEVERITY_ALL;
275 else if( aEvent.GetEventObject() == m_showErrors )
276 flag = RPT_SEVERITY_ERROR;
277 else if( aEvent.GetEventObject() == m_showWarnings )
278 flag = RPT_SEVERITY_WARNING;
279 else if( aEvent.GetEventObject() == m_showExclusions )
280 flag = RPT_SEVERITY_EXCLUSION;
281
282 if( aEvent.IsChecked() )
283 m_severities |= flag;
284 else if( aEvent.GetEventObject() == m_showAll )
285 m_severities = RPT_SEVERITY_ERROR;
286 else
287 m_severities &= ~flag;
288
289 syncCheckboxes();
290
291 // Set the provider's severity levels through the TreeModel so that the old tree
292 // can be torn down before the severity changes.
293 //
294 // It's not clear this is required, but we've had a lot of issues with wxDataView
295 // being cranky on various platforms.
296
297 m_markersTreeModel->SetSeverities( m_severities );
298
299 updateDisplayedCounts();
300 }
301
302
OnCancelClick(wxCommandEvent & aEvent)303 void DIALOG_FOOTPRINT_CHECKER::OnCancelClick( wxCommandEvent& aEvent )
304 {
305 m_frame->FocusOnItem( nullptr );
306
307 SetReturnCode( wxID_CANCEL );
308
309 // Leave the tool to destroy (or not) the dialog
310 FOOTPRINT_EDITOR_CONTROL* tool = m_frame->GetToolManager()->GetTool<FOOTPRINT_EDITOR_CONTROL>();
311 tool->DestroyCheckerDialog();
312 }
313
314
OnClose(wxCloseEvent & aEvent)315 void DIALOG_FOOTPRINT_CHECKER::OnClose( wxCloseEvent& aEvent )
316 {
317 wxCommandEvent dummy;
318 OnCancelClick( dummy );
319 }
320
321
refreshEditor()322 void DIALOG_FOOTPRINT_CHECKER::refreshEditor()
323 {
324 WINDOW_THAWER thawer( m_frame );
325
326 m_frame->GetCanvas()->Refresh();
327 }
328
329
OnDeleteOneClick(wxCommandEvent & aEvent)330 void DIALOG_FOOTPRINT_CHECKER::OnDeleteOneClick( wxCommandEvent& aEvent )
331 {
332 // Clear the selection. It may be the selected DRC marker.
333 m_frame->GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
334
335 m_markersTreeModel->DeleteCurrentItem( true );
336
337 // redraw the pcb
338 refreshEditor();
339
340 updateDisplayedCounts();
341 }
342
343
OnDeleteAllClick(wxCommandEvent & event)344 void DIALOG_FOOTPRINT_CHECKER::OnDeleteAllClick( wxCommandEvent& event )
345 {
346 deleteAllMarkers();
347
348 m_checksRun = false;
349 refreshEditor();
350 updateDisplayedCounts();
351 }
352
353
deleteAllMarkers()354 void DIALOG_FOOTPRINT_CHECKER::deleteAllMarkers()
355 {
356 // Clear current selection list to avoid selection of deleted items
357 m_frame->GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
358
359 m_markersTreeModel->DeleteItems( false, true, true );
360 }
361
362
updateDisplayedCounts()363 void DIALOG_FOOTPRINT_CHECKER::updateDisplayedCounts()
364 {
365 // Collect counts:
366
367 int numErrors = 0;
368 int numWarnings = 0;
369 int numExcluded = 0;
370
371 if( m_markersProvider )
372 {
373 numErrors += m_markersProvider->GetCount( RPT_SEVERITY_ERROR );
374 numWarnings += m_markersProvider->GetCount( RPT_SEVERITY_WARNING );
375 numExcluded += m_markersProvider->GetCount( RPT_SEVERITY_EXCLUSION );
376 }
377
378 // Update badges:
379
380 if( !m_checksRun && numErrors == 0 )
381 numErrors = -1;
382
383 if( !m_checksRun && numWarnings == 0 )
384 numWarnings = -1;
385
386 m_errorsBadge->SetMaximumNumber( numErrors );
387 m_errorsBadge->UpdateNumber( numErrors, RPT_SEVERITY_ERROR );
388
389 m_warningsBadge->SetMaximumNumber( numWarnings );
390 m_warningsBadge->UpdateNumber( numWarnings, RPT_SEVERITY_WARNING );
391
392 m_exclusionsBadge->SetMaximumNumber( numExcluded );
393 m_exclusionsBadge->UpdateNumber( numExcluded, RPT_SEVERITY_EXCLUSION );
394 }
395
396