KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_mgr.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 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Wayne Stambaugh <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <wx/filename.h>
24#include <wx/uri.h>
25
26#include <sch_io/sch_io_mgr.h>
30
38#include <common.h> // for ExpandEnvVarSubstitutions
39
41#include <kiway_player.h>
42#include <string_utils.h>
43
44#define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
45#define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
46
47
48
49// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
50// and code size. Until then, use the simplest method:
51
52// This implementation is one of two which could be done.
53// The other one would cater to DLL/DSO's. But since it would be nearly
54// impossible to link a KICAD type DLL/DSO right now without pulling in all
55// ::Draw() functions, I forgo that option temporarily.
56
57// Some day it may be possible to have some built in AND some DLL/DSO
58// plugins coexisting.
59
60
61SCH_IO* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
62{
63 // This implementation is subject to change, any magic is allowed here.
64 // The public SCH_IO_MGR API is the only pertinent public information.
65
66 switch( aFileType )
67 {
68 case SCH_KICAD: return new SCH_IO_KICAD_SEXPR();
69 case SCH_LEGACY: return new SCH_IO_KICAD_LEGACY();
70 case SCH_ALTIUM: return new SCH_IO_ALTIUM();
71 case SCH_CADSTAR_ARCHIVE: return new SCH_IO_CADSTAR_ARCHIVE();
72 case SCH_DATABASE: return new SCH_IO_DATABASE();
73 case SCH_EAGLE: return new SCH_IO_EAGLE();
74 case SCH_EASYEDA: return new SCH_IO_EASYEDA();
75 case SCH_EASYEDAPRO: return new SCH_IO_EASYEDAPRO();
76 case SCH_LTSPICE: return new SCH_IO_LTSPICE();
77 case SCH_HTTP: return new SCH_IO_HTTP_LIB();
78 default: return nullptr;
79 }
80}
81
82
83const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
84{
85 // keep this function in sync with EnumFromStr() relative to the
86 // text spellings. If you change the spellings, you will obsolete
87 // library tables, so don't do change, only additions are ok.
88
89 switch( aType )
90 {
91 case SCH_KICAD: return wxString( wxT( "KiCad" ) );
92 case SCH_LEGACY: return wxString( wxT( "Legacy" ) );
93 case SCH_ALTIUM: return wxString( wxT( "Altium" ) );
94 case SCH_CADSTAR_ARCHIVE: return wxString( wxT( "CADSTAR Schematic Archive" ) );
95 case SCH_DATABASE: return wxString( wxT( "Database" ) );
96 case SCH_EAGLE: return wxString( wxT( "EAGLE" ) );
97 case SCH_EASYEDA: return wxString( wxT( "EasyEDA (JLCEDA) Std" ) );
98 case SCH_EASYEDAPRO: return wxString( wxT( "EasyEDA (JLCEDA) Pro" ) );
99 case SCH_LTSPICE: return wxString( wxT( "LTspice" ) );
100 case SCH_HTTP: return wxString( wxT( "HTTP" ) );
101 default: return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ),
102 aType );
103 }
104}
105
106
107SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
108{
109 // keep this function in sync with ShowType() relative to the
110 // text spellings. If you change the spellings, you will obsolete
111 // library tables, so don't do change, only additions are ok.
112
113 if( aType == wxT( "KiCad" ) )
114 return SCH_KICAD;
115 else if( aType == wxT( "Legacy" ) )
116 return SCH_LEGACY;
117 else if( aType == wxT( "Altium" ) )
118 return SCH_ALTIUM;
119 else if( aType == wxT( "CADSTAR Schematic Archive" ) )
120 return SCH_CADSTAR_ARCHIVE;
121 else if( aType == wxT( "Database" ) )
122 return SCH_DATABASE;
123 else if( aType == wxT( "EAGLE" ) )
124 return SCH_EAGLE;
125 else if( aType == wxT( "EasyEDA (JLCEDA) Std" ) )
126 return SCH_EASYEDA;
127 else if( aType == wxT( "EasyEDA (JLCEDA) Pro" ) )
128 return SCH_EASYEDAPRO;
129 else if( aType == wxT( "LTspice" ) )
130 return SCH_LTSPICE;
131 else if( aType == wxT( "HTTP" ) )
132 return SCH_HTTP;
133
134 // wxASSERT( blow up here )
135
136 return SCH_FILE_UNKNOWN;
137}
138
139
140SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath, int aCtl )
141{
142 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
143 {
144 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
145
146 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
147 continue;
148
149 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
150 continue;
151
152 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
153
154 if( !pi )
155 continue;
156
157 // For SCH_IO_MGR::SCH_KICAD and KICTL_CREATE option is set, use SCH_IO::CanReadLibrary()
158 // here instead of SCH_IO_KICAD_SEXPR::CanReadLibrary because aLibPath perhaps
159 // does notexist, and we need to use the version that does not test the existence
160 // of the file, just know if aLibPath file type can be handled.
161 if( fileType == SCH_IO_MGR::SCH_KICAD && ( aCtl & KICTL_CREATE ) )
162 {
163 if( pi->SCH_IO::CanReadLibrary( aLibPath ) ) // Test only the file ext
164 return fileType;
165 }
166 else
167 {
168 // Other lib types must be tested using the specific CanReadLibrary() and
169 // in some cases need to read the file
170 if( pi->CanReadLibrary( aLibPath ) )
171 return fileType;
172 }
173 }
174
175 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
176}
177
178
179SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath,
180 int aCtl )
181{
182 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
183 {
184 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
185
186 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
187 continue;
188
189 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
190 continue;
191
192 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
193
194 if( !pi )
195 continue;
196
197 if( pi->CanReadSchematicFile( aSchematicPath ) )
198 return fileType;
199 }
200
201 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
202}
203
204
205bool SCH_IO_MGR::ConvertLibrary( std::map<std::string, UTF8>* aOldFileProps, const wxString& aOldFilePath,
206 const wxString& aNewFilepath )
207{
208 SCH_IO_MGR::SCH_FILE_T oldFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aOldFilePath );
209
210 if( oldFileType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
211 return false;
212
213 IO_RELEASER<SCH_IO> oldFilePI( SCH_IO_MGR::FindPlugin( oldFileType ) );
214 IO_RELEASER<SCH_IO> kicadPI( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
215 std::vector<LIB_SYMBOL*> symbols;
216 std::vector<LIB_SYMBOL*> newSymbols;
217 std::map<LIB_SYMBOL*, LIB_SYMBOL*> symbolMap;
218
219 try
220 {
221 oldFilePI->EnumerateSymbolLib( symbols, aOldFilePath, aOldFileProps );
222
223 // Copy non-derived symbols first, so we can build a map from symbols to newSymbols
224 for( LIB_SYMBOL* symbol : symbols )
225 {
226 if( symbol->IsDerived() )
227 continue;
228
229 symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
230
231 newSymbols.push_back( new LIB_SYMBOL( *symbol ) );
232 symbolMap[symbol] = newSymbols.back();
233 }
234
235 // Now do the derived symbols using the map to hook them up to their newSymbol parents
236 for( LIB_SYMBOL* symbol : symbols )
237 {
238 if( !symbol->IsDerived() )
239 continue;
240
241 symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
242
243 newSymbols.push_back( new LIB_SYMBOL( *symbol ) );
244 newSymbols.back()->SetParent( symbolMap[ symbol->GetParent().lock().get() ] );
245 }
246
247 // Create a blank library
248 kicadPI->SaveLibrary( aNewFilepath );
249
250 // Finally write out newSymbols
251 for( LIB_SYMBOL* symbol : newSymbols )
252 {
253 kicadPI->SaveSymbol( aNewFilepath, symbol );
254 }
255 }
256 catch( ... )
257 {
258 return false;
259 }
260
261 return true;
262}
Define a library symbol object.
Definition: lib_symbol.h:85
A KiCad database library provides both symbol and footprint metadata, so there are "shim" plugins on ...
A SCH_IO derivation for loading 6.x+ Eagle schematic files.
Definition: sch_io_eagle.h:79
A KiCad HTTP library provides both symbol and footprint metadata, so there are "shim" plugins on both...
A SCH_IO derivation for loading schematic files created before the new s-expression file format.
A SCH_IO derivation for loading schematic files using the new s-expression file format.
static bool ConvertLibrary(std::map< std::string, UTF8 > *aOldFileProps, const wxString &aOldFilePath, const wxString &aNewFilepath)
Convert a schematic symbol library to the latest KiCad format.
Definition: sch_io_mgr.cpp:205
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the #SCH_FILE_T from the corresponding plugin type name: "kicad", "legacy",...
Definition: sch_io_mgr.cpp:107
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:83
static SCH_FILE_T GuessPluginTypeFromSchPath(const wxString &aSchematicPath, int aCtl=0)
Return a plugin type given a schematic using the file extension of aSchematicPath.
Definition: sch_io_mgr.cpp:179
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
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io.h:57
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
#define KICTL_CREATE
caller thinks requested project files may not exist.
Definition: kiway_player.h:76
#define KICTL_KICAD_ONLY
chosen file is from KiCad according to user
Definition: kiway_player.h:75
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
Definition: kiway_player.h:74
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
Definition of file extensions used in Kicad.