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 190 of file class_library.h.

Constructor & Destructor Documentation

◆ PART_LIBS()

PART_LIBS::PART_LIBS ( )
inline

Definition at line 197 of file class_library.h.

198  {
200  }
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  lib = PART_LIB::LoadLibrary( aFileName );
274 
275  push_back( lib );
276 
277  return lib;
278 }
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 281 of file class_library.cpp.

282 {
283  // Don't reload the library if it is already loaded.
284  wxFileName fn( aFileName );
285  PART_LIB* lib = FindLibrary( fn.GetName() );
286 
287  if( lib )
288  return lib;
289 
290  lib = PART_LIB::LoadLibrary( aFileName );
291 
292  if( aIterator >= begin() && aIterator < end() )
293  insert( aIterator, lib );
294  else
295  push_back( lib );
296 
297  return lib;
298 }
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 SchematicLibraryFileExtension
const char * name
Definition: DXF_plotter.cpp:61

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

362 {
363  LIB_PART* part = NULL;
364 
365  for( PART_LIB& lib : *this )
366  {
367  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
368  continue;
369 
370  part = lib.FindPart( aLibId.GetLibItemName().wx_str() );
371 
372  if( part )
373  break;
374  }
375 
376  return part;
377 }
const wxString GetName() const
Return the file name without path or extension.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
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(), 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 301 of file class_library.cpp.

302 {
303  for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
304  {
305  if( it->GetName() == aName )
306  return &*it;
307  }
308 
309  return NULL;
310 }

Referenced by AddLibrary().

◆ FindLibraryByFullFileName()

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

Definition at line 325 of file class_library.cpp.

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

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

383 {
384  for( PART_LIB& lib : *this )
385  {
386  if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
387  continue;
388 
389  wxArrayString partNames;
390 
391  lib.GetPartNames( partNames );
392 
393  if( partNames.IsEmpty() )
394  continue;
395 
396  for( size_t i = 0; i < partNames.size(); i++ )
397  {
398  if( partNames[i].CmpNoCase( aEntryName ) == 0 )
399  aCandidates.push_back( lib.FindPart( partNames[i] ) );
400  }
401  }
402 }
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.
size_t i
Definition: json11.cpp:649
Object used to load, save, search, and otherwise manipulate symbol library files.

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

◆ GetCacheLibrary()

PART_LIB * PART_LIBS::GetCacheLibrary ( )

Definition at line 313 of file class_library.cpp.

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

◆ GetLibraryCount()

int PART_LIBS::GetLibraryCount ( )
inline

Definition at line 295 of file class_library.h.

295 { 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 337 of file class_library.cpp.

338 {
339  wxArrayString cacheNames;
340  wxArrayString names;
341 
342  for( PART_LIB& lib : *this )
343  {
344  if( lib.IsCache() && aSorted )
345  cacheNames.Add( lib.GetName() );
346  else
347  names.Add( lib.GetName() );
348  }
349 
350  // Even sorted, the cache library is always at the end of the list.
351  if( aSorted )
352  names.Sort();
353 
354  for( unsigned int i = 0; i<cacheNames.Count(); i++ )
355  names.Add( cacheNames.Item( i ) );
356 
357  return names;
358 }
const wxString GetName() const
Return the file name without path or extension.
size_t i
Definition: json11.cpp:649
Object used to load, save, search, and otherwise manipulate symbol library files.
bool IsCache() const

References PART_LIB::GetName(), i, 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 408 of file class_library.cpp.

409 {
410  int hash = 0;
411 
412  for( PART_LIBS::const_iterator it = begin(); it != end(); ++it )
413  {
414  hash += it->GetModHash();
415  }
416 
417  // Rebuilding the cache (m_cache) does not change the GetModHash() value,
418  // but changes PART_LIBS::s_modify_generation.
419  // Take this change in account:
421 
422  return hash;
423 }
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 426 of file class_library.cpp.

428 {
429  wxString pro = aProject->GetProjectFullName();
430 
431  PARAM_CFG_ARRAY ca;
432 
433  if( aPaths )
434  ca.push_back( new PARAM_CFG_FILENAME( "LibDir", aPaths ) );
435 
436  if( aNames )
437  ca.push_back( new PARAM_CFG_LIBNAME_LIST( wxT( "LibName" ), aNames, GROUP_SCH_LIBS ) );
438 
439  if( doSave )
440  {
441  aProject->ConfigSave( Kiface().KifaceSearch(), GROUP_SCH, ca );
442 
443  /*
444  {
445  wxString msg = wxString::Format( _(
446  "Unable save project's \"%s\" file" ),
447  GetChars( pro )
448  );
449  THROW_IO_ERROR( msg );
450  }
451  */
452  }
453  else
454  {
455  if( !aProject->ConfigLoad( Kiface().KifaceSearch(), GROUP_SCH, ca ) )
456  {
457  wxString msg = wxString::Format( _(
458  "Unable to load project's \"%s\" file" ),
459  GetChars( pro )
460  );
461  THROW_IO_ERROR( msg );
462  }
463  }
464 }
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:323
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
#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 _(s)
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
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:358

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

193 { return PART_LIBS_T; }

References PART_LIBS_T.

Member Data Documentation

◆ s_modify_generation

int PART_LIBS::s_modify_generation = 1
static

helper for GetModifyHash()

Definition at line 195 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: