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 264 of file class_library.cpp.

265 {
266  PART_LIB* lib;
267 
268  wxFileName fn = aFileName;
269  // Don't reload the library if it is already loaded.
270  lib = FindLibrary( fn.GetName() );
271 
272  if( lib )
273  return lib;
274 
275  try
276  {
277  lib = PART_LIB::LoadLibrary( aFileName );
278  push_back( lib );
279 
280  return lib;
281  }
282  catch( ... )
283  {
284  return nullptr;
285  }
286 }
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 289 of file class_library.cpp.

290 {
291  // Don't reload the library if it is already loaded.
292  wxFileName fn( aFileName );
293  PART_LIB* lib = FindLibrary( fn.GetName() );
294 
295  if( lib )
296  return lib;
297 
298  try
299  {
300  lib = PART_LIB::LoadLibrary( aFileName );
301 
302  if( aIterator >= begin() && aIterator < end() )
303  insert( aIterator, lib );
304  else
305  push_back( lib );
306 
307  return lib;
308  }
309  catch( ... )
310  {
311  return nullptr;
312  }
313 }
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 467 of file class_library.cpp.

468 {
469  wxFileName name = aFullProjectFilename;
470 
471  name.SetName( name.GetName() + "-cache" );
473 
474  if( name.FileExists() )
475  return name.GetFullPath();
476 
477  return wxEmptyString;
478 }
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 376 of file class_library.cpp.

377 {
378  LIB_PART* part = NULL;
379 
380  for( PART_LIB& lib : *this )
381  {
382  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
383  continue;
384 
385  part = lib.FindPart( aLibId.GetLibItemName().wx_str() );
386 
387  if( part )
388  break;
389  }
390 
391  return part;
392 }
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 316 of file class_library.cpp.

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

References NULL.

Referenced by AddLibrary().

◆ FindLibraryByFullFileName()

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

Definition at line 340 of file class_library.cpp.

341 {
342  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
343  {
344  if( it->GetFullFileName() == aFullFileName )
345  return &*it;
346  }
347 
348  return NULL;
349 }
#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 395 of file class_library.cpp.

398 {
399  for( PART_LIB& lib : *this )
400  {
401  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
402  continue;
403 
404  wxArrayString partNames;
405 
406  lib.GetPartNames( partNames );
407 
408  if( partNames.IsEmpty() )
409  continue;
410 
411  for( size_t i = 0; i < partNames.size(); i++ )
412  {
413  if( partNames[i].CmpNoCase( aEntryName ) == 0 )
414  aCandidates.push_back( lib.FindPart( partNames[i] ) );
415  }
416  }
417 }
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 328 of file class_library.cpp.

329 {
330  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
331  {
332  if( it->IsCache() )
333  return &*it;
334  }
335 
336  return NULL;
337 }
#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 352 of file class_library.cpp.

353 {
354  wxArrayString cacheNames;
355  wxArrayString names;
356 
357  for( PART_LIB& lib : *this )
358  {
359  if( lib.IsCache() && aSorted )
360  cacheNames.Add( lib.GetName() );
361  else
362  names.Add( lib.GetName() );
363  }
364 
365  // Even sorted, the cache library is always at the end of the list.
366  if( aSorted )
367  names.Sort();
368 
369  for( unsigned int i = 0; i<cacheNames.Count(); i++ )
370  names.Add( cacheNames.Item( i ) );
371 
372  return names;
373 }
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 423 of file class_library.cpp.

424 {
425  int hash = 0;
426 
427  for( PART_LIBS::const_iterator it = begin(); it != end(); ++it )
428  {
429  hash += it->GetModHash();
430  }
431 
432  // Rebuilding the cache (m_cache) does not change the GetModHash() value,
433  // but changes PART_LIBS::s_modify_generation.
434  // Take this change in account:
436 
437  return hash;
438 }
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 441 of file class_library.cpp.

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

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 481 of file class_library.cpp.

