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-2020 KiCad Developers, see change_log.txt for contributors.
6  *
7  * @author Wayne Stambaugh <stambaughw@gmail.com>
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>
27 #include <sch_legacy_plugin.h>
28 #include <sch_eagle_plugin.h>
29 #include <sch_sexpr_plugin.h>
30 
32 
33 #define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
34 #define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
35 
36 
37 
38 // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
39 // and code size. Until then, use the simplest method:
40 
41 // This implementation is one of two which could be done.
42 // The other one would cater to DLL/DSO's. But since it would be nearly
43 // impossible to link a KICAD type DLL/DSO right now without pulling in all
44 // ::Draw() functions, I forgo that option temporarily.
45 
46 // Some day it may be possible to have some built in AND some DLL/DSO
47 // plugins coexisting.
48 
49 
50 SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
51 {
52  // This implementation is subject to change, any magic is allowed here.
53  // The public SCH_IO_MGR API is the only pertinent public information.
54 
55  switch( aFileType )
56  {
57  case SCH_LEGACY:
58  return new SCH_LEGACY_PLUGIN();
59  case SCH_KICAD:
60  return new SCH_SEXPR_PLUGIN();
61  case SCH_EAGLE:
62  return new SCH_EAGLE_PLUGIN();
63  }
64 
65  return NULL;
66 }
67 
68 
70 {
71  // This function is a place holder for a future point in time where
72  // the plugin is a DLL/DSO. It could do reference counting, and then
73  // unload the DLL/DSO when count goes to zero.
74 
75  delete aPlugin;
76 }
77 
78 
79 const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
80 {
81  // keep this function in sync with EnumFromStr() relative to the
82  // text spellings. If you change the spellings, you will obsolete
83  // library tables, so don't do change, only additions are ok.
84 
85  switch( aType )
86  {
87  default:
88  return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aType );
89 
90  case SCH_LEGACY:
91  return wxString( wxT( "Legacy" ) );
92 
93  case SCH_KICAD:
94  return wxString( wxT( "KiCad" ) );
95 
96  case SCH_EAGLE:
97  return wxString( wxT( "EAGLE" ) );
98  }
99 }
100 
101 
102 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
103 {
104  // keep this function in sync with ShowType() relative to the
105  // text spellings. If you change the spellings, you will obsolete
106  // library tables, so don't do change, only additions are ok.
107 
108  if( aType == wxT( "Legacy" ) )
109  return SCH_LEGACY;
110  else if( aType == wxT( "KiCad" ) )
111  return SCH_KICAD;
112  else if( aType == wxT( "EAGLE" ) )
113  return SCH_EAGLE;
114 
115  // wxASSERT( blow up here )
116 
117  return SCH_FILE_T( -1 );
118 }
119 
120 
121 const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
122 {
123  wxString ext = wxEmptyString;
124  SCH_PLUGIN* plugin = FindPlugin( aFileType );
125 
126  if( plugin != NULL )
127  {
128  ext = plugin->GetFileExtension();
129  ReleasePlugin( plugin );
130  }
131 
132  return ext;
133 }
134 
135 
136 const wxString SCH_IO_MGR::GetLibraryFileExtension( SCH_FILE_T aFileType )
137 {
138  wxString ext = wxEmptyString;
139  SCH_PLUGIN* plugin = FindPlugin( aFileType );
140 
141  if( plugin != NULL )
142  {
143  ext = plugin->GetLibraryFileExtension();
144  ReleasePlugin( plugin );
145  }
146 
147  return ext;
148 }
149 
150 
151 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
152 {
153  SCH_FILE_T ret = SCH_LEGACY; // default guess, unless detected otherwise.
154  wxFileName fn( aLibPath );
155 
156  if( fn.GetExt() == SchematicLibraryFileExtension )
157  {
158  ret = SCH_LEGACY;
159  }
160  else if( fn.GetExt() == KiCadSymbolLibFileExtension )
161  {
162  ret = SCH_KICAD;
163  }
164 
165  return ret;
166 }
167 
168 
169 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath )
170 {
171  SCH_FILE_T ret = SCH_LEGACY; // default guess, unless detected otherwise.
172  wxFileName fn( aSchematicPath );
173 
174  if( fn.GetExt() == LegacySchematicFileExtension )
175  {
176  ret = SCH_LEGACY;
177  }
178  else if( fn.GetExt() == KiCadSchematicFileExtension )
179  {
180  ret = SCH_KICAD;
181  }
182 
183  return ret;
184 }
185 
186 
187 SCH_SHEET* SCH_IO_MGR::Load( SCH_FILE_T aFileType, const wxString& aFileName, KIWAY* aKiway,
188  SCH_SHEET* aAppendToMe, const PROPERTIES* aProperties )
189 {
190  // release the SCH_PLUGIN even if an exception is thrown.
191  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
192 
193  if( (SCH_PLUGIN*) pi ) // test pi->plugin
194  {
195  return pi->Load( aFileName, aKiway, aAppendToMe, aProperties ); // virtual
196  }
197 
198  THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
199 }
200 
201 
202 void SCH_IO_MGR::Save( SCH_FILE_T aFileType, const wxString& aFileName,
203  SCH_SCREEN* aSchematic, KIWAY* aKiway, const PROPERTIES* aProperties )
204 {
205  // release the SCH_PLUGIN even if an exception is thrown.
206  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
207 
208  if( (SCH_PLUGIN*) pi ) // test pi->plugin
209  {
210  pi->Save( aFileName, aSchematic, aKiway, aProperties ); // virtual
211  return;
212  }
213 
214  THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
215 }
216 
217 
218 DECLARE_ENUM_VECTOR( SCH_IO_MGR, SCH_FILE_T )
SCH_IO_MGR is a factory which returns an instance of a SCH_PLUGIN.
Definition: sch_io_mgr.h:45
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:151
virtual const wxString GetLibraryFileExtension() const =0
Return the library file extension for the SCH_PLUGIN object.
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:79
virtual SCH_SHEET * Load(const wxString &aFileName, KIWAY *aKiway, SCH_SHEET *aAppendToMe=NULL, const PROPERTIES *aProperties=NULL)
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
Definition: sch_plugin.cpp:50
static const wxString GetLibraryFileExtension(SCH_FILE_T aFileType)
Return the symbol library file extension (if any) for aFileType.
Definition: sch_io_mgr.cpp:136
static void Save(SCH_FILE_T aFileType, const wxString &aFileName, SCH_SCREEN *aSchematic, KIWAY *aKiway, const PROPERTIES *aProperties=NULL)
Write either a full aSchematic to a storage file in a format that this implementation knows about,...
Definition: sch_io_mgr.cpp:202
SCH_EAGLE_PLUGIN is a SCH_PLUGIN derivation for loading 6.x+ Eagle schematic files.
PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
#define FMT_NOTFOUND
Definition: sch_io_mgr.cpp:34
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:169
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:201
#define NULL
const std::string SchematicLibraryFileExtension
#define DECLARE_ENUM_VECTOR(class_name, enum_name)
Definition: enum_vector.h:43
static const wxString GetFileExtension(SCH_FILE_T aFileType)
Return the schematic file extension for aFileType.
Definition: sch_io_mgr.cpp:121
Definition of file extensions used in Kicad.
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
#define THROW_IO_ERROR(msg)
const std::string LegacySchematicFileExtension
A SCH_PLUGIN derivation for loading schematic files using the new s-expression file format.
A SCH_PLUGIN derivation for loading schematic files created before the new s-expression file format.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:215
static SCH_SHEET * Load(SCH_FILE_T aFileType, const wxString &aFileName, KIWAY *aKiway, SCH_SHEET *aAppendToMe=NULL, const PROPERTIES *aProperties=NULL)
Load the requested SCH_PLUGIN and if found, calls the SCH_PLUGIN->Load(..) function on it using the a...
Definition: sch_io_mgr.cpp:187
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:516
virtual void Save(const wxString &aFileName, SCH_SCREEN *aSchematic, KIWAY *aKiway, const PROPERTIES *aProperties=NULL)
Write aSchematic to a storage file in a format that this SCH_PLUGIN implementation knows about,...
Definition: sch_plugin.cpp:58
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
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:102
#define _(s)
Definition: 3d_actions.cpp:33
const std::string KiCadSchematicFileExtension
virtual const wxString GetFileExtension() const =0
Returns the file extension for the SCH_PLUGIN.
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:69
const std::string KiCadSymbolLibFileExtension