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 255 of file symbol_library.cpp.

256{
257 SYMBOL_LIB* lib;
258
259 wxFileName fn = aFileName;
260 // Don't reload the library if it is already loaded.
261 lib = FindLibrary( fn.GetName() );
262
263 if( lib )
264 return lib;
265
266 try
267 {
268 lib = SYMBOL_LIB::LoadLibrary( aFileName );
269 push_back( lib );
270
271 return lib;
272 }
273 catch( ... )
274 {
275 return nullptr;
276 }
277}
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 * LoadLibrary(const wxString &aFileName)
Allocate and load a symbol library file.

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

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 280 of file symbol_library.cpp.

281{
282 // Don't reload the library if it is already loaded.
283 wxFileName fn( aFileName );
284 SYMBOL_LIB* lib = FindLibrary( fn.GetName() );
285
286 if( lib )
287 return lib;
288
289 try
290 {
291 lib = SYMBOL_LIB::LoadLibrary( aFileName );
292
293 if( aIterator >= begin() && aIterator < end() )
294 insert( aIterator, lib );
295 else
296 push_back( lib );
297
298 return lib;
299 }
300 catch( ... )
301 {
302 return nullptr;
303 }
304}

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

◆ 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 437 of file symbol_library.cpp.

438{
439 wxFileName name = aFullProjectFilename;
440
441 name.SetName( name.GetName() + "-cache" );
443
444 if( name.FileExists() )
445 return name.GetFullPath();
446
447 return wxEmptyString;
448}
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 307 of file symbol_library.cpp.

308{
309 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
310 {
311 if( it->GetName() == aName )
312 return &*it;
313 }
314
315 return nullptr;
316}

Referenced by AddLibrary().

◆ FindLibraryByFullFileName()

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

Definition at line 331 of file symbol_library.cpp.

332{
333 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
334 {
335 if( it->GetFullFileName() == aFullFileName )
336 return &*it;
337 }
338
339 return nullptr;
340}

◆ 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 386 of file symbol_library.cpp.

389{
390 for( SYMBOL_LIB& lib : *this )
391 {
392 if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
393 continue;
394
395 wxArrayString partNames;
396
397 lib.GetSymbolNames( partNames );
398
399 if( partNames.IsEmpty() )
400 continue;
401
402 for( size_t i = 0; i < partNames.size(); i++ )
403 {
404 if( partNames[i].CmpNoCase( aEntryName ) == 0 )
405 aCandidates.push_back( lib.FindSymbol( partNames[i] ) );
406 }
407 }
408}
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 367 of file symbol_library.cpp.

368{
369 LIB_SYMBOL* part = nullptr;
370
371 for( SYMBOL_LIB& lib : *this )
372 {
373 if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
374 continue;
375
376 part = lib.FindSymbol( aLibId.GetLibItemName().wx_str() );
377
378 if( part )
379 break;
380 }
381
382 return part;
383}
const UTF8 & GetLibItemName() const
Definition: lib_id.h:101
Define a library symbol object.
Definition: lib_symbol.h:98
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 319 of file symbol_library.cpp.

320{
321 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
322 {
323 if( it->IsCache() )
324 return &*it;
325 }
326
327 return nullptr;
328}

◆ GetLibNamesAndPaths()

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

Definition at line 411 of file symbol_library.cpp.

412{
413 wxCHECK_RET( aProject, "Null PROJECT in GetLibNamesAndPaths" );
414
415 PROJECT_FILE& project = aProject->GetProjectFile();
416
417 if( aPaths )
418 *aPaths = project.m_LegacyLibDir;
419
420 if( aNames )
421 *aNames = project.m_LegacyLibNames;
422}
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:65
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:148

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 343 of file symbol_library.cpp.

344{
345 wxArrayString cacheNames;
346 wxArrayString names;
347
348 for( SYMBOL_LIB& lib : *this )
349 {
350 if( lib.IsCache() && aSorted )
351 cacheNames.Add( lib.GetName() );
352 else
353 names.Add( lib.GetName() );
354 }
355
356 // Even sorted, the cache library is always at the end of the list.
357 if( aSorted )
358 names.Sort();
359
360 for( unsigned int i = 0; i<cacheNames.Count(); i++ )
361 names.Add( cacheNames.Item( i ) );
362
363 return names;
364}
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 451 of file symbol_library.cpp.

