KiCad PCB EDA Suite
symbol_editor_import_export.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) 2016 Jean-Pierre Charras, jp.charras at 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
26#include <confirm.h>
27#include <symbol_lib_table.h>
28#include <symbol_edit_frame.h>
29#include <symbol_library.h>
32#include <wx/filename.h>
33#include <wx/filedlg.h>
34#include <string_utils.h>
35
36
38{
39 wxString msg;
40 wxString libName = getTargetLib();
41
42 if( !m_libMgr->LibraryExists( libName ) )
43 {
44 libName = SelectLibraryFromList();
45
46 if( !m_libMgr->LibraryExists( libName ) )
47 return;
48 }
49
50 wxString wildcards = AllSymbolLibFilesWildcard()
53
54 wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath, wxEmptyString,
55 wildcards, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
56
57 if( dlg.ShowModal() == wxID_CANCEL )
58 return;
59
60 wxFileName fn = dlg.GetPath();
61
62 if( fn.GetExt().IsEmpty() )
63 {
64 fn.SetExt( (dlg.GetFilterIndex() == 0) ? KiCadSymbolLibFileExtension
66 }
67
68 m_mruPath = fn.GetPath();
69
70 wxArrayString symbols;
71 SCH_IO_MGR::SCH_FILE_T piType = SCH_IO_MGR::GuessPluginTypeFromLibPath( fn.GetFullPath() );
72 SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( piType ) );
73
74 // TODO dialog to select the symbol to be imported if there is more than one
75 try
76 {
77 pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
78 }
79 catch( const IO_ERROR& ioe )
80 {
81 msg.Printf( _( "Cannot import symbol library '%s'." ), fn.GetFullPath() );
82 DisplayErrorMessage( this, msg, ioe.What() );
83 return;
84 }
85
86 if( symbols.empty() )
87 {
88 msg.Printf( _( "Symbol library file '%s' is empty." ), fn.GetFullPath() );
89 DisplayError( this, msg );
90 return;
91 }
92
93 wxString symbolName = symbols[0];
94 LIB_SYMBOL* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
95
96 entry->SetName( EscapeString( entry->GetName(), CTX_LIBID ) );
97
98 if( m_libMgr->SymbolExists( entry->GetName(), libName ) )
99 {
100 msg.Printf( _( "Symbol %s already exists in library '%s'." ), symbolName, libName );
101
102 KIDIALOG errorDlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
103 errorDlg.SetOKLabel( _( "Overwrite" ) );
104 errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
105
106 if( errorDlg.ShowModal() == wxID_CANCEL )
107 return;
108 }
109
110 m_libMgr->UpdateSymbol( entry, libName );
111 SyncLibraries( false );
112 LoadSymbol( entry->GetName(), libName, 1 );
113}
114
115
117{
118 wxString msg;
119 LIB_SYMBOL* symbol = getTargetSymbol();
120
121 if( !symbol )
122 {
123 ShowInfoBarError( _( "There is no symbol selected to save." ) );
124 return;
125 }
126
127 wxFileName fn;
128
129 fn.SetName( symbol->GetName().Lower() );
130 fn.SetExt( KiCadSymbolLibFileExtension );
131
132 wxFileDialog dlg( this, _( "Export Symbol" ), m_mruPath, fn.GetFullName(),
133 KiCadSymbolLibFileWildcard(), wxFD_SAVE );
134
135 if( dlg.ShowModal() == wxID_CANCEL )
136 return;
137
138 fn = dlg.GetPath();
139 fn.MakeAbsolute();
140
141 LIB_SYMBOL* old_symbol = nullptr;
142 SCH_IO_MGR::SCH_FILE_T pluginType = SCH_IO_MGR::GuessPluginTypeFromLibPath( fn.GetFullPath() );
143 SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( pluginType ) );
144
145 if( fn.FileExists() )
146 {
147 try
148 {
149 old_symbol = pi->LoadSymbol( fn.GetFullPath(), symbol->GetName() );
150 }
151 catch( const IO_ERROR& ioe )
152 {
153 msg.Printf( _( "Error occurred attempting to load symbol library file '%s'." ),
154 fn.GetFullPath() );
155 DisplayErrorMessage( this, msg, ioe.What() );
156 return;
157 }
158
159 if( old_symbol )
160 {
161 msg.Printf( _( "Symbol %s already exists in library '%s'." ),
162 UnescapeString( symbol->GetName() ),
163 fn.GetFullName() );
164
165 KIDIALOG errorDlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
166 errorDlg.SetOKLabel( _( "Overwrite" ) );
167 errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
168
169 if( errorDlg.ShowModal() == wxID_CANCEL )
170 return;
171 }
172 }
173
174 if( fn.Exists() && !fn.IsDirWritable() )
175 {
176 msg.Printf( _( "Insufficient permissions to save library '%s'." ),
177 fn.GetFullPath() );
178 DisplayError( this, msg );
179 return;
180 }
181
182 try
183 {
184 if( !fn.FileExists() )
185 pi->CreateSymbolLib( fn.GetFullPath() );
186
187 // The flattened symbol is most likely what the user would want. As some point in
188 // the future as more of the symbol library inheritance is implemented, this may have
189 // to be changes to save symbols of inherited symbols.
190 pi->SaveSymbol( fn.GetFullPath(), symbol->Flatten().release() );
191 }
192 catch( const IO_ERROR& ioe )
193 {
194 msg.Printf( _( "Failed to create symbol library file '%s'." ), fn.GetFullPath() );
195 DisplayErrorMessage( this, msg, ioe.What() );
196 msg.Printf( _( "Error creating symbol library '%s'." ), fn.GetFullName() );
197 SetStatusText( msg );
198 return;
199 }
200
201 m_mruPath = fn.GetPath();
202
203 msg.Printf( _( "Symbol %s saved to library '%s'." ),
204 UnescapeString( symbol->GetName() ),
205 fn.GetFullPath() );
206 SetStatusText( msg );
207
208 // See if the user wants it added to a library table (global or project)
209 SYMBOL_LIB_TABLE* libTable = SelectSymLibTable( true );
210
211 if( libTable )
212 {
213 if( !m_libMgr->AddLibrary( fn.GetFullPath(), libTable ) )
214 {
215 DisplayError( this, _( "Could not open the library file." ) );
216 return;
217 }
218
219 bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
220 saveSymbolLibTables( globalTable, !globalTable );
221 }
222}
void ShowInfoBarError(const wxString &aErrorMsg, bool aShowCloseButton=false, WX_INFOBAR::MESSAGE_TYPE aType=WX_INFOBAR::MESSAGE_TYPE::GENERIC)
Show the WX_INFOBAR displayed on the top of the canvas with a message and an error icon on the left o...
wxString m_mruPath
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:46
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: confirm.cpp:76
int ShowModal() override
Definition: confirm.cpp:120
Define a library symbol object.
Definition: lib_symbol.h:99
wxString GetName() const override
Definition: lib_symbol.h:138
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
Definition: lib_symbol.cpp:453
virtual void SetName(const wxString &aName)
Definition: lib_symbol.cpp:437
SYMBOL_LIB_TABLE * SelectSymLibTable(bool aOptional=false)
Display a dialog asking the user to select a symbol library table.
wxString SelectLibraryFromList()
Display a list of loaded libraries and allows the user to select a library.
bool saveSymbolLibTables(bool aGlobal, bool aProject)
Save Symbol Library Tables to disk.
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Return a plugin type given a symbol library using the file extension of aLibPath.
Definition: sch_io_mgr.cpp:156
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:535
virtual void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
Definition: sch_plugin.cpp:67
virtual void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const STRING_UTF8_MAP *aProperties=nullptr)
Write aSymbol to an existing library located at aLibraryPath.
Definition: sch_plugin.cpp:94
virtual LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aPartName, const STRING_UTF8_MAP *aProperties=nullptr)
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
Definition: sch_plugin.cpp:85
virtual void CreateSymbolLib(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Create a new empty symbol library at aLibraryPath.
Definition: sch_plugin.cpp:110
wxString getTargetLib() const
LIB_SYMBOL_LIBRARY_MANAGER * m_libMgr
void LoadSymbol(const wxString &aLibrary, const wxString &aSymbol, int Unit)
void SyncLibraries(bool aShowProgress, bool aPreloadCancelled=false, const wxString &aForceRefresh=wxEmptyString)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
LIB_SYMBOL * getTargetSymbol() const
Return either the library selected in the symbol tree, if context menu is active or the library that ...
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Return true if library exists.
bool SymbolExists(const wxString &aAlias, const wxString &aLibrary) const
Return true if symbol with a specific alias exists in library (either original one or buffered).
bool AddLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Add an existing library.
bool UpdateSymbol(LIB_SYMBOL *aSymbol, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol.
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:300
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:325
This file is part of the common library.
#define _(s)
const std::string KiCadSymbolLibFileExtension
const std::string LegacySymbolLibFileExtension
wxString LegacySymbolLibFileWildcard()
wxString KiCadSymbolLibFileWildcard()
wxString AllSymbolLibFilesWildcard()
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LIBID
Definition: string_utils.h:55
Definition for symbol library class.
Definition of file extensions used in Kicad.