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