KiCad PCB EDA Suite
PART_LIBS Class Reference

A collection of PART_LIB objects. More...

#include <class_library.h>

Inheritance diagram for PART_LIBS:
PROJECT::_ELEM

Public Member Functions

KICAD_T Type () override
 
 PART_LIBS ()
 
int GetModifyHash ()
 Return the modification hash for all libraries. More...
 
PART_LIBAddLibrary (const wxString &aFileName)
 Allocate and adds a part library to the library list. More...
 
PART_LIBAddLibrary (const wxString &aFileName, PART_LIBS::iterator &aIterator)
 Insert a part 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...
 
PART_LIBFindLibrary (const wxString &aName)
 Find a part library by aName. More...
 
PART_LIBFindLibraryByFullFileName (const wxString &aFullFileName)
 
PART_LIBGetCacheLibrary ()
 
wxArrayString GetLibraryNames (bool aSorted=true)
 Return the list of part library file names without path and extension. More...
 
LIB_PARTFindLibPart (const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
 Search all libraries in the list for a part. More...
 
void FindLibraryNearEntries (std::vector< LIB_PART * > &aCandidates, const wxString &aEntryName, const wxString &aLibraryName=wxEmptyString)
 Search all libraries in the list for a LIB_PART using a case insensitive comparison. More...
 
int GetLibraryCount ()
 

Static Public Member Functions

static void LibNamesAndPaths (PROJECT *aProject, bool doSave, wxString *aPaths, wxArrayString *aNames=NULL)
 Save or load the names of the currently configured part 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...
 

Detailed Description

A collection of PART_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 188 of file class_library.h.

Constructor & Destructor Documentation

◆ PART_LIBS()

PART_LIBS::PART_LIBS ( )
inline

Definition at line 195 of file class_library.h.

196  {
198  }
static int s_modify_generation
helper for GetModifyHash()

References s_modify_generation.

Member Function Documentation

◆ AddLibrary() [1/2]

PART_LIB * PART_LIBS::AddLibrary ( const wxString &  aFileName)

Allocate and adds a part library to the library list.

Parameters
aFileName- File name object of part library.
Exceptions
IO_ERRORif there's any problem loading.

Definition at line 263 of file class_library.cpp.

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

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

Referenced by LoadAllLibraries().

◆ AddLibrary() [2/2]

PART_LIB * PART_LIBS::AddLibrary ( const wxString &  aFileName,
PART_LIBS::iterator &  aIterator 
)

Insert a part library into the library list.

Parameters
aFileName- File name object of part library.
aIterator- Iterator to insert library in front of.
Returns
PART_LIB* - the new PART_LIB, which remains owned by this PART_LIBS container.
Exceptions
IO_ERRORif there's any problem loading.

Definition at line 288 of file class_library.cpp.

289 {
290  // Don't reload the library if it is already loaded.
291  wxFileName fn( aFileName );
292  PART_LIB* lib = FindLibrary( fn.GetName() );
293 
294  if( lib )
295  return lib;
296 
297  try
298  {
299  lib = PART_LIB::LoadLibrary( aFileName );
300 
301  if( aIterator >= begin() && aIterator < end() )
302  insert( aIterator, lib );
303  else
304  push_back( lib );
305 
306  return lib;
307  }
308  catch( ... )
309  {
310  return nullptr;
311  }
312 }
PART_LIB * FindLibrary(const wxString &aName)
Find a part library by aName.
static PART_LIB * LoadLibrary(const wxString &aFileName)
Allocate and load a symbol library file.
Object used to load, save, search, and otherwise manipulate symbol library files.

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

◆ CacheName()

const wxString PART_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
aFullProjectFilename- the *.pro filename with absolute path.

Definition at line 466 of file class_library.cpp.

467 {
468  wxFileName name = aFullProjectFilename;
469 
470  name.SetName( name.GetName() + "-cache" );
472 
473  if( name.FileExists() )
474  return name.GetFullPath();
475 
476  return wxEmptyString;
477 }
const std::string LegacySymbolLibFileExtension
const char * name
Definition: DXF_plotter.cpp:60

References LegacySymbolLibFileExtension, and name.

Referenced by LoadAllLibraries().

◆ FindLibPart()

LIB_PART * PART_LIBS::FindLibPart ( const LIB_ID aLibId,
const wxString &  aLibraryName = wxEmptyString 
)

Search all libraries in the list for a part.

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

Parameters
aLibId- The LIB_ID of the symbol to search for.
aLibraryName- Name of the library to search for part.
Returns
LIB_PART* - The part object if found, otherwise NULL.

Definition at line 375 of file class_library.cpp.

376 {
377  LIB_PART* part = NULL;
378 
379  for( PART_LIB& lib : *this )
380  {
381  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
382  continue;
383 
384  part = lib.FindPart( aLibId.GetLibItemName().wx_str() );
385 
386  if( part )
387  break;
388  }
389 
390  return part;
391 }
const wxString GetName() const
Return the file name without path or extension.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
#define NULL
Define a library symbol object.
LIB_PART * FindPart(const wxString &aName) const
Find LIB_PART by aName.
wxString wx_str() const
Definition: utf8.cpp:51
Object used to load, save, search, and otherwise manipulate symbol library files.

References PART_LIB::FindPart(), LIB_ID::GetLibItemName(), PART_LIB::GetName(), NULL, and UTF8::wx_str().

◆ FindLibrary()

PART_LIB * PART_LIBS::FindLibrary ( const wxString &  aName)

Find a part library by aName.

Parameters
aName- Library file name without path or extension to find.
Returns
Part library if found, otherwise NULL.

Definition at line 315 of file class_library.cpp.

316 {
317  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
318  {
319  if( it->GetName() == aName )
320  return &*it;
321  }
322 
323  return NULL;
324 }
#define NULL

References NULL.

Referenced by AddLibrary().

◆ FindLibraryByFullFileName()

PART_LIB * PART_LIBS::FindLibraryByFullFileName ( const wxString &  aFullFileName)

Definition at line 339 of file class_library.cpp.

340 {
341  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
342  {
343  if( it->GetFullFileName() == aFullFileName )
344  return &*it;
345  }
346 
347  return NULL;
348 }
#define NULL

References NULL.

◆ FindLibraryNearEntries()

void PART_LIBS::FindLibraryNearEntries ( std::vector< LIB_PART * > &  aCandidates,
const wxString &  aEntryName,
const wxString &  aLibraryName = wxEmptyString 
)

Search all libraries in the list for a LIB_PART 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 components, the chip name (name of alias in lib) can be broken. This function can be used to display a list of candidates, in component properties dialog.

Parameters
aEntryName- Name of entries to search for (case insensitive).
aLibraryName- Name of the library to search.
aCandidates- a std::vector to store candidates

Definition at line 394 of file class_library.cpp.

397 {
398  for( PART_LIB& lib : *this )
399  {
400  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
401  continue;
402 
403  wxArrayString partNames;
404 
405  lib.GetPartNames( partNames );
406 
407  if( partNames.IsEmpty() )
408  continue;
409 
410  for( size_t i = 0; i < partNames.size(); i++ )
411  {
412  if( partNames[i].CmpNoCase( aEntryName ) == 0 )
413  aCandidates.push_back( lib.FindPart( partNames[i] ) );
414  }
415  }
416 }
const wxString GetName() const
Return the file name without path or extension.
void GetPartNames(wxArrayString &aNames) const
Load a string array with the names of all the entries in this library.
LIB_PART * FindPart(const wxString &aName) const
Find LIB_PART by aName.
Object used to load, save, search, and otherwise manipulate symbol library files.

References PART_LIB::FindPart(), PART_LIB::GetName(), and PART_LIB::GetPartNames().

◆ GetCacheLibrary()

PART_LIB * PART_LIBS::GetCacheLibrary ( )

Definition at line 327 of file class_library.cpp.

328 {
329  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
330  {
331  if( it->IsCache() )
332  return &*it;
333  }
334 
335  return NULL;
336 }
#define NULL

References NULL.

◆ GetLibraryCount()

int PART_LIBS::GetLibraryCount ( )
inline

Definition at line 293 of file class_library.h.

293 { return size(); }

Referenced by SCH_SCREEN::UpdateSymbolLinks().

◆ GetLibraryNames()

wxArrayString PART_LIBS::GetLibraryNames ( bool  aSorted = true)

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

Parameters
aSorted- Sort the list of name if true. Otherwise use the library load order.
Returns
The list of library names.

Definition at line 351 of file class_library.cpp.

352 {
353  wxArrayString cacheNames;
354  wxArrayString names;
355 
356  for( PART_LIB& lib : *this )
357  {
358  if( lib.IsCache() && aSorted )
359  cacheNames.Add( lib.GetName() );
360  else
361  names.Add( lib.GetName() );
362  }
363 
364  // Even sorted, the cache library is always at the end of the list.
365  if( aSorted )
366  names.Sort();
367 
368  for( unsigned int i = 0; i<cacheNames.Count(); i++ )
369  names.Add( cacheNames.Item( i ) );
370 
371  return names;
372 }
const wxString GetName() const
Return the file name without path or extension.
Object used to load, save, search, and otherwise manipulate symbol library files.
bool IsCache() const

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

◆ GetModifyHash()

int PART_LIBS::GetModifyHash ( )

Return the modification hash for all libraries.

The value returned changes on every library modification.

Definition at line 422 of file class_library.cpp.

423 {
424  int hash = 0;
425 
426  for( PART_LIBS::const_iterator it = begin(); it != end(); ++it )
427  {
428  hash += it->GetModHash();
429  }
430 
431  // Rebuilding the cache (m_cache) does not change the GetModHash() value,
432  // but changes PART_LIBS::s_modify_generation.
433  // Take this change in account:
435 
436  return hash;
437 }
static int s_modify_generation
helper for GetModifyHash()

References s_modify_generation.

◆ LibNamesAndPaths()

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

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

Definition at line 440 of file class_library.cpp.

442 {
443  wxCHECK_RET( aProject, "Null PROJECT in LibNamesAndPaths" );
444 
445  PROJECT_FILE& project = aProject->GetProjectFile();
446 
447  if( doSave )
448  {
449  if( aPaths )
450  project.m_LegacyLibDir = *aPaths;
451 
452  if( aNames )
453  project.m_LegacyLibNames = *aNames;
454  }
455  else
456  {
457  if( aPaths )
458  *aPaths = project.m_LegacyLibDir;
459 
460  if( aNames )
461  *aNames = project.m_LegacyLibNames;
462  }
463 }
wxArrayString m_LegacyLibNames
Definition: project_file.h:153
PROJECT_FILE is the backing store for a PROJECT, in JSON format.
Definition: project_file.h:61
VTBL_ENTRY PROJECT_FILE & GetProjectFile() const
Definition: project.h:129
wxString m_LegacyLibDir
Definition: project_file.h:151

References PROJECT::GetProjectFile(), PROJECT_FILE::m_LegacyLibDir, and PROJECT_FILE::m_LegacyLibNames.

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

◆ LoadAllLibraries()

void PART_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 480 of file class_library.cpp.

481 {
482  wxString filename;
483  wxString libs_not_found;
484  SEARCH_STACK* lib_search = aProject->SchSearchS();
485 
486 #if defined(DEBUG) && 0
487  lib_search->Show( __func__ );
488 #endif
489 
490  wxArrayString lib_names;
491 
492  LibNamesAndPaths( aProject, false, NULL, &lib_names );
493 
494  // Post symbol library table, this should be empty. Only the cache library should get loaded.
495  if( !lib_names.empty() )
496  {
497  wxProgressDialog lib_dialog( _( "Loading Symbol Libraries" ),
498  wxEmptyString,
499  lib_names.GetCount(),
500  NULL,
501  wxPD_APP_MODAL );
502 
503  if( aShowProgress )
504  {
505  lib_dialog.Show();
506  }
507 
508  wxString progress_message;
509 
510  for( unsigned i = 0; i < lib_names.GetCount(); ++i )
511  {
512  if( aShowProgress )
513  {
514  lib_dialog.Update( i, _( "Loading " + lib_names[i] ) );
515  }
516 
517  // lib_names[] does not store the file extension. Set it.
518  // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
519  // before adding the extension can create incorrect full filename
520  wxString fullname = lib_names[i] + "." + LegacySymbolLibFileExtension;
521  // Now the full name is set, we can use a wxFileName.
522  wxFileName fn( fullname );
523 
524  // Skip if the file name is not valid..
525  if( !fn.IsOk() )
526  continue;
527 
528  if( !fn.FileExists() )
529  {
530  filename = lib_search->FindValidPath( fn.GetFullPath() );
531 
532  if( !filename )
533  {
534  libs_not_found += fn.GetFullPath();
535  libs_not_found += '\n';
536  continue;
537  }
538  }
539  else
540  { // ensure the lib filename has a absolute path.
541  // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
542  // make a full absolute path, to avoid issues with load library functions which
543  // expects an absolute path.
544  if( !fn.IsAbsolute() )
545  fn.MakeAbsolute();
546 
547  filename = fn.GetFullPath();
548  }
549 
550  try
551  {
552  AddLibrary( filename );
553  }
554  catch( const IO_ERROR& ioe )
555  {
556  wxString msg;
557  msg.Printf( _( "Symbol library \"%s\" failed to load. Error:\n %s" ),
558  GetChars( filename ), GetChars( ioe.What() ) );
559 
560  wxLogError( msg );
561  }
562  }
563  }
564 
565  // add the special cache library.
566  wxString cache_name = CacheName( aProject->GetProjectFullName() );
567  PART_LIB* cache_lib;
568 
569  if( !cache_name.IsEmpty() )
570  {
571  try
572  {
573  cache_lib = AddLibrary( cache_name );
574 
575  if( cache_lib )
576  cache_lib->SetCache();
577  }
578  catch( const IO_ERROR& ioe )
579  {
580  wxString msg = wxString::Format( _(
581  "Symbol library \"%s\" failed to load.\nError: %s" ),
582  GetChars( cache_name ),
583  GetChars( ioe.What() )
584  );
585 
586  THROW_IO_ERROR( msg );
587  }
588  }
589 
590  // Print the libraries not found
591  if( !libs_not_found.IsEmpty() )
592  {
593  // Use a different exception type so catch()er can route to proper use
594  // of the HTML_MESSAGE_BOX.
595  THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
596  }
597 
598 #if defined(DEBUG) && 1
599  printf( "%s: lib_names:\n", __func__ );
600 
601  for( PART_LIBS::const_iterator it = begin(); it < end(); ++it )
602  printf( " %s\n", TO_UTF8( it->GetName() ) );
603 #endif
604 }
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:73
static void LibNamesAndPaths(PROJECT *aProject, bool doSave, wxString *aPaths, wxArrayString *aNames=NULL)
Save or load the names of the currently configured part libraries (without paths).
const std::string LegacySymbolLibFileExtension
SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
static const wxString CacheName(const wxString &aFullProjectFilename)
Return the name of the cache library after potentially fixing it from an older naming scheme.
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
PART_LIB * AddLibrary(const wxString &aFileName)
Allocate and adds a part library to the library list.
#define THROW_IO_ERROR(msg)
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:116
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:153
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:33
#define TO_UTF8(wxstring)
Object used to load, save, search, and otherwise manipulate symbol library files.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

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

Referenced by LEGACY_RESCUER::WriteRescueLibrary().

◆ Type()

KICAD_T PART_LIBS::Type ( )
inlineoverridevirtual

Implements PROJECT::_ELEM.

Definition at line 191 of file class_library.h.

191 { return PART_LIBS_T; }

References PART_LIBS_T.

Member Data Documentation

◆ s_modify_generation

int PART_LIBS::s_modify_generation = 1
static

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