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