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_mail.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( 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( true );
112 m_reset3DModels->SetValue( true );
113 m_matchPadPositions->SetValue( true );
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_matchPadPositions->SetValue( aCheck );
298}
299
300
301void DIALOG_EXCHANGE_FOOTPRINTS::OnOKClicked( wxCommandEvent& aEvent )
302{
303 PCB_SELECTION_TOOL* selTool = m_parent->GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
304 wxBusyCursor dummy;
305
306 m_MessageWindow->Clear();
307 m_MessageWindow->Flush( false );
308
309 m_newFootprints.clear();
311 m_commit.Push( m_updateMode ? _( "Update Footprint" ) : _( "Change Footprint" ) );
312 selTool->AddItemsToSel( &m_newFootprints );
313
314 m_MessageWindow->Flush( false );
315
316 WINDOW_THAWER thawer( m_parent );
317 m_parent->GetCanvas()->Refresh();
318}
319
320
322{
323 LIB_ID newFPID;
324
325 if( m_parent->GetBoard()->Footprints().empty() )
326 return;
327
328 if( !m_updateMode )
329 {
330 newFPID.Parse( m_newID->GetValue() );
331
332 if( !newFPID.IsValid() )
333 return;
334 }
335
336 /*
337 * NB: the change is done from the last footprint because processFootprint() modifies the
338 * last item in the list.
339 */
340 for( FOOTPRINT* footprint : std::ranges::reverse_view( m_parent->GetBoard()->Footprints() ) )
341 {
342 if( !isMatch( footprint ) )
343 continue;
344
345 if( m_updateMode )
346 processFootprint( footprint, footprint->GetFPID() );
347 else
348 processFootprint( footprint, newFPID );
349 }
350}
351
352
354{
355 LIB_ID oldFPID = aFootprint->GetFPID();
356 wxString msg;
357
358 // Load new footprint.
359 if( m_updateMode )
360 {
361 msg.Printf( _( "Updated footprint %s (%s)" ) + wxS( ": " ),
362 aFootprint->GetReference(),
363 oldFPID.Format().c_str() );
364 }
365 else
366 {
367 msg.Printf( _( "Changed footprint %s from '%s' to '%s'" ) + wxS( ": " ),
368 aFootprint->GetReference(),
369 oldFPID.Format().c_str(),
370 aNewFPID.Format().c_str() );
371 }
372
373 FOOTPRINT* newFootprint = m_parent->LoadFootprint( aNewFPID );
374
375 if( !newFootprint )
376 {
377 msg += _( "*** library footprint not found ***" );
378 m_MessageWindow->Report( msg, RPT_SEVERITY_ERROR );
379 return;
380 }
381
382 bool updated = !m_updateMode || aFootprint->FootprintNeedsUpdate( newFootprint );
383
384 m_parent->ExchangeFootprint( aFootprint, newFootprint, m_commit,
385 m_matchPadPositions->GetValue(),
386 m_removeExtraBox->GetValue(),
387 m_resetTextItemLayers->GetValue(),
388 m_resetTextItemEffects->GetValue(),
389 m_resetTextItemPositions->GetValue(),
390 m_resetTextItemContent->GetValue(),
391 m_resetFabricationAttrs->GetValue(),
392 m_resetClearanceOverrides->GetValue(),
393 m_reset3DModels->GetValue(),
394 &updated );
395
396 if( aFootprint == m_currentFootprint )
397 m_currentFootprint = newFootprint;
398
399 if( newFootprint )
400 m_newFootprints.push_back( newFootprint );
401
402 if( m_updateMode && !updated )
403 {
404 msg += _( ": (no changes)" );
405 m_MessageWindow->Report( msg, RPT_SEVERITY_INFO );
406 }
407 else
408 {
409 msg += _( ": OK" );
410 m_MessageWindow->Report( msg, RPT_SEVERITY_ACTION );
411 }
412}
413
414
416{
417 wxString newname = m_newID->GetValue();
418
419 if( KIWAY_PLAYER* frame = Kiway().Player( FRAME_FOOTPRINT_CHOOSER, true, this ) )
420 {
422 {
423 /*
424 * Symbol netlist format:
425 * pinNumber pinName <tab> pinNumber pinName...
426 * fpFilter fpFilter...
427 */
428 wxString netlist;
429
430 wxArrayString pins;
431
432 for( const wxString& pad : m_currentFootprint->GetUniquePadNumbers() )
433 pins.push_back( pad + ' ' + wxEmptyString /* leave pinName empty */ );
434
435 if( !pins.IsEmpty() )
436 netlist << EscapeString( wxJoin( pins, '\t' ), CTX_LINE );
437
438 netlist << wxS( "\r" );
439
440 netlist << EscapeString( m_currentFootprint->GetFilters(), CTX_LINE ) << wxS( "\r" );
441
442 std::string payload( netlist.ToStdString() );
444 frame->KiwayMailIn( mail );
445 }
446
447 if( frame->ShowModal( &newname, this ) )
448 {
449 if( event.GetEventObject() == m_newIDBrowseButton )
450 m_newID->SetValue( newname );
451 else
452 m_specifiedID->SetValue( newname );
453 }
454
455 frame->Destroy();
456 }
457}
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:129
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:371
const wxString & GetValue() const
Definition footprint.h:793
const wxString & GetReference() const
Definition footprint.h:771
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:38
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:108
#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