482 {
483  wxString filename;
484  wxString libs_not_found;
485  SEARCH_STACK* lib_search = aProject->SchSearchS();
486 
487 #if defined(DEBUG) && 0
488  lib_search->Show( __func__ );
489 #endif
490 
491  wxArrayString lib_names;
492 
493  LibNamesAndPaths( aProject, false, NULL, &lib_names );
494 
495  // Post symbol library table, this should be empty. Only the cache library should get loaded.
496  if( !lib_names.empty() )
497  {
498  APP_PROGRESS_DIALOG lib_dialog( _( "Loading Symbol Libraries" ),
499  wxEmptyString,
500  lib_names.GetCount(),
501  NULL,
502  false,
503  wxPD_APP_MODAL );
504 
505  if( aShowProgress )
506  {
507  lib_dialog.Show();
508  }
509 
510  wxString progress_message;
511 
512  for( unsigned i = 0; i < lib_names.GetCount(); ++i )
513  {
514  if( aShowProgress )
515  {
516  lib_dialog.Update( i, _( "Loading " + lib_names[i] ) );
517  }
518 
519  // lib_names[] does not store the file extension. Set it.
520  // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
521  // before adding the extension can create incorrect full filename
522  wxString fullname = lib_names[i] + "." + LegacySymbolLibFileExtension;
523  // Now the full name is set, we can use a wxFileName.
524  wxFileName fn( fullname );
525 
526  // Skip if the file name is not valid..
527  if( !fn.IsOk() )
528  continue;
529 
530  if( !fn.FileExists() )
531  {
532  filename = lib_search->FindValidPath( fn.GetFullPath() );
533 
534  if( !filename )
535  {
536  libs_not_found += fn.GetFullPath();
537  libs_not_found += '\n';
538  continue;
539  }
540  }
541  else
542  { // ensure the lib filename has a absolute path.
543  // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
544  // make a full absolute path, to avoid issues with load library functions which
545  // expects an absolute path.
546  if( !fn.IsAbsolute() )
547  fn.MakeAbsolute();
548 
549  filename = fn.GetFullPath();
550  }
551 
552  try
553  {
554  AddLibrary( filename );
555  }
556  catch( const IO_ERROR& ioe )
557  {
558  wxString msg;
559  msg.Printf( _( "Symbol library \"%s\" failed to load. Error:\n %s" ),
560  GetChars( filename ), GetChars( ioe.What() ) );
561 
562  wxLogError( msg );
563  }
564  }
565  }
566 
567  // add the special cache library.
568  wxString cache_name = CacheName( aProject->GetProjectFullName() );
569  PART_LIB* cache_lib;
570 
571  if( !cache_name.IsEmpty() )
572  {
573  try
574  {
575  cache_lib = AddLibrary( cache_name );
576 
577  if( cache_lib )
578  cache_lib->SetCache();
579  }
580  catch( const IO_ERROR& ioe )
581  {
582  wxString msg = wxString::Format( _(
583  "Symbol library \"%s\" failed to load.\nError: %s" ),
584  GetChars( cache_name ),
585  GetChars( ioe.What() )
586  );
587 
588  THROW_IO_ERROR( msg );
589  }
590  }
591 
592  // Print the libraries not found
593  if( !libs_not_found.IsEmpty() )
594  {
595  // Use a different exception type so catch()er can route to proper use
596  // of the HTML_MESSAGE_BOX.
597  THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
598  }
599 }
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 TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:100
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:162
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
PART_LIB * AddLibrary(const wxString &aFileName)
Allocate and adds a part library to the library list.
virtual bool Update(int aValue, const wxString &aNewMsg=wxEmptyString, bool *aSkip=NULL) override
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:118
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:201
#define _(s)
Definition: 3d_actions.cpp:33
wxProgressDialog with the option to also update the application progress on the taskbar
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
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References _, AddLibrary(), CacheName(), SEARCH_STACK::FindValidPath(), Format(), GetChars(), PROJECT::GetProjectFullName(), LegacySymbolLibFileExtension, LibNamesAndPaths(), NULL, THROW_IO_ERROR, THROW_PARSE_ERROR, TO_UTF8, APP_PROGRESS_DIALOG::Update(), 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: