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.

References s_modify_generation.

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 FindLibrary(), and PART_LIB::LoadLibrary().

Referenced by SCH_EDIT_FRAME::AppendSchematic(), and LoadAllLibraries().

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 FindLibrary(), and 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 SchematicLibraryFileExtension.

Referenced by SCH_EDIT_FRAME::AppendSchematic(), and LoadAllLibraries().

547 {
548  /* until apr 2009 the project cache lib was named: <root_name>.cache.lib,
549  * and after: <root_name>-cache.lib. So if the <name>-cache.lib is not found,
550  * the old file will be renamed and returned.
551  */
552  wxFileName new_name = aFullProjectFilename;
553 
554  new_name.SetName( new_name.GetName() + "-cache" );
555  new_name.SetExt( SchematicLibraryFileExtension );
556 
557  if( new_name.FileExists() )
558  return new_name.GetFullPath();
559  else
560  {
561  wxFileName old_name = aFullProjectFilename;
562  old_name.SetExt( "cache.lib" );
563 
564  if( old_name.FileExists() )
565  {
566  wxRenameFile( old_name.GetFullPath(), new_name.GetFullPath() );
567  return new_name.GetFullPath();
568  }
569  }
570 
571  return wxEmptyString;
572 }
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:115
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.

Referenced by AddLibrary().

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:115
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(), and PART_LIB::GetName().

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.
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(), 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.
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 LoadAllLibraries(), 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:318
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:96
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:353
#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 575 of file class_library.cpp.

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

Referenced by LEGACY_RESCUER::WriteRescueLibrary().

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

helper for GetModifyHash()

Definition at line 193 of file class_library.h.

Referenced by SCH_LEGACY_PLUGIN::cacheLib(), GetModifyHash(), and PART_LIBS().


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