KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
6 * Copyright (C) 2004-2021 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 <confirm.h>
33
34#include <sch_edit_frame.h>
35#include <symbol_library.h>
36#include <sch_symbol.h>
37#include <sch_sheet.h>
38#include <schematic.h>
39
40
41bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
42{
43 wxString tmp;
44 wxString errorMsg;
45 SCH_SCREENS screens( Schematic().Root() );
46
47 // Create a new empty library to archive symbols:
48 std::unique_ptr<SYMBOL_LIB> archLib = std::make_unique<SYMBOL_LIB>( SCH_LIB_TYPE::LT_EESCHEMA,
49 aFileName );
50
51 // Save symbols to file only when the library will be fully filled
52 archLib->EnableBuffering();
53
54 /* Examine all screens (not hierarchical sheets) used in the schematic and build a
55 * library of unique symbols found in all screens. Complex hierarchies are not a
56 * problem because we just want to know the library symbols used in the schematic
57 * not their reference.
58 */
59 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
60 {
61
62 for( SCH_ITEM* aItem : screen->Items().OfType( SCH_SYMBOL_T ) )
63 {
64 LIB_SYMBOL* libSymbol = nullptr;
65 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( aItem );
66
67 try
68 {
69 if( archLib->FindSymbol( symbol->GetLibId() ) )
70 continue;
71
72 libSymbol = GetLibSymbol( symbol->GetLibId(), true );
73 }
74 catch( const IO_ERROR& )
75 {
76 // Queue up error messages for later.
77 tmp.Printf( _( "Failed to add symbol %s to library file '%s'." ),
79 aFileName );
80
81 // Don't bail out here. Attempt to add as many of the symbols to the library
82 // as possible.
83 }
84 catch( ... )
85 {
86 tmp = _( "Unexpected exception occurred." );
87 }
88
89 if( libSymbol )
90 {
91 std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
92
93 // Use the full LIB_ID as the symbol name to prevent symbol name collisions.
94 flattenedSymbol->SetName( symbol->GetLibId().GetUniStringLibId() );
95
96 // AddSymbol() does first clone the symbol before adding.
97 archLib->AddSymbol( flattenedSymbol.get() );
98 }
99 else
100 {
101 tmp.Printf( _( "Symbol %s not found in any library or cache." ),
102 symbol->GetLibId().GetUniStringLibId() );
103 }
104
105 if( !tmp.empty() && !errorMsg.Contains( symbol->GetLibId().GetUniStringLibId() ) )
106 {
107 if( errorMsg.empty() )
108 errorMsg += tmp;
109 else
110 errorMsg += wxS( "\n" ) + tmp;
111 }
112 }
113 }
114
115 if( !errorMsg.empty() )
116 {
117 tmp.Printf( _( "Errors occurred creating symbol library %s." ), aFileName );
118 DisplayErrorMessage( this, tmp, errorMsg );
119 }
120
121 archLib->EnableBuffering( false );
122
123 try
124 {
125 archLib->Save( false );
126 }
127 catch( const IO_ERROR& ioe )
128 {
129 errorMsg.Printf( _( "Failed to save symbol library file '%s'." ), aFileName );
130 DisplayErrorMessage( this, errorMsg, ioe.What() );
131 return false;
132 }
133 catch( std::exception& error )
134 {
135 errorMsg.Printf( _( "Failed to save symbol library file '%s'." ), aFileName );
136 DisplayErrorMessage( this, errorMsg, error.what() );
137 return false;
138 }
139
140 return true;
141}
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
wxString GetUniStringLibId() const
Definition: lib_id.h:148
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition: lib_id.h:112
Define a library symbol object.
Definition: lib_symbol.h:78
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
Definition: lib_symbol.cpp:304
LIB_SYMBOL * GetLibSymbol(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
SCHEMATIC & Schematic() const
bool CreateArchiveLibrary(const wxString &aFileName)
Create a library aFileName that contains all symbols used in the current schematic.
Definition: libarch.cpp:41
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:168
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:712
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
Schematic symbol object.
Definition: sch_symbol.h:77
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:166
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
#define _(s)
Definition for symbol library class.
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
Definition of file extensions used in Kicad.