KiCad PCB EDA Suite
libarch.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 Jean-Pierre Charras, jp.charras ar wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 2004-2017 KiCad Developers, see AUTHORS.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 #include <fctsys.h>
32 #include <confirm.h>
33 #include <wxstruct.h>
35 
36 #include <class_sch_screen.h>
37 #include <schframe.h>
38 #include <symbol_lib_table.h>
39 #include <class_library.h>
40 #include <sch_component.h>
41 #include <sch_sheet.h>
42 
43 
44 bool SCH_EDIT_FRAME::CreateArchiveLibraryCacheFile( bool aUseCurrentSheetFilename )
45 {
46  wxFileName fn;
47 
48  if( aUseCurrentSheetFilename )
49  fn = GetScreen()->GetFileName();
50  else
52 
53  fn.SetName( fn.GetName() + "-cache" );
54  fn.SetExt( SchematicLibraryFileExtension );
55 
56  bool success = CreateArchiveLibrary( fn.GetFullPath() );
57 
58  // Update the schematic symbol library links.
59  // because the lib cache has changed
60  SCH_SCREENS schematic;
61  schematic.UpdateSymbolLinks();
62 
63  return success;
64 }
65 
66 
67 bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
68 {
69  wxString tmp;
70  wxString errorMsg;
71  SCH_SCREENS screens;
72 
73  // Create a new empty library to archive components:
74  std::unique_ptr<PART_LIB> archLib( new PART_LIB( LIBRARY_TYPE_EESCHEMA, aFileName ) );
75 
76  // Save symbols to file only when the library will be fully filled
77  archLib->EnableBuffering();
78 
79  /* Examine all screens (not hierarchical sheets) used in the schematic and build a
80  * library of unique symbols found in all screens. Complex hierarchies are not a
81  * problem because we just want to know the library symbols used in the schematic
82  * not their reference.
83  */
84  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
85  {
86  for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() )
87  {
88  if( item->Type() != SCH_COMPONENT_T )
89  continue;
90 
91  LIB_PART* part = nullptr;
92  SCH_COMPONENT* component = (SCH_COMPONENT*) item;
93 
94  try
95  {
96  if( archLib->FindAlias( component->GetLibId().GetLibItemName() ) )
97  continue;
98 
99  part = GetLibPart( component->GetLibId(), true );
100  }
101  catch( const IO_ERROR& ioe )
102  {
103  // Queue up error messages for later.
104  tmp.Printf( _( "Failed to add symbol %s to library file." ),
105  component->GetLibId().GetLibItemName().wx_str(), aFileName );
106 
107  // Don't bail out here. Attempt to add as many of the symbols to the library
108  // as possible.
109  }
110  catch( ... )
111  {
112  tmp = _( "Unexpected exception occurred." );
113  }
114 
115  if( part )
116  {
117  // AddPart() does first clone the part before adding.
118  archLib->AddPart( part );
119  }
120  else
121  {
122  tmp.Printf( _( "Symbol %s not found in any library or cache." ),
123  component->GetLibId().Format().wx_str() );
124  }
125 
126  if( !tmp.empty() )
127  {
128  if( errorMsg.empty() )
129  errorMsg += tmp;
130  else
131  errorMsg += "\n" + tmp;
132  }
133  }
134  }
135 
136  if( !errorMsg.empty() )
137  {
138  tmp.Printf( _( "Errors occurred creating symbol library %s." ), aFileName );
139  DisplayErrorMessage( this, tmp, errorMsg );
140  }
141 
142  archLib->EnableBuffering( false );
143 
144  try
145  {
146  archLib->Save( false );
147  }
148  catch( ... /* IO_ERROR ioe */ )
149  {
150  errorMsg.Printf( _( "Failed to save symbol library file '%s'" ), aFileName );
151  DisplayError( this, errorMsg );
152  return false;
153  }
154 
155  return true;
156 }
Definition of the SCH_SHEET class for Eeschema.
SCH_SCREEN * GetNext()
This file is part of the common library.
const wxString & GetFileName() const
LIB_PART * GetLibPart(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
SCH_SCREEN * GetScreen()
Definition: sch_sheet.h:279
SCH_ITEM * Next() const
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: schframe.cpp:532
wxString wx_str() const
Definition: utf8.cpp:48
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:87
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:55
Base window classes and related definitions.
Definitions for the Eeschema program SCH_SCREEN class.
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
Class LIB_PART defines a library part object.
The common library.
bool CreateArchiveLibraryCacheFile(bool aUseCurrentSheetFilename=false)
Create a symbol library file with the name of the root document plus the '-cache' suffix...
Definition: libarch.cpp:44
Definition the SCH_COMPONENT class for Eeschema.
void UpdateSymbolLinks(bool aForce=false)
Initialize or reinitialize the weak reference to the LIB_PART for each SCH_COMPONENT found in the ful...
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
const LIB_ID & GetLibId() const
UTF8 Format() const
Function Format.
Definition: lib_id.cpp:263
SCH_SCREEN * GetFirst()
Definition for part library class.
const wxString SchematicLibraryFileExtension
bool CreateArchiveLibrary(const wxString &aFileName)
Create a library aFileName that contains all components used in the current schematic.
Definition: libarch.cpp:67
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73
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
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Container class that holds multiple SCH_SCREEN objects in a hierarchy.