KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_rescue.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 Chris Pavlina <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <sch_draw_panel.h>
26#include <symbol_library.h>
27#include <confirm.h>
28#include <connection_graph.h>
29#include <invoke_sch_dialog.h>
30#include <kiway.h>
31#include <symbol_viewer_frame.h>
32#include <project_rescue.h>
33#include <project_sch.h>
34#include <sch_edit_frame.h>
35#include <string_utils.h>
36#include <symbol_lib_table.h>
38#include <wx/msgdlg.h>
39
40#include <cctype>
41#include <map>
42
43
44// Helper sort function, used in getSymbols, to sort a symbol list by lib_id
45static bool sort_by_libid( const SCH_SYMBOL* ref, SCH_SYMBOL* cmp )
46{
47 return ref->GetLibId() < cmp->GetLibId();
48}
49
50
60static void getSymbols( SCHEMATIC* aSchematic, std::vector<SCH_SYMBOL*>& aSymbols )
61{
62 SCH_SCREENS screens( aSchematic->Root() );
63
64 // Get the full list
65 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
66 {
67 for( EDA_ITEM* aItem : screen->Items().OfType( SCH_SYMBOL_T ) )
68 aSymbols.push_back( static_cast<SCH_SYMBOL*>( aItem ) );
69 }
70
71 if( aSymbols.empty() )
72 return;
73
74 // sort aSymbols by lib symbol. symbols will be grouped by same lib symbol.
75 std::sort( aSymbols.begin(), aSymbols.end(), sort_by_libid );
76}
77
78
86static LIB_SYMBOL* findSymbol( const wxString& aName, SYMBOL_LIBS* aLibs, bool aCached )
87{
88 LIB_SYMBOL *symbol = nullptr;
89
90 for( SYMBOL_LIB& each_lib : *aLibs )
91 {
92 if( aCached && !each_lib.IsCache() )
93 continue;
94
95 if( !aCached && each_lib.IsCache() )
96 continue;
97
98 symbol = each_lib.FindSymbol( aName );
99
100 if( symbol )
101 break;
102 }
103
104 return symbol;
105}
106
107
108static wxFileName GetRescueLibraryFileName( SCHEMATIC* aSchematic )
109{
110 wxFileName fn = aSchematic->GetFileName();
111 fn.SetName( fn.GetName() + wxT( "-rescue" ) );
113 return fn;
114}
115
116
117RESCUE_CASE_CANDIDATE::RESCUE_CASE_CANDIDATE( const wxString& aRequestedName,
118 const wxString& aNewName,
119 LIB_SYMBOL* aLibCandidate,
120 int aUnit,
121 int aConvert )
122{
123 m_requested_name = aRequestedName;
124 m_new_name = aNewName;
125 m_lib_candidate = aLibCandidate;
126 m_unit = aUnit;
127 m_convert = aConvert;
128}
129
130
132 boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates )
133{
134 std::map<wxString, RESCUE_CASE_CANDIDATE> candidate_map;
135
136 // Remember the list of symbols is sorted by symbol name.
137 // So a search in libraries is made only once by group
138 LIB_SYMBOL* case_sensitive_match = nullptr;
139 std::vector<LIB_SYMBOL*> case_insensitive_matches;
140
141 wxString symbol_name;
142 wxString last_symbol_name;
143
144 for( SCH_SYMBOL* eachSymbol : *( aRescuer.GetSymbols() ) )
145 {
146 symbol_name = eachSymbol->GetLibId().GetUniStringLibItemName();
147
148 if( last_symbol_name != symbol_name )
149 {
150 // A new symbol name is found (a new group starts here).
151 // Search the symbol names candidates only once for this group:
152 last_symbol_name = symbol_name;
153 case_insensitive_matches.clear();
154
155 LIB_ID id( wxEmptyString, symbol_name );
156
157 case_sensitive_match = PROJECT_SCH::SchLibs( aRescuer.GetPrj() )->FindLibSymbol( id );
158
159 if( case_sensitive_match )
160 continue;
161
162 // If the case sensitive match failed, try a case insensitive match.
163 PROJECT_SCH::SchLibs( aRescuer.GetPrj() )
164 ->FindLibraryNearEntries( case_insensitive_matches, symbol_name );
165
166 // If there are not case insensitive matches either, the symbol cannot be rescued.
167 if( !case_insensitive_matches.size() )
168 continue;
169
170 RESCUE_CASE_CANDIDATE candidate( symbol_name, case_insensitive_matches[0]->GetName(),
171 case_insensitive_matches[0], eachSymbol->GetUnit(),
172 eachSymbol->GetBodyStyle() );
173
174 candidate_map[symbol_name] = candidate;
175 }
176 }
177
178 // Now, dump the map into aCandidates
179 for( const auto& [ name, candidate ] : candidate_map )
180 aCandidates.push_back( new RESCUE_CASE_CANDIDATE( candidate ) );
181}
182
183
185{
186 wxString action;
187 action.Printf( _( "Rename %s to %s" ), m_requested_name, m_new_name );
188 return action;
189}
190
191
193{
194 wxCHECK( m_lib_candidate, true );
195
196 std::unique_ptr<LIB_SYMBOL> new_symbol = m_lib_candidate->Flatten();
197 new_symbol->SetName( m_new_name );
198 aRescuer->AddSymbol( new_symbol.get() );
199
200 for( SCH_SYMBOL* eachSymbol : *aRescuer->GetSymbols() )
201 {
202 if( eachSymbol->GetLibId().GetLibItemName() != UTF8( m_requested_name ) )
203 continue;
204
205 LIB_ID libId;
206
207 libId.SetLibItemName( m_new_name );
208 eachSymbol->SetLibId( libId );
209 eachSymbol->ClearFlags();
210 aRescuer->LogRescue( eachSymbol, m_requested_name, m_new_name );
211 }
212
213 return true;
214}
215
216
218 const wxString& aNewName,
219 LIB_SYMBOL* aCacheCandidate,
220 LIB_SYMBOL* aLibCandidate,
221 int aUnit,
222 int aConvert )
223{
224 m_requested_name = aRequestedName;
225 m_new_name = aNewName;
226 m_cache_candidate = aCacheCandidate;
227 m_lib_candidate = aLibCandidate;
228 m_unit = aUnit;
229 m_convert = aConvert;
230}
231
232
234{
235 m_cache_candidate = nullptr;
236 m_lib_candidate = nullptr;
237}
238
239
241 boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates )
242{
243 std::map<wxString, RESCUE_CACHE_CANDIDATE> candidate_map;
244
245 // Remember the list of symbols is sorted by symbol name.
246 // So a search in libraries is made only once by group
247 LIB_SYMBOL* cache_match = nullptr;
248 LIB_SYMBOL* lib_match = nullptr;
249 wxString symbol_name;
250 wxString old_symbol_name;
251
252 for( SCH_SYMBOL* eachSymbol : *( aRescuer.GetSymbols() ) )
253 {
254 symbol_name = eachSymbol->GetLibId().GetUniStringLibItemName();
255
256 if( old_symbol_name != symbol_name )
257 {
258 // A new symbol name is found (a new group starts here).
259 // Search the symbol names candidates only once for this group:
260 old_symbol_name = symbol_name;
261 cache_match = findSymbol( symbol_name, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ),
262 true );
263 lib_match = findSymbol( symbol_name, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ), false );
264
265 // At some point during V5 development, the LIB_ID delimiter character ':' was
266 // replaced by '_' when writing the symbol cache library so we have to test for
267 // the LIB_NICKNAME_LIB_SYMBOL_NAME case.
268 if( !cache_match && eachSymbol->GetLibId().IsValid() )
269 {
270 wxString tmp = wxString::Format( wxT( "%s-%s" ),
271 eachSymbol->GetLibId().GetLibNickname().wx_str(),
272 eachSymbol->GetLibId().GetLibItemName().wx_str() );
273 cache_match = findSymbol( tmp, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ), true );
274 }
275
276 // Test whether there is a conflict or if the symbol can only be found in the cache
277 // and the symbol name does not have any illegal characters.
278 if( cache_match && lib_match
279 && !cache_match->PinsConflictWith( *lib_match, true, true, true, true, false ) )
280 {
281 continue;
282 }
283
284 if( !cache_match && lib_match )
285 continue;
286
287 // Check if the symbol has already been rescued.
288 RESCUE_CACHE_CANDIDATE candidate( symbol_name, symbol_name, cache_match, lib_match,
289 eachSymbol->GetUnit(), eachSymbol->GetBodyStyle() );
290
291 candidate_map[symbol_name] = candidate;
292 }
293 }
294
295 // Now, dump the map into aCandidates
296 for( const auto& [name, candidate] : candidate_map )
297 aCandidates.push_back( new RESCUE_CACHE_CANDIDATE( candidate ) );
298}
299
300
302{
303 wxString action;
304
306 {
307 action.Printf( _( "Cannot rescue symbol %s which is not available in any library or "
308 "the cache." ),
310 }
312 {
313 action.Printf( _( "Rescue symbol %s found only in cache library to %s." ),
315 m_new_name );
316 }
317 else
318 {
319 action.Printf( _( "Rescue modified symbol %s to %s" ),
321 m_new_name );
322 }
323
324 return action;
325}
326
327
329{
331
332 // A symbol that cannot be rescued is a valid condition so just bail out here.
333 if( !tmp )
334 return true;
335
336 std::unique_ptr<LIB_SYMBOL> new_symbol = tmp->Flatten();
337 new_symbol->SetName( m_new_name );
338 aRescuer->AddSymbol( new_symbol.get() );
339
340 for( SCH_SYMBOL* eachSymbol : *aRescuer->GetSymbols() )
341 {
342 if( eachSymbol->GetLibId().GetLibItemName() != UTF8( m_requested_name ) )
343 continue;
344
345 LIB_ID libId;
346
347 libId.SetLibItemName( m_new_name );
348 eachSymbol->SetLibId( libId );
349 eachSymbol->ClearFlags();
350 aRescuer->LogRescue( eachSymbol, m_requested_name, m_new_name );
351 }
352
353 return true;
354}
355
356
358 const LIB_ID& aNewId,
359 LIB_SYMBOL* aCacheCandidate,
360 LIB_SYMBOL* aLibCandidate,
361 int aUnit, int aConvert ) :
363{
364 m_requested_id = aRequestedId;
365 m_requested_name = aRequestedId.Format().wx_str();
366 m_new_id = aNewId;
367 m_lib_candidate = aLibCandidate;
368 m_cache_candidate = aCacheCandidate;
369 m_unit = aUnit;
370 m_convert = aConvert;
371}
372
373
375{
376 m_cache_candidate = nullptr;
377 m_lib_candidate = nullptr;
378}
379
380
382 boost::ptr_vector<RESCUE_CANDIDATE>& aCandidates )
383{
384 std::map<LIB_ID, RESCUE_SYMBOL_LIB_TABLE_CANDIDATE> candidate_map;
385
386 // Remember the list of symbols is sorted by LIB_ID.
387 // So a search in libraries is made only once by group
388 LIB_SYMBOL* cache_match = nullptr;
389 LIB_SYMBOL* lib_match = nullptr;
390 LIB_ID old_symbol_id;
391
392 wxString symbolName;
393
394 for( SCH_SYMBOL* eachSymbol : *( aRescuer.GetSymbols() ) )
395 {
396 const LIB_ID& symbol_id = eachSymbol->GetLibId();
397
398 if( old_symbol_id != symbol_id )
399 {
400 // A new symbol name is found (a new group starts here).
401 // Search the symbol names candidates only once for this group:
402 old_symbol_id = symbol_id;
403
404 symbolName = symbol_id.Format().wx_str();
405
406 // Get the library symbol from the cache library. It will be a flattened
407 // symbol by default (no inheritance).
408 cache_match = findSymbol( symbolName, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ), true );
409
410 // At some point during V5 development, the LIB_ID delimiter character ':' was
411 // replaced by '_' when writing the symbol cache library so we have to test for
412 // the LIB_NICKNAME_LIB_SYMBOL_NAME case.
413 if( !cache_match )
414 {
415 symbolName.Printf( wxT( "%s-%s" ),
416 symbol_id.GetLibNickname().wx_str(),
417 symbol_id.GetLibItemName().wx_str() );
418 cache_match = findSymbol( symbolName, PROJECT_SCH::SchLibs( aRescuer.GetPrj() ),
419 true );
420 }
421
422 // Get the library symbol from the symbol library table.
423 lib_match = SchGetLibSymbol( symbol_id,
425
426 if( !cache_match && !lib_match )
427 continue;
428
429 LIB_SYMBOL_SPTR lib_match_parent;
430
431 // If it's a derived symbol, use the parent symbol to perform the pin test.
432 if( lib_match && lib_match->IsDerived() )
433 {
434 lib_match_parent = lib_match->GetRootSymbol();
435
436 if( !lib_match_parent )
437 lib_match = nullptr;
438 else
439 lib_match = lib_match_parent.get();
440 }
441
442 // Test whether there is a conflict or if the symbol can only be found in the cache.
443 if( LIB_ID::HasIllegalChars( symbol_id.GetLibItemName() ) == -1 )
444 {
445 if( cache_match && lib_match
446 && !cache_match->PinsConflictWith( *lib_match, true, true, true, true, false ) )
447 {
448 continue;
449 }
450
451 if( !cache_match && lib_match )
452 continue;
453 }
454
455 // Fix illegal LIB_ID name characters.
456 wxString new_name = EscapeString( symbol_id.GetLibItemName(), CTX_LIBID );
457
458 // Differentiate symbol name in the rescue library by appending the original symbol
459 // library table nickname to the symbol name to prevent name clashes in the rescue
460 // library.
461 wxString libNickname = GetRescueLibraryFileName( aRescuer.Schematic() ).GetName();
462
463 LIB_ID new_id( libNickname, wxString::Format( wxT( "%s-%s" ),
464 new_name,
465 symbol_id.GetLibNickname().wx_str() ) );
466
467 RESCUE_SYMBOL_LIB_TABLE_CANDIDATE candidate( symbol_id, new_id, cache_match, lib_match,
468 eachSymbol->GetUnit(),
469 eachSymbol->GetBodyStyle() );
470
471 candidate_map[symbol_id] = candidate;
472 }
473 }
474
475 // Now, dump the map into aCandidates
476 for( const auto& [name, candidate] : candidate_map )
477 aCandidates.push_back( new RESCUE_SYMBOL_LIB_TABLE_CANDIDATE( candidate ) );
478}
479
480
482{
483 wxString action;
484
486 {
487 action.Printf( _( "Cannot rescue symbol %s which is not available in any library or "
488 "the cache." ),
490 }
492 {
493 action.Printf( _( "Rescue symbol %s found only in cache library to %s." ),
496 }
497 else
498 {
499 action.Printf( _( "Rescue modified symbol %s to %s" ),
502 }
503
504 return action;
505}
506
507
509{
511
512 wxCHECK_MSG( tmp, false, wxS( "Both cache and library symbols undefined." ) );
513
514 std::unique_ptr<LIB_SYMBOL> new_symbol = tmp->Flatten();
515 new_symbol->SetLibId( m_new_id );
516 new_symbol->SetName( m_new_id.GetLibItemName() );
517 aRescuer->AddSymbol( new_symbol.get() );
518
519 for( SCH_SYMBOL* eachSymbol : *aRescuer->GetSymbols() )
520 {
521 if( eachSymbol->GetLibId() != m_requested_id )
522 continue;
523
524 eachSymbol->SetLibId( m_new_id );
525 eachSymbol->ClearFlags();
526 aRescuer->LogRescue( eachSymbol, m_requested_id.Format(), m_new_id.Format() );
527 }
528
529 return true;
530}
531
532
533RESCUER::RESCUER( PROJECT& aProject, SCHEMATIC* aSchematic, SCH_SHEET_PATH* aCurrentSheet,
534 EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType )
535{
536 m_schematic = aSchematic ? aSchematic : aCurrentSheet->LastScreen()->Schematic();
537
538 wxASSERT( m_schematic );
539
540 if( m_schematic )
542
543 m_prj = &aProject;
544 m_currentSheet = aCurrentSheet;
545 m_galBackEndType = aGalBackEndType;
546}
547
548
549void RESCUER::LogRescue( SCH_SYMBOL* aSymbol, const wxString &aOldName,
550 const wxString &aNewName )
551{
552 RESCUE_LOG logitem;
553 logitem.symbol = aSymbol;
554 logitem.old_name = aOldName;
555 logitem.new_name = aNewName;
556 m_rescue_log.push_back( logitem );
557}
558
559
561{
562 for( RESCUE_CANDIDATE* each_candidate : m_chosen_candidates )
563 {
564 if( ! each_candidate->PerformAction( this ) )
565 return false;
566 }
567
568 return true;
569}
570
571
573{
574 for( RESCUE_LOG& each_logitem : m_rescue_log )
575 {
576 LIB_ID libId;
577
578 libId.SetLibItemName( each_logitem.old_name );
579 each_logitem.symbol->SetLibId( libId );
580 each_logitem.symbol->ClearFlags();
581 }
582}
583
584
585bool RESCUER::RescueProject( wxWindow* aParent, RESCUER& aRescuer, bool aRunningOnDemand )
586{
587 aRescuer.FindCandidates();
588
589 if( !aRescuer.GetCandidateCount() )
590 {
591 if( aRunningOnDemand )
592 {
593 wxMessageDialog dlg( aParent, _( "This project has nothing to rescue." ),
594 _( "Project Rescue Helper" ) );
595 dlg.ShowModal();
596 }
597
598 return true;
599 }
600
601 aRescuer.RemoveDuplicates();
602 aRescuer.InvokeDialog( aParent, !aRunningOnDemand );
603
604 // If no symbols were rescued, let the user know what's going on. He might
605 // have clicked cancel by mistake, and should have some indication of that.
606 if( !aRescuer.GetChosenCandidateCount() )
607 {
608 wxMessageDialog dlg( aParent, _( "No symbols were rescued." ),
609 _( "Project Rescue Helper" ) );
610 dlg.ShowModal();
611
612 // Set the modified flag even on Cancel. Many users seem to instinctively want to Save at
613 // this point, due to the reloading of the symbols, so we'll make the save button active.
614 return true;
615 }
616
617 aRescuer.OpenRescueLibrary();
618
619 if( !aRescuer.DoRescues() )
620 {
621 aRescuer.UndoRescues();
622 return false;
623 }
624
625 aRescuer.WriteRescueLibrary( aParent );
626
627 return true;
628}
629
630
632{
633 std::vector<wxString> names_seen;
634
635 for( boost::ptr_vector<RESCUE_CANDIDATE>::iterator it = m_all_candidates.begin();
636 it != m_all_candidates.end(); )
637 {
638 bool seen_already = false;
639
640 for( wxString& name_seen : names_seen )
641 {
642 if( name_seen == it->GetRequestedName() )
643 {
644 seen_already = true;
645 break;
646 }
647 }
648
649 if( seen_already )
650 {
651 it = m_all_candidates.erase( it );
652 }
653 else
654 {
655 names_seen.push_back( it->GetRequestedName() );
656 ++it;
657 }
658 }
659}
660
661
663{
666}
667
668
669void LEGACY_RESCUER::InvokeDialog( wxWindow* aParent, bool aAskShowAgain )
670{
671 InvokeDialogRescueEach( aParent, static_cast< RESCUER& >( *this ), m_currentSheet,
672 m_galBackEndType, aAskShowAgain );
673}
674
675
677{
678 wxFileName fn = GetRescueLibraryFileName( m_schematic );
679
680 std::unique_ptr<SYMBOL_LIB> rescue_lib =
681 std::make_unique<SYMBOL_LIB>( SCH_LIB_TYPE::LT_EESCHEMA, fn.GetFullPath() );
682
683 m_rescue_lib = std::move( rescue_lib );
684 m_rescue_lib->EnableBuffering();
685
686 // If a rescue library already exists copy the contents of that library so we do not
687 // lose any previous rescues.
688 SYMBOL_LIB* rescueLib = PROJECT_SCH::SchLibs( m_prj )->FindLibrary( fn.GetName() );
689
690 if( rescueLib )
691 {
692 // For items in the rescue library, aliases are the root symbol.
693 std::vector< LIB_SYMBOL* > symbols;
694
695 rescueLib->GetSymbols( symbols );
696
697 for( LIB_SYMBOL* symbol : symbols )
698 {
699 // The LIB_SYMBOL copy constructor flattens derived symbols (formerly known as aliases).
700 m_rescue_lib->AddSymbol( new LIB_SYMBOL( *symbol, m_rescue_lib.get() ) );
701 }
702 }
703}
704
705
706bool LEGACY_RESCUER::WriteRescueLibrary( wxWindow *aParent )
707{
708 try
709 {
710 m_rescue_lib->Save( false );
711 }
712 catch( ... /* IO_ERROR ioe */ )
713 {
714 wxString msg;
715
716 msg.Printf( _( "Failed to create symbol library file '%s'." ),
717 m_rescue_lib->GetFullFileName() );
718 DisplayError( aParent, msg );
719 return false;
720 }
721
722 wxArrayString libNames;
723 wxString libPaths;
724
725 wxString libName = m_rescue_lib->GetName();
726 SYMBOL_LIBS* libs =
728
729 if( !libs )
730 {
731 libs = new SYMBOL_LIBS();
733 }
734
735 try
736 {
737 SYMBOL_LIBS::GetLibNamesAndPaths( m_prj, &libPaths, &libNames );
738
739 // Make sure the library is not already in the list
740 while( libNames.Index( libName ) != wxNOT_FOUND )
741 libNames.Remove( libName );
742
743 // Add the library to the top of the list and save.
744 libNames.Insert( libName, 0 );
745 SYMBOL_LIBS::SetLibNamesAndPaths( m_prj, libPaths, libNames );
746 }
747 catch( const IO_ERROR& )
748 {
749 // Could not get or save the current libraries.
750 return false;
751 }
752
753 // Save the old libraries in case there is a problem after clear(). We'll
754 // put them back in.
755 boost::ptr_vector<SYMBOL_LIB> libsSave;
756 libsSave.transfer( libsSave.end(), libs->begin(), libs->end(), *libs );
757
759
760 libs = new SYMBOL_LIBS();
761
762 try
763 {
764 libs->LoadAllLibraries( m_prj );
765 }
766 catch( const PARSE_ERROR& )
767 {
768 // Some libraries were not found. There's no point in showing the error,
769 // because it was already shown. Just don't do anything.
770 }
771 catch( const IO_ERROR& )
772 {
773 // Restore the old list
774 libs->clear();
775 libs->transfer( libs->end(), libsSave.begin(), libsSave.end(), libsSave );
776 return false;
777 }
778
780
781 // Update the schematic symbol library links since the library list has changed.
782 SCH_SCREENS schematic( m_schematic->Root() );
783 schematic.UpdateSymbolLinks();
784 return true;
785}
786
787
789{
790 wxCHECK_RET( aNewSymbol, wxS( "Invalid LIB_SYMBOL pointer." ) );
791
792 aNewSymbol->SetLib( m_rescue_lib.get() );
793 m_rescue_lib->AddSymbol( aNewSymbol );
794}
795
796
798 SCH_SHEET_PATH* aCurrentSheet,
799 EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType ) :
800 RESCUER( aProject, aSchematic, aCurrentSheet, aGalBackEndType )
801{
802 m_properties = std::make_unique<std::map<std::string, UTF8>>();
803}
804
805
807{
809}
810
811
812void SYMBOL_LIB_TABLE_RESCUER::InvokeDialog( wxWindow* aParent, bool aAskShowAgain )
813{
814 InvokeDialogRescueEach( aParent, static_cast< RESCUER& >( *this ), m_currentSheet,
815 m_galBackEndType, aAskShowAgain );
816}
817
818
820{
821 (*m_properties)[ SCH_IO_KICAD_LEGACY::PropBuffering ] = "";
822
823 wxFileName fn = GetRescueLibraryFileName( m_schematic );
824
826
827 // If a rescue library already exists copy the contents of that library so we do not
828 // lose any previous rescues.
829 if( row )
830 {
831 if( SCH_IO_MGR::EnumFromStr( row->GetType() ) == SCH_IO_MGR::SCH_KICAD )
833
834 std::vector<LIB_SYMBOL*> symbols;
835
836 try
837 {
838 PROJECT_SCH::SchSymbolLibTable( m_prj )->LoadSymbolLib( symbols, fn.GetName() );
839 }
840 catch( ... /* IO_ERROR */ )
841 {
842 return;
843 }
844
845 for( LIB_SYMBOL* symbol : symbols )
846 m_rescueLibSymbols.emplace_back( std::make_unique<LIB_SYMBOL>( *symbol ) );
847 }
848}
849
850
852{
853 wxString msg;
854 wxFileName fn = GetRescueLibraryFileName( m_schematic );
856
858
859 try
860 {
861 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
862
863 for( const std::unique_ptr<LIB_SYMBOL>& symbol : m_rescueLibSymbols )
864 pi->SaveSymbol( fn.GetFullPath(), new LIB_SYMBOL( *symbol.get() ), m_properties.get() );
865
866 pi->SaveLibrary( fn.GetFullPath() );
867 }
868 catch( const IO_ERROR& ioe )
869 {
870 msg.Printf( _( "Failed to save rescue library %s." ), fn.GetFullPath() );
871 DisplayErrorMessage( aParent, msg, ioe.What() );
872 return false;
873 }
874
875 // If the rescue library already exists in the symbol library table no need save it to add
876 // it to the table.
877 if( !row || ( SCH_IO_MGR::EnumFromStr( row->GetType() ) == SCH_IO_MGR::SCH_LEGACY ) )
878 {
879 wxString uri = wxS( "${KIPRJMOD}/" ) + fn.GetFullName();
880 wxString libNickname = fn.GetName();
881
882 row = new SYMBOL_LIB_TABLE_ROW( libNickname, uri, wxT( "KiCad" ) );
884
886
887 try
888 {
889 PROJECT_SCH::SchSymbolLibTable( m_prj )->Save( fn.GetFullPath() );
890 }
891 catch( const IO_ERROR& ioe )
892 {
893 msg.Printf( _( "Error occurred saving project specific symbol library table." ) );
894 DisplayErrorMessage( aParent, msg, ioe.What() );
895 return false;
896 }
897 }
898
900
901 // This can only happen if the symbol library table file was corrupted on write.
903 return false;
904
905 // Update the schematic symbol library links since the library list has changed.
906 SCH_SCREENS schematic( m_schematic->Root() );
907 schematic.UpdateSymbolLinks();
908 return true;
909}
910
911
913{
914 wxCHECK_RET( aNewSymbol, wxS( "Invalid LIB_SYMBOL pointer." ) );
915
916 m_rescueLibSymbols.emplace_back( std::make_unique<LIB_SYMBOL>( *aNewSymbol ) );
917}
const char * name
Definition: DXF_plotter.cpp:59
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual void FindCandidates() override
Populate the RESCUER with all possible candidates.
virtual bool WriteRescueLibrary(wxWindow *aParent) override
Write the rescue library.
virtual void InvokeDialog(wxWindow *aParent, bool aAskShowAgain) override
Display a dialog to allow the user to select rescues.
virtual void AddSymbol(LIB_SYMBOL *aNewSymbol) override
std::unique_ptr< SYMBOL_LIB > m_rescue_lib
virtual void OpenRescueLibrary() override
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:110
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
static int HasIllegalChars(const UTF8 &aLibItemName)
Examine aLibItemName for invalid LIB_ID item name characters.
Definition: lib_id.cpp:175
UTF8 Format() const
Definition: lib_id.cpp:118
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition: lib_id.h:112
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
Define a library symbol object.
Definition: lib_symbol.h:85
const LIB_ID & GetLibId() const override
Definition: lib_symbol.h:155
bool PinsConflictWith(const LIB_SYMBOL &aOtherSymbol, bool aTestNums, bool aTestNames, bool aTestType, bool aTestOrientation, bool aTestLength) const
Return true if this symbol's pins do not match another symbol's pins.
Definition: lib_symbol.cpp:846
bool IsDerived() const
Definition: lib_symbol.h:207
void SetLib(SYMBOL_LIB *aLibrary)
Definition: lib_symbol.h:212
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
Definition: lib_symbol.cpp:335
LIB_SYMBOL_SPTR GetRootSymbol() const
Get the parent symbol that does not have another parent.
Definition: lib_symbol.cpp:267
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
static SYMBOL_LIBS * SchLibs(PROJECT *aProject)
These are all prefaced with "Sch".
Definition: project_sch.cpp:90
Container for project specific data.
Definition: project.h:64
virtual void SetElem(PROJECT::ELEM aIndex, _ELEM *aElem)
Definition: project.cpp:359
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:146
virtual _ELEM * GetElem(PROJECT::ELEM aIndex)
Get and set the elements for this project.
Definition: project.cpp:348
virtual void AddSymbol(LIB_SYMBOL *aNewSymbol)=0
virtual bool WriteRescueLibrary(wxWindow *aParent)=0
Write the rescue library.
std::vector< RESCUE_LOG > m_rescue_log
SCHEMATIC * m_schematic
PROJECT * m_prj
void UndoRescues()
Reverse the effects of all rescues on the project.
bool DoRescues()
Perform all chosen rescue actions, logging them to be undone if necessary.
std::vector< SCH_SYMBOL * > m_symbols
static bool RescueProject(wxWindow *aParent, RESCUER &aRescuer, bool aRunningOnDemand)
void LogRescue(SCH_SYMBOL *aSymbol, const wxString &aOldName, const wxString &aNewName)
Used by individual RESCUE_CANDIDATE objects to log a rescue for undoing.
SCH_SHEET_PATH * m_currentSheet
std::vector< RESCUE_CANDIDATE * > m_chosen_candidates
std::vector< SCH_SYMBOL * > * GetSymbols()
Get the list of symbols that need rescued.
size_t GetCandidateCount()
Return the number of rescue candidates found.
PROJECT * GetPrj()
Return the #SCH_PROJECT object for access to the symbol libraries.
virtual void InvokeDialog(wxWindow *aParent, bool aAskShowAgain)=0
Display a dialog to allow the user to select rescues.
SCHEMATIC * Schematic()
EDA_DRAW_PANEL_GAL::GAL_TYPE m_galBackEndType
RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
virtual void OpenRescueLibrary()=0
size_t GetChosenCandidateCount()
Get the number of rescue candidates chosen by the user.
boost::ptr_vector< RESCUE_CANDIDATE > m_all_candidates
virtual void FindCandidates()=0
Populate the RESCUER with all possible candidates.
void RemoveDuplicates()
Filter out duplicately named rescue candidates.
LIB_SYMBOL * m_cache_candidate
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_CACHE_CANDIDATE objects into a vector.
virtual wxString GetActionDescription() const override
Get a description of the action proposed, for displaying in the UI.
virtual bool PerformAction(RESCUER *aRescuer) override
Perform the actual rescue action.
wxString m_requested_name
LIB_SYMBOL * m_lib_candidate
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_CASE_CANDIDATE objects into a vector.
virtual wxString GetActionDescription() const override
Get a description of the action proposed, for displaying in the UI.
virtual bool PerformAction(RESCUER *aRescuer) override
Perform the actual rescue action.
wxString old_name
wxString new_name
SCH_SYMBOL * symbol
virtual bool PerformAction(RESCUER *aRescuer) override
Perform the actual rescue action.
virtual wxString GetActionDescription() const override
Get a description of the action proposed, for displaying in the UI.
static void FindRescues(RESCUER &aRescuer, boost::ptr_vector< RESCUE_CANDIDATE > &aCandidates)
Grab all possible RESCUE_SYMBOL_LIB_TABLE_CANDIDATE objects into a vector.
Holds all the data relating to one schematic.
Definition: schematic.h:69
wxString GetFileName() const
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:300
SCH_SHEET & Root() const
Definition: schematic.h:117
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the #SCH_FILE_T from the corresponding plugin type name: "kicad", "legacy",...
Definition: sch_io_mgr.cpp:107
int GetUnit() const
Definition: sch_item.h:233
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:710
SCH_SCREEN * GetNext()
void UpdateSymbolLinks(REPORTER *aReporter=nullptr)
Initialize the LIB_SYMBOL reference for each SCH_SYMBOL found in the full schematic.
SCH_SCREEN * GetFirst()
SCHEMATIC * Schematic() const
Definition: sch_screen.cpp:102
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SCREEN * LastScreen()
Schematic symbol object.
Definition: sch_symbol.h:77
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:166
A collection of SYMBOL_LIB objects.
void FindLibraryNearEntries(std::vector< LIB_SYMBOL * > &aCandidates, const wxString &aEntryName, const wxString &aLibraryName=wxEmptyString)
Search all libraries in the list for a LIB_SYMBOL using a case insensitive comparison.
static void SetLibNamesAndPaths(PROJECT *aProject, const wxString &aPaths, const wxArrayString &aNames)
SYMBOL_LIB * FindLibrary(const wxString &aName)
Find a symbol library by aName.
void LoadAllLibraries(PROJECT *aProject, bool aShowProgress=true)
Load all of the project's libraries into this container, which should be cleared before calling it.
LIB_SYMBOL * FindLibSymbol(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Search all libraries in the list for a symbol.
static void GetLibNamesAndPaths(PROJECT *aProject, wxString *aPaths, wxArrayString *aNames=nullptr)
virtual void OpenRescueLibrary() override
virtual bool WriteRescueLibrary(wxWindow *aParent) override
Write the rescue library.
SYMBOL_LIB_TABLE_RESCUER(PROJECT &aProject, SCHEMATIC *aSchematic, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackeEndType)
virtual void InvokeDialog(wxWindow *aParent, bool aAskShowAgain) override
Display a dialog to allow the user to select rescues.
std::unique_ptr< std::map< std::string, UTF8 > > m_properties
Library plugin properties.
virtual void FindCandidates() override
Populate the RESCUER with all possible candidates.
virtual void AddSymbol(LIB_SYMBOL *aNewSymbol) override
std::vector< std::unique_ptr< LIB_SYMBOL > > m_rescueLibSymbols
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_IO object i...
const wxString GetType() const override
Return the type of symbol library table represented by this row.
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
static const wxString GetSymbolLibTableFileName()
SYMBOL_LIB_TABLE_ROW * FindRow(const wxString &aNickName, bool aCheckIfEnabled=false)
Return an SYMBOL_LIB_TABLE_ROW if aNickName is found in this table or in any chained fallBack table f...
Object used to load, save, search, and otherwise manipulate symbol library files.
void GetSymbols(std::vector< LIB_SYMBOL * > &aSymbols) const
Load a vector with all the entries in this library.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:72
wxString wx_str() const
Definition: utf8.cpp:45
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
int InvokeDialogRescueEach(wxWindow *aParent, RESCUER &aRescuer, SCH_SHEET_PATH *aCurrentSheet, EDA_DRAW_PANEL_GAL::GAL_TYPE aGalBackEndType, bool aAskShowAgain)
This dialog asks the user which rescuable, cached parts he wants to rescue.
#define _(s)
static const std::string LegacySymbolLibFileExtension
static const std::string KiCadSymbolLibFileExtension
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition: io_mgr.h:33
std::shared_ptr< LIB_SYMBOL > LIB_SYMBOL_SPTR
shared pointer to LIB_SYMBOL
Definition: lib_symbol.h:52
static bool sort_by_libid(const SCH_SYMBOL *ref, SCH_SYMBOL *cmp)
static LIB_SYMBOL * findSymbol(const wxString &aName, SYMBOL_LIBS *aLibs, bool aCached)
Search the libraries for the first symbol with a given name.
static void getSymbols(SCHEMATIC *aSchematic, std::vector< SCH_SYMBOL * > &aSymbols)
Fill a vector with all of the project's symbols, to ease iterating over them.
static wxFileName GetRescueLibraryFileName(SCHEMATIC *aSchematic)
LIB_SYMBOL * SchGetLibSymbol(const LIB_ID &aLibId, SYMBOL_LIB_TABLE *aLibTable, SYMBOL_LIB *aCacheLib, wxWindow *aParent, bool aShowErrorMsg)
Load symbol from symbol library table.
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:...
@ CTX_LIBID
Definition: string_utils.h:54
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:120
Definition for symbol library class.
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
Definition of file extensions used in Kicad.