KiCad PCB EDA Suite
Loading...
Searching...
No Matches
legacy_symbol_library.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) 2004-2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright (C) 2022 CERN
7 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <algorithm>
24#include <kiface_base.h>
25#include <eda_base_frame.h>
26#include <string_utils.h>
27#include <macros.h>
28#include <richio.h>
31#include <project_rescue.h>
32#include <project_sch.h>
34
37
38#include <wx/log.h>
39#include <wx/progdlg.h>
40#include <wx/tokenzr.h>
41#include <sim/sim_model.h>
42
43LEGACY_SYMBOL_LIB::LEGACY_SYMBOL_LIB( SCH_LIB_TYPE aType, const wxString& aFileName,
44 SCH_IO_MGR::SCH_FILE_T aPluginType ) :
45 m_pluginType( aPluginType )
46{
47 type = aType;
48 isModified = false;
49 timeStamp = 0;
50 timeStamp = wxDateTime::Now();
51 versionMajor = 0; // Will be updated after reading the lib file
52 versionMinor = 0; // Will be updated after reading the lib file
53
54 fileName = aFileName;
55
56 if( !fileName.IsOk() )
57 fileName = "unnamed.lib";
58
59 m_plugin.reset( SCH_IO_MGR::FindPlugin( m_pluginType ) );
60 m_properties = std::make_unique<std::map<std::string, UTF8>>();
61 m_mod_hash = 0;
62}
63
64
68
69
70void LEGACY_SYMBOL_LIB::Save( bool aSaveDocFile )
71{
72 wxCHECK_RET( m_plugin != nullptr,
73 wxString::Format( wxT( "no plugin defined for library `%s`." ),
74 fileName.GetFullPath() ) );
75
76 std::map<std::string, UTF8> props;
77
78 if( !aSaveDocFile )
80
81 m_plugin->SaveLibrary( fileName.GetFullPath(), &props );
82 isModified = false;
83}
84
85
86void LEGACY_SYMBOL_LIB::Create( const wxString& aFileName )
87{
88 wxString tmpFileName = fileName.GetFullPath();
89
90 if( !aFileName.IsEmpty() )
91 tmpFileName = aFileName;
92
93 m_plugin->CreateLibrary( tmpFileName, m_properties.get() );
94}
95
96
97void LEGACY_SYMBOL_LIB::SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType )
98{
99 if( m_pluginType != aPluginType )
100 {
101 m_pluginType = aPluginType;
102 m_plugin.reset( SCH_IO_MGR::FindPlugin( m_pluginType ) );
103 }
104}
105
106
108{
110}
111
112
114{
115 (*m_properties)[ SCH_IO_KICAD_LEGACY::PropNoDocFile ] = "";
116}
117
118
123
124
126{
127 if( aEnable )
128 (*m_properties)[ SCH_IO_KICAD_LEGACY::PropBuffering ] = "";
129 else
131}
132
133
134void LEGACY_SYMBOL_LIB::GetSymbolNames( wxArrayString& aNames ) const
135{
136 m_plugin->EnumerateSymbolLib( aNames, fileName.GetFullPath(), m_properties.get() );
137
138 aNames.Sort();
139}
140
141
142void LEGACY_SYMBOL_LIB::GetSymbols( std::vector<LIB_SYMBOL*>& aSymbols ) const
143{
144 m_plugin->EnumerateSymbolLib( aSymbols, fileName.GetFullPath(), m_properties.get() );
145
146 std::sort( aSymbols.begin(), aSymbols.end(),
147 [](LIB_SYMBOL *lhs, LIB_SYMBOL *rhs) -> bool
148 {
149 return lhs->GetName() < rhs->GetName();
150 } );
151}
152
153
154LIB_SYMBOL* LEGACY_SYMBOL_LIB::FindSymbol( const wxString& aName ) const
155{
156 LIB_SYMBOL* symbol = m_plugin->LoadSymbol( fileName.GetFullPath(), aName, m_properties.get() );
157
158 if( symbol )
159 {
160 // Set the library to this even though technically the legacy cache plugin owns the
161 // symbols. This allows the symbol library table conversion tool to determine the
162 // correct library where the symbol was found.
163 if( !symbol->GetLib() )
164 symbol->SetLib( const_cast<LEGACY_SYMBOL_LIB*>( this ) );
165
166 SIM_MODEL::MigrateSimModel<LIB_SYMBOL>( *symbol, nullptr );
167 }
168
169 return symbol;
170}
171
172
174{
175 return FindSymbol( aLibId.Format().wx_str() );
176}
177
178
180{
181 // add a clone, not the caller's copy, the plugin take ownership of the new symbol.
182 m_plugin->SaveSymbol( fileName.GetFullPath(),
183 new LIB_SYMBOL( *aSymbol->SharedPtr().get(), this ),
184 m_properties.get() );
185
186 // If we are not buffering, the library file is updated immediately when the plugin
187 // SaveSymbol() function is called.
188 if( IsBuffering() )
189 isModified = true;
190
191 ++m_mod_hash;
192}
193
194
196{
197 wxCHECK_MSG( aEntry != nullptr, nullptr, "NULL pointer cannot be removed from library." );
198
199 m_plugin->DeleteSymbol( fileName.GetFullPath(), aEntry->GetName(), m_properties.get() );
200
201 // If we are not buffering, the library file is updated immediately when the plugin
202 // SaveSymbol() function is called.
203 if( IsBuffering() )
204 isModified = true;
205
206 ++m_mod_hash;
207 return nullptr;
208}
209
210
212{
213 wxASSERT( aOldSymbol != nullptr );
214 wxASSERT( aNewSymbol != nullptr );
215
216 m_plugin->DeleteSymbol( fileName.GetFullPath(), aOldSymbol->GetName(), m_properties.get() );
217
218 LIB_SYMBOL* my_part = new LIB_SYMBOL( *aNewSymbol, this );
219
220 m_plugin->SaveSymbol( fileName.GetFullPath(), my_part, m_properties.get() );
221
222 // If we are not buffering, the library file is updated immediately when the plugin
223 // SaveSymbol() function is called.
224 if( IsBuffering() )
225 isModified = true;
226
227 ++m_mod_hash;
228 return my_part;
229}
230
231
233{
234 std::unique_ptr<LEGACY_SYMBOL_LIB> lib = std::make_unique<LEGACY_SYMBOL_LIB>( SCH_LIB_TYPE::LT_EESCHEMA,
235 aFileName );
236
237 std::vector<LIB_SYMBOL*> parts;
238 // This loads the library.
239 lib->GetSymbols( parts );
240
241 // Now, set the LIB_SYMBOL m_library member but it will only be used
242 // when loading legacy libraries in the future. Once the symbols in the
243 // schematic have a full #LIB_ID, this will not get called.
244 for( size_t ii = 0; ii < parts.size(); ii++ )
245 {
246 LIB_SYMBOL* part = parts[ii];
247
248 part->SetLib( lib.get() );
249 }
250
251 LEGACY_SYMBOL_LIB* ret = lib.release();
252 return ret;
253}
254
255
257{
259
260 wxFileName fn = aFileName;
261 // Don't reload the library if it is already loaded.
262 lib = FindLibrary( fn.GetName() );
263
264 if( lib )
265 return lib;
266
267 try
268 {
269 lib = LEGACY_SYMBOL_LIB::LoadSymbolLibrary( aFileName );
270 push_back( lib );
271
272 return lib;
273 }
274 catch( ... )
275 {
276 return nullptr;
277 }
278}
279
280
281LEGACY_SYMBOL_LIB* LEGACY_SYMBOL_LIBS::AddLibrary( const wxString& aFileName, LEGACY_SYMBOL_LIBS::iterator& aIterator )
282{
283 // Don't reload the library if it is already loaded.
284 wxFileName fn( aFileName );
285 LEGACY_SYMBOL_LIB* lib = FindLibrary( fn.GetName() );
286
287 if( lib )
288 return lib;
289
290 try
291 {
292 lib = LEGACY_SYMBOL_LIB::LoadSymbolLibrary( aFileName );
293
294 if( aIterator >= begin() && aIterator < end() )
295 insert( aIterator, lib );
296 else
297 push_back( lib );
298
299 return lib;
300 }
301 catch( ... )
302 {
303 return nullptr;
304 }
305}
306
307
308bool LEGACY_SYMBOL_LIBS::ReloadLibrary( const wxString &aFileName )
309{
310 wxFileName fn = aFileName;
311 LEGACY_SYMBOL_LIB* lib = FindLibrary( fn.GetName() );
312
313 // Check if the library already exists.
314 if( !lib )
315 return false;
316
317 // Create a clone of the library pointer in case we need to re-add it
318 LEGACY_SYMBOL_LIB *cloneLib = lib;
319
320 // Try to find the iterator of the library
321 for( auto it = begin(); it != end(); ++it )
322 {
323 if( it->GetName() == fn.GetName() )
324 {
325 // Remove the old library and keep the pointer
326 lib = &*it;
327 release( it );
328 break;
329 }
330 }
331
332 // Try to reload the library
333 try
334 {
335 lib = LEGACY_SYMBOL_LIB::LoadSymbolLibrary( aFileName );
336
337 // If the library is successfully reloaded, add it back to the set.
338 push_back( lib );
339 return true;
340 }
341 catch( ... )
342 {
343 // If an exception occurs, ensure that the SYMBOL_LIBS remains unchanged
344 // by re-adding the old library back to the set.
345 push_back( cloneLib );
346 return false;
347 }
348}
349
350
352{
353 for( LEGACY_SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
354 {
355 if( it->GetName() == aName )
356 return &*it;
357 }
358
359 return nullptr;
360}
361
362
364{
365 for( LEGACY_SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
366 {
367 if( it->IsCache() )
368 return &*it;
369 }
370
371 return nullptr;
372}
373
374
376{
377 for( LEGACY_SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
378 {
379 if( it->GetFullFileName() == aFullFileName )
380 return &*it;
381 }
382
383 return nullptr;
384}
385
386
387wxArrayString LEGACY_SYMBOL_LIBS::GetLibraryNames( bool aSorted )
388{
389 wxArrayString cacheNames;
390 wxArrayString names;
391
392 for( LEGACY_SYMBOL_LIB& lib : *this )
393 {
394 if( lib.IsCache() && aSorted )
395 cacheNames.Add( lib.GetName() );
396 else
397 names.Add( lib.GetName() );
398 }
399
400 // Even sorted, the cache library is always at the end of the list.
401 if( aSorted )
402 names.Sort();
403
404 for( unsigned int i = 0; i<cacheNames.Count(); i++ )
405 names.Add( cacheNames.Item( i ) );
406
407 return names;
408}
409
410
411LIB_SYMBOL* LEGACY_SYMBOL_LIBS::FindLibSymbol( const LIB_ID& aLibId, const wxString& aLibraryName )
412{
413 LIB_SYMBOL* part = nullptr;
414
415 for( LEGACY_SYMBOL_LIB& lib : *this )
416 {
417 if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
418 continue;
419
420 part = lib.FindSymbol( aLibId.GetLibItemName().wx_str() );
421
422 if( part )
423 break;
424 }
425
426 return part;
427}
428
429
430void LEGACY_SYMBOL_LIBS::FindLibraryNearEntries( std::vector<LIB_SYMBOL*>& aCandidates,
431 const wxString& aEntryName,
432 const wxString& aLibraryName )
433{
434 for( LEGACY_SYMBOL_LIB& lib : *this )
435 {
436 if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
437 continue;
438
439 wxArrayString partNames;
440
441 lib.GetSymbolNames( partNames );
442
443 if( partNames.IsEmpty() )
444 continue;
445
446 for( size_t i = 0; i < partNames.size(); i++ )
447 {
448 if( partNames[i].CmpNoCase( aEntryName ) == 0 )
449 aCandidates.push_back( lib.FindSymbol( partNames[i] ) );
450 }
451 }
452}
453
454
455void LEGACY_SYMBOL_LIBS::GetLibNamesAndPaths( PROJECT* aProject, wxString* aPaths, wxArrayString* aNames )
456{
457 wxCHECK_RET( aProject, "Null PROJECT in GetLibNamesAndPaths" );
458
459 PROJECT_FILE& project = aProject->GetProjectFile();
460
461 if( aPaths )
462 *aPaths = project.m_LegacyLibDir;
463
464 if( aNames )
465 *aNames = project.m_LegacyLibNames;
466}
467
468
469void LEGACY_SYMBOL_LIBS::SetLibNamesAndPaths( PROJECT* aProject, const wxString& aPaths,
470 const wxArrayString& aNames )
471{
472 wxCHECK_RET( aProject, "Null PROJECT in SetLibNamesAndPaths" );
473
474 PROJECT_FILE& project = aProject->GetProjectFile();
475
476 project.m_LegacyLibDir = aPaths;
477 project.m_LegacyLibNames = aNames;
478}
479
480
481const wxString LEGACY_SYMBOL_LIBS::CacheName( const wxString& aFullProjectFilename )
482{
483 wxFileName filename( aFullProjectFilename );
484 wxString name = filename.GetName();
485
486 filename.SetName( name + "-cache" );
487 filename.SetExt( FILEEXT::LegacySymbolLibFileExtension );
488
489 if( filename.FileExists() )
490 return filename.GetFullPath();
491
492 // Try the old (2007) cache name
493 filename.SetName( name + ".cache" );
494
495 if( filename.FileExists() )
496 return filename.GetFullPath();
497
498 return wxEmptyString;
499}
500
501
502void LEGACY_SYMBOL_LIBS::LoadAllLibraries( PROJECT* aProject, bool aShowProgress )
503{
504 wxString filename;
505 wxString libs_not_found;
506 SEARCH_STACK* lib_search = PROJECT_SCH::SchSearchS( aProject );
507
508#if defined(DEBUG) && 0
509 lib_search->Show( __func__ );
510#endif
511
512 wxArrayString lib_names;
513
514 GetLibNamesAndPaths( aProject, nullptr, &lib_names );
515
516 // Post symbol library table, this should be empty. Only the cache library should get loaded.
517 if( !lib_names.empty() )
518 {
519 APP_PROGRESS_DIALOG lib_dialog( _( "Loading Symbol Libraries" ),
520 wxEmptyString,
521 lib_names.GetCount(),
522 nullptr,
523 false,
524 wxPD_APP_MODAL );
525
526 if( aShowProgress )
527 {
528 lib_dialog.Show();
529 }
530
531 for( unsigned i = 0; i < lib_names.GetCount(); ++i )
532 {
533 if( aShowProgress )
534 {
535 lib_dialog.Update( i, wxString::Format( _( "Loading %s..." ), lib_names[i] ) );
536 }
537
538 // lib_names[] does not store the file extension. Set it.
539 // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
540 // before adding the extension can create incorrect full filename
541 wxString fullname = lib_names[i] + "." + FILEEXT::LegacySymbolLibFileExtension;
542
543 // Now the full name is set, we can use a wxFileName.
544 wxFileName fn( fullname );
545
546 // Skip if the file name is not valid..
547 if( !fn.IsOk() )
548 continue;
549
550 if( !fn.FileExists() )
551 {
552 filename = lib_search->FindValidPath( fn.GetFullPath() );
553
554 if( !filename )
555 {
556 libs_not_found += fn.GetFullPath();
557 libs_not_found += '\n';
558 continue;
559 }
560 }
561 else
562 { // ensure the lib filename has a absolute path.
563 // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
564 // make a full absolute path, to avoid issues with load library functions which
565 // expects an absolute path.
566 if( !fn.IsAbsolute() )
567 fn.MakeAbsolute();
568
569 filename = fn.GetFullPath();
570 }
571
572 try
573 {
574 AddLibrary( filename );
575 }
576 catch( const IO_ERROR& ioe )
577 {
578 wxString msg;
579 msg.Printf( _( "Symbol library '%s' failed to load." ), filename );
580
581 wxLogError( msg + wxS( "\n" ) + ioe.What() );
582 }
583 }
584 }
585
586 // add the special cache library.
587 wxString cache_name = CacheName( aProject->GetProjectFullName() );
588 LEGACY_SYMBOL_LIB* cache_lib;
589
590 if( !aProject->IsNullProject() && !cache_name.IsEmpty() )
591 {
592 try
593 {
594 cache_lib = AddLibrary( cache_name );
595
596 if( cache_lib )
597 cache_lib->SetCache();
598 }
599 catch( const IO_ERROR& ioe )
600 {
601 wxString msg = wxString::Format( _( "Error loading symbol library '%s'." )
602 + wxS( "\n%s" ),
603 cache_name,
604 ioe.What() );
605
606 THROW_IO_ERROR( msg );
607 }
608 }
609
610 // Print the libraries not found
611 if( !libs_not_found.IsEmpty() )
612 {
613 // Use a different exception type so catch()er can route to proper use
614 // of the HTML_MESSAGE_BOX.
615 THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
616 }
617}
const char * name
wxProgressDialog with the option to also update the application progress on the taskbar
virtual bool Update(int aValue, const wxString &aNewMsg=wxEmptyString, bool *aSkip=nullptr) override
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
LEGACY_SYMBOL_LIB * GetCacheLibrary()
wxArrayString GetLibraryNames(bool aSorted=true)
Return the list of symbol library file names without path and extension.
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.
LEGACY_SYMBOL_LIB * FindLibraryByFullFileName(const wxString &aFullFileName)
static void GetLibNamesAndPaths(PROJECT *aProject, wxString *aPaths, wxArrayString *aNames=nullptr)
void LoadAllLibraries(PROJECT *aProject, bool aShowProgress=true)
Load all of the project's libraries into this container, which should be cleared before calling it.
static void SetLibNamesAndPaths(PROJECT *aProject, const wxString &aPaths, const wxArrayString &aNames)
LEGACY_SYMBOL_LIB * FindLibrary(const wxString &aName)
Find a symbol library by aName.
LIB_SYMBOL * FindLibSymbol(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Search all libraries in the list for a symbol.
LEGACY_SYMBOL_LIB * AddLibrary(const wxString &aFileName)
Allocate and adds a symbol library to the library list.
bool ReloadLibrary(const wxString &aFileName)
Refreshes the library from the (possibly updated) contents on disk.
static const wxString CacheName(const wxString &aFullProjectFilename)
Return the name of the cache library after potentially fixing it from an older naming scheme.
Object used to load, save, search, and otherwise manipulate symbol library files.
static LEGACY_SYMBOL_LIB * LoadSymbolLibrary(const wxString &aFileName)
Allocate and load a symbol library file.
wxFileName fileName
Library file name.
int versionMajor
Library major version number.
std::unique_ptr< std::map< std::string, UTF8 > > m_properties
Library properties.
void EnableBuffering(bool aEnable=true)
LIB_SYMBOL * ReplaceSymbol(LIB_SYMBOL *aOldSymbol, LIB_SYMBOL *aNewSymbol)
Replace an existing symbol entry in the library.
wxDateTime timeStamp
Library save time and date.
std::unique_ptr< SCH_IO > m_plugin
SCH_IO_MGR::SCH_FILE_T m_pluginType
LIB_SYMBOL * FindSymbol(const wxString &aName) const
Find LIB_SYMBOL by aName.
LIB_SYMBOL * RemoveSymbol(LIB_SYMBOL *aEntry)
Safely remove aEntry from the library and return the next entry.
void Create(const wxString &aFileName=wxEmptyString)
int m_mod_hash
incremented each time library is changed.
LEGACY_SYMBOL_LIB(SCH_LIB_TYPE aType, const wxString &aFileName, SCH_IO_MGR::SCH_FILE_T aPluginType=SCH_IO_MGR::SCH_LEGACY)
void GetSymbolNames(wxArrayString &aNames) const
Load a string array with the names of all the entries in this library.
void AddSymbol(LIB_SYMBOL *aSymbol)
Add aSymbol entry to library.
const wxString GetName() const
Return the file name without path or extension.
void Save(bool aSaveDocFile=true)
bool isModified
Library modification status.
void GetSymbols(std::vector< LIB_SYMBOL * > &aSymbols) const
Load a vector with all the entries in this library.
int versionMinor
Library minor version number.
void SetPluginType(SCH_IO_MGR::SCH_FILE_T aPluginType)
SCH_LIB_TYPE type
Library type indicator.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
UTF8 Format() const
Definition lib_id.cpp:115
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
Define a library symbol object.
Definition lib_symbol.h:79
void SetLib(LEGACY_SYMBOL_LIB *aLibrary)
LEGACY_SYMBOL_LIB * GetLib() const
Definition lib_symbol.h:200
wxString GetName() const override
Definition lib_symbol.h:141
std::shared_ptr< LIB_SYMBOL > SharedPtr() const
http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/sp_techniques.html#weak_without_shared.
Definition lib_symbol.h:88
The backing store for a PROJECT, in JSON format.
static SEARCH_STACK * SchSearchS(PROJECT *aProject)
Accessor for Eeschema search stack.
Container for project specific data.
Definition project.h:62
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition project.cpp:177
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:200
virtual bool IsNullProject() const
Check if this project is a null project (i.e.
Definition project.cpp:201
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
static const char * PropNoDocFile
The property used internally by the plugin to disable writing the library documentation (....
Look for files in a number of paths.
static void MigrateSimModel(T &aSymbol, const PROJECT *aProject)
wxString wx_str() const
Definition utf8.cpp:41
#define _(s)
Base window classes and related definitions.
static const std::string LegacySymbolLibFileExtension
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
table push_back({ "Source", "Layer", "Vertices", "Strategy", "Build(us)", "ns/query", "Mquery/s", "Inside" })
VECTOR2I end
Definition of file extensions used in Kicad.