KiCad PCB EDA Suite
class_library.h
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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #ifndef CLASS_LIBRARY_H
32 #define CLASS_LIBRARY_H
33 
34 #include <wx/filename.h>
35 
36 #include <class_libentry.h>
37 #include <sch_io_mgr.h>
38 
39 #include <project.h>
40 
41 #include <map>
42 
43 class LIB_ID;
44 class LINE_READER;
45 class OUTPUTFORMATTER;
46 class SCH_LEGACY_PLUGIN;
47 class SCH_PLUGIN;
48 
49 
50 #define DOC_EXT "dcm"
51 
52 /*
53  * Part Library version and file header macros.
54  */
55 #define LIB_VERSION_MAJOR 2
56 #define LIB_VERSION_MINOR 4
57 
58 /* Must be the first line of part library (.lib) files. */
59 #define LIBFILE_IDENT "EESchema-LIBRARY Version"
60 
61 #define LIB_VERSION( major, minor ) ( major * 100 + minor )
62 
63 #define IS_LIB_CURRENT_VERSION( major, minor ) \
64  ( \
65  LIB_VERSION( major1, minor1 ) == \
66  LIB_VERSION( LIB_VERSION_MAJOR, LIB_VERSION_MINOR) \
67  )
68 
69 /*
70  * Library versions 2.4 and lower use the old separate library (.lib) and
71  * document (.dcm) files. Part libraries after 2.4 merged the library
72  * and document files into a single library file. This macro checks if the
73  * library version supports the old format
74  */
75 #define USE_OLD_DOC_FILE_FORMAT( major, minor ) \
76  ( LIB_VERSION( major, minor ) <= LIB_VERSION( 2, 4 ) )
77 
78 // Helper class to filter a list of libraries, and/or a list of PART_LIB
79 // in dialogs
81 {
82  wxArrayString m_allowedLibs;
83  bool m_filterPowerParts;
85  bool m_forceLoad; // When true, load a part lib from the lib
86  // which is given in m_allowedLibs[0]
87 
88 public:
90  {
91  m_filterPowerParts = false;
92  m_forceLoad = false;
93  }
94 
98  void AddLib( const wxString& aLibName )
99  {
100  m_allowedLibs.Add( aLibName );
101  m_forceLoad = false;
102  }
103 
104 
108  void LoadFrom( const wxString& aLibName )
109  {
110  m_allowedLibs.Clear();
111  m_allowedLibs.Add( aLibName );
112  m_forceLoad = true;
113  }
114 
119  {
120  m_allowedLibs.Clear();
121  m_forceLoad = false;
122  }
123 
127  void FilterPowerParts( bool aFilterEnable )
128  {
129  m_filterPowerParts = aFilterEnable;
130  }
131 
132  // Accessors
133 
137  bool GetFilterPowerParts() const { return m_filterPowerParts; }
138 
139 
143  const wxArrayString& GetAllowedLibList() const { return m_allowedLibs; }
144 
149  const wxString& GetLibSource() const
150  {
151  static wxString dummy;
152 
153  if( m_forceLoad && m_allowedLibs.GetCount() > 0 )
154  return m_allowedLibs[0];
155  else
156  return dummy;
157  }
158 };
159 
160 
161 /* Helpers for creating a list of part libraries. */
162 class PART_LIB;
163 class wxRegEx;
164 
169 {
170  bool operator() ( const wxString& aItem1, const wxString& aItem2 ) const
171  {
172  return aItem1 < aItem2;
173  }
174 };
175 
177 
178 typedef std::map< wxString, LIB_ALIAS*, AliasMapSort > LIB_ALIAS_MAP;
179 typedef std::vector< LIB_ALIAS* > LIB_ALIASES;
180 typedef boost::ptr_vector< PART_LIB > PART_LIBS_BASE;
181 
182 
190 {
191 public:
192  KICAD_T Type() override { return PART_LIBS_T; }
193 
194  static int s_modify_generation;
195 
197  {
198  ++s_modify_generation;
199  }
200 
203  int GetModifyHash();
204 
211  PART_LIB* AddLibrary( const wxString& aFileName );
212 
221  PART_LIB* AddLibrary( const wxString& aFileName, PART_LIBS::iterator& aIterator );
222 
230  void LoadAllLibraries( PROJECT* aProject, bool aShowProgress=true );
231 
235  static void LibNamesAndPaths( PROJECT* aProject, bool doSave,
236  wxString* aPaths, wxArrayString* aNames=NULL );
237 
244  static const wxString CacheName( const wxString& aFullProjectFilename );
245 
252  PART_LIB* FindLibrary( const wxString& aName );
253 
254  PART_LIB* FindLibraryByFullFileName( const wxString& aFullFileName );
255 
256  PART_LIB* GetCacheLibrary();
257 
264  wxArrayString GetLibraryNames( bool aSorted = true );
265 
276  LIB_PART* FindLibPart( const LIB_ID& aLibId, const wxString& aLibraryName = wxEmptyString );
277 
288  LIB_ALIAS* FindLibraryAlias( const LIB_ID& aLibId,
289  const wxString& aLibraryName = wxEmptyString );
290 
304  void FindLibraryNearEntries( std::vector<LIB_ALIAS*>& aCandidates, const wxString& aEntryName,
305  const wxString& aLibraryName = wxEmptyString );
306 
307  int GetLibraryCount() { return size(); }
308 };
309 
310 
314 class PART_LIB
315 {
316  int type;
317  wxFileName fileName;
318  wxDateTime timeStamp;
321  wxString header;
322  bool isModified;
324 
325  SCH_IO_MGR::SCH_FILE_T m_pluginType;
326  std::unique_ptr< SCH_PLUGIN > m_plugin;
327  std::unique_ptr< PROPERTIES > m_properties;
328 
329 public:
330  PART_LIB( int aType, const wxString& aFileName,
331  SCH_IO_MGR::SCH_FILE_T aPluginType = SCH_IO_MGR::SCH_LEGACY );
332  ~PART_LIB();
333 
337  int GetModHash() const { return m_mod_hash; }
338 
339  SCH_IO_MGR::SCH_FILE_T GetPluginType() const { return m_pluginType; }
340 
341  void SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType );
342 
343  void Create( const wxString& aFileName = wxEmptyString );
344 
345  void SetFileName( const wxString& aFileName ) { fileName = aFileName; }
346 
352  bool IsEmpty() const
353  {
354  return m_plugin->GetSymbolLibCount( fileName.GetFullPath() ) == 0;
355  }
356 
362  int GetCount() const
363  {
364  return (int) m_plugin->GetSymbolLibCount( fileName.GetFullPath() );
365  }
366 
367  bool IsModified() const
368  {
369  return isModified;
370  }
371 
372  bool IsCache() const;
373 
374  void SetCache();
375 
376  bool IsBuffering() const;
377 
378  void EnableBuffering( bool aEnable = true );
379 
380  void Save( bool aSaveDocFile = true );
381 
385  bool IsReadOnly() const { return !fileName.IsFileWritable(); }
386 
392  void GetAliasNames( wxArrayString& aNames ) const;
393 
399  void GetAliases( std::vector<LIB_ALIAS*>& aAliases ) const;
400 
406  void GetEntryTypePowerNames( wxArrayString& aNames ) const;
407 
414  LIB_ALIAS* FindAlias( const wxString& aName ) const;
415 
416  LIB_ALIAS* FindAlias( const LIB_ID& aLibId ) const;
417 
427  LIB_PART* FindPart( const wxString& aName ) const;
428 
429  LIB_PART* FindPart( const LIB_ID& aLibId ) const;
430 
439  void AddPart( LIB_PART* aPart );
440 
452  LIB_ALIAS* RemoveAlias( LIB_ALIAS* aEntry );
453 
461  LIB_PART* ReplacePart( LIB_PART* aOldPart, LIB_PART* aNewPart );
462 
468  const wxString GetName() const { return fileName.GetName(); }
469 
475  wxString GetFullFileName() const { return fileName.GetFullPath(); }
476 
482  const wxString GetLogicalName() const
483  {
484  /* for now is the filename without path or extension.
485 
486  Technically the library should not know its logical name!
487  This will eventually come out of a pair of lookup tables using a
488  reverse lookup using the full name or library pointer as a key.
489  Search will be by project lookup table and then user lookup table if
490  not found.
491  */
492  return fileName.GetName();
493  }
494 
495 
503  static PART_LIB* LoadLibrary( const wxString& aFileName );
504 
509  bool HasPowerParts() const;
510 };
511 
512 
516 bool operator==( const PART_LIB& aLibrary, const wxString& aName );
517 bool operator!=( const PART_LIB& aLibrary, const wxString& aName );
518 
519 #endif // CLASS_LIBRARY_H
int m_mod_hash
incremented each time library is changed.
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
Part library alias object definition.
std::unique_ptr< SCH_PLUGIN > m_plugin
void FilterPowerParts(bool aFilterEnable)
set the filtering of power parts
Class PROJECT holds project specific data.
Definition: project.h:57
bool m_filterPowerParts
true to filter (show only) power parts
Definition: class_library.h:84
static int s_modify_generation
helper for GetModifyHash()
bool isModified
Library modification status.
void LoadFrom(const wxString &aLibName)
add a lib name to the allowed libraries
wxString header
first line of loaded library.
SCH_IO_MGR::SCH_FILE_T GetPluginType() const
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
int GetLibraryCount()
int type
Library type indicator.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool operator==(const PART_LIB &aLibrary, const wxString &aName)
Case insensitive library name comparison.
LIB_ALIAS map sorting.
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
std::map< wxString, LIB_ALIAS *, AliasMapSort > LIB_ALIAS_MAP
Alias map used by part library object.
void AddLib(const wxString &aLibName)
add a lib name to the allowed libraries
Definition: class_library.h:98
wxString GetFullFileName() const
Return the full file library name with path and extension.
static struct PcbQueue * Save
Definition: queue.cpp:56
void ClearLibList()
Clear the allowed libraries list (allows all libs)
Base class that schematic file and library loading and saving plugins should derive from...
Definition: sch_io_mgr.h:188
const wxArrayString & GetAllowedLibList() const
KICAD_T Type() override
Class LIB_ITEM definition.
wxDateTime timeStamp
Library save time and date.
const wxString GetName() const
Return the file name without path or extension.
Define a library symbol object.
boost::ptr_vector< PART_LIB > PART_LIBS_BASE
std::unique_ptr< PROPERTIES > m_properties
Library properties.
const wxString GetLogicalName() const
Return the logical name of the library.
bool GetFilterPowerParts() const
SCH_IO_MGR::SCH_FILE_T m_pluginType
bool IsModified() const
std::vector< LIB_ALIAS * > LIB_ALIASES
wxArrayString m_allowedLibs
a list of lib names to list some libraries if empty: no filter
Definition: class_library.h:82
A SCH_PLUGIN derivation for loading schematic files created before the new s-expression file format...
wxFileName fileName
Library file name.
A collection of PART_LIB objects.
bool IsEmpty() const
Get library entry status.
int GetModHash() const
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
int versionMinor
Library minor version number.
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:67
bool IsReadOnly() const
const wxString & GetLibSource() const
void SetFileName(const wxString &aFileName)
Object used to load, save, search, and otherwise manipulate symbol library files. ...
int versionMajor
Library major version number.
bool operator!=(const PART_LIB &aLibrary, const wxString &aName)
int GetCount() const
Return the number of entries in the library.