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
38#include <common.h> // for ExpandEnvVarSubstitutions
39
41#include <kiway_player.h>
42
43#define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
44#define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
45
46
47
48// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
49// and code size. Until then, use the simplest method:
50
51// This implementation is one of two which could be done.
52// The other one would cater to DLL/DSO's. But since it would be nearly
53// impossible to link a KICAD type DLL/DSO right now without pulling in all
54// ::Draw() functions, I forgo that option temporarily.
55
56// Some day it may be possible to have some built in AND some DLL/DSO
57// plugins coexisting.
58
59
60SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
61{
62 // This implementation is subject to change, any magic is allowed here.
63 // The public SCH_IO_MGR API is the only pertinent public information.
64
65 switch( aFileType )
66 {
67 case SCH_KICAD: return new SCH_SEXPR_PLUGIN();
68 case SCH_LEGACY: return new SCH_LEGACY_PLUGIN();
69 case SCH_ALTIUM: return new SCH_ALTIUM_PLUGIN();
70 case SCH_CADSTAR_ARCHIVE: return new CADSTAR_SCH_ARCHIVE_PLUGIN();
71 case SCH_DATABASE: return new SCH_DATABASE_PLUGIN();
72 case SCH_EAGLE: return new SCH_EAGLE_PLUGIN();
73 case SCH_EASYEDA: return new SCH_EASYEDA_PLUGIN();
74 case SCH_EASYEDAPRO: return new SCH_EASYEDAPRO_PLUGIN();
75 case SCH_LTSPICE: return new SCH_LTSPICE_PLUGIN();
76 case SCH_HTTP: return new SCH_HTTP_LIB_PLUGIN();
77 default: return nullptr;
78 }
79}
80
81
83{
84 // This function is a place holder for a future point in time where
85 // the plugin is a DLL/DSO. It could do reference counting, and then
86 // unload the DLL/DSO when count goes to zero.
87
88 delete aPlugin;
89}
90
91
92const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
93{
94 // keep this function in sync with EnumFromStr() relative to the
95 // text spellings. If you change the spellings, you will obsolete
96 // library tables, so don't do change, only additions are ok.
97
98 switch( aType )
99 {
100 case SCH_KICAD: return wxString( wxT( "KiCad" ) );
101 case SCH_LEGACY: return wxString( wxT( "Legacy" ) );
102 case SCH_ALTIUM: return wxString( wxT( "Altium" ) );
103 case SCH_CADSTAR_ARCHIVE: return wxString( wxT( "CADSTAR Schematic Archive" ) );
104 case SCH_DATABASE: return wxString( wxT( "Database" ) );
105 case SCH_EAGLE: return wxString( wxT( "EAGLE" ) );
106 case SCH_EASYEDA: return wxString( wxT( "EasyEDA (JLCEDA) Std" ) );
107 case SCH_EASYEDAPRO: return wxString( wxT( "EasyEDA (JLCEDA) Pro" ) );
108 case SCH_LTSPICE: return wxString( wxT( "LTspice" ) );
109 case SCH_HTTP: return wxString( wxT( "HTTP" ) );
110 default: return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ),
111 aType );
112 }
113}
114
115
116SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
117{
118 // keep this function in sync with ShowType() relative to the
119 // text spellings. If you change the spellings, you will obsolete
120 // library tables, so don't do change, only additions are ok.
121
122 if( aType == wxT( "KiCad" ) )
123 return SCH_KICAD;
124 else if( aType == wxT( "Legacy" ) )
125 return SCH_LEGACY;
126 else if( aType == wxT( "Altium" ) )
127 return SCH_ALTIUM;
128 else if( aType == wxT( "CADSTAR Schematic Archive" ) )
129 return SCH_CADSTAR_ARCHIVE;
130 else if( aType == wxT( "Database" ) )
131 return SCH_DATABASE;
132 else if( aType == wxT( "EAGLE" ) )
133 return SCH_EAGLE;
134 else if( aType == wxT( "EasyEDA (JLCEDA) Std" ) )
135 return SCH_EASYEDA;
136 else if( aType == wxT( "EasyEDA (JLCEDA) Pro" ) )
137 return SCH_EASYEDAPRO;
138 else if( aType == wxT( "LTspice" ) )
139 return SCH_LTSPICE;
140 else if( aType == wxT( "HTTP" ) )
141 return SCH_HTTP;
142
143 // wxASSERT( blow up here )
144
145 return SCH_FILE_UNKNOWN;
146}
147
148
149SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath, int aCtl )
150{
151 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
152 {
153 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
154
155 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
156 continue;
157
158 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
159 continue;
160
161 SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( fileType ) );
162
163 if( !pi )
164 continue;
165
166 if( pi->CanReadLibrary( aLibPath ) )
167 return fileType;
168 }
169
170 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
171}
172
173
174SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath,
175 int aCtl )
176{
177 for( const SCH_IO_MGR::SCH_FILE_T& fileType : SCH_IO_MGR::SCH_FILE_T_vector )
178 {
179 bool isKiCad = fileType == SCH_IO_MGR::SCH_KICAD || fileType == SCH_IO_MGR::SCH_LEGACY;
180
181 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
182 continue;
183
184 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
185 continue;
186
187 SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( fileType ) );
188
189 if( !pi )
190 continue;
191
192 if( pi->CanReadSchematicFile( aSchematicPath ) )
193 return fileType;
194 }
195
196 return SCH_IO_MGR::SCH_FILE_UNKNOWN;
197}
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.
A KiCad HTTP library provides both symbol and footprint metadata, so there are "shim" plugins on both...
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:116
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:92
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:174
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:82
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:149
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:530
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:145
virtual bool CanReadSchematicFile(const wxString &aFileName) const
Checks if this SCH_PLUGIN can read the specified schematic file.
Definition: sch_plugin.cpp:51
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this SCH_PLUGIN can read the specified symbol library file.
Definition: sch_plugin.cpp:67
A SCH_PLUGIN derivation for loading schematic files using the new s-expression file format.
The common library.
#define _(s)
#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)
Definition of file extensions used in Kicad.