KiCad PCB EDA Suite
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-2021 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
35
36#define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
37#define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
38
39
40
41// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
42// and code size. Until then, use the simplest method:
43
44// This implementation is one of two which could be done.
45// The other one would cater to DLL/DSO's. But since it would be nearly
46// impossible to link a KICAD type DLL/DSO right now without pulling in all
47// ::Draw() functions, I forgo that option temporarily.
48
49// Some day it may be possible to have some built in AND some DLL/DSO
50// plugins coexisting.
51
52
53SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
54{
55 // This implementation is subject to change, any magic is allowed here.
56 // The public SCH_IO_MGR API is the only pertinent public information.
57
58 switch( aFileType )
59 {
60 case SCH_LEGACY: return new SCH_LEGACY_PLUGIN();
61 case SCH_KICAD: return new SCH_SEXPR_PLUGIN();
62 case SCH_ALTIUM: return new SCH_ALTIUM_PLUGIN();
63 case SCH_CADSTAR_ARCHIVE: return new CADSTAR_SCH_ARCHIVE_PLUGIN();
64 case SCH_EAGLE: return new SCH_EAGLE_PLUGIN();
65 case SCH_DATABASE: return new SCH_DATABASE_PLUGIN();
66 default: return nullptr;
67 }
68}
69
70
72{
73 // This function is a place holder for a future point in time where
74 // the plugin is a DLL/DSO. It could do reference counting, and then
75 // unload the DLL/DSO when count goes to zero.
76
77 delete aPlugin;
78}
79
80
81const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
82{
83 // keep this function in sync with EnumFromStr() relative to the
84 // text spellings. If you change the spellings, you will obsolete
85 // library tables, so don't do change, only additions are ok.
86
87 switch( aType )
88 {
89 case SCH_LEGACY: return wxString( wxT( "Legacy" ) );
90 case SCH_KICAD: return wxString( wxT( "KiCad" ) );
91 case SCH_ALTIUM: return wxString( wxT( "Altium" ) );
92 case SCH_CADSTAR_ARCHIVE: return wxString( wxT( "CADSTAR Schematic Archive" ) );
93 case SCH_EAGLE: return wxString( wxT( "EAGLE" ) );
94 case SCH_DATABASE: return wxString( wxT( "Database" ) );
95 default: return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ),
96 aType );
97 }
98}
99
100
101SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
102{
103 // keep this function in sync with ShowType() relative to the
104 // text spellings. If you change the spellings, you will obsolete
105 // library tables, so don't do change, only additions are ok.
106
107 if( aType == wxT( "Legacy" ) )
108 return SCH_LEGACY;
109 else if( aType == wxT( "KiCad" ) )
110 return SCH_KICAD;
111 else if( aType == wxT( "Altium" ) )
112 return SCH_ALTIUM;
113 else if( aType == wxT( "CADSTAR Schematic Archive" ) )
114 return SCH_CADSTAR_ARCHIVE;
115 else if( aType == wxT( "EAGLE" ) )
116 return SCH_EAGLE;
117 else if( aType == wxT( "Database" ) )
118 return SCH_DATABASE;
119
120 // wxASSERT( blow up here )
121
122 return SCH_FILE_T( -1 );
123}
124
125
126const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
127{
128 wxString ext = wxEmptyString;
129 SCH_PLUGIN* plugin = FindPlugin( aFileType );
130
131 if( plugin != nullptr )
132 {
133 ext = plugin->GetFileExtension();
134 ReleasePlugin( plugin );
135 }
136
137 return ext;
138}
139
140
141const wxString SCH_IO_MGR::GetLibraryFileExtension( SCH_FILE_T aFileType )
142{
143 wxString ext = wxEmptyString;
144 SCH_PLUGIN* plugin = FindPlugin( aFileType );
145
146 if( plugin != nullptr )
147 {
148 ext = plugin->GetLibraryFileExtension();
149 ReleasePlugin( plugin );
150 }
151
152 return ext;
153}
154
155
156SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
157{
158 SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
159 wxFileName fn( aLibPath );
160
161 if( fn.GetExt() == LegacySymbolLibFileExtension )
162 {
163 ret = SCH_LEGACY;
164 }
165 else if( fn.GetExt() == KiCadSymbolLibFileExtension )
166 {
167 ret = SCH_KICAD;
168 }
169
170 return ret;
171}
172
173
174SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath )
175{
176 SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
177 wxFileName fn( aSchematicPath );
178
179 if( fn.GetExt() == LegacySchematicFileExtension )
180 {
181 ret = SCH_LEGACY;
182 }
183 else if( fn.GetExt() == KiCadSchematicFileExtension )
184 {
185 ret = SCH_KICAD;
186 }
187
188 return ret;
189}
190
191
192DECLARE_ENUM_VECTOR( SCH_IO_MGR, SCH_FILE_T )
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.
A factory which returns an instance of a SCH_PLUGIN.
Definition: sch_io_mgr.h:49
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:101
static const wxString GetFileExtension(SCH_FILE_T aFileType)
Return the schematic file extension for aFileType.
Definition: sch_io_mgr.cpp:126
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:81
static const wxString GetLibraryFileExtension(SCH_FILE_T aFileType)
Return the symbol library file extension (if any) for aFileType.
Definition: sch_io_mgr.cpp:141
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:71
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:174
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:156
A SCH_PLUGIN derivation for loading schematic files created before the new s-expression file format.
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:155
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.
A SCH_PLUGIN derivation for loading schematic files using the new s-expression file format.
#define _(s)
#define DECLARE_ENUM_VECTOR(className, enumName)
Definition: enum_vector.h:48
const std::string KiCadSymbolLibFileExtension
const std::string LegacySchematicFileExtension
const std::string KiCadSchematicFileExtension
const std::string LegacySymbolLibFileExtension
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Definition of file extensions used in Kicad.