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 The 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 <sch_io/sch_io.h>
31#include <symbol_edit_frame.h>
35#include <wx/filename.h>
36#include <wx/filedlg.h>
37#include <string_utils.h>
38#include <kiplatform/ui.h>
39
40
42{
43 wxString msg;
44 wxString libName = getTargetLib();
45
46 if( !m_libMgr->LibraryExists( libName ) )
47 libName = SelectLibrary( _( "Import Symbol" ), _( "Import symbol to library:" ) );
48
49 if( !m_libMgr->LibraryExists( libName ) )
50 return;
51
52 wxString fileFiltersStr;
53 wxString allWildcardsStr;
54
55 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
56 {
57 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
58
59 if( !pi )
60 continue;
61
62 const IO_BASE::IO_FILE_DESC& desc = pi->GetLibraryFileDesc();
63
64 if( desc.m_FileExtensions.empty() )
65 continue;
66
67 if( !fileFiltersStr.IsEmpty() )
68 fileFiltersStr += wxChar( '|' );
69
70 fileFiltersStr += desc.FileFilter();
71
72 for( const std::string& ext : desc.m_FileExtensions )
73 allWildcardsStr << wxT( "*." ) << formatWildcardExt( ext ) << wxT( ";" );
74 }
75
76 fileFiltersStr = _( "All supported formats" ) + wxT( "|" ) + allWildcardsStr + wxT( "|" )
77 + fileFiltersStr;
78
79 wxFileDialog fileDlg( this, _( "Import Symbol" ), m_mruPath, wxEmptyString, fileFiltersStr,
80 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
81
83
84 if( fileDlg.ShowModal() == wxID_CANCEL )
85 return;
86
87 wxFileName fn( fileDlg.GetPath() );
88
89 m_mruPath = fn.GetPath();
90
91 SCH_IO_MGR::SCH_FILE_T piType = SCH_IO_MGR::GuessPluginTypeFromLibPath( fn.GetFullPath() );
92
93 if( piType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
94 {
95 msg.Printf( _( "Unable to find a reader for '%s'." ), fn.GetFullPath() );
96 DisplayError( this, msg );
97 return;
98 }
99
100 DIALOG_IMPORT_SYMBOL_SELECT selectDlg( this, fn.GetFullPath(), libName, piType );
101
102 if( selectDlg.ShowModal() != wxID_OK )
103 return;
104
105 std::vector<wxString> selectedSymbols = selectDlg.GetSelectedSymbols();
106
107 if( selectedSymbols.empty() )
108 return;
109
110 const auto& conflictResolutions = selectDlg.GetConflictResolutions();
111
112 // Load and import each selected symbol
113 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( piType ) );
114 wxString firstImported;
115 int importedCount = 0;
116 int skippedCount = 0;
117
118 for( const wxString& symbolName : selectedSymbols )
119 {
120 auto conflictIt = conflictResolutions.find( symbolName );
121
122 if( conflictIt != conflictResolutions.end()
123 && conflictIt->second == CONFLICT_RESOLUTION::SKIP )
124 {
125 skippedCount++;
126 continue;
127 }
128
129 try
130 {
131 LIB_SYMBOL* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
132
133 if( entry )
134 {
135 entry->SetName( EscapeString( entry->GetName(), CTX_LIBID ) );
136 m_libMgr->UpdateSymbol( entry, libName );
137
138 if( firstImported.IsEmpty() )
139 firstImported = entry->GetName();
140
141 importedCount++;
142 }
143 }
144 catch( const IO_ERROR& ioe )
145 {
146 msg.Printf( _( "Cannot import symbol '%s': %s" ), symbolName, ioe.What() );
147 DisplayErrorMessage( this, msg );
148 }
149 }
150
151 if( importedCount > 0 )
152 {
153 SyncLibraries( false );
154 LoadSymbol( firstImported, libName, 1 );
155
156 if( skippedCount > 0 )
157 {
158 msg.Printf( _( "Imported %d symbol(s), skipped %d." ), importedCount, skippedCount );
159 DisplayInfoMessage( this, msg );
160 }
161 }
162}
163
164
Dialog to select symbols for import from an external library file.
std::vector< wxString > GetSelectedSymbols() const
Get the list of symbols selected for import.
const std::map< wxString, CONFLICT_RESOLUTION > & GetConflictResolutions() const
Get the conflict resolutions chosen by the user.
int ShowModal() override
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
Define a library symbol object.
Definition lib_symbol.h:83
wxString GetName() const override
Definition lib_symbol.h:145
virtual void SetName(const wxString &aName)
wxString SelectLibrary(const wxString &aDialogTitle, const wxString &aListLabel, const std::vector< std::pair< wxString, bool * > > &aExtraCheckboxes={})
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.
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 ...
The common library.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:230
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:202
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:177
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
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:717
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
Container that describes file type info.
Definition io_base.h:43
std::vector< std::string > m_FileExtensions
Filter used for file pickers if m_IsFile is true.
Definition io_base.h:47
wxString FileFilter() const
Definition io_base.cpp:40
@ SKIP
Don't import this symbol.
wxString formatWildcardExt(const wxString &aWildcard)
Format wildcard extension to support case sensitive file dialogs.
Definition of file extensions used in Kicad.