KiCad PCB EDA Suite
SYMBOL_LIBS Class Reference

A collection of SYMBOL_LIB objects. More...

#include <symbol_library.h>

Inheritance diagram for SYMBOL_LIBS:
PROJECT::_ELEM

Public Member Functions

KICAD_T Type () override
 
 SYMBOL_LIBS ()
 
SYMBOL_LIBAddLibrary (const wxString &aFileName)
 Allocate and adds a symbol library to the library list. More...
 
SYMBOL_LIBAddLibrary (const wxString &aFileName, SYMBOL_LIBS::iterator &aIterator)
 Insert a symbol library into the library list. More...
 
void LoadAllLibraries (PROJECT *aProject, bool aShowProgress=true)
 Load all of the project's libraries into this container, which should be cleared before calling it. More...
 
SYMBOL_LIBFindLibrary (const wxString &aName)
 Find a symbol library by aName. More...
 
SYMBOL_LIBFindLibraryByFullFileName (const wxString &aFullFileName)
 
SYMBOL_LIBGetCacheLibrary ()
 
wxArrayString GetLibraryNames (bool aSorted=true)
 Return the list of symbol library file names without path and extension. More...
 
LIB_SYMBOLFindLibSymbol (const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
 Search all libraries in the list for a symbol. More...
 
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. More...
 
int GetLibraryCount ()
 

Static Public Member Functions

static void GetLibNamesAndPaths (PROJECT *aProject, wxString *aPaths, wxArrayString *aNames=nullptr)
 
static void SetLibNamesAndPaths (PROJECT *aProject, const wxString &aPaths, const wxArrayString &aNames)
 
static const wxString CacheName (const wxString &aFullProjectFilename)
 Return the name of the cache library after potentially fixing it from an older naming scheme. More...
 

Detailed Description

A collection of SYMBOL_LIB objects.

It extends from PROJECT::_ELEM so it can be hung in the PROJECT. It does not use any UI calls, but rather simply throws an IO_ERROR when there is a problem.

Definition at line 62 of file symbol_library.h.

Constructor & Destructor Documentation

◆ SYMBOL_LIBS()

SYMBOL_LIBS::SYMBOL_LIBS ( )
inline

Definition at line 67 of file symbol_library.h.

67{}

Member Function Documentation

◆ AddLibrary() [1/2]

SYMBOL_LIB * SYMBOL_LIBS::AddLibrary ( const wxString &  aFileName)

Allocate and adds a symbol library to the library list.

Parameters
aFileNameis the file name object of symbol library.
Exceptions
IO_ERRORif there's any problem loading.

Definition at line 262 of file symbol_library.cpp.

263{
264 SYMBOL_LIB* lib;
265
266 wxFileName fn = aFileName;
267 // Don't reload the library if it is already loaded.
268 lib = FindLibrary( fn.GetName() );
269
270 if( lib )
271 return lib;
272
273 try
274 {
275 lib = SYMBOL_LIB::LoadSymbolLibrary( aFileName );
276 push_back( lib );
277
278 return lib;
279 }
280 catch( ... )
281 {
282 return nullptr;
283 }
284}
SYMBOL_LIB * FindLibrary(const wxString &aName)
Find a symbol library by aName.
Object used to load, save, search, and otherwise manipulate symbol library files.
static SYMBOL_LIB * LoadSymbolLibrary(const wxString &aFileName)
Allocate and load a symbol library file.

References FindLibrary(), and SYMBOL_LIB::LoadSymbolLibrary().

Referenced by LoadAllLibraries().

◆ AddLibrary() [2/2]

SYMBOL_LIB * SYMBOL_LIBS::AddLibrary ( const wxString &  aFileName,
SYMBOL_LIBS::iterator &  aIterator 
)

Insert a symbol library into the library list.

Parameters
aFileNameis the file name object of symbol library.
aIteratoris an iterator to insert library in front of.
Returns
the new SYMBOL_LIB, which remains owned by this SYMBOL_LIBS container.
Exceptions
IO_ERRORif there's any problem loading.

Definition at line 287 of file symbol_library.cpp.

288{
289 // Don't reload the library if it is already loaded.
290 wxFileName fn( aFileName );
291 SYMBOL_LIB* lib = FindLibrary( fn.GetName() );
292
293 if( lib )
294 return lib;
295
296 try
297 {
298 lib = SYMBOL_LIB::LoadSymbolLibrary( aFileName );
299
300 if( aIterator >= begin() && aIterator < end() )
301 insert( aIterator, lib );
302 else
303 push_back( lib );
304
305 return lib;
306 }
307 catch( ... )
308 {
309 return nullptr;
310 }
311}

References FindLibrary(), and SYMBOL_LIB::LoadSymbolLibrary().

◆ CacheName()

const wxString SYMBOL_LIBS::CacheName ( const wxString &  aFullProjectFilename)
static

Return the name of the cache library after potentially fixing it from an older naming scheme.

That is, the old file is renamed if needed.

Parameters
aFullProjectFilenameis the *.pro filename with absolute path.

Definition at line 444 of file symbol_library.cpp.

445{
446 wxFileName name = aFullProjectFilename;
447
448 name.SetName( name.GetName() + "-cache" );
450
451 if( name.FileExists() )
452 return name.GetFullPath();
453
454 return wxEmptyString;
455}
const char * name
Definition: DXF_plotter.cpp:56
const std::string LegacySymbolLibFileExtension

References LegacySymbolLibFileExtension, and name.

Referenced by LoadAllLibraries().

◆ FindLibrary()

SYMBOL_LIB * SYMBOL_LIBS::FindLibrary ( const wxString &  aName)

Find a symbol library by aName.

Parameters
aNameis the library file name without path or extension to find.
Returns
the symbol library if found, otherwise NULL.

Definition at line 314 of file symbol_library.cpp.

315{
316 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
317 {
318 if( it->GetName() == aName )
319 return &*it;
320 }
321
322 return nullptr;
323}

Referenced by AddLibrary().

◆ FindLibraryByFullFileName()

SYMBOL_LIB * SYMBOL_LIBS::FindLibraryByFullFileName ( const wxString &  aFullFileName)

Definition at line 338 of file symbol_library.cpp.

339{
340 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
341 {
342 if( it->GetFullFileName() == aFullFileName )
343 return &*it;
344 }
345
346 return nullptr;
347}

◆ FindLibraryNearEntries()

void SYMBOL_LIBS::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.

Helper function used in dialog to find all candidates. During a long time, eeschema was using a case insensitive search. Therefore, for old schematics (<= 2013), or libs, for some symbols, the chip name (name of alias in lib) can be broken. This function can be used to display a list of candidates, in symbol properties dialog.

Parameters
aEntryNameis the name of entries to search for (case insensitive).
aLibraryNameis the name of the library to search.
aCandidatesis a std::vector to store candidates.

Definition at line 393 of file symbol_library.cpp.

396{
397 for( SYMBOL_LIB& lib : *this )
398 {
399 if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
400 continue;
401
402 wxArrayString partNames;
403
404 lib.GetSymbolNames( partNames );
405
406 if( partNames.IsEmpty() )
407 continue;
408
409 for( size_t i = 0; i < partNames.size(); i++ )
410 {
411 if( partNames[i].CmpNoCase( aEntryName ) == 0 )
412 aCandidates.push_back( lib.FindSymbol( partNames[i] ) );
413 }
414 }
415}
LIB_SYMBOL * FindSymbol(const wxString &aName) const
Find LIB_SYMBOL by aName.
void GetSymbolNames(wxArrayString &aNames) const
Load a string array with the names of all the entries in this library.
const wxString GetName() const
Return the file name without path or extension.

References SYMBOL_LIB::FindSymbol(), SYMBOL_LIB::GetName(), and SYMBOL_LIB::GetSymbolNames().

◆ FindLibSymbol()

LIB_SYMBOL * SYMBOL_LIBS::FindLibSymbol ( const LIB_ID aLibId,
const wxString &  aLibraryName = wxEmptyString 
)

Search all libraries in the list for a symbol.

A symbol object will always be returned. If the entry found is an alias. The root symbol will be found and returned.

Parameters
aLibIdis the LIB_ID of the symbol to search for.
aLibraryNameis the name of the library to search for symbol.
Returns
the symbol object if found, otherwise NULL.

Definition at line 374 of file symbol_library.cpp.

375{
376 LIB_SYMBOL* part = nullptr;
377
378 for( SYMBOL_LIB& lib : *this )
379 {
380 if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
381 continue;
382
383 part = lib.FindSymbol( aLibId.GetLibItemName().wx_str() );
384
385 if( part )
386 break;
387 }
388
389 return part;
390}
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
Define a library symbol object.
Definition: lib_symbol.h:99
wxString wx_str() const
Definition: utf8.cpp:46

References SYMBOL_LIB::FindSymbol(), LIB_ID::GetLibItemName(), SYMBOL_LIB::GetName(), and UTF8::wx_str().

◆ GetCacheLibrary()

SYMBOL_LIB * SYMBOL_LIBS::GetCacheLibrary ( )

Definition at line 326 of file symbol_library.cpp.

327{
328 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
329 {
330 if( it->IsCache() )
331 return &*it;
332 }
333
334 return nullptr;
335}

◆ GetLibNamesAndPaths()

void SYMBOL_LIBS::GetLibNamesAndPaths ( PROJECT aProject,
wxString *  aPaths,
wxArrayString *  aNames = nullptr 
)
static

Definition at line 418 of file symbol_library.cpp.

419{
420 wxCHECK_RET( aProject, "Null PROJECT in GetLibNamesAndPaths" );
421
422 PROJECT_FILE& project = aProject->GetProjectFile();
423
424 if( aPaths )
425 *aPaths = project.m_LegacyLibDir;
426
427 if( aNames )
428 *aNames = project.m_LegacyLibNames;
429}
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:65
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:149

References PROJECT::GetProjectFile(), and project.

Referenced by LoadAllLibraries(), SCH_EDIT_FRAME::OpenProjectFiles(), and LEGACY_RESCUER::WriteRescueLibrary().

◆ GetLibraryCount()

int SYMBOL_LIBS::GetLibraryCount ( )
inline

Definition at line 158 of file symbol_library.h.

158{ return size(); }

Referenced by SCH_EDIT_FRAME::OpenProjectFiles(), and SCH_SCREEN::UpdateSymbolLinks().

◆ GetLibraryNames()

wxArrayString SYMBOL_LIBS::GetLibraryNames ( bool  aSorted = true)

Return the list of symbol library file names without path and extension.

Parameters
aSortedsort the list of name if true. Otherwise use the library load order.
Returns
the list of library names.

Definition at line 350 of file symbol_library.cpp.

351{
352 wxArrayString cacheNames;
353 wxArrayString names;
354
355 for( SYMBOL_LIB& lib : *this )
356 {
357 if( lib.IsCache() && aSorted )
358 cacheNames.Add( lib.GetName() );
359 else
360 names.Add( lib.GetName() );
361 }
362
363 // Even sorted, the cache library is always at the end of the list.
364 if( aSorted )
365 names.Sort();
366
367 for( unsigned int i = 0; i<cacheNames.Count(); i++ )
368 names.Add( cacheNames.Item( i ) );
369
370 return names;
371}
bool IsCache() const

References SYMBOL_LIB::GetName(), and SYMBOL_LIB::IsCache().

◆ LoadAllLibraries()

void SYMBOL_LIBS::LoadAllLibraries ( PROJECT aProject,
bool  aShowProgress = true 
)

Load all of the project's libraries into this container, which should be cleared before calling it.

Note
This method is only to be used when loading legacy projects. All further symbol library access should be done via the symbol library table.

Definition at line 458 of file symbol_library.cpp.

459{
460 wxString filename;
461 wxString libs_not_found;
462 SEARCH_STACK* lib_search = aProject->SchSearchS();
463
464#if defined(DEBUG) && 0
465 lib_search->Show( __func__ );
466#endif
467
468 wxArrayString lib_names;
469
470 GetLibNamesAndPaths( aProject, nullptr, &lib_names );
471
472 // Post symbol library table, this should be empty. Only the cache library should get loaded.
473 if( !lib_names.empty() )
474 {
475 APP_PROGRESS_DIALOG lib_dialog( _( "Loading Symbol Libraries" ),
476 wxEmptyString,
477 lib_names.GetCount(),
478 nullptr,
479 false,
480 wxPD_APP_MODAL );
481
482 if( aShowProgress )
483 {
484 lib_dialog.Show();
485 }
486
487 for( unsigned i = 0; i < lib_names.GetCount(); ++i )
488 {
489 if( aShowProgress )
490 {
491 lib_dialog.Update( i, wxString::Format( _( "Loading %s..." ), lib_names[i] ) );
492 }
493
494 // lib_names[] does not store the file extension. Set it.
495 // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
496 // before adding the extension can create incorrect full filename
497 wxString fullname = lib_names[i] + "." + LegacySymbolLibFileExtension;
498 // Now the full name is set, we can use a wxFileName.
499 wxFileName fn( fullname );
500
501 // Skip if the file name is not valid..
502 if( !fn.IsOk() )
503 continue;
504
505 if( !fn.FileExists() )
506 {
507 filename = lib_search->FindValidPath( fn.GetFullPath() );
508
509 if( !filename )
510 {
511 libs_not_found += fn.GetFullPath();
512 libs_not_found += '\n';
513 continue;
514 }
515 }
516 else
517 { // ensure the lib filename has a absolute path.
518 // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
519 // make a full absolute path, to avoid issues with load library functions which
520 // expects an absolute path.
521 if( !fn.IsAbsolute() )
522 fn.MakeAbsolute();
523
524 filename = fn.GetFullPath();
525 }
526
527 try
528 {
529 AddLibrary( filename );
530 }
531 catch( const IO_ERROR& ioe )
532 {
533 wxString msg;
534 msg.Printf( _( "Symbol library '%s' failed to load." ), filename );
535
536 wxLogError( msg + wxS( "\n" ) + ioe.What() );
537 }
538 }
539 }
540
541 // add the special cache library.
542 wxString cache_name = CacheName( aProject->GetProjectFullName() );
543 SYMBOL_LIB* cache_lib;
544
545 if( !aProject->IsNullProject() && !cache_name.IsEmpty() )
546 {
547 try
548 {
549 cache_lib = AddLibrary( cache_name );
550
551 if( cache_lib )
552 cache_lib->SetCache();
553 }
554 catch( const IO_ERROR& ioe )
555 {
556 wxString msg = wxString::Format( _( "Error loading symbol library '%s'." )
557 + wxS( "\n%s" ),
558 cache_name,
559 ioe.What() );
560
561 THROW_IO_ERROR( msg );
562 }
563 }
564
565 // Print the libraries not found
566 if( !libs_not_found.IsEmpty() )
567 {
568 // Use a different exception type so catch()er can route to proper use
569 // of the HTML_MESSAGE_BOX.
570 THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
571 }
572}
wxProgressDialog with the option to also update the application progress on the taskbar
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:120
virtual bool IsNullProject() const
Check if this project is a null project (i.e.
Definition: project.cpp:138
Look for files in a number of paths.
Definition: search_stack.h:42
static const wxString CacheName(const wxString &aFullProjectFilename)
Return the name of the cache library after potentially fixing it from an older naming scheme.
SYMBOL_LIB * AddLibrary(const wxString &aFileName)
Allocate and adds a symbol library to the library list.
static void GetLibNamesAndPaths(PROJECT *aProject, wxString *aPaths, wxArrayString *aNames=nullptr)
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References _, AddLibrary(), CacheName(), Format(), GetLibNamesAndPaths(), PROJECT::GetProjectFullName(), PROJECT::IsNullProject(), LegacySymbolLibFileExtension, SYMBOL_LIB::SetCache(), THROW_IO_ERROR, THROW_PARSE_ERROR, TO_UTF8, APP_PROGRESS_DIALOG::Update(), and IO_ERROR::What().

Referenced by LEGACY_RESCUER::WriteRescueLibrary().

◆ SetLibNamesAndPaths()

void SYMBOL_LIBS::SetLibNamesAndPaths ( PROJECT aProject,
const wxString &  aPaths,
const wxArrayString &  aNames 
)
static

Definition at line 432 of file symbol_library.cpp.

434{
435 wxCHECK_RET( aProject, "Null PROJECT in SetLibNamesAndPaths" );
436
437 PROJECT_FILE& project = aProject->GetProjectFile();
438
439 project.m_LegacyLibDir = aPaths;
440 project.m_LegacyLibNames = aNames;
441}

References PROJECT::GetProjectFile(), and project.

Referenced by DIALOG_SYMBOL_REMAP::OnRemapSymbols(), SCH_EDIT_FRAME::OpenProjectFiles(), and LEGACY_RESCUER::WriteRescueLibrary().

◆ Type()

KICAD_T SYMBOL_LIBS::Type ( )
inlineoverridevirtual

Implements PROJECT::_ELEM.

Definition at line 65 of file symbol_library.h.

65{ return SYMBOL_LIBS_T; }
@ SYMBOL_LIBS_T
Definition: typeinfo.h:236

References SYMBOL_LIBS_T.


The documentation for this class was generated from the following files: