KiCad PCB EDA Suite
symbol_library.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2004-2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright (C) 2022 CERN
7 * Copyright (C) 2004-2022 KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <algorithm>
28#include <kiface_base.h>
29#include <eda_base_frame.h>
30#include <string_utils.h>
31#include <macros.h>
32#include <richio.h>
33#include <config_params.h>
36#include <project_rescue.h>
37#include <string_utf8_map.h>
39
40#include <general.h>
41#include <symbol_library.h>
43
44#include <wx/log.h>
45#include <wx/progdlg.h>
46#include <wx/tokenzr.h>
47#include "sim/sim_model.h"
48
49SYMBOL_LIB::SYMBOL_LIB( SCH_LIB_TYPE aType, const wxString& aFileName,
50 SCH_IO_MGR::SCH_FILE_T aPluginType ) :
51 m_pluginType( aPluginType )
52{
53 type = aType;
54 isModified = false;
55 timeStamp = 0;
56 timeStamp = wxDateTime::Now();
57 versionMajor = 0; // Will be updated after reading the lib file
58 versionMinor = 0; // Will be updated after reading the lib file
59
60 fileName = aFileName;
61
62 if( !fileName.IsOk() )
63 fileName = "unnamed.lib";
64
65 m_plugin.reset( SCH_IO_MGR::FindPlugin( m_pluginType ) );
66 m_properties = std::make_unique<STRING_UTF8_MAP>();
67 m_mod_hash = 0;
68}
69
70
72{
73}
74
75
76void SYMBOL_LIB::Save( bool aSaveDocFile )
77{
78 wxCHECK_RET( m_plugin != nullptr,
79 wxString::Format( wxT( "no plugin defined for library `%s`." ),
80 fileName.GetFullPath() ) );
81
82 STRING_UTF8_MAP props;
83
84 if( !aSaveDocFile )
86
87 m_plugin->SaveLibrary( fileName.GetFullPath(), &props );
88 isModified = false;
89}
90
91
92void SYMBOL_LIB::Create( const wxString& aFileName )
93{
94 wxString tmpFileName = fileName.GetFullPath();
95
96 if( !aFileName.IsEmpty() )
97 tmpFileName = aFileName;
98
99 m_plugin->CreateSymbolLib( tmpFileName, m_properties.get() );
100}
101
102
103void SYMBOL_LIB::SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType )
104{
105 if( m_pluginType != aPluginType )
106 {
107 m_pluginType = aPluginType;
108 m_plugin.reset( SCH_IO_MGR::FindPlugin( m_pluginType ) );
109 }
110}
111
112
114{
116}
117
118
120{
121 (*m_properties)[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
122}
123
124
126{
128}
129
130
131void SYMBOL_LIB::EnableBuffering( bool aEnable )
132{
133 if( aEnable )
134 (*m_properties)[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
135 else
137}
138
139
140void SYMBOL_LIB::GetSymbolNames( wxArrayString& aNames ) const
141{
142 m_plugin->EnumerateSymbolLib( aNames, fileName.GetFullPath(), m_properties.get() );
143
144 aNames.Sort();
145}
146
147
148void SYMBOL_LIB::GetSymbols( std::vector<LIB_SYMBOL*>& aSymbols ) const
149{
150 m_plugin->EnumerateSymbolLib( aSymbols, fileName.GetFullPath(), m_properties.get() );
151
152 std::sort( aSymbols.begin(), aSymbols.end(),
153 [](LIB_SYMBOL *lhs, LIB_SYMBOL *rhs) -> bool
154 {
155 return lhs->GetName() < rhs->GetName();
156 } );
157}
158
159
160LIB_SYMBOL* SYMBOL_LIB::FindSymbol( const wxString& aName ) const
161{
162 LIB_SYMBOL* symbol = m_plugin->LoadSymbol( fileName.GetFullPath(), aName, m_properties.get() );
163
164 if( symbol )
165 {
166 // Set the library to this even though technically the legacy cache plugin owns the
167 // symbols. This allows the symbol library table conversion tool to determine the
168 // correct library where the symbol was found.
169 if( !symbol->GetLib() )
170 symbol->SetLib( const_cast<SYMBOL_LIB*>( this ) );
171
172 SIM_MODEL::MigrateSimModel<LIB_SYMBOL, LIB_FIELD>( *symbol, nullptr );
173 }
174
175 return symbol;
176}
177
178
180{
181 return FindSymbol( aLibId.Format().wx_str() );
182}
183
184
186{
187 // add a clone, not the caller's copy, the plugin take ownership of the new symbol.
188 m_plugin->SaveSymbol( fileName.GetFullPath(),
189 new LIB_SYMBOL( *aSymbol->SharedPtr().get(), this ),
190 m_properties.get() );
191
192 // If we are not buffering, the library file is updated immediately when the plugin
193 // SaveSymbol() function is called.
194 if( IsBuffering() )
195 isModified = true;
196
197 ++m_mod_hash;
198}
199
200
202{
203 wxCHECK_MSG( aEntry != nullptr, nullptr, "NULL pointer cannot be removed from library." );
204
205 m_plugin->DeleteSymbol( fileName.GetFullPath(), aEntry->GetName(), m_properties.get() );
206
207 // If we are not buffering, the library file is updated immediately when the plugin
208 // SaveSymbol() function is called.
209 if( IsBuffering() )
210 isModified = true;
211
212 ++m_mod_hash;
213 return nullptr;
214}
215
216
218{
219 wxASSERT( aOldSymbol != nullptr );
220 wxASSERT( aNewSymbol != nullptr );
221
222 m_plugin->DeleteSymbol( fileName.GetFullPath(), aOldSymbol->GetName(), m_properties.get() );
223
224 LIB_SYMBOL* my_part = new LIB_SYMBOL( *aNewSymbol, this );
225
226 m_plugin->SaveSymbol( fileName.GetFullPath(), my_part, m_properties.get() );
227
228 // If we are not buffering, the library file is updated immediately when the plugin
229 // SaveSymbol() function is called.
230 if( IsBuffering() )
231 isModified = true;
232
233 ++m_mod_hash;
234 return my_part;
235}
236
237
238SYMBOL_LIB* SYMBOL_LIB::LoadSymbolLibrary( const wxString& aFileName )
239{
240 std::unique_ptr<SYMBOL_LIB> lib = std::make_unique<SYMBOL_LIB>( SCH_LIB_TYPE::LT_EESCHEMA,
241 aFileName );
242
243 std::vector<LIB_SYMBOL*> parts;
244 // This loads the library.
245 lib->GetSymbols( parts );
246
247 // Now, set the LIB_SYMBOL m_library member but it will only be used
248 // when loading legacy libraries in the future. Once the symbols in the
249 // schematic have a full #LIB_ID, this will not get called.
250 for( size_t ii = 0; ii < parts.size(); ii++ )
251 {
252 LIB_SYMBOL* part = parts[ii];
253
254 part->SetLib( lib.get() );
255 }
256
257 SYMBOL_LIB* ret = lib.release();
258 return ret;
259}
260
261
262SYMBOL_LIB* SYMBOL_LIBS::AddLibrary( const wxString& aFileName )
263{
264 SYMBOL_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 = SYMBOL_LIB::LoadSymbolLibrary( aFileName );
276 push_back( lib );
277
278 return lib;
279 }
280 catch( ... )
281 {
282 return nullptr;
283 }
284}
285
286
287SYMBOL_LIB* SYMBOL_LIBS::AddLibrary( const wxString& aFileName, SYMBOL_LIBS::iterator& aIterator )
288{
289 // Don't reload the library if it is already loaded.
290 wxFileName fn( aFileName );
291 SYMBOL_LIB* lib = FindLibrary( fn.GetName() );
292
293 if( lib )
294 return lib;
295
296 try
297 {
298 lib = SYMBOL_LIB::LoadSymbolLibrary( 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}
312
313
314SYMBOL_LIB* SYMBOL_LIBS::FindLibrary( const wxString& aName )
315{
316 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
317 {
318 if( it->GetName() == aName )
319 return &*it;
320 }
321
322 return nullptr;
323}
324
325
327{
328 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
329 {
330 if( it->IsCache() )
331 return &*it;
332 }
333
334 return nullptr;
335}
336
337
338SYMBOL_LIB* SYMBOL_LIBS::FindLibraryByFullFileName( const wxString& aFullFileName )
339{
340 for( SYMBOL_LIBS::iterator it = begin(); it!=end(); ++it )
341 {
342 if( it->GetFullFileName() == aFullFileName )
343 return &*it;
344 }
345
346 return nullptr;
347}
348
349
350wxArrayString SYMBOL_LIBS::GetLibraryNames( bool aSorted )
351{
352 wxArrayString cacheNames;
353 wxArrayString names;
354
355 for( SYMBOL_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}
372
373
374LIB_SYMBOL* SYMBOL_LIBS::FindLibSymbol( const LIB_ID& aLibId, const wxString& aLibraryName )
375{
376 LIB_SYMBOL* part = nullptr;
377
378 for( SYMBOL_LIB& lib : *this )
379 {
380 if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
381 continue;
382
383 part = lib.FindSymbol( aLibId.GetLibItemName().wx_str() );
384
385 if( part )
386 break;
387 }
388
389 return part;
390}
391
392
393void SYMBOL_LIBS::FindLibraryNearEntries( std::vector<LIB_SYMBOL*>& aCandidates,
394 const wxString& aEntryName,
395 const wxString& aLibraryName )
396{
397 for( SYMBOL_LIB& lib : *this )
398 {
399 if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
400 continue;
401
402 wxArrayString partNames;
403
404 lib.GetSymbolNames( 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.FindSymbol( partNames[i] ) );
413 }
414 }
415}
416
417
418void SYMBOL_LIBS::GetLibNamesAndPaths( PROJECT* aProject, wxString* aPaths, wxArrayString* aNames )
419{
420 wxCHECK_RET( aProject, "Null PROJECT in GetLibNamesAndPaths" );
421
422 PROJECT_FILE& project = aProject->GetProjectFile();
423
424 if( aPaths )
425 *aPaths = project.m_LegacyLibDir;
426
427 if( aNames )
428 *aNames = project.m_LegacyLibNames;
429}
430
431
432void SYMBOL_LIBS::SetLibNamesAndPaths( PROJECT* aProject, const wxString& aPaths,
433 const wxArrayString& aNames )
434{
435 wxCHECK_RET( aProject, "Null PROJECT in SetLibNamesAndPaths" );
436
437 PROJECT_FILE& project = aProject->GetProjectFile();
438
439 project.m_LegacyLibDir = aPaths;
440 project.m_LegacyLibNames = aNames;
441}
442
443
444const wxString SYMBOL_LIBS::CacheName( const wxString& aFullProjectFilename )
445{
446 wxFileName name = aFullProjectFilename;
447
448 name.SetName( name.GetName() + "-cache" );
450
451 if( name.FileExists() )
452 return name.GetFullPath();
453
454 return wxEmptyString;
455}
456
457
458void SYMBOL_LIBS::LoadAllLibraries( PROJECT* aProject, bool aShowProgress )
459{
460 wxString filename;
461 wxString libs_not_found;
462 SEARCH_STACK* lib_search = aProject->SchSearchS();
463
464#if defined(DEBUG) && 0
465 lib_search->Show( __func__ );
466#endif
467
468 wxArrayString lib_names;
469
470 GetLibNamesAndPaths( aProject, nullptr, &lib_names );
471
472 // Post symbol library table, this should be empty. Only the cache library should get loaded.
473 if( !lib_names.empty() )
474 {
475 APP_PROGRESS_DIALOG lib_dialog( _( "Loading Symbol Libraries" ),
476 wxEmptyString,
477 lib_names.GetCount(),
478 nullptr,
479 false,
480 wxPD_APP_MODAL );
481
482 if( aShowProgress )
483 {
484 lib_dialog.Show();
485 }
486
487 for( unsigned i = 0; i < lib_names.GetCount(); ++i )
488 {
489 if( aShowProgress )
490 {
491 lib_dialog.Update( i, wxString::Format( _( "Loading %s..." ), lib_names[i] ) );
492 }
493
494 // lib_names[] does not store the file extension. Set it.
495 // Remember lib_names[i] can contain a '.' in name, so using a wxFileName
496 // before adding the extension can create incorrect full filename
497 wxString fullname = lib_names[i] + "." + LegacySymbolLibFileExtension;
498 // Now the full name is set, we can use a wxFileName.
499 wxFileName fn( fullname );
500
501 // Skip if the file name is not valid..
502 if( !fn.IsOk() )
503 continue;
504
505 if( !fn.FileExists() )
506 {
507 filename = lib_search->FindValidPath( fn.GetFullPath() );
508
509 if( !filename )
510 {
511 libs_not_found += fn.GetFullPath();
512 libs_not_found += '\n';
513 continue;
514 }
515 }
516 else
517 { // ensure the lib filename has a absolute path.
518 // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
519 // make a full absolute path, to avoid issues with load library functions which
520 // expects an absolute path.
521 if( !fn.IsAbsolute() )
522 fn.MakeAbsolute();
523
524 filename = fn.GetFullPath();
525 }
526
527 try
528 {
529 AddLibrary( filename );
530 }
531 catch( const IO_ERROR& ioe )
532 {
533 wxString msg;
534 msg.Printf( _( "Symbol library '%s' failed to load." ), filename );
535
536 wxLogError( msg + wxS( "\n" ) + ioe.What() );
537 }
538 }
539 }
540
541 // add the special cache library.
542 wxString cache_name = CacheName( aProject->GetProjectFullName() );
543 SYMBOL_LIB* cache_lib;
544
545 if( !aProject->IsNullProject() && !cache_name.IsEmpty() )
546 {
547 try
548 {
549 cache_lib = AddLibrary( cache_name );
550
551 if( cache_lib )
552 cache_lib->SetCache();
553 }
554 catch( const IO_ERROR& ioe )
555 {
556 wxString msg = wxString::Format( _( "Error loading symbol library '%s'." )
557 + wxS( "\n%s" ),
558 cache_name,
559 ioe.What() );
560
561 THROW_IO_ERROR( msg );
562 }
563 }
564
565 // Print the libraries not found
566 if( !libs_not_found.IsEmpty() )
567 {
568 // Use a different exception type so catch()er can route to proper use
569 // of the HTML_MESSAGE_BOX.
570 THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8( libs_not_found ), 0, 0 );
571 }
572}
const char * name
Definition: DXF_plotter.cpp:56
wxProgressDialog with the option to also update the application progress on the taskbar
virtual bool Update(int aValue, const wxString &aNewMsg=wxEmptyString, bool *aSkip=nullptr) override
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
UTF8 Format() const
Definition: lib_id.cpp:117
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
Define a library symbol object.
Definition: lib_symbol.h:99
wxString GetName() const override
Definition: lib_symbol.h:138
void SetLib(SYMBOL_LIB *aLibrary)
Definition: lib_symbol.h:193
LIB_SYMBOL_SPTR SharedPtr() const
Definition: lib_symbol.h:110
SYMBOL_LIB * GetLib() const
Definition: lib_symbol.h:192
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:65
Container for project specific data.
Definition: project.h:64
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:120
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:149
virtual bool IsNullProject() const
Check if this project is a null project (i.e.
Definition: project.cpp:138
static const char * PropNoDocFile
The property used internally by the plugin to disable writing the library documentation (....
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
Look for files in a number of paths.
Definition: search_stack.h:42
A name/value tuple with unique names and optional values.
void FindLibraryNearEntries(std::vector< LIB_SYMBOL * > &aCandidates, const wxString &aEntryName, const wxString &aLibraryName=wxEmptyString)
Search all libraries in the list for a LIB_SYMBOL using a case insensitive comparison.
static void SetLibNamesAndPaths(PROJECT *aProject, const wxString &aPaths, const wxArrayString &aNames)
SYMBOL_LIB * FindLibrary(const wxString &aName)
Find a symbol library by aName.
SYMBOL_LIB * FindLibraryByFullFileName(const wxString &aFullFileName)
wxArrayString GetLibraryNames(bool aSorted=true)
Return the list of symbol library file names without path and extension.
void LoadAllLibraries(PROJECT *aProject, bool aShowProgress=true)
Load all of the project's libraries into this container, which should be cleared before calling it.
static const wxString CacheName(const wxString &aFullProjectFilename)
Return the name of the cache library after potentially fixing it from an older naming scheme.
LIB_SYMBOL * FindLibSymbol(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Search all libraries in the list for a symbol.
SYMBOL_LIB * AddLibrary(const wxString &aFileName)
Allocate and adds a symbol library to the library list.
static void GetLibNamesAndPaths(PROJECT *aProject, wxString *aPaths, wxArrayString *aNames=nullptr)
SYMBOL_LIB * GetCacheLibrary()
Object used to load, save, search, and otherwise manipulate symbol library files.
SCH_IO_MGR::SCH_FILE_T m_pluginType
void Create(const wxString &aFileName=wxEmptyString)
void GetSymbols(std::vector< LIB_SYMBOL * > &aSymbols) const
Load a vector with all the entries in this library.
bool IsBuffering() const
bool IsCache() const
std::unique_ptr< SCH_PLUGIN > m_plugin
void SetPluginType(SCH_IO_MGR::SCH_FILE_T aPluginType)
SCH_LIB_TYPE type
Library type indicator.
SYMBOL_LIB(SCH_LIB_TYPE aType, const wxString &aFileName, SCH_IO_MGR::SCH_FILE_T aPluginType=SCH_IO_MGR::SCH_LEGACY)
void EnableBuffering(bool aEnable=true)
void Save(bool aSaveDocFile=true)
LIB_SYMBOL * FindSymbol(const wxString &aName) const
Find LIB_SYMBOL by aName.
void AddSymbol(LIB_SYMBOL *aSymbol)
Add aSymbol entry to library.
static SYMBOL_LIB * LoadSymbolLibrary(const wxString &aFileName)
Allocate and load a symbol library file.
bool isModified
Library modification status.
void GetSymbolNames(wxArrayString &aNames) const
Load a string array with the names of all the entries in this library.
wxDateTime timeStamp
Library save time and date.
int versionMinor
Library minor version number.
int m_mod_hash
incremented each time library is changed.
LIB_SYMBOL * ReplaceSymbol(LIB_SYMBOL *aOldSymbol, LIB_SYMBOL *aNewSymbol)
Replace an existing symbol entry in the library.
wxFileName fileName
Library file name.
int versionMajor
Library major version number.
std::unique_ptr< STRING_UTF8_MAP > m_properties
Library properties.
const wxString GetName() const
Return the file name without path or extension.
LIB_SYMBOL * RemoveSymbol(LIB_SYMBOL *aEntry)
Safely remove aEntry from the library and return the next entry.
wxString wx_str() const
Definition: utf8.cpp:46
#define _(s)
Base window classes and related definitions.
const std::string LegacySymbolLibFileExtension
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Definition for symbol library class.
Definition of file extensions used in Kicad.