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, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <bitmaps.h>
28#include <board.h>
29#include <footprint.h>
30#include <pad.h>
32#include <string_utils.h>
33#include <kiway.h>
34#include <kiway_express.h>
35#include <macros.h>
36#include <pcb_edit_frame.h>
39#include <tool/tool_manager.h>
41
42#include <ranges>
43
44
45#define ID_MATCH_FP_ALL 4200
46#define ID_MATCH_FP_SELECTED 4201
47#define ID_MATCH_FP_REF 4202
48#define ID_MATCH_FP_VAL 4203
49#define ID_MATCH_FP_ID 4204
50
55
56
58 bool updateMode, bool selectedMode ) :
60 m_commit( aParent ),
61 m_parent( aParent ),
62 m_currentFootprint( aFootprint ),
63 m_updateMode( updateMode )
64{
65 if( !updateMode )
66 {
67 SetTitle( _( "Change Footprints" ) );
68 m_matchAll->SetLabel( _( "Change all footprints on board" ) );
69 m_matchSelected->SetLabel( _( "Change selected footprint(s)" ) );
70 m_matchSpecifiedRef->SetLabel( _( "Change footprints matching reference designator:" ) );
71 m_matchSpecifiedValue->SetLabel( _( "Change footprints matching value:" ) );
72 m_matchSpecifiedID->SetLabel( _( "Change footprints with library id:" ) );
73 m_resetTextItemLayers->SetLabel( _( "Update text layers and visibilities" ) );
74 m_resetTextItemEffects->SetLabel( _( "Update text sizes and styles" ) );
75 m_resetTextItemPositions->SetLabel( _( "Update text positions" ) );
76 m_resetTextItemContent->SetLabel( _( "Update text content" ) );
77 m_resetFabricationAttrs->SetLabel( _( "Update fabrication attributes" ) );
78 m_resetClearanceOverrides->SetLabel( _( "Update clearance overrides" ) );
79 m_reset3DModels->SetLabel( _( "Update 3D models" ) );
80 }
81
82#if 0 // translator hint
83 wxString x = _( "Update/reset strings: there are two cases these descriptions need to cover: "
84 "the user made overrides to a footprint on the PCB and wants to remove them, "
85 "or the user made changes to the library footprint and wants to propagate "
86 "them back to the PCB." );
87#endif
88
89 if( m_updateMode )
90 m_changeSizer->Show( false );
91 else
92 m_upperSizer->FindItem( m_matchAll )->Show( false );
93
95 m_upperSizer->FindItem( m_matchSelected )->Show( false );
96
99
100 m_upperSizer->SetEmptyCellSize( wxSize( 0, 0 ) );
101 // The upper sizer has its content modified: re-layout it:
102 m_upperSizer->Layout();
103
104 // initialize controls based on update mode in case there is no saved state yet
105 m_removeExtraBox->SetValue( m_updateMode ? false : false );
106 m_resetTextItemLayers->SetValue( m_updateMode ? false : true );
107 m_resetTextItemEffects->SetValue( m_updateMode ? false : true );
108 m_resetTextItemPositions->SetValue( m_updateMode ? false : true );
109 m_resetTextItemContent->SetValue( m_updateMode ? false : true );
110 m_resetFabricationAttrs->SetValue( m_updateMode ? false : true );
111 m_resetClearanceOverrides->SetValue( m_updateMode ? true : true );
112 m_reset3DModels->SetValue( m_updateMode ? true : true );
113
114 // initialize match-mode
115 if( m_updateMode )
117 else
119
120 m_MessageWindow->SetLazyUpdate( true );
121 m_MessageWindow->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
122
123 // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
124 // because the update and change versions of this dialog have different controls.
125 m_hash_key = TO_UTF8( GetTitle() );
126
127 wxString okLabel = m_updateMode ? _( "Update" ) : _( "Change" );
128
129 SetupStandardButtons( { { wxID_OK, okLabel },
130 { wxID_CANCEL, _( "Close" ) } } );
131
132 // Now all widgets have the size fixed, call FinishDialogSettings
134}
135
136
138{
140 {
141 if( m_updateMode )
142 m_newID->ChangeValue( From_UTF8( m_currentFootprint->GetFPID().Format().c_str() ) );
143
144 // Use ChangeValue() instead of SetValue() so we don't generate events.
145 m_specifiedRef->ChangeValue( m_currentFootprint->GetReference() );
146 m_specifiedValue->ChangeValue( m_currentFootprint->GetValue() );
147 m_specifiedID->ChangeValue( From_UTF8( m_currentFootprint->GetFPID().Format().c_str() ) );
148 }
149
150 wxCommandEvent event;
151 event.SetEventObject( this );
152
153 switch( *m_matchMode )
154 {
155 case ID_MATCH_FP_ALL: OnMatchAllClicked( event ); break;
156 case ID_MATCH_FP_SELECTED: OnMatchSelectedClicked( event ); break;
157 case ID_MATCH_FP_REF: OnMatchRefClicked( event ); break;
158 case ID_MATCH_FP_VAL: OnMatchValueClicked( event ); break;
159 case ID_MATCH_FP_ID: OnMatchIDClicked( event ); break;
160 default: break;
161 }
162
163 return true;
164}
165
166
168{
169 LIB_ID specifiedID;
170
171 switch( *m_matchMode )
172 {
173 case ID_MATCH_FP_ALL:
174 return true;
176 return aFootprint == m_currentFootprint || aFootprint->IsSelected();
177 case ID_MATCH_FP_REF:
178 return WildCompareString( m_specifiedRef->GetValue(), aFootprint->GetReference(), false );
179 case ID_MATCH_FP_VAL:
180 return WildCompareString( m_specifiedValue->GetValue(), aFootprint->GetValue(), false );
181 case ID_MATCH_FP_ID:
182 specifiedID.Parse( m_specifiedID->GetValue() );
183 return aFootprint->GetFPID() == specifiedID;
184 default:
185 return false; // just to quiet compiler warnings....
186 }
187}
188
189
191{
192 switch( *m_matchMode )
193 {
194 case ID_MATCH_FP_ALL: return m_matchAll;
199 default: return nullptr;
200 }
201}
202
203
205{
206 wxRadioButton* rb_button = getRadioButtonForMode();
207
208 wxRadioButton* rb_butt_list[] =
209 {
215 nullptr // end of list
216 };
217
218 // Ensure the button state is ok. Only one button can be checked
219 // Change button state only if its state is incorrect, otherwise
220 // we have issues depending on the platform.
221 for( int ii = 0; rb_butt_list[ii]; ++ii )
222 {
223 bool state = rb_butt_list[ii] == rb_button;
224
225 if( rb_butt_list[ii]->GetValue() != state )
226 rb_butt_list[ii]->SetValue( state );
227 }
228}
229
230
232{
234
235 if( aEvent.GetEventObject() == this )
237 else
238 m_matchAll->SetFocus();
239}
240
241
243{
245
246 if( aEvent.GetEventObject() == this )
248 else
249 m_matchSelected->SetFocus();
250}
251
252
254{
256
257 if( aEvent.GetEventObject() == this )
259 else if( aEvent.GetEventObject() != m_specifiedRef )
260 m_specifiedRef->SetFocus();
261}
262
263
265{
267
268 if( aEvent.GetEventObject() == this )
270 else if( aEvent.GetEventObject() != m_specifiedValue )
271 m_specifiedValue->SetFocus();
272}
273
274
276{
278
279 if( aEvent.GetEventObject() == this )
281 else if( aEvent.GetEventObject() != m_specifiedID )
282 m_specifiedID->SetFocus();
283}
284
285
287{
288 m_removeExtraBox->SetValue( aCheck );
289 m_resetTextItemLayers->SetValue( aCheck );
290 m_resetTextItemEffects->SetValue( aCheck );
291 m_resetTextItemPositions->SetValue( aCheck );
292 m_resetTextItemContent->SetValue( aCheck );
293 m_resetFabricationAttrs->SetValue( aCheck );
294 m_resetClearanceOverrides->SetValue( aCheck );
295 m_reset3DModels->SetValue( aCheck );
296}
297
298
299void DIALOG_EXCHANGE_FOOTPRINTS::OnOKClicked( wxCommandEvent& aEvent )
300{
301 PCB_SELECTION_TOOL* selTool = m_parent->GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
302 wxBusyCursor dummy;
303
304 m_MessageWindow->Clear();
305 m_MessageWindow->Flush( false );
306
307 m_newFootprints.clear();
309 m_commit.Push( m_updateMode ? _( "Update Footprint" ) : _( "Change Footprint" ) );
310 selTool->AddItemsToSel( &m_newFootprints );
311
312 m_MessageWindow->Flush( false );
313
314 WINDOW_THAWER thawer( m_parent );
315 m_parent->GetCanvas()->Refresh();
316}
317
318
320{
321 LIB_ID newFPID;
322
323 if( m_parent->GetBoard()->Footprints().empty() )
324 return;
325
326 if( !m_updateMode )
327 {
328 newFPID.Parse( m_newID->GetValue() );
329
330 if( !newFPID.IsValid() )
331 return;
332 }
333
334 /*
335 * NB: the change is done from the last footprint because processFootprint() modifies the
336 * last item in the list.
337 */
338 for( FOOTPRINT* footprint : std::ranges::reverse_view( m_parent->GetBoard()->Footprints() ) )
339 {
340 if( !isMatch( footprint ) )
341 continue;
342
343 if( m_updateMode )
344 processFootprint( footprint, footprint->GetFPID() );
345 else
346 processFootprint( footprint, newFPID );
347 }
348}
349
350
352{
353 LIB_ID oldFPID = aFootprint->GetFPID();
354 wxString msg;
355
356 // Load new footprint.
357 if( m_updateMode )
358 {
359 msg.Printf( _( "Updated footprint %s (%s)" ) + wxS( ": " ),
360 aFootprint->GetReference(),
361 oldFPID.Format().c_str() );
362 }
363 else
364 {
365 msg.Printf( _( "Changed footprint %s from '%s' to '%s'" ) + wxS( ": " ),
366 aFootprint->GetReference(),
367 oldFPID.Format().c_str(),
368 aNewFPID.Format().c_str() );
369 }
370
371 FOOTPRINT* newFootprint = m_parent->LoadFootprint( aNewFPID );
372
373 if( !newFootprint )
374 {
375 msg += _( "*** library footprint not found ***" );
376 m_MessageWindow->Report( msg, RPT_SEVERITY_ERROR );
377 return;
378 }
379
380 bool updated = !m_updateMode || aFootprint->FootprintNeedsUpdate( newFootprint );
381
382 m_parent->ExchangeFootprint( aFootprint, newFootprint, m_commit,
383 m_removeExtraBox->GetValue(),
384 m_resetTextItemLayers->GetValue(),
385 m_resetTextItemEffects->GetValue(),
386 m_resetTextItemPositions->GetValue(),
387 m_resetTextItemContent->GetValue(),
388 m_resetFabricationAttrs->GetValue(),
389 m_resetClearanceOverrides->GetValue(),
390 m_reset3DModels->GetValue(),
391 &updated );
392
393 if( aFootprint == m_currentFootprint )
394 m_currentFootprint = newFootprint;
395
396 if( newFootprint )
397 m_newFootprints.push_back( newFootprint );
398
399 if( m_updateMode && !updated )
400 {
401 msg += _( ": (no changes)" );
402 m_MessageWindow->Report( msg, RPT_SEVERITY_INFO );
403 }
404 else
405 {
406 msg += _( ": OK" );
407 m_MessageWindow->Report( msg, RPT_SEVERITY_ACTION );
408 }
409}
410
411
413{
414 wxString newname = m_newID->GetValue();
415
416 if( KIWAY_PLAYER* frame = Kiway().Player( FRAME_FOOTPRINT_CHOOSER, true, this ) )
417 {
419 {
420 /*
421 * Symbol netlist format:
422 * pinNumber pinName <tab> pinNumber pinName...
423 * fpFilter fpFilter...
424 */
425 wxString netlist;
426
427 wxArrayString pins;
428
429 for( const wxString& pad : m_currentFootprint->GetUniquePadNumbers() )
430 pins.push_back( pad + ' ' + wxEmptyString /* leave pinName empty */ );
431
432 if( !pins.IsEmpty() )
433 netlist << EscapeString( wxJoin( pins, '\t' ), CTX_LINE );
434
435 netlist << wxS( "\r" );
436
437 netlist << EscapeString( m_currentFootprint->GetFilters(), CTX_LINE ) << wxS( "\r" );
438
439 std::string payload( netlist.ToStdString() );
441 frame->KiwayMailIn( mail );
442 }
443
444 if( frame->ShowModal( &newname, this ) )
445 {
446 if( event.GetEventObject() == m_newIDBrowseButton )
447 m_newID->SetValue( newname );
448 else
449 m_specifiedID->SetValue( newname );
450 }
451
452 frame->Destroy();
453 }
454}
455
456
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
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:82
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:127
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:269
const wxString & GetValue() const
Definition footprint.h:683
const wxString & GetReference() const
Definition footprint.h:661
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
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.
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:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:52
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:172
UTF8 Format() const
Definition lib_id.cpp:119
The main frame for Pcbnew.
The selection tool: currently supports:
int AddItemsToSel(const TOOL_EVENT &aEvent)
const char * c_str() const
Definition utf8.h:109
#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:44
This file contains miscellaneous commonly used macros and functions.
@ MAIL_SYMBOL_NETLIST
Definition mail_type.h:45
@ 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