KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_exchange_footprints.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 2013 Wayne Stambaugh <[email protected]>
7 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <bitmaps.h>
24#include <board.h>
25#include <footprint.h>
26#include <pad.h>
28#include <string_utils.h>
29#include <kiway.h>
30#include <kiway_mail.h>
31#include <macros.h>
32#include <pcb_edit_frame.h>
35#include <tool/tool_manager.h>
37
38#include <ranges>
39
40
41#define ID_MATCH_FP_ALL 4200
42#define ID_MATCH_FP_SELECTED 4201
43#define ID_MATCH_FP_REF 4202
44#define ID_MATCH_FP_VAL 4203
45#define ID_MATCH_FP_ID 4204
46
51
52
54 bool updateMode, bool selectedMode ) :
56 m_commit( aParent ),
57 m_parent( aParent ),
58 m_currentFootprint( aFootprint ),
59 m_updateMode( updateMode )
60{
61 if( !updateMode )
62 {
63 SetTitle( _( "Change Footprints" ) );
64 m_matchAll->SetLabel( _( "Change all footprints on board" ) );
65 m_matchSelected->SetLabel( _( "Change selected footprint(s)" ) );
66 m_matchSpecifiedRef->SetLabel( _( "Change footprints matching reference designator:" ) );
67 m_matchSpecifiedValue->SetLabel( _( "Change footprints matching value:" ) );
68 m_matchSpecifiedID->SetLabel( _( "Change footprints with library id:" ) );
69 m_resetTextItemLayers->SetLabel( _( "Update text layers and visibilities" ) );
70 m_resetTextItemEffects->SetLabel( _( "Update text sizes and styles" ) );
71 m_resetTextItemPositions->SetLabel( _( "Update text positions" ) );
72 m_resetTextItemContent->SetLabel( _( "Update text content" ) );
73 m_resetFabricationAttrs->SetLabel( _( "Update fabrication attributes" ) );
74 m_resetClearanceOverrides->SetLabel( _( "Update clearance overrides" ) );
75 m_reset3DModels->SetLabel( _( "Update 3D models" ) );
76 }
77
78#if 0 // translator hint
79 wxString x = _( "Update/reset strings: there are two cases these descriptions need to cover: "
80 "the user made overrides to a footprint on the PCB and wants to remove them, "
81 "or the user made changes to the library footprint and wants to propagate "
82 "them back to the PCB." );
83#endif
84
85 if( m_updateMode )
86 m_changeSizer->Show( false );
87 else
88 m_upperSizer->FindItem( m_matchAll )->Show( false );
89
91 m_upperSizer->FindItem( m_matchSelected )->Show( false );
92
95
96 m_upperSizer->SetEmptyCellSize( wxSize( 0, 0 ) );
97 // The upper sizer has its content modified: re-layout it:
98 m_upperSizer->Layout();
99
100 // initialize controls based on update mode in case there is no saved state yet
101 m_removeExtraBox->SetValue( false );
102 m_resetTextItemLayers->SetValue( m_updateMode ? false : true );
103 m_resetTextItemEffects->SetValue( m_updateMode ? false : true );
104 m_resetTextItemPositions->SetValue( m_updateMode ? false : true );
105 m_resetTextItemContent->SetValue( m_updateMode ? false : true );
106 m_resetFabricationAttrs->SetValue( m_updateMode ? false : true );
107 m_resetClearanceOverrides->SetValue( true );
108 m_reset3DModels->SetValue( true );
109 m_resetTransform->SetValue( m_updateMode ? false : true );
110 m_matchPadPositions->SetValue( true );
111
112 if( m_updateMode )
113 m_resetTransform->Show( false );
114
115 // initialize match-mode
116 if( m_updateMode )
118 else
120
121 m_MessageWindow->SetLazyUpdate( true );
122 m_MessageWindow->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
123
124 // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
125 // because the update and change versions of this dialog have different controls.
126 m_hash_key = TO_UTF8( GetTitle() );
127
128 wxString okLabel = m_updateMode ? _( "Update" ) : _( "Change" );
129
130 SetupStandardButtons( { { wxID_OK, okLabel },
131 { wxID_CANCEL, _( "Close" ) } } );
132
133 // Now all widgets have the size fixed, call FinishDialogSettings
135}
136
137
139{
141 {
142 if( m_updateMode )
143 m_newID->ChangeValue( From_UTF8( m_currentFootprint->GetFPID().Format().c_str() ) );
144
145 // Use ChangeValue() instead of SetValue() so we don't generate events.
146 m_specifiedRef->ChangeValue( m_currentFootprint->GetReference() );
147 m_specifiedValue->ChangeValue( m_currentFootprint->GetValue() );
148 m_specifiedID->ChangeValue( From_UTF8( m_currentFootprint->GetFPID().Format().c_str() ) );
149 }
150
151 wxCommandEvent event;
152 event.SetEventObject( this );
153
154 switch( *m_matchMode )
155 {
156 case ID_MATCH_FP_ALL: OnMatchAllClicked( event ); break;
157 case ID_MATCH_FP_SELECTED: OnMatchSelectedClicked( event ); break;
158 case ID_MATCH_FP_REF: OnMatchRefClicked( event ); break;
159 case ID_MATCH_FP_VAL: OnMatchValueClicked( event ); break;
160 case ID_MATCH_FP_ID: OnMatchIDClicked( event ); break;
161 default: break;
162 }
163
164 return true;
165}
166
167
169{
170 LIB_ID specifiedID;
171
172 switch( *m_matchMode )
173 {
174 case ID_MATCH_FP_ALL:
175 return true;
177 return aFootprint == m_currentFootprint || aFootprint->IsSelected();
178 case ID_MATCH_FP_REF:
179 return WildCompareString( m_specifiedRef->GetValue(), aFootprint->GetReference(), false );
180 case ID_MATCH_FP_VAL:
181 return WildCompareString( m_specifiedValue->GetValue(), aFootprint->GetValue(), false );
182 case ID_MATCH_FP_ID:
183 specifiedID.Parse( m_specifiedID->GetValue() );
184 return aFootprint->GetFPID() == specifiedID;
185 default:
186 return false; // just to quiet compiler warnings....
187 }
188}
189
190
192{
193 switch( *m_matchMode )
194 {
195 case ID_MATCH_FP_ALL: return m_matchAll;
200 default: return nullptr;
201 }
202}
203
204
206{
207 wxRadioButton* rb_button = getRadioButtonForMode();
208
209 wxRadioButton* rb_butt_list[] =
210 {
216 nullptr // end of list
217 };
218
219 // Ensure the button state is ok. Only one button can be checked
220 // Change button state only if its state is incorrect, otherwise
221 // we have issues depending on the platform.
222 for( int ii = 0; rb_butt_list[ii]; ++ii )
223 {
224 bool state = rb_butt_list[ii] == rb_button;
225
226 if( rb_butt_list[ii]->GetValue() != state )
227 rb_butt_list[ii]->SetValue( state );
228 }
229}
230
231
233{
235
236 if( aEvent.GetEventObject() == this )
238 else
239 m_matchAll->SetFocus();
240}
241
242
244{
246
247 if( aEvent.GetEventObject() == this )
249 else
250 m_matchSelected->SetFocus();
251}
252
253
255{
257
258 if( aEvent.GetEventObject() == this )
260 else if( aEvent.GetEventObject() != m_specifiedRef )
261 m_specifiedRef->SetFocus();
262}
263
264
266{
268
269 if( aEvent.GetEventObject() == this )
271 else if( aEvent.GetEventObject() != m_specifiedValue )
272 m_specifiedValue->SetFocus();
273}
274
275
277{
279
280 if( aEvent.GetEventObject() == this )
282 else if( aEvent.GetEventObject() != m_specifiedID )
283 m_specifiedID->SetFocus();
284}
285
286
288{
289 m_removeExtraBox->SetValue( aCheck );
290 m_resetTextItemLayers->SetValue( aCheck );
291 m_resetTextItemEffects->SetValue( aCheck );
292 m_resetTextItemPositions->SetValue( aCheck );
293 m_resetTextItemContent->SetValue( aCheck );
294 m_resetFabricationAttrs->SetValue( aCheck );
295 m_resetClearanceOverrides->SetValue( aCheck );
296 m_reset3DModels->SetValue( aCheck );
297 m_resetTransform->SetValue( aCheck );
298 m_matchPadPositions->SetValue( aCheck );
299}
300
301
302void DIALOG_EXCHANGE_FOOTPRINTS::OnOKClicked( wxCommandEvent& aEvent )
303{
304 PCB_SELECTION_TOOL* selTool = m_parent->GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
305 wxBusyCursor dummy;
306
307 m_MessageWindow->Clear();
308 m_MessageWindow->Flush( false );
309
310 m_newFootprints.clear();
312 m_commit.Push( m_updateMode ? _( "Update Footprint" ) : _( "Change Footprint" ) );
313 selTool->AddItemsToSel( &m_newFootprints );
314
315 m_MessageWindow->Flush( false );
316
317 WINDOW_THAWER thawer( m_parent );
318 m_parent->GetCanvas()->Refresh();
319}
320
321
323{
324 LIB_ID newFPID;
325
326 if( m_parent->GetBoard()->Footprints().empty() )
327 return;
328
329 if( !m_updateMode )
330 {
331 newFPID.Parse( m_newID->GetValue() );
332
333 if( !newFPID.IsValid() )
334 return;
335 }
336
337 /*
338 * NB: the change is done from the last footprint because processFootprint() modifies the
339 * last item in the list.
340 */
341 for( FOOTPRINT* footprint : std::ranges::reverse_view( m_parent->GetBoard()->Footprints() ) )
342 {
343 if( !isMatch( footprint ) )
344 continue;
345
346 if( m_updateMode )
347 processFootprint( footprint, footprint->GetFPID() );
348 else
349 processFootprint( footprint, newFPID );
350 }
351}
352
353
355{
356 LIB_ID oldFPID = aFootprint->GetFPID();
357 wxString msg;
358
359 // Load new footprint.
360 if( m_updateMode )
361 {
362 msg.Printf( _( "Updated footprint %s (%s)" ) + wxS( ": " ),
363 aFootprint->GetReference(),
364 oldFPID.Format().c_str() );
365 }
366 else
367 {
368 msg.Printf( _( "Changed footprint %s from '%s' to '%s'" ) + wxS( ": " ),
369 aFootprint->GetReference(),
370 oldFPID.Format().c_str(),
371 aNewFPID.Format().c_str() );
372 }
373
374 FOOTPRINT* newFootprint = m_parent->LoadFootprint( aNewFPID );
375
376 if( !newFootprint )
377 {
378 msg += _( "*** library footprint not found ***" );
379 m_MessageWindow->Report( msg, RPT_SEVERITY_ERROR );
380 return;
381 }
382
383 bool updated = !m_updateMode || aFootprint->FootprintNeedsUpdate( newFootprint );
384
385 m_parent->GetBoard()->ExchangeFootprint( aFootprint, newFootprint, m_commit,
386 m_matchPadPositions->GetValue(),
387 m_removeExtraBox->GetValue(),
388 m_resetTextItemLayers->GetValue(),
389 m_resetTextItemEffects->GetValue(),
390 m_resetTextItemPositions->GetValue(),
391 m_resetTextItemContent->GetValue(),
392 m_resetFabricationAttrs->GetValue(),
393 m_resetClearanceOverrides->GetValue(),
394 m_reset3DModels->GetValue(),
395 m_resetTransform->GetValue(),
396 &updated );
397
398 if( aFootprint == m_currentFootprint )
399 m_currentFootprint = newFootprint;
400
401 if( newFootprint )
402 m_newFootprints.push_back( newFootprint );
403
404 if( m_updateMode && !updated )
405 {
406 msg += _( ": (no changes)" );
407 m_MessageWindow->Report( msg, RPT_SEVERITY_INFO );
408 }
409 else
410 {
411 msg += _( ": OK" );
412 m_MessageWindow->Report( msg, RPT_SEVERITY_ACTION );
413 }
414}
415
416
418{
419 wxString newname = m_newID->GetValue();
420
421 if( KIWAY_PLAYER* frame = Kiway().Player( FRAME_FOOTPRINT_CHOOSER, true, this ) )
422 {
424 {
425 /*
426 * Symbol netlist format:
427 * pinNumber pinName <tab> pinNumber pinName...
428 * fpFilter fpFilter...
429 */
430 wxString netlist;
431
432 wxArrayString pins;
433
434 for( const wxString& pad : m_currentFootprint->GetUniquePadNumbers() )
435 pins.push_back( pad + ' ' + wxEmptyString /* leave pinName empty */ );
436
437 if( !pins.IsEmpty() )
438 netlist << EscapeString( wxJoin( pins, '\t' ), CTX_LINE );
439
440 netlist << wxS( "\r" );
441
442 netlist << EscapeString( m_currentFootprint->GetFilters(), CTX_LINE ) << wxS( "\r" );
443
444 std::string payload( netlist.ToStdString() );
446 frame->KiwayMailIn( mail );
447 }
448
449 if( frame->ShowModal( &newname, this ) )
450 {
451 if( event.GetEventObject() == m_newIDBrowseButton )
452 m_newID->SetValue( newname );
453 else
454 m_specifiedID->SetValue( newname );
455 }
456
457 frame->Destroy();
458 }
459}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
DIALOG_EXCHANGE_FOOTPRINTS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Update Footprints from Library"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void OnMatchIDClicked(wxCommandEvent &aEvent) override
void updateMatchModeRadioButtons(wxUpdateUIEvent &aEvent) override
void processFootprint(FOOTPRINT *aFootprint, const LIB_ID &aNewFPID)
void OnMatchValueClicked(wxCommandEvent &aEvent) override
void OnOKClicked(wxCommandEvent &aEvent) override
void OnMatchSelectedClicked(wxCommandEvent &aEvent) override
void OnMatchRefClicked(wxCommandEvent &aEvent) override
void ViewAndSelectFootprint(wxCommandEvent &aEvent) override
DIALOG_EXCHANGE_FOOTPRINTS(PCB_EDIT_FRAME *aParent, FOOTPRINT *aFootprint, bool updateMode, bool selectedMode)
void OnMatchAllClicked(wxCommandEvent &aEvent) override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:79
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
bool IsSelected() const
Definition eda_item.h:132
bool FootprintNeedsUpdate(const FOOTPRINT *aLibFP, int aCompareFlags=0, REPORTER *aReporter=nullptr)
Return true if a board footprint differs from the library version.
const LIB_ID & GetFPID() const
Definition footprint.h:441
const wxString & GetValue() const
Definition footprint.h:863
const wxString & GetReference() const
Definition footprint.h:841
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition kiway_mail.h:34
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:48
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
UTF8 Format() const
Definition lib_id.cpp:115
The main frame for Pcbnew.
The selection tool: currently supports:
int AddItemsToSel(const TOOL_EVENT &aEvent)
const char * c_str() const
Definition utf8.h:104
#define ID_MATCH_FP_ALL
#define ID_MATCH_FP_VAL
#define ID_MATCH_FP_SELECTED
#define ID_MATCH_FP_REF
int g_matchModeForExchange
int g_matchModeForUpdateSelected
#define ID_MATCH_FP_ID
int g_matchModeForExchangeSelected
#define _(s)
@ FRAME_FOOTPRINT_CHOOSER
Definition frame_type.h:40
This file contains miscellaneous commonly used macros and functions.
@ MAIL_SYMBOL_NETLIST
Definition mail_type.h:42
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
std::vector< FAB_LAYER_COLOR > dummy
bool WildCompareString(const wxString &pattern, const wxString &string_to_tst, bool case_sensitive)
Compare a string against wild card (* and ?) pattern using the usual rules.
wxString From_UTF8(const char *cstring)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
@ CTX_LINE
std::string netlist