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

263 {
264  PART_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 = PART_LIB::LoadLibrary( aFileName );
276  push_back( lib );
277 
278  return lib;
279  }
280  catch( ... )
281  {
282  return nullptr;
283  }
284 }
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 287 of file class_library.cpp.

288 {
289  // Don't reload the library if it is already loaded.
290  wxFileName fn( aFileName );
291  PART_LIB* lib = FindLibrary( fn.GetName() );
292 
293  if( lib )
294  return lib;
295 
296  try
297  {
298  lib = PART_LIB::LoadLibrary( 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 }
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 487 of file class_library.cpp.

488 {
489  wxFileName name = aFullProjectFilename;
490 
491  name.SetName( name.GetName() + "-cache" );
493 
494  if( name.FileExists() )
495  return name.GetFullPath();
496 
497  return wxEmptyString;
498 }
const std::string SchematicLibraryFileExtension
const char * name
Definition: DXF_plotter.cpp:60

References name, and SchematicLibraryFileExtension.

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

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

Referenced by SCH_COMPONENT::Resolve().

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

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

References NULL.

Referenced by AddLibrary().

◆ FindLibraryByFullFileName()

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

Definition at line 338 of file class_library.cpp.

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

396 {
397  for( PART_LIB& lib : *this )
398  {
399  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
400  continue;
401 
402  wxArrayString partNames;
403 
404  lib.GetPartNames( 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.FindPart( partNames[i] ) );
413  }
414  }
415 }
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 326 of file class_library.cpp.

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

References NULL.

◆ GetLibraryCount()

int PART_LIBS::GetLibraryCount ( )
inline

Definition at line 293 of file class_library.h.

293 { return size(); }

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

351 {
352  wxArrayString cacheNames;
353  wxArrayString names;
354 
355  for( PART_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 }
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 421 of file class_library.cpp.

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

441 {
442  wxString pro = aProject->GetProjectFullName();
443 
444  std::vector<PARAM_CFG*> ca;
445 
446  try
447  {
448  if( aPaths )
449  ca.push_back( new PARAM_CFG_FILENAME( "LibDir", aPaths ) );
450 
451  if( aNames )
452  ca.push_back( new PARAM_CFG_LIBNAME_LIST( wxT( "LibName" ), aNames, GROUP_SCH_LIBS ) );
453  }
454  catch( boost::bad_pointer& )
455  {
456  // Out of memory? Ship's going down anyway....
457  }
458 
459  if( doSave )
460  {
461  aProject->ConfigSave( Kiface().KifaceSearch(), GROUP_SCH, ca );
462 
463  /*
464  {
465  wxString msg = wxString::Format( _(
466  "Unable save project's \"%s\" file" ),
467  GetChars( pro )
468  );
469  THROW_IO_ERROR( msg );
470  }
471  */
472  }
473  else
474  {
475  if( !aProject->ConfigLoad( Kiface().KifaceSearch(), GROUP_SCH, ca ) )
476  {
477  wxString msg = wxString::Format( _(
478  "Unable to load project's \"%s\" file" ),
479  GetChars( pro )
480  );
481  THROW_IO_ERROR( msg );
482  }
483  }
484 }
Configuration parameter - PARAM_CFG_FILENAME Class Same as PARAM_CFG_WXSTRING, but stores "\" as "/".
VTBL_ENTRY bool ConfigLoad(const SEARCH_STACK &aSearchS, const wxString &aGroupName, const std::vector< PARAM_CFG * > &aParams, const wxString &aForeignConfigFileName=wxEmptyString)
Function ConfigLoad reads a subset of parameters from the "project" file.
Definition: project.cpp:385
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
VTBL_ENTRY void ConfigSave(const SEARCH_STACK &aSList, const wxString &aGroupName, const std::vector< PARAM_CFG * > &aParams, const wxString &aFileName=wxEmptyString)
Function ConfigSave saves the current "project" parameters into the wxConfigBase* derivative.
Definition: project.cpp:350
#define GROUP_SCH
Definition: config_params.h:41
#define GROUP_SCH_LIBS
(Now in fp lib tables)
Definition: config_params.h:47
#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:96
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:101
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

References _, PROJECT::ConfigLoad(), PROJECT::ConfigSave(), Format(), GetChars(), PROJECT::GetProjectFullName(), GROUP_SCH, GROUP_SCH_LIBS, Kiface(), and THROW_IO_ERROR.

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

502 {
503  wxString filename;
504  wxString libs_not_found;
505  SEARCH_STACK* lib_search = aProject->SchSearchS();
506 
507 #if defined(DEBUG) && 0
508  lib_search->Show( __func__ );
509 #endif
510 
511  wxArrayString lib_names;
512 
513  LibNamesAndPaths( aProject, false, NULL, &lib_names );
514 
515  // Post symbol library table, this should be empty. Only the cache library should get loaded.
516  if( !lib_names.empty() )
517  {
518  wxProgressDialog lib_dialog( _( "Loading Symbol Libraries" ),
519  wxEmptyString,
520  lib_names.GetCount(),
521  NULL,
522  wxPD_APP_MODAL );
523 
524  if( aShowProgress )
525  {
526  lib_dialog.Show();
527  }
528 
529  wxString progress_message;
530 
531  for( unsigned i = 0; i < lib_names.GetCount(); ++i )
532  {
533  if( aShowProgress )
534  {
535  lib_dialog.Update( i, _( "Loading " + lib_names[i] ) );
536  }
537 
538  // lib_names[] does not store the file extension. Set it.
539  // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
540  // before adding the extension can create incorrect full filename
541  wxString fullname = lib_names[i] + "." + SchematicLibraryFileExtension;
542  // Now the full name is set, we can use a wxFileName.
543  wxFileName fn( fullname );
544 
545  // Skip if the file name is not valid..
546  if( !fn.IsOk() )
547  continue;
548 
549  if( !fn.FileExists() )
550  {
551  filename = lib_search->FindValidPath( fn.GetFullPath() );
552 
553  if( !filename )
554  {
555  libs_not_found += fn.GetFullPath();
556  libs_not_found += '\n';
557  continue;
558  }
559  }
560  else
561  { // ensure the lib filename has a absolute path.
562  // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
563  // make a full absolute path, to avoid issues with load library functions which
564  // expects an absolute path.
565  if( !fn.IsAbsolute() )
566  fn.MakeAbsolute();
567 
568  filename = fn.GetFullPath();
569  }
570 
571  try
572  {
573  AddLibrary( filename );
574  }
575  catch( const IO_ERROR& ioe )
576  {
577  wxString msg;
578  msg.Printf( _( "Symbol library \"%s\" failed to load. Error:\n %s" ),
579  GetChars( filename ), GetChars( ioe.What() ) );
580 
581  wxLogError( msg );
582  }
583  }
584  }
585 
586  // add the special cache library.
587  wxString cache_name = CacheName( aProject->GetProjectFullName() );
588  PART_LIB* cache_lib;
589 
590  if( !cache_name.IsEmpty() )
591  {
592  try
593  {
594  cache_lib = AddLibrary( cache_name );
595 
596  if( cache_lib )
597  cache_lib->SetCache();
598  }
599  catch( const IO_ERROR& ioe )
600  {
601  wxString msg = wxString::Format( _(
602  "Symbol library \"%s\" failed to load.\nError: %s" ),
603  GetChars( cache_name ),
604  GetChars( ioe.What() )
605  );
606 
607  THROW_IO_ERROR( msg );
608  }
609  }
610 
611  // Print the libraries not found
612  if( !libs_not_found.IsEmpty() )
613  {
614  // Use a different exception type so catch()er can route to proper use
615  // of the HTML_MESSAGE_BOX.
616  THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
617  }
618 
619 #if defined(DEBUG) && 1
620  printf( "%s: lib_names:\n", __func__ );
621 
622  for( PART_LIBS::const_iterator it = begin(); it < end(); ++it )
623  printf( " %s\n", TO_UTF8( it->GetName() ) );
624 #endif
625 }
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).
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:48
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const std::string SchematicLibraryFileExtension
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:96
#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:101
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
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(), LibNamesAndPaths(), NULL, SchematicLibraryFileExtension, 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: