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 ()
 
int GetModifyHash ()
 Return the modification hash for all libraries. More...
 
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 IncrementModifyGeneration ()
 
static int GetModifyGeneration ()
 
static void LibNamesAndPaths (PROJECT *aProject, bool doSave, wxString *aPaths, wxArrayString *aNames=nullptr)
 Save or load the names of the currently configured symbol libraries (without paths). More...
 
static const wxString CacheName (const wxString &aFullProjectFilename)
 Return the name of the cache library after potentially fixing it from an older naming scheme. More...
 

Static Public Attributes

static int s_modify_generation = 1
 helper for GetModifyHash() More...
 
static std::mutex s_generationMutex
 

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 195 of file symbol_library.h.

Constructor & Destructor Documentation

◆ SYMBOL_LIBS()

SYMBOL_LIBS::SYMBOL_LIBS ( )
inline

Definition at line 203 of file symbol_library.h.

204  {
206  }
static void IncrementModifyGeneration()

References IncrementModifyGeneration().

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

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

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

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

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

459 {
460  wxFileName name = aFullProjectFilename;
461 
462  name.SetName( name.GetName() + "-cache" );
464 
465  if( name.FileExists() )
466  return name.GetFullPath();
467 
468  return wxEmptyString;
469 }
const std::string LegacySymbolLibFileExtension
const char * name
Definition: DXF_plotter.cpp:56

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

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

Referenced by AddLibrary().

◆ FindLibraryByFullFileName()

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

Definition at line 330 of file symbol_library.cpp.

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

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

388 {
389  for( SYMBOL_LIB& lib : *this )
390  {
391  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
392  continue;
393 
394  wxArrayString partNames;
395 
396  lib.GetSymbolNames( partNames );
397 
398  if( partNames.IsEmpty() )
399  continue;
400 
401  for( size_t i = 0; i < partNames.size(); i++ )
402  {
403  if( partNames[i].CmpNoCase( aEntryName ) == 0 )
404  aCandidates.push_back( lib.FindSymbol( partNames[i] ) );
405  }
406  }
407 }
LIB_SYMBOL * FindSymbol(const wxString &aName) const
Find LIB_SYMBOL by aName.
Object used to load, save, search, and otherwise manipulate symbol library files.
const wxString GetName() const
Return the file name without path or extension.
void GetSymbolNames(wxArrayString &aNames) const
Load a string array with the names of all the entries in this library.

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

367 {
368  LIB_SYMBOL* part = nullptr;
369 
370  for( SYMBOL_LIB& lib : *this )
371  {
372  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
373  continue;
374 
375  part = lib.FindSymbol( aLibId.GetLibItemName().wx_str() );
376 
377  if( part )
378  break;
379  }
380 
381  return part;
382 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
LIB_SYMBOL * FindSymbol(const wxString &aName) const
Find LIB_SYMBOL by aName.
Object used to load, save, search, and otherwise manipulate symbol library files.
Define a library symbol object.
Definition: lib_symbol.h:96
const wxString GetName() const
Return the file name without path or extension.
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 318 of file symbol_library.cpp.

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

◆ GetLibraryCount()

int SYMBOL_LIBS::GetLibraryCount ( )
inline

Definition at line 313 of file symbol_library.h.

313 { 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 342 of file symbol_library.cpp.

343 {
344  wxArrayString cacheNames;
345  wxArrayString names;
346 
347  for( SYMBOL_LIB& lib : *this )
348  {
349  if( lib.IsCache() && aSorted )
350  cacheNames.Add( lib.GetName() );
351  else
352  names.Add( lib.GetName() );
353  }
354 
355  // Even sorted, the cache library is always at the end of the list.
356  if( aSorted )
357  names.Sort();
358 
359  for( unsigned int i = 0; i<cacheNames.Count(); i++ )
360  names.Add( cacheNames.Item( i ) );
361 
362  return names;
363 }
bool IsCache() const
Object used to load, save, search, and otherwise manipulate symbol library files.
const wxString GetName() const
Return the file name without path or extension.

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

◆ GetModifyGeneration()

static int SYMBOL_LIBS::GetModifyGeneration ( )
inlinestatic

Definition at line 214 of file symbol_library.h.

215  {
216  std::lock_guard<std::mutex> mut( SYMBOL_LIBS::s_generationMutex );
218  }
static int s_modify_generation
helper for GetModifyHash()
static std::mutex s_generationMutex

References s_generationMutex, and s_modify_generation.

Referenced by GetModifyHash().

◆ GetModifyHash()

int SYMBOL_LIBS::GetModifyHash ( )

Return the modification hash for all libraries.

The value returned changes on every library modification.

Definition at line 414 of file symbol_library.cpp.

415 {
416  int hash = 0;
417 
418  for( SYMBOL_LIBS::const_iterator it = begin(); it != end(); ++it )
419  {
420  hash += it->GetModHash();
421  }
422 
423  // Rebuilding the cache (m_cache) does not change the GetModHash() value,
424  // but changes SYMBOL_LIBS::s_modify_generation.
425  // Take this change in account:
427 
428  return hash;
429 }
static int GetModifyGeneration()

References GetModifyGeneration().

◆ IncrementModifyGeneration()

static void SYMBOL_LIBS::IncrementModifyGeneration ( )
inlinestatic

Definition at line 208 of file symbol_library.h.

209  {
210  std::lock_guard<std::mutex> mut( SYMBOL_LIBS::s_generationMutex );
212  }
static int s_modify_generation
helper for GetModifyHash()
static std::mutex s_generationMutex

References s_generationMutex, and s_modify_generation.

Referenced by SCH_SEXPR_PLUGIN::cacheLib(), SCH_LEGACY_PLUGIN::cacheLib(), and SYMBOL_LIBS().

◆ LibNamesAndPaths()

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

Save or load the names of the currently configured symbol libraries (without paths).

Definition at line 432 of file symbol_library.cpp.

434 {
435  wxCHECK_RET( aProject, "Null PROJECT in LibNamesAndPaths" );
436 
437  PROJECT_FILE& project = aProject->GetProjectFile();
438 
439  if( doSave )
440  {
441  if( aPaths )
442  project.m_LegacyLibDir = *aPaths;
443 
444  if( aNames )
445  project.m_LegacyLibNames = *aNames;
446  }
447  else
448  {
449  if( aPaths )
450  *aPaths = project.m_LegacyLibDir;
451 
452  if( aNames )
453  *aNames = project.m_LegacyLibNames;
454  }
455 }
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:145
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:64

References PROJECT::GetProjectFile(), and project.

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

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

473 {
474  wxString filename;
475  wxString libs_not_found;
476  SEARCH_STACK* lib_search = aProject->SchSearchS();
477 
478 #if defined(DEBUG) && 0
479  lib_search->Show( __func__ );
480 #endif
481 
482  wxArrayString lib_names;
483 
484  LibNamesAndPaths( aProject, false, nullptr, &lib_names );
485 
486  // Post symbol library table, this should be empty. Only the cache library should get loaded.
487  if( !lib_names.empty() )
488  {
489  APP_PROGRESS_DIALOG lib_dialog( _( "Loading Symbol Libraries" ),
490  wxEmptyString,
491  lib_names.GetCount(),
492  nullptr,
493  false,
494  wxPD_APP_MODAL );
495 
496  if( aShowProgress )
497  {
498  lib_dialog.Show();
499  }
500 
501  wxString progress_message;
502 
503  for( unsigned i = 0; i < lib_names.GetCount(); ++i )
504  {
505  if( aShowProgress )
506  {
507  lib_dialog.Update( i, wxString::Format( _( "Loading %s..." ), lib_names[i] ) );
508  }
509 
510  // lib_names[] does not store the file extension. Set it.
511  // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
512  // before adding the extension can create incorrect full filename
513  wxString fullname = lib_names[i] + "." + LegacySymbolLibFileExtension;
514  // Now the full name is set, we can use a wxFileName.
515  wxFileName fn( fullname );
516 
517  // Skip if the file name is not valid..
518  if( !fn.IsOk() )
519  continue;
520 
521  if( !fn.FileExists() )
522  {
523  filename = lib_search->FindValidPath( fn.GetFullPath() );
524 
525  if( !filename )
526  {
527  libs_not_found += fn.GetFullPath();
528  libs_not_found += '\n';
529  continue;
530  }
531  }
532  else
533  { // ensure the lib filename has a absolute path.
534  // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
535  // make a full absolute path, to avoid issues with load library functions which
536  // expects an absolute path.
537  if( !fn.IsAbsolute() )
538  fn.MakeAbsolute();
539 
540  filename = fn.GetFullPath();
541  }
542 
543  try
544  {
545  AddLibrary( filename );
546  }
547  catch( const IO_ERROR& ioe )
548  {
549  wxString msg;
550  msg.Printf( _( "Symbol library '%s' failed to load." ), filename );
551 
552  wxLogError( msg + wxS( "\n" ) + ioe.What() );
553  }
554  }
555  }
556 
557  // add the special cache library.
558  wxString cache_name = CacheName( aProject->GetProjectFullName() );
559  SYMBOL_LIB* cache_lib;
560 
561  if( !aProject->IsNullProject() && !cache_name.IsEmpty() )
562  {
563  try
564  {
565  cache_lib = AddLibrary( cache_name );
566 
567  if( cache_lib )
568  cache_lib->SetCache();
569  }
570  catch( const IO_ERROR& ioe )
571  {
572  wxString msg = wxString::Format( _( "Error loading symbol library '%s'." )
573  + wxS( "\n%s" ),
574  cache_name,
575  ioe.What() );
576 
577  THROW_IO_ERROR( msg );
578  }
579  }
580 
581  // Print the libraries not found
582  if( !libs_not_found.IsEmpty() )
583  {
584  // Use a different exception type so catch()er can route to proper use
585  // of the HTML_MESSAGE_BOX.
586  THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
587  }
588 }
virtual bool Update(int aValue, const wxString &aNewMsg=wxEmptyString, bool *aSkip=nullptr) override
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:70
SYMBOL_LIB * AddLibrary(const wxString &aFileName)
Allocate and adds a symbol library to the library list.
Object used to load, save, search, and otherwise manipulate symbol library files.
const std::string LegacySymbolLibFileExtension
static void LibNamesAndPaths(PROJECT *aProject, bool doSave, wxString *aPaths, wxArrayString *aNames=nullptr)
Save or load the names of the currently configured symbol libraries (without paths).
static const wxString CacheName(const wxString &aFullProjectFilename)
Return the name of the cache library after potentially fixing it from an older naming scheme.
Look for files in a number of paths.
Definition: search_stack.h:41
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
#define _(s)
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:116
virtual bool IsNullProject() const
Check if this project is a null project (i.e.
Definition: project.cpp:134
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
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:75
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References _, AddLibrary(), CacheName(), SEARCH_STACK::FindValidPath(), Format(), PROJECT::GetProjectFullName(), PROJECT::IsNullProject(), LegacySymbolLibFileExtension, LibNamesAndPaths(), THROW_IO_ERROR, THROW_PARSE_ERROR, TO_UTF8, APP_PROGRESS_DIALOG::Update(), and IO_ERROR::What().

Referenced by LEGACY_RESCUER::WriteRescueLibrary().

◆ Type()

KICAD_T SYMBOL_LIBS::Type ( )
inlineoverridevirtual

Implements PROJECT::_ELEM.

Definition at line 198 of file symbol_library.h.

198 { return SYMBOL_LIBS_T; }

References SYMBOL_LIBS_T.

Member Data Documentation

◆ s_generationMutex

std::mutex SYMBOL_LIBS::s_generationMutex
static

Definition at line 201 of file symbol_library.h.

Referenced by GetModifyGeneration(), and IncrementModifyGeneration().

◆ s_modify_generation

int SYMBOL_LIBS::s_modify_generation = 1
static

helper for GetModifyHash()

Definition at line 200 of file symbol_library.h.

Referenced by GetModifyGeneration(), and IncrementModifyGeneration().


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