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
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <memory>
24
25#include <wx/filename.h>
26#include <wx/uri.h>
27
28#include <sch_io/sch_io_mgr.h>
32
47#include <common.h> // for ExpandEnvVarSubstitutions
48#include <ki_exception.h>
49
51#include <kiway_player.h>
52#include <string_utils.h>
54
55#define FMT_UNIMPLEMENTED _( "Plugin '%s' does not implement the '%s' function." )
56#define FMT_NOTFOUND _( "Plugin type '%s' is not found." )
57
58
59
60// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
61// and code size. Until then, use the simplest method:
62
63// This implementation is one of two which could be done.
64// The other one would cater to DLL/DSO's. But since it would be nearly
65// impossible to link a KICAD type DLL/DSO right now without pulling in all
66// ::Draw() functions, I forgo that option temporarily.
67
68// Some day it may be possible to have some built in AND some DLL/DSO
69// plugins coexisting.
70
71
72SCH_IO* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
73{
74 // This implementation is subject to change, any magic is allowed here.
75 // The public SCH_IO_MGR API is the only pertinent public information.
76
77 switch( aFileType )
78 {
79 case SCH_KICAD: return new SCH_IO_KICAD_SEXPR();
80 case SCH_LEGACY: return new SCH_IO_KICAD_LEGACY();
81 case SCH_ALTIUM: return new SCH_IO_ALTIUM();
82 case SCH_CADSTAR_ARCHIVE: return new SCH_IO_CADSTAR_ARCHIVE();
83 case SCH_DATABASE: return new SCH_IO_DATABASE();
84 case SCH_EAGLE: return new SCH_IO_EAGLE();
85 case SCH_EASYEDA: return new SCH_IO_EASYEDA();
86 case SCH_EASYEDAPRO: return new SCH_IO_EASYEDAPRO();
87 case SCH_EASYEDAPRO_V3: return new SCH_IO_EASYEDAPRO_V3();
88 case SCH_GEDA: return new SCH_IO_GEDA();
89 case SCH_LTSPICE: return new SCH_IO_LTSPICE();
90 case SCH_HTTP: return new SCH_IO_HTTP_LIB();
91 case SCH_PADS: return new SCH_IO_PADS();
92 case SCH_DIPTRACE: return new SCH_IO_DIPTRACE();
93 case SCH_PCAD: return new SCH_IO_PCAD();
94 case SCH_ORCAD: return new SCH_IO_ORCAD();
95 case SCH_EASYPC: return new SCH_IO_EASYPC();
96 default: return nullptr;
97 }
98}
99
100
101const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aFileType )
102{
103 // keep this function in sync with EnumFromStr() relative to the
104 // text spellings. If you change the spellings, you will obsolete
105 // library tables, so don't do change, only additions are ok.
106
107 switch( aFileType )
108 {
109 case SCH_KICAD: return wxString( wxT( "KiCad" ) );
110 case SCH_LEGACY: return wxString( wxT( "Legacy" ) );
111 case SCH_ALTIUM: return wxString( wxT( "Altium" ) );
112 case SCH_CADSTAR_ARCHIVE: return wxString( wxT( "CADSTAR Schematic Archive" ) );
113 case SCH_DATABASE: return wxString( wxT( "Database" ) );
114 case SCH_EAGLE: return wxString( wxT( "EAGLE" ) );
115 case SCH_EASYEDA: return wxString( wxT( "EasyEDA (JLCEDA) Std" ) );
116 case SCH_EASYEDAPRO: return wxString( wxT( "EasyEDA (JLCEDA) Pro" ) );
117 case SCH_EASYEDAPRO_V3: return wxString( wxT( "EasyEDA (JLCEDA) Pro v3" ) );
118 case SCH_GEDA: return wxString( wxT( "gEDA / Lepton EDA" ) );
119 case SCH_LTSPICE: return wxString( wxT( "LTspice" ) );
120 case SCH_HTTP: return wxString( wxT( "HTTP" ) );
121 case SCH_PADS: return wxString( wxT( "PADS Logic" ) );
122 case SCH_DIPTRACE: return wxString( wxT( "DipTrace" ) );
123 case SCH_PCAD: return wxString( wxT( "P-CAD" ) );
124 case SCH_ORCAD: return wxString( wxT( "OrCAD" ) );
125 case SCH_EASYPC: return wxString( wxT( "Easy-PC" ) );
126 case SCH_NESTED_TABLE: return LIBRARY_TABLE_ROW::TABLE_TYPE_NAME;
127 default: return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aFileType );
128 }
129}
130
131
132SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
133{
134 // keep this function in sync with ShowType() relative to the
135 // text spellings. If you change the spellings, you will obsolete
136 // library tables, so don't do change, only additions are ok.
137
138 if( aType == wxT( "KiCad" ) )
139 return SCH_KICAD;
140 else if( aType == wxT( "Legacy" ) )
141 return SCH_LEGACY;
142 else if( aType == wxT( "Altium" ) )
143 return SCH_ALTIUM;
144 else if( aType == wxT( "CADSTAR Schematic Archive" ) )
145 return SCH_CADSTAR_ARCHIVE;
146 else if( aType == wxT( "Database" ) )
147 return SCH_DATABASE;
148 else if( aType == wxT( "EAGLE" ) )
149 return SCH_EAGLE;
150 else if( aType == wxT( "EasyEDA (JLCEDA) Std" ) )
151 return SCH_EASYEDA;
152 else if( aType == wxT( "EasyEDA (JLCEDA) Pro" ) )
153 return SCH_EASYEDAPRO;
154 else if( aType == wxT( "EasyEDA (JLCEDA) Pro v3" ) )
155 return SCH_EASYEDAPRO_V3;
156 else if( aType == wxT( "gEDA / Lepton EDA" ) )
157 return SCH_GEDA;
158 else if( aType == wxT( "LTspice" ) )
159 return SCH_LTSPICE;
160 else if( aType == wxT( "HTTP" ) )
161 return SCH_HTTP;
162 else if( aType == wxT( "PADS Logic" ) )
163 return SCH_PADS;
164 else if( aType == wxT( "DipTrace" ) )
165 return SCH_DIPTRACE;
166 else if( aType == wxT( "P-CAD" ) )
167 return SCH_PCAD;
168 else if( aType == wxT( "OrCAD" ) )
169 return SCH_ORCAD;
170 else if( aType == wxT( "Easy-PC" ) )
171 return SCH_EASYPC;
172 else if( aType == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
173 return SCH_NESTED_TABLE;
174
175 return SCH_FILE_UNKNOWN;
176}
177
178
179SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath, int aCtl )
180{
182
183 if( parser.Parse( aLibPath.ToStdString() ).has_value() )
184 return SCH_NESTED_TABLE;
185
186 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
187 {
188 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
189
190 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
191 continue;
192
193 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
194 continue;
195
196 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
197
198 if( !pi )
199 continue;
200
201 // For SCH_IO_MGR::SCH_KICAD and KICTL_CREATE option is set, use SCH_IO::CanReadLibrary()
202 // here instead of SCH_IO_KICAD_SEXPR::CanReadLibrary because aLibPath perhaps
203 // does notexist, and we need to use the version that does not test the existence
204 // of the file, just know if aLibPath file type can be handled.
205 if( fileType == SCH_IO_MGR::SCH_KICAD && ( aCtl & KICTL_CREATE ) )
206 {
207 if( pi->SCH_IO::CanReadLibrary( aLibPath ) ) // Test only the file ext
208 return fileType;
209 }
210 else
211 {
212 // Other lib types must be tested using the specific CanReadLibrary() and
213 // in some cases need to read the file
214 if( pi->CanReadLibrary( aLibPath ) )
215 return fileType;
216 }
217 }
218
219 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
220}
221
222
223SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath, int aCtl )
224{
225 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
226 {
227 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
228
229 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
230 continue;
231
232 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
233 continue;
234
235 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
236
237 if( !pi )
238 continue;
239
240 if( pi->CanReadSchematicFile( aSchematicPath ) )
241 return fileType;
242 }
243
244 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
245}
246
247
248bool SCH_IO_MGR::ConvertLibrary( std::map<std::string, UTF8>* aOldFileProps, const wxString& aOldFilePath,
249 const wxString& aNewFilepath, REPORTER* aReporter )
250{
251 auto report = [&]( const wxString& aMessage, SEVERITY aSeverity )
252 {
253 if( aReporter )
254 aReporter->Report( aMessage, aSeverity );
255 };
256
257 SCH_IO_MGR::SCH_FILE_T oldFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aOldFilePath );
258
259 if( oldFileType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
260 {
261 report( _( "Unrecognized library type" ), RPT_SEVERITY_ERROR );
262 return false;
263 }
264
265 // A nested library table has no plugin to enumerate it; reject it before the null-plugin
266 // path below.
267 if( oldFileType == SCH_IO_MGR::SCH_NESTED_TABLE )
268 return false;
269
270 // Live backends are not convertible to a static .kicad_sym snapshot. HTTP requires a
271 // library manager adapter that ConvertLibrary cannot provide, and DATABASE has the same
272 // semantic mismatch. Without this guard the call silently produces an empty library.
273 if( oldFileType == SCH_IO_MGR::SCH_HTTP || oldFileType == SCH_IO_MGR::SCH_DATABASE )
274 return false;
275
276 IO_RELEASER<SCH_IO> oldFilePI( SCH_IO_MGR::FindPlugin( oldFileType ) );
277 IO_RELEASER<SCH_IO> kicadPI( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
278
279 if( !oldFilePI || !kicadPI )
280 return false;
281
282 if( aReporter )
283 oldFilePI->SetReporter( aReporter );
284
285 std::vector<LIB_SYMBOL*> symbols;
286 std::vector<std::unique_ptr<LIB_SYMBOL>> newSymbols;
287 std::map<LIB_SYMBOL*, LIB_SYMBOL*> symbolMap;
288
289 report( wxString::Format( _( "Loading symbol library '%s'" ), aOldFilePath ), RPT_SEVERITY_ACTION );
290
291 try
292 {
293 oldFilePI->EnumerateSymbolLib( symbols, aOldFilePath, aOldFileProps );
294
295 // Copy non-derived symbols first, so we can build a map from symbols to newSymbols
296 for( LIB_SYMBOL* symbol : symbols )
297 {
298 if( symbol->IsDerived() )
299 continue;
300
301 symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
302
303 newSymbols.push_back( std::make_unique<LIB_SYMBOL>( *symbol ) );
304 symbolMap[symbol] = newSymbols.back().get();
305 }
306
307 // Now do the derived symbols using the map to hook them up to their newSymbol parents
308 for( LIB_SYMBOL* symbol : symbols )
309 {
310 if( !symbol->IsDerived() )
311 continue;
312
313 symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
314
315 newSymbols.push_back( std::make_unique<LIB_SYMBOL>( *symbol ) );
316 newSymbols.back()->SetParent( symbolMap[ symbol->GetParent().lock().get() ] );
317 }
318
319 // Create a blank library
320 kicadPI->SaveLibrary( aNewFilepath );
321 }
322 catch( const IO_ERROR& io_err )
323 {
324 report( wxString::Format( _( "Library '%s' Convert err: \"%s\"" ), aOldFilePath, io_err.What() ),
326 return false;
327 }
328 catch( const std::exception& e )
329 {
330 report( wxString::Format( _( "Error loading library '%s': %s" ), aOldFilePath, e.what() ), RPT_SEVERITY_ERROR );
331 return false;
332 }
333 catch( ... )
334 {
335 report( wxString::Format( _( "Error loading library '%s': unknown error" ), aOldFilePath ),
337 return false;
338 }
339
340 bool ok = true;
341
342 for( std::unique_ptr<LIB_SYMBOL>& symbol : newSymbols )
343 {
344 const wxString symbolName = symbol->GetName();
345
346 try
347 {
348 kicadPI->SaveSymbol( aNewFilepath, std::move( symbol ) );
349 }
350 catch( const IO_ERROR& io_err )
351 {
352 report( wxString::Format( _( "Error saving symbol '%s': %s" ), symbolName, io_err.What() ),
354 ok = false;
355 }
356 catch( const std::exception& e )
357 {
358 report( wxString::Format( _( "Error saving symbol '%s': %s" ), symbolName, e.what() ),
360 ok = false;
361 }
362 catch( ... )
363 {
364 report( wxString::Format( _( "Error saving symbol '%s': unknown error" ), symbolName ),
366 ok = false;
367 }
368 }
369
370 return ok;
371}
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()
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > Parse(const std::filesystem::path &aPath)
static const wxString TABLE_TYPE_NAME
Define a library symbol object.
Definition lib_symbol.h:114
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
A KiCad database library provides both symbol and footprint metadata, so there are "shim" plugins on ...
A SCH_IO derivation for loading DipTrace schematic files (.dch).
A SCH_IO derivation for loading Eagle schematic files.
Imports Number One Systems Easy-PC and RS DesignSpark PCB schematics (.sch), projects (....
A SCH_IO derivation for loading gEDA/gschem schematic files (.sch).
Definition sch_io_geda.h:74
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, REPORTER *aReporter=nullptr)
Convert a schematic symbol library to the latest KiCad format.
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the file type from the corresponding plugin type name: "kicad", "legacy", etc.
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
static SCH_FILE_T GuessPluginTypeFromSchPath(const wxString &aSchematicPath, int aCtl=0)
Return a plugin type given a schematic using the file extension of aSchematicPath.
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.
A SCH_IO derivation for loading OrCAD Capture schematic designs (.dsn).
A SCH_IO derivation for loading PADS Logic schematic files.
Definition sch_io_pads.h:39
A SCH_IO derivation for loading P-CAD 2006 ASCII schematic files (.SCH).
Definition sch_io_pcad.h:49
Base class that schematic file and library loading and saving plugins should derive from.
Definition sch_io.h:60
#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.
#define KICTL_KICAD_ONLY
chosen file is from KiCad according to user
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
SEVERITY
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_ACTION
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 of file extensions used in Kicad.