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 (C) 2016-2023 KiCad Developers, see change_log.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 if( pi->CanReadLibrary( aLibPath ) )
158 return fileType;
159 }
160
161 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
162}
163
164
165SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath,
166 int aCtl )
167{
168 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
169 {
170 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
171
172 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
173 continue;
174
175 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
176 continue;
177
178 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( fileType ) );
179
180 if( !pi )
181 continue;
182
183 if( pi->CanReadSchematicFile( aSchematicPath ) )
184 return fileType;
185 }
186
187 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
188}
189
190
191bool SCH_IO_MGR::ConvertLibrary( STRING_UTF8_MAP* aOldFileProps, const wxString& aOldFilePath,
192 const wxString& aNewFilepath )
193{
194 SCH_IO_MGR::SCH_FILE_T oldFileType = SCH_IO_MGR::GuessPluginTypeFromLibPath( aOldFilePath );
195
196 if( oldFileType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
197 return false;
198
199 IO_RELEASER<SCH_IO> oldFilePI( SCH_IO_MGR::FindPlugin( oldFileType ) );
200 IO_RELEASER<SCH_IO> kicadPI( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
201 std::vector<LIB_SYMBOL*> symbols;
202 std::vector<LIB_SYMBOL*> newSymbols;
203 std::map<LIB_SYMBOL*, LIB_SYMBOL*> symbolMap;
204
205 try
206 {
207 oldFilePI->EnumerateSymbolLib( symbols, aOldFilePath, aOldFileProps );
208
209 // Copy non-aliases first, so we can build a map from symbols to newSymbols
210 for( LIB_SYMBOL* symbol : symbols )
211 {
212 if( symbol->IsAlias() )
213 continue;
214
215 symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
216
217 newSymbols.push_back( new LIB_SYMBOL( *symbol ) );
218 symbolMap[symbol] = newSymbols.back();
219 }
220
221 // Now do the aliases using the map to hook them up to their newSymbol parents
222 for( LIB_SYMBOL* symbol : symbols )
223 {
224 if( !symbol->IsAlias() )
225 continue;
226
227 symbol->SetName( EscapeString( symbol->GetName(), CTX_LIBID ) );
228
229 newSymbols.push_back( new LIB_SYMBOL( *symbol ) );
230 newSymbols.back()->SetParent( symbolMap[ symbol->GetParent().lock().get() ] );
231 }
232
233 // Create a blank library
234 kicadPI->SaveLibrary( aNewFilepath );
235
236 // Finally write out newSymbols
237 for( LIB_SYMBOL* symbol : newSymbols )
238 {
239 kicadPI->SaveSymbol( aNewFilepath, symbol );
240 }
241 }
242 catch( ... )
243 {
244 return false;
245 }
246
247 return true;
248}
Define a library symbol object.
Definition: lib_symbol.h:77
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:76
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 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:165
static bool ConvertLibrary(STRING_UTF8_MAP *aOldFileProps, const wxString &aOldFilePath, const wxString &aNewFilepath)
Convert a schematic symbol library to the latest KiCad format.
Definition: sch_io_mgr.cpp:191
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
A name/value tuple with unique names and optional values.
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_KICAD_ONLY
chosen file is from KiCad according to user
Definition: kiway_player.h:77
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
Definition: kiway_player.h:76
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.