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, 2024 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 <kidialog.h>
28#include <common.h>
30#include <symbol_lib_table.h>
31#include <symbol_edit_frame.h>
32#include <symbol_library.h>
35#include <wx/filename.h>
36#include <wx/filedlg.h>
37#include <string_utils.h>
39
40
42{
43 wxString msg;
44 wxString libName = getTargetLib();
45
46 if( !m_libMgr->LibraryExists( libName ) )
47 {
48 libName = SelectLibraryFromList();
49
50 if( !m_libMgr->LibraryExists( libName ) )
51 return;
52 }
53
54 wxString fileFiltersStr;
55 wxString allWildcardsStr;
56
57 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
58 {
59 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
60
61 if( !pi )
62 continue;
63
64 const IO_BASE::IO_FILE_DESC& desc = pi->GetLibraryFileDesc();
65
66 if( desc.m_FileExtensions.empty() )
67 continue;
68
69 if( !fileFiltersStr.IsEmpty() )
70 fileFiltersStr += wxChar( '|' );
71
72 fileFiltersStr += desc.FileFilter();
73
74 for( const std::string& ext : desc.m_FileExtensions )
75 allWildcardsStr << wxT( "*." ) << formatWildcardExt( ext ) << wxT( ";" );
76 }
77
78 fileFiltersStr = _( "All supported formats" ) + wxT( "|" ) + allWildcardsStr + wxT( "|" )
79 + fileFiltersStr;
80
81 wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath, wxEmptyString, fileFiltersStr,
82 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
83
84 if( dlg.ShowModal() == wxID_CANCEL )
85 return;
86
87 wxFileName fn( dlg.GetPath() );
88
89 m_mruPath = fn.GetPath();
90
91 wxArrayString symbols;
92 SCH_IO_MGR::SCH_FILE_T piType = SCH_IO_MGR::GuessPluginTypeFromLibPath( fn.GetFullPath() );
93
94 if( piType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
95 {
96 msg.Printf( _( "Unable to find a reader for '%s'." ), fn.GetFullPath() );
97 DisplayError( this, msg );
98 return;
99 }
100
101 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( piType ) );
102
103 // TODO dialog to select the symbol to be imported if there is more than one
104 try
105 {
106 pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
107 }
108 catch( const IO_ERROR& ioe )
109 {
110 msg.Printf( _( "Cannot import symbol library '%s'." ), fn.GetFullPath() );
111 DisplayErrorMessage( this, msg, ioe.What() );
112 return;
113 }
114 catch( const XML_PARSER_ERROR& ioe )
115 {
116 msg.Printf( _( "Cannot import symbol library '%s'." ), fn.GetFullPath() );
117 DisplayErrorMessage( this, msg, ioe.what() );
118 return;
119 }
120
121 if( symbols.empty() )
122 {
123 msg.Printf( _( "Symbol library file '%s' is empty." ), fn.GetFullPath() );
124 DisplayError( this, msg );
125 return;
126 }
127
128 wxString symbolName = symbols[0];
129 LIB_SYMBOL* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
130
131 entry->SetName( EscapeString( entry->GetName(), CTX_LIBID ) );
132
133 if( m_libMgr->SymbolExists( entry->GetName(), libName ) )
134 {
135 msg.Printf( _( "Symbol %s already exists in library '%s'." ), symbolName, libName );
136
137 KIDIALOG errorDlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
138 errorDlg.SetOKLabel( _( "Overwrite" ) );
139 errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
140
141 if( errorDlg.ShowModal() == wxID_CANCEL )
142 return;
143 }
144
145 m_libMgr->UpdateSymbol( entry, libName );
146 SyncLibraries( false );
147 LoadSymbol( entry->GetName(), libName, 1 );
148}
149
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: kidialog.h:43
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: kidialog.cpp:51
int ShowModal() override
Definition: kidialog.cpp:95
Define a library symbol object.
Definition: lib_symbol.h:78
wxString GetName() const override
Definition: lib_symbol.h:137
virtual void SetName(const wxString &aName)
Definition: lib_symbol.cpp:288
wxString SelectLibraryFromList()
Display a list of loaded libraries and allows the user to select a library.
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 ...
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 UpdateSymbol(LIB_SYMBOL *aSymbol, const wxString &aLibrary)
Update the symbol buffer with a new version of the symbol.
The common library.
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
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)
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition: io_mgr.h:33
This file is part of the common library.
MODEL3D_FORMAT_TYPE fileType(const char *aFileName)
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:40
std::vector< std::string > m_FileExtensions
Filter used for file pickers if m_IsFile is true.
Definition: io_base.h:42
wxString FileFilter() const
Definition: io_base.cpp:40
Implement a simple wrapper around runtime_error to isolate the errors thrown by the Eagle XML parser.
Definition: eagle_parser.h:82
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.