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-2019 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>
34 
35 #include <sch_edit_frame.h>
36 #include <symbol_lib_table.h>
37 #include <class_library.h>
38 #include <sch_component.h>
39 #include <sch_sheet.h>
40 
41 
42 bool SCH_EDIT_FRAME::CreateArchiveLibraryCacheFile( bool aUseCurrentSheetFilename )
43 {
44  wxFileName fn;
45 
46  if( aUseCurrentSheetFilename )
47  fn = GetScreen()->GetFileName();
48  else
50 
51  fn.SetName( fn.GetName() + "-cache" );
52  fn.SetExt( SchematicLibraryFileExtension );
53 
54  bool success = CreateArchiveLibrary( fn.GetFullPath() );
55 
56  // Update the schematic symbol library links.
57  // because the lib cache has changed
58  SCH_SCREENS schematic;
59  schematic.UpdateSymbolLinks();
60 
61  return success;
62 }
63 
64 
65 bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
66 {
67  wxString tmp;
68  wxString errorMsg;
69  SCH_SCREENS screens;
70 
71  // Create a new empty library to archive components:
72  std::unique_ptr<PART_LIB> archLib( new PART_LIB( LIBRARY_TYPE_EESCHEMA, aFileName ) );
73 
74  // Save symbols to file only when the library will be fully filled
75  archLib->EnableBuffering();
76 
77  /* Examine all screens (not hierarchical sheets) used in the schematic and build a
78  * library of unique symbols found in all screens. Complex hierarchies are not a
79  * problem because we just want to know the library symbols used in the schematic
80  * not their reference.
81  */
82  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
83  {
84 
85  for( auto aItem : screen->Items().OfType( SCH_COMPONENT_T ) )
86  {
87  LIB_PART* part = nullptr;
88  auto component = static_cast<SCH_COMPONENT*>( aItem );
89 
90  try
91  {
92  if( archLib->FindPart( component->GetLibId() ) )
93  continue;
94 
95  part = GetLibPart( component->GetLibId(), true );
96  }
97  catch( const IO_ERROR& )
98  {
99  // Queue up error messages for later.
100  tmp.Printf( _( "Failed to add symbol \"%s\" to library file \"%s\"." ),
101  component->GetLibId().GetUniStringLibItemName(), aFileName );
102 
103  // Don't bail out here. Attempt to add as many of the symbols to the library
104  // as possible.
105  }
106  catch( ... )
107  {
108  tmp = _( "Unexpected exception occurred." );
109  }
110 
111  if( part )
112  {
113  // Use the full LIB_ID as the symbol name to prevent symbol name collisions.
114  wxString oldName = part->GetName();
115  part->SetName( component->GetLibId().GetUniStringLibId() );
116 
117  // AddPart() does first clone the part before adding.
118  archLib->AddPart( part );
119  part->SetName( oldName );
120  }
121  else
122  {
123  tmp.Printf( _( "Symbol %s not found in any library or cache." ),
124  component->GetLibId().GetUniStringLibId() );
125  }
126 
127  if( !tmp.empty() && !errorMsg.Contains( component->GetLibId().GetUniStringLibId() ) )
128  {
129  if( errorMsg.empty() )
130  errorMsg += tmp;
131  else
132  errorMsg += "\n" + tmp;
133  }
134  }
135  }
136 
137  if( !errorMsg.empty() )
138  {
139  tmp.Printf( _( "Errors occurred creating symbol library %s." ), aFileName );
140  DisplayErrorMessage( this, tmp, errorMsg );
141  }
142 
143  archLib->EnableBuffering( false );
144 
145  try
146  {
147  archLib->Save( false );
148  }
149  catch( const IO_ERROR& ioe )
150  {
151  errorMsg.Printf( _( "Failed to save symbol library file \"%s\"" ), aFileName );
152  DisplayErrorMessage( this, errorMsg, ioe.What() );
153  return false;
154  }
155  catch( std::exception& error )
156  {
157  errorMsg.Printf( _( "Failed to save symbol library file \"%s\"" ), aFileName );
158  DisplayErrorMessage( this, errorMsg, error.what() );
159  return false;
160  }
161 
162  return true;
163 }
const wxString & GetFileName() const
Definition: sch_screen.h:157
SCH_SCREEN * GetNext()
Definition: sch_screen.cpp:970
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:252
wxString GetName() const override
This file is part of the common library.
LIB_PART * GetLibPart(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
SCH_SCREEN * GetScreen()
Definition: sch_sheet.h:282
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const std::string SchematicLibraryFileExtension
Define a library symbol object.
Definition of file extensions used in Kicad.
bool CreateArchiveLibraryCacheFile(bool aUseCurrentSheetFilename=false)
Create a symbol library file with the name of the root document plus the '-cache' suffix,...
Definition: libarch.cpp:42
void UpdateSymbolLinks(bool aForce=false)
Initialize or reinitialize the weak reference to the LIB_PART for each SCH_COMPONENT found in the ful...
#define _(s)
Definition: 3d_actions.cpp:33
virtual void SetName(const wxString &aName)
SCH_SCREEN * GetFirst()
Definition: sch_screen.cpp:959
SCH_SHEET * g_RootSheet
Definition for part library class.
bool CreateArchiveLibrary(const wxString &aFileName)
Create a library aFileName that contains all components used in the current schematic.
Definition: libarch.cpp:65
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
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:491