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

 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...
 
LIB_ALIASFindLibraryAlias (const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
 Function FindLibraryEntry searches all libraries in the list for an entry. More...
 
void FindLibraryNearEntries (std::vector< LIB_ALIAS * > &aCandidates, const wxString &aEntryName, const wxString &aLibraryName=wxEmptyString)
 Function FindLibraryNearEntries Searches all libraries in the list for an entry, 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 189 of file class_library.h.

Constructor & Destructor Documentation

PART_LIBS::PART_LIBS ( )
inline

Definition at line 195 of file class_library.h.

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

Member Function Documentation

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

References PART_LIB::LoadLibrary().

Referenced by SCH_EDIT_FRAME::AppendSchematic().

323 {
324  PART_LIB* lib;
325 
326  wxFileName fn = aFileName;
327  // Don't reload the library if it is already loaded.
328  lib = FindLibrary( fn.GetName() );
329 
330  if( lib )
331  return lib;
332 
333  lib = PART_LIB::LoadLibrary( aFileName );
334 
335  push_back( lib );
336 
337  return lib;
338 }
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. ...
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 341 of file class_library.cpp.

References PART_LIB::LoadLibrary().

342 {
343  // Don't reload the library if it is already loaded.
344  wxFileName fn( aFileName );
345  PART_LIB* lib = FindLibrary( fn.GetName() );
346 
347  if( lib )
348  return lib;
349 
350  lib = PART_LIB::LoadLibrary( aFileName );
351 
352  if( aIterator >= begin() && aIterator < end() )
353  insert( aIterator, lib );
354  else
355  push_back( lib );
356 
357  return lib;
358 }
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. ...
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 546 of file class_library.cpp.

References name, and SchematicLibraryFileExtension.

Referenced by SCH_EDIT_FRAME::AppendSchematic().

547 {
548  wxFileName name = aFullProjectFilename;
549 
550  name.SetName( name.GetName() + "-cache" );
551  name.SetExt( SchematicLibraryFileExtension );
552 
553  if( name.FileExists() )
554  return name.GetFullPath();
555 
556  return wxEmptyString;
557 }
const char * name
Definition: DXF_plotter.cpp:61
const wxString SchematicLibraryFileExtension
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 421 of file class_library.cpp.

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

Referenced by SCH_COMPONENT::Resolve().

422 {
423  LIB_PART* part = NULL;
424 
425  for( PART_LIB& lib : *this )
426  {
427  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
428  continue;
429 
430  part = lib.FindPart( aLibId.GetLibItemName().wx_str() );
431 
432  if( part )
433  break;
434  }
435 
436  return part;
437 }
wxString wx_str() const
Definition: utf8.cpp:48
const UTF8 & GetLibItemName() const
Definition: lib_id.h:118
const wxString GetName() const
Return the file name without path or extension.
Define a library symbol object.
LIB_PART * FindPart(const wxString &aName) const
Find part by aName.
Object used to load, save, search, and otherwise manipulate symbol library files. ...
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 361 of file class_library.cpp.

362 {
363  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
364  {
365  if( it->GetName() == aName )
366  return &*it;
367  }
368 
369  return NULL;
370 }
LIB_ALIAS * PART_LIBS::FindLibraryAlias ( const LIB_ID aLibId,
const wxString &  aLibraryName = wxEmptyString 
)

Function FindLibraryEntry searches all libraries in the list for an entry.

The object can be either a part or an alias.

Parameters
aLibId- The library indentifaction of entry to search for (case sensitive).
aLibraryName- Name of the library to search.
Returns
The entry object if found, otherwise NULL.

Definition at line 440 of file class_library.cpp.

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

441 {
442  LIB_ALIAS* entry = NULL;
443 
444  for( PART_LIB& lib : *this )
445  {
446  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
447  continue;
448 
449  entry = lib.FindAlias( aLibId.GetLibItemName().wx_str() );
450 
451  if( entry )
452  break;
453  }
454 
455  return entry;
456 }
Part library alias object definition.
wxString wx_str() const
Definition: utf8.cpp:48
const UTF8 & GetLibItemName() const
Definition: lib_id.h:118
const wxString GetName() const
Return the file name without path or extension.
LIB_ALIAS * FindAlias(const wxString &aName) const
Find LIB_ALIAS by aName.
Object used to load, save, search, and otherwise manipulate symbol library files. ...
PART_LIB * PART_LIBS::FindLibraryByFullFileName ( const wxString &  aFullFileName)

Definition at line 385 of file class_library.cpp.

386 {
387  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
388  {
389  if( it->GetFullFileName() == aFullFileName )
390  return &*it;
391  }
392 
393  return NULL;
394 }
void PART_LIBS::FindLibraryNearEntries ( std::vector< LIB_ALIAS * > &  aCandidates,
const wxString &  aEntryName,
const wxString &  aLibraryName = wxEmptyString 
)

Function FindLibraryNearEntries Searches all libraries in the list for an entry, 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 459 of file class_library.cpp.

References PART_LIB::FindAlias(), PART_LIB::GetAliasNames(), PART_LIB::GetName(), i, and s_modify_generation.

462 {
463  for( PART_LIB& lib : *this )
464  {
465  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
466  continue;
467 
468  wxArrayString aliasNames;
469 
470  lib.GetAliasNames( aliasNames );
471 
472  if( aliasNames.IsEmpty() )
473  continue;
474 
475  for( size_t i = 0; i < aliasNames.size(); i++ )
476  {
477  if( aliasNames[i].CmpNoCase( aEntryName ) == 0 )
478  aCandidates.push_back( lib.FindAlias( aliasNames[i] ) );
479  }
480  }
481 }
void GetAliasNames(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.
LIB_ALIAS * FindAlias(const wxString &aName) const
Find LIB_ALIAS by aName.
size_t i
Definition: json11.cpp:597
Object used to load, save, search, and otherwise manipulate symbol library files. ...
PART_LIB * PART_LIBS::GetCacheLibrary ( )

Definition at line 373 of file class_library.cpp.

374 {
375  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
376  {
377  if( it->IsCache() )
378  return &*it;
379  }
380 
381  return NULL;
382 }
int PART_LIBS::GetLibraryCount ( )
inline

Definition at line 306 of file class_library.h.

306 { return size(); }
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 397 of file class_library.cpp.

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

398 {
399  wxArrayString cacheNames;
400  wxArrayString names;
401 
402  for( PART_LIB& lib : *this )
403  {
404  if( lib.IsCache() && aSorted )
405  cacheNames.Add( lib.GetName() );
406  else
407  names.Add( lib.GetName() );
408  }
409 
410  // Even sorted, the cache library is always at the end of the list.
411  if( aSorted )
412  names.Sort();
413 
414  for( unsigned int i = 0; i<cacheNames.Count(); i++ )
415  names.Add( cacheNames.Item( i ) );
416 
417  return names;
418 }
bool IsCache() const
const wxString GetName() const
Return the file name without path or extension.
size_t i
Definition: json11.cpp:597
Object used to load, save, search, and otherwise manipulate symbol library files. ...
int PART_LIBS::GetModifyHash ( )

Return the modification hash for all libraries.

The value returned changes on every library modification.

Definition at line 487 of file class_library.cpp.

References s_modify_generation.

488 {
489  int hash = 0;
490 
491  for( PART_LIBS::const_iterator it = begin(); it != end(); ++it )
492  {
493  hash += it->GetModHash();
494  }
495 
496  // Rebuilding the cache (m_cache) does not change the GetModHash() value,
497  // but changes PART_LIBS::s_modify_generation.
498  // Take this change in account:
500 
501  return hash;
502 }
static int s_modify_generation
helper for GetModifyHash()
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 505 of file class_library.cpp.

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

Referenced by add_search_paths(), DIALOG_SYMBOL_REMAP::OnRemapSymbols(), and LEGACY_RESCUER::WriteRescueLibrary().

507 {
508  wxString pro = aProject->GetProjectFullName();
509 
510  PARAM_CFG_ARRAY ca;
511 
512  if( aPaths )
513  ca.push_back( new PARAM_CFG_FILENAME( "LibDir", aPaths ) );
514 
515  if( aNames )
516  ca.push_back( new PARAM_CFG_LIBNAME_LIST( wxT( "LibName" ), aNames, GROUP_SCH_LIBS ) );
517 
518  if( doSave )
519  {
520  aProject->ConfigSave( Kiface().KifaceSearch(), GROUP_SCH, ca );
521 
522  /*
523  {
524  wxString msg = wxString::Format( _(
525  "Unable save project's \"%s\" file" ),
526  GetChars( pro )
527  );
528  THROW_IO_ERROR( msg );
529  }
530  */
531  }
532  else
533  {
534  if( !aProject->ConfigLoad( Kiface().KifaceSearch(), GROUP_SCH, ca ) )
535  {
536  wxString msg = wxString::Format( _(
537  "Unable to load project's \"%s\" file" ),
538  GetChars( pro )
539  );
540  THROW_IO_ERROR( msg );
541  }
542  }
543 }
A list of parameters type.
VTBL_ENTRY void ConfigSave(const SEARCH_STACK &aSList, const wxString &aGroupName, const PARAM_CFG_ARRAY &aParams, const wxString &aFileName=wxEmptyString)
Function ConfigSave saves the current "project" parameters into the wxConfigBase* derivative...
Definition: project.cpp:316
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:94
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
Configuration parameter - PARAM_CFG_FILENAME Class Same as PARAM_CFG_WXSTRING, but stores "\" as "/"...
#define GROUP_SCH
Definition: config_params.h:44
#define GROUP_SCH_LIBS
(Now in fp lib tables)
Definition: config_params.h:50
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:92
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
VTBL_ENTRY bool ConfigLoad(const SEARCH_STACK &aSearchS, const wxString &aGroupName, const PARAM_CFG_ARRAY &aParams, const wxString &aForeignConfigFileName=wxEmptyString)
Function ConfigLoad reads a subset of parameters from the "project" file.
Definition: project.cpp:351
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
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 560 of file class_library.cpp.

References SEARCH_STACK::FindValidPath(), Format(), GetChars(), PROJECT::GetProjectFullName(), i, SchematicLibraryFileExtension, THROW_IO_ERROR, THROW_PARSE_ERROR, TO_UTF8, and IO_ERROR::What().

Referenced by add_search_paths(), and LEGACY_RESCUER::WriteRescueLibrary().

561 {
562  wxString filename;
563  wxString libs_not_found;
564  SEARCH_STACK* lib_search = aProject->SchSearchS();
565 
566 #if defined(DEBUG) && 0
567  lib_search->Show( __func__ );
568 #endif
569 
570  wxArrayString lib_names;
571 
572  LibNamesAndPaths( aProject, false, NULL, &lib_names );
573 
574  // Post symbol library table, this should be empty. Only the cache library should get loaded.
575  if( !lib_names.empty() )
576  {
577  wxProgressDialog lib_dialog( _( "Loading Symbol Libraries" ),
578  wxEmptyString,
579  lib_names.GetCount(),
580  NULL,
581  wxPD_APP_MODAL );
582 
583  if( aShowProgress )
584  {
585  lib_dialog.Show();
586  }
587 
588  wxString progress_message;
589 
590  for( unsigned i = 0; i < lib_names.GetCount(); ++i )
591  {
592  if( aShowProgress )
593  {
594  lib_dialog.Update( i, _( "Loading " + lib_names[i] ) );
595  }
596 
597  // lib_names[] does not store the file extension. Set it.
598  // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
599  // before adding the extension can create incorrect full filename
600  wxString fullname = lib_names[i] + "." + SchematicLibraryFileExtension;
601  // Now the full name is set, we can use a wxFileName.
602  wxFileName fn( fullname );
603 
604  // Skip if the file name is not valid..
605  if( !fn.IsOk() )
606  continue;
607 
608  if( !fn.FileExists() )
609  {
610  filename = lib_search->FindValidPath( fn.GetFullPath() );
611 
612  if( !filename )
613  {
614  libs_not_found += fn.GetFullPath();
615  libs_not_found += '\n';
616  continue;
617  }
618  }
619  else
620  { // ensure the lib filename has a absolute path.
621  // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
622  // make a full absolute path, to avoid issues with load library functions which
623  // expects an absolute path.
624  if( !fn.IsAbsolute() )
625  fn.MakeAbsolute();
626 
627  filename = fn.GetFullPath();
628  }
629 
630  try
631  {
632  AddLibrary( filename );
633  }
634  catch( const IO_ERROR& ioe )
635  {
636  wxString msg;
637  msg.Printf( _( "Symbol library \"%s\" failed to load. Error:\n %s" ),
638  GetChars( filename ), GetChars( ioe.What() ) );
639 
640  wxLogError( msg );
641  }
642  }
643  }
644 
645  // add the special cache library.
646  wxString cache_name = CacheName( aProject->GetProjectFullName() );
647  PART_LIB* cache_lib;
648 
649  if( !cache_name.IsEmpty() )
650  {
651  try
652  {
653  cache_lib = AddLibrary( cache_name );
654 
655  if( cache_lib )
656  cache_lib->SetCache();
657  }
658  catch( const IO_ERROR& ioe )
659  {
660  wxString msg = wxString::Format( _(
661  "Symbol library \"%s\" failed to load.\nError: %s" ),
662  GetChars( cache_name ),
663  GetChars( ioe.What() )
664  );
665 
666  THROW_IO_ERROR( msg );
667  }
668  }
669 
670  // Print the libraries not found
671  if( !libs_not_found.IsEmpty() )
672  {
673  // Use a different exception type so catch()er can route to proper use
674  // of the HTML_MESSAGE_BOX.
675  THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
676  }
677 
678 #if defined(DEBUG) && 1
679  printf( "%s: lib_names:\n", __func__ );
680 
681  for( PART_LIBS::const_iterator it = begin(); it < end(); ++it )
682  printf( " %s\n", TO_UTF8( it->GetName() ) );
683 #endif
684 }
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).
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:94
Class 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:47
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:133
PART_LIB * AddLibrary(const wxString &aFileName)
Allocate and adds a part library to the library list.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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:92
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
size_t i
Definition: json11.cpp:597
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:71
const wxString SchematicLibraryFileExtension
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:47
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

Member Data Documentation

int PART_LIBS::s_modify_generation = 1
static

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