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_mgr.h>
30
36#include <common.h> // for ExpandEnvVarSubstitutions
37
38#define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
39#define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
40
41
42
43// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
44// and code size. Until then, use the simplest method:
45
46// This implementation is one of two which could be done.
47// The other one would cater to DLL/DSO's. But since it would be nearly
48// impossible to link a KICAD type DLL/DSO right now without pulling in all
49// ::Draw() functions, I forgo that option temporarily.
50
51// Some day it may be possible to have some built in AND some DLL/DSO
52// plugins coexisting.
53
54
55SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
56{
57 // This implementation is subject to change, any magic is allowed here.
58 // The public SCH_IO_MGR API is the only pertinent public information.
59
60 switch( aFileType )
61 {
62 case SCH_LEGACY: return new SCH_LEGACY_PLUGIN();
63 case SCH_KICAD: return new SCH_SEXPR_PLUGIN();
64 case SCH_ALTIUM: return new SCH_ALTIUM_PLUGIN();
65 case SCH_CADSTAR_ARCHIVE: return new CADSTAR_SCH_ARCHIVE_PLUGIN();
66 case SCH_EAGLE: return new SCH_EAGLE_PLUGIN();
67 case SCH_DATABASE: return new SCH_DATABASE_PLUGIN();
68 case SCH_LTSPICE: return new SCH_LTSPICE_PLUGIN();
69 default: return nullptr;
70 }
71}
72
73
75{
76 // This function is a place holder for a future point in time where
77 // the plugin is a DLL/DSO. It could do reference counting, and then
78 // unload the DLL/DSO when count goes to zero.
79
80 delete aPlugin;
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_LEGACY: return wxString( wxT( "Legacy" ) );
93 case SCH_KICAD: return wxString( wxT( "KiCad" ) );
94 case SCH_ALTIUM: return wxString( wxT( "Altium" ) );
95 case SCH_CADSTAR_ARCHIVE: return wxString( wxT( "CADSTAR Schematic Archive" ) );
96 case SCH_EAGLE: return wxString( wxT( "EAGLE" ) );
97 case SCH_DATABASE: return wxString( wxT( "Database" ) );
98 case SCH_LTSPICE: return wxString( wxT( "LTSpice Schematic" ) );
99 default: return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ),
100 aType );
101 }
102}
103
104
105SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
106{
107 // keep this function in sync with ShowType() relative to the
108 // text spellings. If you change the spellings, you will obsolete
109 // library tables, so don't do change, only additions are ok.
110
111 if( aType == wxT( "Legacy" ) )
112 return SCH_LEGACY;
113 else if( aType == wxT( "KiCad" ) )
114 return SCH_KICAD;
115 else if( aType == wxT( "Altium" ) )
116 return SCH_ALTIUM;
117 else if( aType == wxT( "CADSTAR Schematic Archive" ) )
118 return SCH_CADSTAR_ARCHIVE;
119 else if( aType == wxT( "EAGLE" ) )
120 return SCH_EAGLE;
121 else if( aType == wxT( "Database" ) )
122 return SCH_DATABASE;
123
124 // wxASSERT( blow up here )
125
126 return SCH_FILE_T( -1 );
127}
128
129
130const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
131{
132 wxString ext = wxEmptyString;
133 SCH_PLUGIN* plugin = FindPlugin( aFileType );
134
135 if( plugin != nullptr )
136 {
137 ext = plugin->GetFileExtension();
138 ReleasePlugin( plugin );
139 }
140
141 return ext;
142}
143
144
145const wxString SCH_IO_MGR::GetLibraryFileExtension( SCH_FILE_T aFileType )
146{
147 wxString ext = wxEmptyString;
148 SCH_PLUGIN* plugin = FindPlugin( aFileType );
149
150 if( plugin != nullptr )
151 {
152 ext = plugin->GetLibraryFileExtension();
153 ReleasePlugin( plugin );
154 }
155
156 return ext;
157}
158
159
160SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
161{
162 SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
163 wxFileName fn( aLibPath );
164 wxString ext = fn.GetExt().Lower();
165
166 // .lib is shared between CADSTAR and Legacy KiCad file formats. Let's read the header
168 {
169 wxString fullName = ExpandEnvVarSubstitutions( aLibPath, nullptr );
170
171 // Of course the file should exist to be read. If not, use the SCH_LEGACY
172 // format: it is more usual than SCH_CADSTAR_ARCHIVE
173 if( !wxFileExists( fullName ) )
174 return SCH_LEGACY;
175
176 for( SCH_FILE_T pluginType : { SCH_LEGACY, SCH_CADSTAR_ARCHIVE } )
177 {
178 SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( pluginType ) );
179
180 if( pi )
181 {
182 if( pi->CheckHeader( fullName ) )
183 return pluginType;
184 }
185 }
186
187 // If not found, use the SCH_LEGACY.
188 return SCH_LEGACY;
189 }
190
191 for( SCH_IO_MGR::SCH_FILE_T piType : SCH_IO_MGR::SCH_FILE_T_vector )
192 {
193 if( SCH_IO_MGR::GetLibraryFileExtension( piType ).Lower() == ext )
194 {
195 ret = piType;
196 break;
197 }
198 }
199
200 return ret;
201}
202
203
204SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath )
205{
206 SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
207 wxFileName fn( aSchematicPath );
208
209 if( fn.GetExt() == LegacySchematicFileExtension )
210 {
211 ret = SCH_LEGACY;
212 }
213 else if( fn.GetExt() == KiCadSchematicFileExtension )
214 {
215 ret = SCH_KICAD;
216 }
217
218 return ret;
219}
SCH_ALTIUM_PLUGIN is a SCH_PLUGIN derivation for loading Altium .SchDoc schematic files.
A KiCad database library provides both symbol and footprint metadata, so there are "shim" plugins on ...
A SCH_PLUGIN derivation for loading 6.x+ Eagle schematic files.
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:105
static const wxString GetFileExtension(SCH_FILE_T aFileType)
Return the schematic file extension for aFileType.
Definition: sch_io_mgr.cpp:130
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:84
static const wxString GetLibraryFileExtension(SCH_FILE_T aFileType)
Return the symbol library file extension (if any) for aFileType.
Definition: sch_io_mgr.cpp:145
static void ReleasePlugin(SCH_PLUGIN *aPlugin)
Release a SCH_PLUGIN back to the system, and may cause it to be unloaded from memory.
Definition: sch_io_mgr.cpp:74
static SCH_FILE_T GuessPluginTypeFromSchPath(const wxString &aSchematicPath)
Return a plugin type given a schematic using the file extension of aSchematicPath.
Definition: sch_io_mgr.cpp:204
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Return a plugin type given a symbol library using the file extension of aLibPath.
Definition: sch_io_mgr.cpp:160
A SCH_PLUGIN derivation for loading schematic files created before the new s-expression file format.
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:536
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:156
virtual const wxString GetLibraryFileExtension() const =0
Return the library file extension for the SCH_PLUGIN object.
virtual const wxString GetFileExtension() const =0
Return the file extension for the SCH_PLUGIN.
virtual bool CheckHeader(const wxString &aFileName)
Return true if the first line in aFileName begins with the expected header.
Definition: sch_plugin.cpp:131
A SCH_PLUGIN derivation for loading schematic files using the new s-expression file format.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:299
The common library.
#define _(s)
const std::string LegacySchematicFileExtension
const std::string KiCadSchematicFileExtension
const std::string LegacySymbolLibFileExtension
Definition of file extensions used in Kicad.