KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_change_symbols.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) 2020-2021 CERN
5 * Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Wayne Stambaugh <[email protected]>
8 *
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <algorithm>
24
25#include <bitmaps.h>
26#include <connection_graph.h>
27#include <string_utils.h> // WildCompareString
28#include <kiway.h>
29#include <refdes_utils.h>
30#include <core/kicad_algo.h>
32#include <sch_symbol.h>
33#include <sch_edit_frame.h>
34#include <sch_screen.h>
35#include <schematic.h>
36#include <template_fieldnames.h>
39#include <sch_commit.h>
40
41bool g_selectRefDes = false;
42bool g_selectValue = false;
43 // { change, update }
44bool g_removeExtraFields[2] = { false, false };
45bool g_resetEmptyFields[2] = { false, false };
46bool g_resetFieldText[2] = { true, true };
47bool g_resetFieldVisibilities[2] = { true, false };
48bool g_resetFieldEffects[2] = { true, false };
49bool g_resetFieldPositions[2] = { true, false };
50bool g_resetAttributes[2] = { true, false };
51bool g_resetCustomPower[2] = { false, false };
52
53
55 MODE aMode ) :
57 m_symbol( aSymbol),
58 m_mode( aMode )
59{
60 wxASSERT( aParent );
61
62 if( m_mode == MODE::UPDATE )
63 {
64 m_newIdSizer->Show( false );
65 }
66 else
67 {
68 m_matchAll->SetLabel( _( "Change all symbols in schematic" ) );
69 SetTitle( _( "Change Symbols" ) );
70 m_matchSizer->FindItem( m_matchAll )->Show( false );
71 }
72
73 if( m_symbol )
74 {
75 SCH_SHEET_PATH* currentSheet = &aParent->Schematic().CurrentSheet();
76
77 if( m_mode == MODE::CHANGE )
78 m_matchBySelection->SetLabel( _( "Change selected symbol(s)" ) );
79
80 m_newId->ChangeValue( UnescapeString( m_symbol->GetLibId().Format() ) );
81 m_specifiedReference->ChangeValue( m_symbol->GetRef( currentSheet ) );
83 m_specifiedId->ChangeValue( UnescapeString( m_symbol->GetLibId().Format() ) );
84 }
85 else
86 {
87 m_matchSizer->FindItem( m_matchBySelection )->Show( false );
88 }
89
90 m_matchIdBrowserButton->SetBitmap( KiBitmapBundle( BITMAPS::small_library ) );
91 m_newIdBrowserButton->SetBitmap( KiBitmapBundle( BITMAPS::small_library ) );
92
93 if( m_mode == MODE::CHANGE )
94 {
95 m_matchByReference->SetLabel( _( "Change symbols matching reference designator:" ) );
96 m_matchByValue->SetLabel( _( "Change symbols matching value:" ) );
97 m_matchById->SetLabel( _( "Change symbols matching library identifier:" ) );
98 }
99
100 m_matchSizer->SetEmptyCellSize( wxSize( 0, 0 ) );
101 m_matchSizer->Layout();
102
103 for( int i = 0; i < MANDATORY_FIELDS; ++i )
104 {
106
107 if( i == REFERENCE_FIELD )
108 m_fieldsBox->Check( i, g_selectRefDes );
109 else if( i == VALUE_FIELD )
110 m_fieldsBox->Check( i, g_selectValue );
111 else
112 m_fieldsBox->Check( i, true );
113 }
114
116 m_messagePanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
117
118 if( aSymbol && aSymbol->IsSelected() )
119 {
120 m_matchBySelection->SetValue( true );
121 }
122 else
123 {
124 if( aMode == MODE::UPDATE )
125 m_matchAll->SetValue( true );
126 else
127 m_matchByReference->SetValue( true );
128 }
129
131
132 if( m_mode == MODE::CHANGE )
133 {
134 m_updateFieldsSizer->GetStaticBox()->SetLabel( _( "Update Fields" ) );
135 m_removeExtraBox->SetLabel( _( "Remove fields if not in new symbol" ) );
136 m_resetEmptyFields->SetLabel( _( "Reset fields if empty in new symbol" ) );
137 m_resetFieldText->SetLabel( _( "Update field text" ) );
138 m_resetFieldVisibilities->SetLabel( _( "Update field visibilities" ) );
139 m_resetFieldEffects->SetLabel( _( "Update field sizes and styles" ) );
140 m_resetFieldPositions->SetLabel( _( "Update field positions" ) );
141 m_resetAttributes->SetLabel( _( "Update symbol attributes" ) );
142 }
143
144 m_removeExtraBox->SetValue( g_removeExtraFields[ (int) m_mode ] );
145 m_resetEmptyFields->SetValue( g_resetEmptyFields[ (int) m_mode ] );
146 m_resetFieldText->SetValue( g_resetFieldText[ (int) m_mode ] );
148 m_resetFieldEffects->SetValue( g_resetFieldEffects[ (int) m_mode ] );
150 m_resetAttributes->SetValue( g_resetAttributes[ (int) m_mode ] );
151 m_resetCustomPower->SetValue( g_resetCustomPower[ (int) m_mode ] );
152
153 // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
154 // because the update and change versions of this dialog have different controls.
155 m_hash_key = TO_UTF8( GetTitle() );
156
157 wxString okLabel = m_mode == MODE::CHANGE ? _( "Change" ) : _( "Update" );
158
159 SetupStandardButtons( { { wxID_OK, okLabel },
160 { wxID_CANCEL, _( "Close" ) } } );
161
162 // Now all widgets have the size fixed, call FinishDialogSettings
164}
165
166
167void DIALOG_CHANGE_SYMBOLS::onMatchByAll( wxCommandEvent& aEvent )
168{
170}
171
172
173void DIALOG_CHANGE_SYMBOLS::onMatchBySelected( wxCommandEvent& aEvent )
174{
176}
177
178
179void DIALOG_CHANGE_SYMBOLS::onMatchByReference( wxCommandEvent& aEvent )
180{
182 m_specifiedReference->SetFocus();
183}
184
185
186void DIALOG_CHANGE_SYMBOLS::onMatchByValue( wxCommandEvent& aEvent )
187{
189 m_specifiedValue->SetFocus();
190}
191
192
193void DIALOG_CHANGE_SYMBOLS::onMatchById( wxCommandEvent& aEvent )
194{
196 m_specifiedId->SetFocus();
197}
198
199
201{
203 event.Skip(); // Mandatory in wxFocusEvent
204}
205
206
208{
210 event.Skip(); // Mandatory in wxFocusEvent
211}
212
213
215{
217 event.Skip(); // Mandatory in wxFocusEvent
218}
219
220
222{
224 g_selectValue = m_fieldsBox->IsChecked( VALUE_FIELD );
225
226 g_removeExtraFields[ (int) m_mode ] = m_removeExtraBox->GetValue();
227 g_resetEmptyFields[ (int) m_mode ] = m_resetEmptyFields->GetValue();
228 g_resetFieldText[ (int) m_mode ] = m_resetFieldText->GetValue();
230 g_resetFieldEffects[ (int) m_mode ] = m_resetFieldEffects->GetValue();
232 g_resetAttributes[ (int) m_mode ] = m_resetAttributes->GetValue();
233 g_resetCustomPower[ (int) m_mode ] = m_resetCustomPower->GetValue();
234}
235
236
237wxString getLibIdValue( const wxTextCtrl* aCtrl )
238{
239 wxString rawValue = aCtrl->GetValue();
240 wxString itemName;
241 wxString libName = rawValue.BeforeFirst( ':', &itemName );
242
243 return EscapeString( libName, CTX_LIBID ) + ':' + EscapeString( itemName, CTX_LIBID );
244}
245
246
248{
249 wxString newName = getLibIdValue( m_specifiedId );
250
251 KIWAY_PLAYER* frame = Kiway().Player( FRAME_SYMBOL_CHOOSER, true, this );
252
253 if( frame->ShowModal( &newName, this ) )
254 {
255 m_specifiedId->SetValue( UnescapeString( newName ) );
257 }
258
259 frame->Destroy();
260}
261
262
264{
265 wxString newName = getLibIdValue( m_newId );
266
267 KIWAY_PLAYER* frame = Kiway().Player( FRAME_SYMBOL_CHOOSER, true, this );
268
269 if( frame->ShowModal( &newName, this ) )
270 {
271 m_newId->SetValue( UnescapeString( newName ) );
273 }
274
275 frame->Destroy();
276}
277
278
280{
281 SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
282
283 wxCHECK( frame, /* void */ );
284
285 SCH_SHEET_LIST hierarchy = frame->Schematic().GetSheets();
286
287 // Load non-mandatory fields from all matching symbols and their library symbols
288 std::vector<SCH_FIELD*> fields;
289 std::vector<SCH_FIELD*> libFields;
290 std::set<wxString> fieldNames;
291
292 for( SCH_SHEET_PATH& instance : hierarchy )
293 {
294 SCH_SCREEN* screen = instance.LastScreen();
295
296 wxCHECK2( screen, continue );
297
298 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
299 {
300 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
301
302 wxCHECK2( symbol, continue );
303
304 if( !isMatch( symbol, &instance ) )
305 continue;
306
307 fields.clear();
308 symbol->GetFields( fields, false );
309
310 for( unsigned i = MANDATORY_FIELDS; i < fields.size(); ++i )
311 fieldNames.insert( fields[i]->GetName() );
312
313 if( m_mode == MODE::UPDATE && symbol->GetLibId().IsValid() )
314 {
315 LIB_SYMBOL* libSymbol = frame->GetLibSymbol( symbol->GetLibId() );
316
317 if( libSymbol )
318 {
319 std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
320
321 flattenedSymbol->GetFields( libFields );
322
323 for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
324 fieldNames.insert( libFields[i]->GetName() );
325
326 libFields.clear(); // flattenedSymbol is about to go out of scope...
327 }
328 }
329 }
330 }
331
332 // Load non-mandatory fields from the change-to library symbol
333 if( m_mode == MODE::CHANGE )
334 {
335 LIB_ID newId;
336
337 newId.Parse( getLibIdValue( m_newId ) );
338
339 if( newId.IsValid() )
340 {
341 LIB_SYMBOL* libSymbol = frame->GetLibSymbol( newId );
342
343 if( libSymbol )
344 {
345 std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
346
347 flattenedSymbol->GetFields( libFields );
348
349 for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
350 fieldNames.insert( libFields[i]->GetName() );
351
352 libFields.clear(); // flattenedSymbol is about to go out of scope...
353 }
354 }
355 }
356
357 // Update the listbox widget
358 wxArrayInt checkedItems;
359 wxArrayString checkedNames;
360
361 m_fieldsBox->GetCheckedItems( checkedItems );
362
363 for( int ii : checkedItems )
364 checkedNames.push_back( m_fieldsBox->GetString( ii ) );
365
366 bool allChecked = true;
367
368 for( unsigned ii = 0; ii < m_fieldsBox->GetCount(); ++ii )
369 {
370 if( ii == REFERENCE_FIELD || ii == VALUE_FIELD )
371 continue;
372
373 if( !m_fieldsBox->IsChecked( ii ) )
374 allChecked = false;
375 }
376
377 for( unsigned ii = m_fieldsBox->GetCount() - 1; ii >= MANDATORY_FIELDS; --ii )
378 m_fieldsBox->Delete( ii );
379
380 for( const wxString& fieldName : fieldNames )
381 {
382 m_fieldsBox->Append( fieldName );
383
384 if( allChecked || alg::contains( checkedNames, fieldName ) )
385 m_fieldsBox->Check( m_fieldsBox->GetCount() - 1, true );
386 }
387}
388
389
391{
392 for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
393 m_fieldsBox->Check( i, aCheck );
394}
395
396
397void DIALOG_CHANGE_SYMBOLS::onOkButtonClicked( wxCommandEvent& aEvent )
398{
399 SCH_EDIT_FRAME* parent = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
400
401 wxCHECK( parent, /* void */ );
402
403 wxBusyCursor dummy;
404 SCH_COMMIT commit( parent );
405
407 m_messagePanel->Flush( false );
408
409 // Create the set of fields to be updated. Use non translated (canonical) names
410 // for mandatory fields
411 m_updateFields.clear();
412
413 for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
414 {
415 if( m_fieldsBox->IsChecked( i ) )
416 {
417 if( i < MANDATORY_FIELDS )
418 {
419 SCH_FIELD dummy_field( nullptr, i );
420 m_updateFields.insert( dummy_field.GetCanonicalName() );
421 }
422 else
423 {
424 m_updateFields.insert( m_fieldsBox->GetString( i ) );
425 }
426 }
427 }
428
429 if( processMatchingSymbols( &commit) )
430 commit.Push( m_mode == MODE::CHANGE ? _( "Change Symbols" ) : _( "Update Symbols" ) );
431
432 m_messagePanel->Flush( false );
433}
434
435
437{
438 LIB_ID id;
439
440 wxCHECK( aSymbol, false );
441
442 SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
443
444 wxCHECK( frame, false );
445
446 if( m_matchAll->GetValue() )
447 {
448 return true;
449 }
450 else if( m_matchBySelection->GetValue() )
451 {
452 return aSymbol == m_symbol || aSymbol->IsSelected();
453 }
454 else if( m_matchByReference->GetValue() )
455 {
456 return WildCompareString( m_specifiedReference->GetValue(),
457 UnescapeString( aSymbol->GetRef( aInstance, false ) ),
458 false );
459 }
460 else if( m_matchByValue->GetValue() )
461 {
462 return WildCompareString( m_specifiedValue->GetValue(),
463 UnescapeString( aSymbol->GetField( VALUE_FIELD )->GetText() ),
464 false );
465 }
466 else if( m_matchById )
467 {
468 id.Parse( getLibIdValue( m_specifiedId ) );
469 return aSymbol->GetLibId() == id;
470 }
471
472 return false;
473}
474
475
477{
478 SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
479
480 wxCHECK( frame, false );
481
482 LIB_ID newId;
483 wxString msg;
484 int matchesProcessed = 0;
485 SCH_SYMBOL* symbol = nullptr;
486 SCH_SHEET_LIST hierarchy = frame->Schematic().GetSheets();
487
488 if( m_mode == MODE::CHANGE )
489 {
490 newId.Parse( getLibIdValue( m_newId ) );
491
492 if( !newId.IsValid() )
493 return false;
494 }
495
496 std::map<SCH_SYMBOL*, SYMBOL_CHANGE_INFO> symbols;
497
498 for( SCH_SHEET_PATH& instance : hierarchy )
499 {
500 SCH_SCREEN* screen = instance.LastScreen();
501
502 wxCHECK2( screen, continue );
503
504 // Fetch all the symbols that meet the change criteria.
505 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
506 {
507 symbol = static_cast<SCH_SYMBOL*>( item );
508
509 wxCHECK2( symbol, continue );
510
511 if( !isMatch( symbol, &instance ) )
512 continue;
513
514 if( m_mode == MODE::UPDATE )
515 newId = symbol->GetLibId();
516
517 auto it = symbols.find( symbol );
518
519 if( it == symbols.end() )
520 {
522
523 info.m_Instances.emplace_back( instance );
524 info.m_LibId = newId;
525 symbols.insert( { symbol, info } );
526 }
527 else
528 {
529 it->second.m_Instances.emplace_back( instance );
530 }
531 }
532 }
533
534 if( symbols.size() > 0 )
535 matchesProcessed += processSymbols( aCommit, symbols );
536 else
537 m_messagePanel->Report( _( "*** No symbols matching criteria found ***" ),
539
541
542 return matchesProcessed;
543}
544
545
547 const std::map<SCH_SYMBOL*,
548 SYMBOL_CHANGE_INFO>& aSymbols )
549{
550 wxCHECK( !aSymbols.empty(), 0 );
551
552 int matchesProcessed = 0;
553 SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
554 wxString msg;
555
556 wxCHECK( frame, 0 );
557
558 std::map<SCH_SYMBOL*, SYMBOL_CHANGE_INFO> symbols = aSymbols;
559 std::map<SCH_SYMBOL*, SYMBOL_CHANGE_INFO>::iterator it = symbols.begin();
560
561 // Remove all symbols that don't have a valid library symbol link or enough units to
562 // satify the library symbol update.
563 while( it != symbols.end() )
564 {
565 SCH_SYMBOL* symbol = it->first;
566
567 wxCHECK2( symbol && it->second.m_LibId.IsValid(), continue );
568
569 LIB_SYMBOL* libSymbol = frame->GetLibSymbol( it->second.m_LibId );
570
571 if( !libSymbol )
572 {
573 msg = getSymbolReferences( *symbol, it->second.m_LibId );
574 msg << wxT( ": " ) << _( "*** symbol not found ***" );
576 it = symbols.erase( it );
577 continue;
578 }
579
580 std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
581
582 if( flattenedSymbol->GetUnitCount() < symbol->GetUnit() )
583 {
584 msg = getSymbolReferences( *symbol, it->second.m_LibId );
585 msg << wxT( ": " ) << _( "*** new symbol has too few units ***" );
587 it = symbols.erase( it );
588 }
589 else
590 {
591 ++it;
592 }
593 }
594
595 // Removing the symbol needs to be done before the LIB_SYMBOL is changed to prevent stale
596 // library symbols in the schematic file.
597 for( const auto& [ symbol, symbol_change_info ] : symbols )
598 {
599 wxCHECK( symbol && !symbol_change_info.m_Instances.empty(), 0 );
600
601 SCH_SCREEN* screen = symbol_change_info.m_Instances[0].LastScreen();
602
603 wxCHECK( screen, 0 );
604
605 screen->Remove( symbol );
606 SCH_SYMBOL* symbol_copy = static_cast<SCH_SYMBOL*>( symbol->Clone() );
607 aCommit->Modified( symbol, symbol_copy, screen );
608
609 CONNECTION_GRAPH* connectionGraph = screen->Schematic()->ConnectionGraph();
610
611 // When we replace the lib symbol below, we free the associated pins if the new symbol has
612 // fewer than the original. This will cause the connection graph to be out of date unless
613 // we replace references in the graph to the old symbol/pins with references to the ones stored
614 // in the undo stack.
615 if( connectionGraph )
616 connectionGraph->ExchangeItem( symbol, symbol_copy );
617 }
618
619 for( const auto& [ symbol, symbol_change_info ] : symbols )
620 {
621 // Remember initial link before changing for diags purpose
622 wxString initialLibLinkName = UnescapeString( symbol->GetLibId().Format() );
623
624 if( symbol_change_info.m_LibId != symbol->GetLibId() )
625 symbol->SetLibId( symbol_change_info.m_LibId );
626
627 LIB_SYMBOL* libSymbol = frame->GetLibSymbol( symbol_change_info.m_LibId );
628 std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
629 SCH_SCREEN* screen = symbol_change_info.m_Instances[0].LastScreen();
630
631 symbol->SetLibSymbol( flattenedSymbol.release() );
632
633 if( m_resetAttributes->GetValue() )
634 {
635 // Fetch the attributes from the *flattened* library symbol. They are not supported
636 // in derived symbols.
637 symbol->SetExcludedFromSim( symbol->GetLibSymbolRef()->GetExcludedFromSim() );
638 symbol->SetExcludedFromBOM( symbol->GetLibSymbolRef()->GetExcludedFromBOM() );
639 symbol->SetExcludedFromBoard( symbol->GetLibSymbolRef()->GetExcludedFromBoard() );
640 }
641
642 bool removeExtras = m_removeExtraBox->GetValue();
643 bool resetVis = m_resetFieldVisibilities->GetValue();
644 bool resetEffects = m_resetFieldEffects->GetValue();
645 bool resetPositions = m_resetFieldPositions->GetValue();
646
647 for( unsigned i = 0; i < symbol->GetFields().size(); ++i )
648 {
649 SCH_FIELD& field = symbol->GetFields()[i];
650 SCH_FIELD* libField = nullptr;
651
652 // Mandatory fields always exist in m_updateFields, but these names can be translated.
653 // so use GetCanonicalName().
655 continue;
656
657 if( i < MANDATORY_FIELDS )
658 libField = symbol->GetLibSymbolRef()->GetFieldById( (int) i );
659 else
660 libField = symbol->GetLibSymbolRef()->FindField( field.GetName() );
661
662 if( libField )
663 {
664 bool resetText = libField->GetText().IsEmpty() ? m_resetEmptyFields->GetValue()
665 : m_resetFieldText->GetValue();
666
667 if( resetText )
668 {
669 if( i == REFERENCE_FIELD )
670 {
671 wxString prefix = UTIL::GetRefDesPrefix( libField->GetText() );
672
673 for( const SCH_SHEET_PATH& instance : symbol_change_info.m_Instances )
674 {
675 wxString ref = symbol->GetRef( &instance );
676 int number = UTIL::GetRefDesNumber( ref );
677
678 if( number >= 0 )
679 ref.Printf( wxS( "%s%d" ), prefix, number );
680 else
681 ref = UTIL::GetRefDesUnannotated( prefix );
682
683 symbol->SetRef( &instance, ref );
684 }
685 }
686 else if( i == VALUE_FIELD )
687 {
688 if( ( symbol->IsPower() && m_resetCustomPower->IsChecked() )
689 || !symbol->IsPower() )
690 symbol->SetValueFieldText( UnescapeString( libField->GetText() ) );
691 }
692 else if( i == FOOTPRINT_FIELD )
693 {
694 symbol->SetFootprintFieldText( libField->GetText() );
695 }
696 else
697 {
698 field.SetText( libField->GetText() );
699 }
700 }
701
702 if( resetVis )
703 field.SetVisible( libField->IsVisible() );
704
705 if( resetEffects )
706 {
707 // Careful: the visible bit and position are also set by SetAttributes()
708 bool visible = field.IsVisible();
709 VECTOR2I pos = field.GetPosition();
710
711 field.SetAttributes( *libField );
712
713 field.SetVisible( visible );
714 field.SetPosition( pos );
715 field.SetNameShown( libField->IsNameShown() );
716 field.SetCanAutoplace( libField->CanAutoplace() );
717 }
718
719 if( resetPositions )
720 field.SetTextPos( symbol->GetPosition() + libField->GetTextPos() );
721 }
722 else if( i >= MANDATORY_FIELDS && removeExtras )
723 {
724 symbol->RemoveField( field.GetName() );
725 i--;
726 }
727 }
728
729 std::vector<SCH_FIELD*> libFields;
730 symbol->GetLibSymbolRef()->GetFields( libFields );
731
732 for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
733 {
734 const SCH_FIELD& libField = *libFields[i];
735
736 if( !alg::contains( m_updateFields, libField.GetCanonicalName() ) )
737 continue;
738
739 if( !symbol->FindField( libField.GetName(), false ) )
740 {
741 wxString fieldName = libField.GetCanonicalName();
742 SCH_FIELD newField( VECTOR2I( 0, 0 ), symbol->GetFieldCount(), symbol,
743 fieldName );
744 SCH_FIELD* schField = symbol->AddField( newField );
745
746 // Careful: the visible bit and position are also set by SetAttributes()
747 schField->SetAttributes( libField );
748 schField->SetText( libField.GetText() );
749 schField->SetTextPos( symbol->GetPosition() + libField.GetTextPos() );
750 }
751
752 if( resetPositions && frame->eeconfig()->m_AutoplaceFields.enable )
753 symbol->AutoAutoplaceFields( screen );
754 }
755
756 symbol->SetSchSymbolLibraryName( wxEmptyString );
757 screen->Append( symbol );
758
759 if( resetPositions )
760 symbol->AutoAutoplaceFields( screen );
761
762 frame->GetCanvas()->GetView()->Update( symbol );
763
764 msg = getSymbolReferences( *symbol, symbol_change_info.m_LibId, &initialLibLinkName );
765 msg += wxS( ": OK" );
767 matchesProcessed +=1;
768 }
769
770 return matchesProcessed;
771}
772
773
775 const LIB_ID& aNewId,
776 const wxString* aOldLibLinkName )
777{
778 wxString msg;
779 wxString references;
780 LIB_ID oldId = aSymbol.GetLibId();
781
782 wxString oldLibLinkName; // For report
783
784 if( aOldLibLinkName )
785 oldLibLinkName = *aOldLibLinkName;
786 else
787 oldLibLinkName = UnescapeString( oldId.Format() );
788
789 SCH_EDIT_FRAME* parent = dynamic_cast< SCH_EDIT_FRAME* >( GetParent() );
790
791 wxCHECK( parent, msg );
792
793 SCH_SHEET_LIST sheets = parent->Schematic().GetSheets();
794
795 for( const SCH_SYMBOL_INSTANCE& instance : aSymbol.GetInstances() )
796 {
797 // Only include the symbol instances for the current project.
798 if( !sheets.HasPath( instance.m_Path ) )
799 continue;
800
801 if( references.IsEmpty() )
802 references = instance.m_Reference;
803 else
804 references += wxT( " " ) + instance.m_Reference;
805 }
806
807 if( m_mode == MODE::UPDATE )
808 {
809 if( aSymbol.GetInstances().size() == 1 )
810 {
811 msg.Printf( _( "Update symbol %s from '%s' to '%s'" ),
812 references,
813 oldLibLinkName,
814 UnescapeString( aNewId.Format() ) );
815 }
816 else
817 {
818 msg.Printf( _( "Update symbols %s from '%s' to '%s'" ),
819 references,
820 oldLibLinkName,
821 UnescapeString( aNewId.Format() ) );
822 }
823 }
824 else // mode is MODE::CHANGE
825 {
826 if( aSymbol.GetInstances().size() == 1 )
827 {
828 msg.Printf( _( "Change symbol %s from '%s' to '%s'" ),
829 references,
830 oldLibLinkName,
831 UnescapeString( aNewId.Format() ) );
832 }
833 else
834 {
835 msg.Printf( _( "Change symbols %s from '%s' to '%s'" ),
836 references,
837 oldLibLinkName,
838 UnescapeString( aNewId.Format() ) );
839 }
840 }
841
842 return msg;
843}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy, BASE_SCREEN *aScreen=nullptr)
Definition: commit.h:112
Calculate the connectivity of a schematic and generates netlists.
void ExchangeItem(SCH_ITEM *aOldItem, SCH_ITEM *aNewItem)
Replace all references to #aOldItem with #aNewItem in the graph.
Class DIALOG_CHANGE_SYMBOLS_BASE.
WX_HTML_REPORT_PANEL * m_messagePanel
STD_BITMAP_BUTTON * m_matchIdBrowserButton
void onNewLibIDKillFocus(wxFocusEvent &event) override
void onMatchBySelected(wxCommandEvent &aEvent) override
MODE m_mode
Set of field names that should have values updated.
bool isMatch(SCH_SYMBOL *aSymbol, SCH_SHEET_PATH *aInstance)
void onOkButtonClicked(wxCommandEvent &aEvent) override
std::set< wxString > m_updateFields
void onMatchById(wxCommandEvent &aEvent) override
void launchMatchIdSymbolBrowser(wxCommandEvent &aEvent) override
void onMatchByReference(wxCommandEvent &aEvent) override
void onMatchByValue(wxCommandEvent &aEvent) override
int processMatchingSymbols(SCH_COMMIT *aCommit)
DIALOG_CHANGE_SYMBOLS(SCH_EDIT_FRAME *aParent, SCH_SYMBOL *aSymbol, MODE aMode=MODE::UPDATE)
void onMatchTextKillFocus(wxFocusEvent &event) override
void onMatchIDKillFocus(wxFocusEvent &event) override
wxString getSymbolReferences(SCH_SYMBOL &aSymbol, const LIB_ID &aNewId, const wxString *aOldLibLinkName=nullptr)
void launchNewIdSymbolBrowser(wxCommandEvent &aEvent) override
void onMatchByAll(wxCommandEvent &aEvent) override
int processSymbols(SCH_COMMIT *aCommit, const std::map< SCH_SYMBOL *, SYMBOL_CHANGE_INFO > &aSymbols)
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
Definition: dialog_shim.h:206
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:109
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:234
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
virtual bool IsVisible() const
Definition: eda_text.h:151
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:419
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition: eda_text.cpp:292
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:245
AUTOPLACE_FIELDS m_AutoplaceFields
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1631
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.
Definition: kiway_holder.h:55
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:67
virtual bool ShowModal(wxString *aResult=nullptr, wxWindow *aResultantFocusWindow=nullptr)
Show this wxFrame as if it were a modal dialog, with all other instantiated wxFrames disabled until t...
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:406
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:51
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
UTF8 Format() const
Definition: lib_id.cpp:118
Define a library symbol object.
Definition: lib_symbol.h:77
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
Definition: lib_symbol.cpp:579
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:136
CONNECTION_GRAPH * ConnectionGraph() const override
Definition: schematic.h:146
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:100
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
EESCHEMA_SETTINGS * eeconfig() const
LIB_SYMBOL * GetLibSymbol(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
Definition: sch_commit.cpp:405
KIGFX::SCH_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
Schematic editor (Eeschema) main window.
SCH_SHEET_PATH & GetCurrentSheet() const
SCHEMATIC & Schematic() const
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1407
bool IsNameShown() const
Definition: sch_field.h:205
void SetCanAutoplace(bool aCanPlace)
Definition: sch_field.h:217
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: sch_field.cpp:1174
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1149
void SetPosition(const VECTOR2I &aPosition) override
Definition: sch_field.cpp:1387
bool CanAutoplace() const
Definition: sch_field.h:216
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1138
void SetNameShown(bool aShown=true)
Definition: sch_field.h:206
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:174
int GetUnit() const
Definition: sch_item.h:237
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Definition: sch_screen.cpp:150
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:109
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
Definition: sch_screen.cpp:320
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:97
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
bool HasPath(const KIID_PATH &aPath) const
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void UpdateAllScreenReferences() const
Update all the symbol references for this sheet path.
Schematic symbol object.
Definition: sch_symbol.h:108
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition: sch_symbol.h:167
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:910
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:197
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:958
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:711
void SetBitmap(const wxBitmapBundle &aBmp)
void Clear()
return the number of messages matching the given severity mask.
void SetLazyUpdate(bool aLazyUpdate)
Forces updating the HTML page, after the report is built in lazy mode If aSort = true,...
void SetFileName(const wxString &aReportFileName)
void Report(const wxString &aText, SEVERITY aSeverity, REPORTER::LOCATION aLocation=REPORTER::LOC_BODY)
Reports the string.
void Flush(bool aSort=false)
Set the visible severity filter.
bool g_resetFieldEffects[2]
bool g_selectRefDes
bool g_resetFieldPositions[2]
bool g_resetAttributes[2]
bool g_removeExtraFields[2]
bool g_resetFieldVisibilities[2]
bool g_resetEmptyFields[2]
wxString getLibIdValue(const wxTextCtrl *aCtrl)
bool g_resetFieldText[2]
bool g_selectValue
bool g_resetCustomPower[2]
#define _(s)
@ FRAME_SYMBOL_CHOOSER
Definition: frame_type.h:37
wxString GetRefDesPrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.
wxString GetRefDesUnannotated(const wxString &aSource)
Return an unannotated refdes from either a prefix or an existing refdes.
int GetRefDesNumber(const wxString &aRefDes)
Get the numeric suffix from a refdes - e.g.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
Collection of utility functions for component reference designators (refdes)
@ RPT_SEVERITY_ERROR
@ 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 UnescapeString(const wxString &aSource)
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.
Definition: string_utils.h:391
@ CTX_LIBID
Definition: string_utils.h:54
A simple container for schematic symbol instance information.
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslateForHI=false)
Return a default symbol field name for field aFieldNdx for all components.
#define DO_TRANSLATE
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ MANDATORY_FIELDS
The first 5 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588