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 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 
31 
32 #define FMT_UNIMPLEMENTED _( "Plugin '%s' does not implement the '%s' function." )
33 #define FMT_NOTFOUND _( "Plugin type '%s' is not found." )
34 
35 
36 
37 // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
38 // and code size. Until then, use the simplest method:
39 
40 // This implementation is one of two which could be done.
41 // The other one would cater to DLL/DSO's. But since it would be nearly
42 // impossible to link a KICAD type DLL/DSO right now without pulling in all
43 // ::Draw() functions, I forgo that option temporarily.
44 
45 // Some day it may be possible to have some built in AND some DLL/DSO
46 // plugins coexisting.
47 
48 
49 SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
50 {
51  // This implementation is subject to change, any magic is allowed here.
52  // The public SCH_IO_MGR API is the only pertinent public information.
53 
54  switch( aFileType )
55  {
56  case SCH_LEGACY:
57  return new SCH_LEGACY_PLUGIN();
58  case SCH_EAGLE:
59  return new SCH_EAGLE_PLUGIN();
60  case SCH_KICAD:
61  return NULL;
62  }
63 
64  return NULL;
65 }
66 
67 
69 {
70  // This function is a place holder for a future point in time where
71  // the plugin is a DLL/DSO. It could do reference counting, and then
72  // unload the DLL/DSO when count goes to zero.
73 
74  delete aPlugin;
75 }
76 
77 
78 const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
79 {
80  // keep this function in sync with EnumFromStr() relative to the
81  // text spellings. If you change the spellings, you will obsolete
82  // library tables, so don't do change, only additions are ok.
83 
84  switch( aType )
85  {
86  default:
87  return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aType );
88 
89  case SCH_LEGACY:
90  return wxString( wxT( "Legacy" ) );
91 
92  case SCH_EAGLE:
93  return wxString( wxT( "EAGLE" ) );
94  }
95 }
96 
97 
98 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
99 {
100  // keep this function in sync with ShowType() relative to the
101  // text spellings. If you change the spellings, you will obsolete
102  // library tables, so don't do change, only additions are ok.
103 
104  if( aType == wxT( "Legacy" ) )
105  return SCH_LEGACY;
106  else if( aType == wxT( "EAGLE" ) )
107  return SCH_EAGLE;
108 
109  // wxASSERT( blow up here )
110 
111  return SCH_FILE_T( -1 );
112 }
113 
114 
115 const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
116 {
117  wxString ext = wxEmptyString;
118  SCH_PLUGIN* plugin = FindPlugin( aFileType );
119 
120  if( plugin != NULL )
121  {
122  ext = plugin->GetFileExtension();
123  ReleasePlugin( plugin );
124  }
125 
126  return ext;
127 }
128 
129 
130 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
131 {
132  SCH_FILE_T ret = SCH_LEGACY; // default guess, unless detected otherwise.
133  wxFileName fn( aLibPath );
134 
135  if( fn.GetExt() == SchematicFileWildcard )
136  {
137  ret = SCH_LEGACY;
138  }
139 
140  return ret;
141 }
142 
143 
144 SCH_SHEET* SCH_IO_MGR::Load( SCH_FILE_T aFileType, const wxString& aFileName, KIWAY* aKiway,
145  SCH_SHEET* aAppendToMe, const PROPERTIES* aProperties )
146 {
147  // release the SCH_PLUGIN even if an exception is thrown.
148  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
149 
150  if( (SCH_PLUGIN*) pi ) // test pi->plugin
151  {
152  return pi->Load( aFileName, aKiway, aAppendToMe, aProperties ); // virtual
153  }
154 
155  THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
156 }
157 
158 
159 void SCH_IO_MGR::Save( SCH_FILE_T aFileType, const wxString& aFileName,
160  SCH_SCREEN* aSchematic, KIWAY* aKiway, const PROPERTIES* aProperties )
161 {
162  // release the SCH_PLUGIN even if an exception is thrown.
163  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
164 
165  if( (SCH_PLUGIN*) pi ) // test pi->plugin
166  {
167  pi->Save( aFileName, aSchematic, aKiway, aProperties ); // virtual
168  return;
169  }
170 
171  THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
172 }
173 
174 
175 DECLARE_ENUM_VECTOR( SCH_IO_MGR, SCH_FILE_T );
Class SCH_IO_MGR is a factory which returns an instance of a SCH_PLUGIN.
Definition: sch_io_mgr.h:46
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Return a plugin type given a footprint library's libPath.
Definition: sch_io_mgr.cpp:130
static APIEXPORT SCH_PLUGIN * FindPlugin(SCH_FILE_T aFileType)
Return a SCH_PLUGIN which the caller can use to import, export, save, or load design documents...
Definition: sch_io_mgr.cpp:49
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:78
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:58
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:159
Class SCH_EAGLE_PLUGIN is a SCH_PLUGIN derivation for loading 6.x+ Eagle schematic files...
Class PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
#define FMT_NOTFOUND
Definition: sch_io_mgr.cpp:33
Base class that schematic file and library loading and saving plugins should derive from...
Definition: sch_io_mgr.h:188
DECLARE_ENUM_VECTOR(SCH_IO_MGR, SCH_FILE_T)
static const wxString GetFileExtension(SCH_FILE_T aFileType)
Return the file extension for aFileType.
Definition: sch_io_mgr.cpp:115
The common library.
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:257
const wxString SchematicFileWildcard
Class SCH_LEGACY_PLUGIN.
Class SCH_SHEET is the sheet symbol placed in a schematic, and is the entry point for a sub schematic...
Definition: sch_sheet.h:216
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:144
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:513
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:66
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", etc.
Definition: sch_io_mgr.cpp:98
virtual const wxString GetFileExtension() const =0
Returns the file extension for the SCH_PLUGIN.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
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:68