452{
453 wxString filename;
454 wxString libs_not_found;
455 SEARCH_STACK* lib_search = aProject->SchSearchS();
456
457#if defined(DEBUG) && 0
458 lib_search->Show( __func__ );
459#endif
460
461 wxArrayString lib_names;
462
463 GetLibNamesAndPaths( aProject, nullptr, &lib_names );
464
465 // Post symbol library table, this should be empty. Only the cache library should get loaded.
466 if( !lib_names.empty() )
467 {
468 APP_PROGRESS_DIALOG lib_dialog( _( "Loading Symbol Libraries" ),
469 wxEmptyString,
470 lib_names.GetCount(),
471 nullptr,
472 false,
473 wxPD_APP_MODAL );
474
475 if( aShowProgress )
476 {
477 lib_dialog.Show();
478 }
479
480 for( unsigned i = 0; i < lib_names.GetCount(); ++i )
481 {
482 if( aShowProgress )
483 {
484 lib_dialog.Update( i, wxString::Format( _( "Loading %s..." ), lib_names[i] ) );
485 }
486
487 // lib_names[] does not store the file extension. Set it.
488 // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
489 // before adding the extension can create incorrect full filename
490 wxString fullname = lib_names[i] + "." + LegacySymbolLibFileExtension;
491 // Now the full name is set, we can use a wxFileName.
492 wxFileName fn( fullname );
493
494 // Skip if the file name is not valid..
495 if( !fn.IsOk() )
496 continue;
497
498 if( !fn.FileExists() )
499 {
500 filename = lib_search->FindValidPath( fn.GetFullPath() );
501
502 if( !filename )
503 {
504 libs_not_found += fn.GetFullPath();
505 libs_not_found += '\n';
506 continue;
507 }
508 }
509 else
510 { // ensure the lib filename has a absolute path.
511 // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
512 // make a full absolute path, to avoid issues with load library functions which
513 // expects an absolute path.
514 if( !fn.IsAbsolute() )
515 fn.MakeAbsolute();
516
517 filename = fn.GetFullPath();
518 }
519
520 try
521 {
522 AddLibrary( filename );
523 }
524 catch( const IO_ERROR& ioe )
525 {
526 wxString msg;
527 msg.Printf( _( "Symbol library '%s' failed to load." ), filename );
528
529 wxLogError( msg + wxS( "\n" ) + ioe.What() );
530 }
531 }
532 }
533
534 // add the special cache library.
535 wxString cache_name = CacheName( aProject->GetProjectFullName() );
536 SYMBOL_LIB* cache_lib;
537
538 if( !aProject->IsNullProject() && !cache_name.IsEmpty() )
539 {
540 try
541 {
542 cache_lib = AddLibrary( cache_name );
543
544 if( cache_lib )
545 cache_lib->SetCache();
546 }
547 catch( const IO_ERROR& ioe )
548 {
549 wxString msg = wxString::Format( _( "Error loading symbol library '%s'." )
550 + wxS( "\n%s" ),
551 cache_name,
552 ioe.What() );
553
554 THROW_IO_ERROR( msg );
555 }
556 }
557
558 // Print the libraries not found
559 if( !libs_not_found.IsEmpty() )
560 {
561 // Use a different exception type so catch()er can route to proper use
562 // of the HTML_MESSAGE_BOX.
563 THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
564 }
565}
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 425 of file symbol_library.cpp.

427{
428 wxCHECK_RET( aProject, "Null PROJECT in SetLibNamesAndPaths" );
429
430 PROJECT_FILE& project = aProject->GetProjectFile();
431
432 project.m_LegacyLibDir = aPaths;
433 project.m_LegacyLibNames = aNames;
434}

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:237

References SYMBOL_LIBS_T.


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