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