KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 <common.h>
28#include <symbol_lib_table.h>
29#include <symbol_edit_frame.h>
30#include <symbol_library.h>
33#include <wx/filename.h>
34#include <wx/filedlg.h>
35#include <string_utils.h>
36
37
39{
40 wxString msg;
41 wxString libName = getTargetLib();
42
43 if( !m_libMgr->LibraryExists( libName ) )
44 {
45 libName = SelectLibraryFromList();
46
47 if( !m_libMgr->LibraryExists( libName ) )
48 return;
49 }
50
51 wxString fileFiltersStr;
52 wxString allWildcardsStr;
53
54 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
55 {
56 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
57
58 if( !pi )
59 continue;
60
61 const IO_BASE::IO_FILE_DESC& desc = pi->GetLibraryFileDesc();
62
63 if( desc.m_FileExtensions.empty() )
64 continue;
65
66 if( !fileFiltersStr.IsEmpty() )
67 fileFiltersStr += wxChar( '|' );
68
69 fileFiltersStr += desc.FileFilter();
70
71 for( const std::string& ext : desc.m_FileExtensions )
72 allWildcardsStr << wxT( "*." ) << formatWildcardExt( ext ) << wxT( ";" );
73 }
74
75 fileFiltersStr = _( "All supported formats" ) + wxT( "|" ) + allWildcardsStr + wxT( "|" )
76 + fileFiltersStr;
77
78 wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath, wxEmptyString, fileFiltersStr,
79 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
80
81 if( dlg.ShowModal() == wxID_CANCEL )
82 return;
83
84 wxFileName fn( dlg.GetPath() );
85
86 m_mruPath = fn.GetPath();
87
88 wxArrayString symbols;
89 SCH_IO_MGR::SCH_FILE_T piType = SCH_IO_MGR::GuessPluginTypeFromLibPath( fn.GetFullPath() );
90
91 if( piType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
92 {
93 msg.Printf( _( "Unable to find a reader for '%s'." ), fn.GetFullPath() );
94 DisplayError( this, msg );
95 return;
96 }
97
98 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( piType ) );
99
100 // TODO dialog to select the symbol to be imported if there is more than one
101 try
102 {
103 pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
104 }
105 catch( const IO_ERROR& ioe )
106 {
107 msg.Printf( _( "Cannot import symbol library '%s'." ), fn.GetFullPath() );
108 DisplayErrorMessage( this, msg, ioe.What() );
109 return;
110 }
111
112 if( symbols.empty() )
113 {
114 msg.Printf( _( "Symbol library file '%s' is empty." ), fn.GetFullPath() );
115 DisplayError( this, msg );
116 return;
117 }
118
119 wxString symbolName = symbols[0];
120 LIB_SYMBOL* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
121
122 entry->SetName( EscapeString( entry->GetName(), CTX_LIBID ) );
123
124 if( m_libMgr->SymbolExists( entry->GetName(), libName ) )
125 {
126 msg.Printf( _( "Symbol %s already exists in library '%s'." ), symbolName, libName );
127
128 KIDIALOG errorDlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
129 errorDlg.SetOKLabel( _( "Overwrite" ) );
130 errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
131
132 if( errorDlg.ShowModal() == wxID_CANCEL )
133 return;
134 }
135
136 m_libMgr->UpdateSymbol( entry, libName );
137 SyncLibraries( false );
138 LoadSymbol( entry->GetName(), libName, 1 );
139}
140
141
143{
144 wxString msg;
145 LIB_SYMBOL* symbol = getTargetSymbol();
146
147 if( !symbol )
148 {
149 ShowInfoBarError( _( "There is no symbol selected to save." ) );
150 return;
151 }
152
153 wxFileName fn;
154
155 fn.SetName( symbol->GetName().Lower() );
157
158 wxFileDialog dlg( this, _( "Export Symbol" ), m_mruPath, fn.GetFullName(),
160
161 if( dlg.ShowModal() == wxID_CANCEL )
162 return;
163
164 fn = dlg.GetPath();
165 fn.MakeAbsolute();
166
167 LIB_SYMBOL* old_symbol = nullptr;
168 SCH_IO_MGR::SCH_FILE_T pluginType = SCH_IO_MGR::GuessPluginTypeFromLibPath( fn.GetFullPath() );
169
170 if( pluginType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
171 pluginType = SCH_IO_MGR::SCH_KICAD;
172
173 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( pluginType ) );
174
175 if( fn.FileExists() )
176 {
177 try
178 {
179 old_symbol = pi->LoadSymbol( fn.GetFullPath(), symbol->GetName() );
180 }
181 catch( const IO_ERROR& ioe )
182 {
183 msg.Printf( _( "Error occurred attempting to load symbol library file '%s'." ),
184 fn.GetFullPath() );
185 DisplayErrorMessage( this, msg, ioe.What() );
186 return;
187 }
188
189 if( old_symbol )
190 {
191 msg.Printf( _( "Symbol %s already exists in library '%s'." ),
192 UnescapeString( symbol->GetName() ),
193 fn.GetFullName() );
194
195 KIDIALOG errorDlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
196 errorDlg.SetOKLabel( _( "Overwrite" ) );
197 errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
198
199 if( errorDlg.ShowModal() == wxID_CANCEL )
200 return;
201 }
202 }
203
204 if( fn.Exists() && !fn.IsDirWritable() )
205 {
206 msg.Printf( _( "Insufficient permissions to save library '%s'." ),
207 fn.GetFullPath() );
208 DisplayError( this, msg );
209 return;
210 }
211
212 try
213 {
214 if( !fn.FileExists() )
215 pi->CreateLibrary( fn.GetFullPath() );
216
217 // The flattened symbol is most likely what the user would want. As some point in
218 // the future as more of the symbol library inheritance is implemented, this may have
219 // to be changes to save symbols of inherited symbols.
220 pi->SaveSymbol( fn.GetFullPath(), symbol->Flatten().release() );
221 }
222 catch( const IO_ERROR& ioe )
223 {
224 msg.Printf( _( "Failed to create symbol library file '%s'." ), fn.GetFullPath() );
225 DisplayErrorMessage( this, msg, ioe.What() );
226 msg.Printf( _( "Error creating symbol library '%s'." ), fn.GetFullName() );
227 SetStatusText( msg );
228 return;
229 }
230
231 m_mruPath = fn.GetPath();
232
233 msg.Printf( _( "Symbol %s saved to library '%s'." ),
234 UnescapeString( symbol->GetName() ),
235 fn.GetFullPath() );
236 SetStatusText( msg );
237
238 // See if the user wants it added to a library table (global or project)
239 SYMBOL_LIB_TABLE* libTable = SelectSymLibTable( true );
240
241 if( libTable )
242 {
243 if( !m_libMgr->AddLibrary( fn.GetFullPath(), libTable ) )
244 {
245 DisplayError( this, _( "Could not open the library file." ) );
246 return;
247 }
248
249 bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
250 saveSymbolLibTables( globalTable, !globalTable );
251 }
252}
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:77
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:47
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: confirm.cpp:56
int ShowModal() override
Definition: confirm.cpp:100
Define a library symbol object.
Definition: lib_symbol.h:99
wxString GetName() const override
Definition: lib_symbol.h:160
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
Definition: lib_symbol.cpp:605
virtual void SetName(const wxString &aName)
Definition: lib_symbol.cpp:589
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, int aCtl=0)
Return a plugin type given a symbol library using the file extension of aLibPath.
Definition: sch_io_mgr.cpp:140
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()
The common library.
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:305
This file is part of the common library.
#define _(s)
static const std::string KiCadSymbolLibFileExtension
static wxString KiCadSymbolLibFileWildcard()
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition: io_mgr.h:33
MODEL3D_FORMAT_TYPE fileType(const char *aFileName)
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:54
Container that describes file type info.
Definition: io_base.h:39
std::vector< std::string > m_FileExtensions
Filter used for file pickers if m_IsFile is true.
Definition: io_base.h:41
wxString FileFilter() const
Definition: io_base.cpp:38
Definition for symbol library class.
wxString formatWildcardExt(const wxString &aWildcard)
Format wildcard extension to support case sensitive file dialogs.
Definition of file extensions used in Kicad.