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
40#include <common.h> // for ExpandEnvVarSubstitutions
41
43#include <kiway_player.h>
44#include <string_utils.h>
46
47#define FMT_UNIMPLEMENTED _( "Plugin '%s' does not implement the '%s' function." )
48#define FMT_NOTFOUND _( "Plugin type '%s' is not found." )
49
50
51
52// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
53// and code size. Until then, use the simplest method:
54
55// This implementation is one of two which could be done.
56// The other one would cater to DLL/DSO's. But since it would be nearly
57// impossible to link a KICAD type DLL/DSO right now without pulling in all
58// ::Draw() functions, I forgo that option temporarily.
59
60// Some day it may be possible to have some built in AND some DLL/DSO
61// plugins coexisting.
62
63
64SCH_IO* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
65{
66 // This implementation is subject to change, any magic is allowed here.
67 // The public SCH_IO_MGR API is the only pertinent public information.
68
69 switch( aFileType )
70 {
71 case SCH_KICAD: return new SCH_IO_KICAD_SEXPR();
72 case SCH_LEGACY: return new SCH_IO_KICAD_LEGACY();
73 case SCH_ALTIUM: return new SCH_IO_ALTIUM();
74 case SCH_CADSTAR_ARCHIVE: return new SCH_IO_CADSTAR_ARCHIVE();
75 case SCH_DATABASE: return new SCH_IO_DATABASE();
76 case SCH_EAGLE: return new SCH_IO_EAGLE();
77 case SCH_EASYEDA: return new SCH_IO_EASYEDA();
78 case SCH_EASYEDAPRO: return new SCH_IO_EASYEDAPRO();
79 case SCH_GEDA: return new SCH_IO_GEDA();
80 case SCH_LTSPICE: return new SCH_IO_LTSPICE();
81 case SCH_HTTP: return new SCH_IO_HTTP_LIB();
82 case SCH_PADS: return new SCH_IO_PADS();
83 default: return nullptr;
84 }
85}
86
87
88const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
89{
90 // keep this function in sync with EnumFromStr() relative to the
91 // text spellings. If you change the spellings, you will obsolete
92 // library tables, so don't do change, only additions are ok.
93
94 switch( aType )
95 {
96 case SCH_KICAD: return wxString( wxT( "KiCad" ) );
97 case SCH_LEGACY: return wxString( wxT( "Legacy" ) );
98 case SCH_ALTIUM: return wxString( wxT( "Altium" ) );
99 case SCH_CADSTAR_ARCHIVE: return wxString( wxT( "CADSTAR Schematic Archive" ) );
100 case SCH_DATABASE: return wxString( wxT( "Database" ) );
101 case SCH_EAGLE: return wxString( wxT( "EAGLE" ) );
102 case SCH_EASYEDA: return wxString( wxT( "EasyEDA (JLCEDA) Std" ) );
103 case SCH_EASYEDAPRO: return wxString( wxT( "EasyEDA (JLCEDA) Pro" ) );
104 case SCH_GEDA: return wxString( wxT( "gEDA / Lepton EDA" ) );
105 case SCH_LTSPICE: return wxString( wxT( "LTspice" ) );
106 case SCH_HTTP: return wxString( wxT( "HTTP" ) );
107 case SCH_PADS: return wxString( wxT( "PADS Logic" ) );
108 case SCH_NESTED_TABLE: return LIBRARY_TABLE_ROW::TABLE_TYPE_NAME;
109 default: return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aType );
110 }
111}
112
113
114SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
115{
116 // keep this function in sync with ShowType() relative to the
117 // text spellings. If you change the spellings, you will obsolete
118 // library tables, so don't do change, only additions are ok.
119
120 if( aType == wxT( "KiCad" ) )
121 return SCH_KICAD;
122 else if( aType == wxT( "Legacy" ) )
123 return SCH_LEGACY;
124 else if( aType == wxT( "Altium" ) )
125 return SCH_ALTIUM;
126 else if( aType == wxT( "CADSTAR Schematic Archive" ) )
127 return SCH_CADSTAR_ARCHIVE;
128 else if( aType == wxT( "Database" ) )
129 return SCH_DATABASE;
130 else if( aType == wxT( "EAGLE" ) )
131 return SCH_EAGLE;
132 else if( aType == wxT( "EasyEDA (JLCEDA) Std" ) )
133 return SCH_EASYEDA;
134 else if( aType == wxT( "EasyEDA (JLCEDA) Pro" ) )
135 return SCH_EASYEDAPRO;
136 else if( aType == wxT( "gEDA / Lepton EDA" ) )
137 return SCH_GEDA;
138 else if( aType == wxT( "LTspice" ) )
139 return SCH_LTSPICE;
140 else if( aType == wxT( "HTTP" ) )
141 return SCH_HTTP;
142 else if( aType == wxT( "PADS Logic" ) )
143 return SCH_PADS;
144 else if( aType == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
145 return SCH_NESTED_TABLE;
146
147 return SCH_FILE_UNKNOWN;
148}
149
150
151SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath, int aCtl )
152{
154
155 if( parser.Parse( aLibPath.ToStdString() ).has_value() )
156 return SCH_NESTED_TABLE;
157
158 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
159 {
160 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
161
162 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
163 continue;
164
165 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
166 continue;
167
168 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
169
170 if( !pi )
171 continue;
172
173 // For SCH_IO_MGR::SCH_KICAD and KICTL_CREATE option is set, use SCH_IO::CanReadLibrary()
174 // here instead of SCH_IO_KICAD_SEXPR::CanReadLibrary because aLibPath perhaps
175 // does notexist, and we need to use the version that does not test the existence
176 // of the file, just know if aLibPath file type can be handled.
177 if( fileType == SCH_IO_MGR::SCH_KICAD && ( aCtl & KICTL_CREATE ) )
178 {
179 if( pi->SCH_IO::CanReadLibrary( aLibPath ) ) // Test only the file ext
180 return fileType;
181 }
182 else
183 {
184 // Other lib types must be tested using the specific CanReadLibrary() and
185 // in some cases need to read the file
186 if( pi->CanReadLibrary( aLibPath ) )
187 return fileType;
188 }
189 }
190
191 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
192}
193
194
195SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath,
196 int aCtl )
197{
198 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
199 {
200 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
201
202 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
203 continue;
204
205 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
206 continue;
207
208 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
209
210 if( !pi )
211 continue;
212
213 if( pi->CanReadSchematicFile( aSchematicPath ) )
214 return fileType;
215 }
216
217 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
218}
219
220
221bool SCH_IO_MGR::ConvertLibrary( std::map<std::string, UTF8>* aOldFileProps, const wxString& aOldFilePath,
222 const wxString& aNewFilepath )
223{
224 SCH_IO_MGR::SCH_FILE_T oldFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aOldFilePath );
225
226 if( oldFileType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
227 return false;
228
229 IO_RELEASER<SCH_IO> oldFilePI( SCH_IO_MGR::FindPlugin( oldFileType ) );
230 IO_RELEASER<SCH_IO> kicadPI( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
231
232 if( !oldFilePI || !kicadPI )
233 return false;
234
235 std::vector<LIB_SYMBOL*> symbols;
236 std::vector<LIB_SYMBOL*> newSymbols;
237 std::map<LIB_SYMBOL*, LIB_SYMBOL*> symbolMap;
238
239 try
240 {
241 oldFilePI->EnumerateSymbolLib( symbols, aOldFilePath, aOldFileProps );
242
243 // Copy non-derived symbols first, so we can build a map from symbols to newSymbols
244 for( LIB_SYMBOL* symbol : symbols )
245 {
246 if( symbol->IsDerived() )
247 continue;
248
249 symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
250
251 newSymbols.push_back( new LIB_SYMBOL( *symbol ) );
252 symbolMap[symbol] = newSymbols.back();
253 }
254
255 // Now do the derived symbols using the map to hook them up to their newSymbol parents
256 for( LIB_SYMBOL* symbol : symbols )
257 {
258 if( !symbol->IsDerived() )
259 continue;
260
261 symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
262
263 newSymbols.push_back( new LIB_SYMBOL( *symbol ) );
264 newSymbols.back()->SetParent( symbolMap[ symbol->GetParent().lock().get() ] );
265 }
266
267 // Create a blank library
268 kicadPI->SaveLibrary( aNewFilepath );
269
270 // Finally write out newSymbols
271 for( LIB_SYMBOL* symbol : newSymbols )
272 {
273 kicadPI->SaveSymbol( aNewFilepath, symbol );
274 }
275 }
276 catch( ... )
277 {
278 return false;
279 }
280
281 return true;
282}
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:83
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.
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)
Convert a schematic symbol library to the latest KiCad format.
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the #SCH_FILE_T from the corresponding plugin type name: "kicad", "legacy",...
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 PADS Logic schematic files.
Definition sch_io_pads.h:40
Base class that schematic file and library loading and saving plugins should derive from.
Definition sch_io.h:59
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.
#define KICTL_KICAD_ONLY
chosen file is from KiCad according to user
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
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.