KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_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) 2011-2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <wx/filename.h>
26#include <wx/uri.h>
27
28#include <config.h>
29#include <kiway_player.h>
31
32#include <pcb_io/pcb_io_mgr.h>
33
49#include <reporter.h>
50
51
52
53#define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
54#define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
55
56
57// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
58// and code size. Until then, use the simplest method:
59
60// This implementation is one of two which could be done.
61// The other one would cater to DLL/DSO's. But since it would be nearly
62// impossible to link a KICAD type DLL/DSO right now without pulling in all
63// ::Draw() functions, I forgo that option temporarily.
64
65// Some day it may be possible to have some built in AND some DLL/DSO
66// plugins coexisting.
67
68
70{
71 // This implementation is subject to change, any magic is allowed here.
72 // The public IO_MGR API is the only pertinent public information.
73
74 return PLUGIN_REGISTRY::Instance()->Create( aFileType );
75}
76
77
78const wxString PCB_IO_MGR::ShowType( PCB_FILE_T aType )
79{
80 const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
81
82 for( const auto& plugin : plugins )
83 {
84 if ( plugin.m_type == aType )
85 {
86 return plugin.m_name;
87 }
88 }
89
90 return wxString::Format( _( "UNKNOWN (%d)" ), aType );
91}
92
93
95{
96 const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
97
98 for( const auto& plugin : plugins )
99 {
100 if ( plugin.m_name == aType )
101 {
102 return plugin.m_type;
103 }
104 }
105
106 return PCB_FILE_T( -1 );
107}
108
109
110// The KIWAY_PLAYER::OpenProjectFiles() API knows nothing about plugins, so
111// determine how to load the BOARD here
113{
114 const auto& plugins = PCB_IO_MGR::PLUGIN_REGISTRY::Instance()->AllPlugins();
115
116 for( const auto& plugin : plugins )
117 {
118 bool isKiCad = plugin.m_type == PCB_IO_MGR::KICAD_SEXP || plugin.m_type == PCB_IO_MGR::LEGACY;
119
120 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
121 continue;
122
123 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
124 continue;
125
126 IO_RELEASER<PCB_IO> pi( plugin.m_createFunc() );
127
128 if( pi->CanReadBoard( aFileName ) )
129 return plugin.m_type;
130 }
131
133}
134
135
137{
138 const auto& plugins = PCB_IO_MGR::PLUGIN_REGISTRY::Instance()->AllPlugins();
139
140 for( const auto& plugin : plugins )
141 {
142 bool isKiCad = plugin.m_type == PCB_IO_MGR::KICAD_SEXP || plugin.m_type == PCB_IO_MGR::LEGACY;
143
144 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
145 continue;
146
147 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
148 continue;
149
150 IO_RELEASER<PCB_IO> pi( plugin.m_createFunc() );
151
152 if( pi->CanReadLibrary( aLibPath ) )
153 return plugin.m_type;
154 }
155
157}
158
159
160BOARD* PCB_IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aAppendToMe,
161 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject,
162 PROGRESS_REPORTER* aProgressReporter )
163{
164 IO_RELEASER<PCB_IO> pi( PluginFind( aFileType ) );
165
166 if( pi ) // test pi->plugin
167 {
168 pi->SetProgressReporter( aProgressReporter );
169 return pi->LoadBoard( aFileName, aAppendToMe, aProperties, aProject );
170 }
171
172 THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
173}
174
175
176void PCB_IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard,
177 const std::map<std::string, UTF8>* aProperties )
178{
179 IO_RELEASER<PCB_IO> pi( PluginFind( aFileType ) );
180
181 if( pi )
182 {
183 pi->SaveBoard( aFileName, aBoard, aProperties ); // virtual
184 return;
185 }
186
187 THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
188}
189
190
191bool PCB_IO_MGR::ConvertLibrary( std::map<std::string, UTF8>* aOldFileProps, const wxString& aOldFilePath,
192 const wxString& aNewFilePath, REPORTER* aReporter )
193{
195
196 if( oldFileType == PCB_IO_MGR::FILE_TYPE_NONE )
197 return false;
198
199 IO_RELEASER<PCB_IO> oldFilePI( PCB_IO_MGR::PluginFind( oldFileType ) );
201 wxArrayString fpNames;
202 wxFileName newFileName( aNewFilePath );
203
204 if( newFileName.HasExt() )
205 {
206 wxString extraDir = newFileName.GetFullName();
207 newFileName.ClearExt();
208 newFileName.SetName( "" );
209 newFileName.AppendDir( extraDir );
210 }
211
212 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
213 return false;
214
215 try
216 {
217 bool bestEfforts = false; // throw on first error
218 oldFilePI->FootprintEnumerate( fpNames, aOldFilePath, bestEfforts, aOldFileProps );
219 std::map<std::string, UTF8> props { { "skip_cache_validation", "" } };
220
221 for ( const wxString& fpName : fpNames )
222 {
223 std::unique_ptr<const FOOTPRINT> fp(
224 oldFilePI->GetEnumeratedFootprint( aOldFilePath, fpName, aOldFileProps ) );
225
226 try
227 {
228 kicadPI->FootprintSave( aNewFilePath, fp.get(), &props );
229 }
230 catch( ... )
231 {
232 // Footprints that cannot be saved are just skipped. This is not see
233 // as a fatal error.
234 // this can be just a illegal filename used for the footprint
235 if( aReporter )
236 aReporter->Report( wxString::Format( "Footprint \"%s\" can't be saved. Skipped",
237 fpName ),
238 SEVERITY::RPT_SEVERITY_WARNING );
239 }
240 }
241 }
242 catch( IO_ERROR& io_err )
243 {
244 if( aReporter )
245 aReporter->Report( wxString::Format( "Library '%s' Convert err: \"%s\"",
246 aOldFilePath, io_err.What() ),
247 SEVERITY::RPT_SEVERITY_ERROR );
248 return false;
249 }
250 catch( ... )
251 {
252 return false;
253 }
254
255 return true;
256}
257
258
259// These text strings are "truth" for identifying the plugins. If you change the spellings,
260// you will obsolete library tables, so don't do it. Additions are OK.
261
262// clang-format off
265 wxT( "KiCad" ),
266 []() -> PCB_IO* { return new PCB_IO_KICAD_SEXPR; } );
267
270 wxT( "Legacy" ),
271 []() -> PCB_IO* { return new PCB_IO_KICAD_LEGACY; } );
272
273// Keep non-KiCad plugins in alphabetical order
274
277 wxT( "Altium Circuit Maker" ),
278 []() -> PCB_IO* { return new PCB_IO_ALTIUM_CIRCUIT_MAKER; } );
279
282 wxT( "Altium Circuit Studio" ),
283 []() -> PCB_IO* { return new PCB_IO_ALTIUM_CIRCUIT_STUDIO; } );
284
287 wxT( "Altium Designer" ),
288 []() -> PCB_IO* { return new PCB_IO_ALTIUM_DESIGNER; } );
289
292 wxT( "CADSTAR PCB Archive" ),
293 []() -> PCB_IO* { return new PCB_IO_CADSTAR_ARCHIVE; } );
294
297 wxT( "Eagle" ),
298 []() -> PCB_IO* { return new PCB_IO_EAGLE; } );
299
302 wxT( "EasyEDA / JLCEDA Std" ),
303 []() -> PCB_IO* { return new PCB_IO_EASYEDA; });
304
307 wxT( "EasyEDA / JLCEDA Pro" ),
308 []() -> PCB_IO* { return new PCB_IO_EASYEDAPRO; });
309
312 wxT( "Fabmaster" ),
313 []() -> PCB_IO* { return new PCB_IO_FABMASTER; } );
314
317 wxT( "GEDA/Pcb" ),
318 []() -> PCB_IO* { return new PCB_IO_GEDA; } );
319
322 wxT( "P-Cad" ),
323 []() -> PCB_IO* { return new PCB_IO_PCAD; } );
324
327 wxT( "Solidworks PCB" ),
328 []() -> PCB_IO* { return new PCB_IO_SOLIDWORKS; } );
329
332 wxT( "IPC-2581" ),
333 []() -> PCB_IO* { return new PCB_IO_IPC2581; } );
334
337 wxT( "ODB++" ),
338 []() -> PCB_IO* { return new PCB_IO_ODBPP; } );
339// clang-format on
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
Works with Eagle 6.x XML board files and footprints to implement the Pcbnew #PLUGIN API or a portion ...
Definition: pcb_io_eagle.h:133
A #PLUGIN derivation for saving and loading Geda PCB files.
Definition: pcb_io_geda.h:48
A #PLUGIN derivation which could possibly be put into a DLL/DSO.
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
PCB_IO * Create(PCB_FILE_T aFileType) const
Definition: pcb_io_mgr.h:115
const std::vector< ENTRY > & AllPlugins() const
Definition: pcb_io_mgr.h:128
static PLUGIN_REGISTRY * Instance()
Definition: pcb_io_mgr.h:94
static PCB_IO * PluginFind(PCB_FILE_T aFileType)
Return a #PLUGIN which the caller can use to import, export, save, or load design documents.
Definition: pcb_io_mgr.cpp:69
static BOARD * Load(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Find the requested #PLUGIN and if found, calls the #PLUGIN::LoadBoard() function on it using the argu...
Definition: pcb_io_mgr.cpp:160
static PCB_FILE_T EnumFromStr(const wxString &aFileType)
Return the PCB_FILE_T from the corresponding plugin type name: "kicad", "legacy", etc.
Definition: pcb_io_mgr.cpp:94
PCB_FILE_T
The set of file types that the PCB_IO_MGR knows about, and for which there has been a plugin written,...
Definition: pcb_io_mgr.h:56
@ FILE_TYPE_NONE
Definition: pcb_io_mgr.h:77
@ KICAD_SEXP
S-expression Pcbnew file format.
Definition: pcb_io_mgr.h:58
@ GEDA_PCB
Geda PCB file formats.
Definition: pcb_io_mgr.h:68
@ ALTIUM_DESIGNER
Definition: pcb_io_mgr.h:62
@ LEGACY
Legacy Pcbnew file formats prior to s-expression.
Definition: pcb_io_mgr.h:59
@ SOLIDWORKS_PCB
Definition: pcb_io_mgr.h:70
@ ALTIUM_CIRCUIT_MAKER
Definition: pcb_io_mgr.h:60
@ ALTIUM_CIRCUIT_STUDIO
Definition: pcb_io_mgr.h:61
@ CADSTAR_PCB_ARCHIVE
Definition: pcb_io_mgr.h:63
static bool ConvertLibrary(std::map< std::string, UTF8 > *aOldFileProps, const wxString &aOldFilePath, const wxString &aNewFilePath, REPORTER *aReporter)
Convert a schematic symbol library to the latest KiCad format.
Definition: pcb_io_mgr.cpp:191
static PCB_FILE_T FindPluginTypeFromBoardPath(const wxString &aFileName, int aCtl=0)
Return a plugin type given a path for a board file.
Definition: pcb_io_mgr.cpp:112
static void Save(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aBoard, const std::map< std::string, UTF8 > *aProperties=nullptr)
Write either a full aBoard to a storage file in a format that this implementation knows about,...
Definition: pcb_io_mgr.cpp:176
static PCB_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a footprint library's libPath.
Definition: pcb_io_mgr.cpp:136
static const wxString ShowType(PCB_FILE_T aFileType)
Return a brief name for a plugin given aFileType enum.
Definition: pcb_io_mgr.cpp:78
A base class that BOARD loading and saving plugins should derive from.
Definition: pcb_io.h:71
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition: project.h:64
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
#define _(s)
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition: io_mgr.h:33
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
#define KICTL_KICAD_ONLY
chosen file is from KiCad according to user
Definition: kiway_player.h:75
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
Definition: kiway_player.h:74
static PCB_IO_MGR::REGISTER_PLUGIN registerODBPPPlugin(PCB_IO_MGR::ODBPP, wxT("ODB++"), []() -> PCB_IO *{ return new PCB_IO_ODBPP;})
static PCB_IO_MGR::REGISTER_PLUGIN registerSolidworksPCBPlugin(PCB_IO_MGR::SOLIDWORKS_PCB, wxT("Solidworks PCB"), []() -> PCB_IO *{ return new PCB_IO_SOLIDWORKS;})
static PCB_IO_MGR::REGISTER_PLUGIN registerEasyEDAProPlugin(PCB_IO_MGR::EASYEDAPRO, wxT("EasyEDA / JLCEDA Pro"), []() -> PCB_IO *{ return new PCB_IO_EASYEDAPRO;})
static PCB_IO_MGR::REGISTER_PLUGIN registerEasyEDAPlugin(PCB_IO_MGR::EASYEDA, wxT("EasyEDA / JLCEDA Std"), []() -> PCB_IO *{ return new PCB_IO_EASYEDA;})
static PCB_IO_MGR::REGISTER_PLUGIN registerGPCBPlugin(PCB_IO_MGR::GEDA_PCB, wxT("GEDA/Pcb"), []() -> PCB_IO *{ return new PCB_IO_GEDA;})
static PCB_IO_MGR::REGISTER_PLUGIN registerKicadPlugin(PCB_IO_MGR::KICAD_SEXP, wxT("KiCad"), []() -> PCB_IO *{ return new PCB_IO_KICAD_SEXPR;})
static PCB_IO_MGR::REGISTER_PLUGIN registerIPC2581Plugin(PCB_IO_MGR::IPC2581, wxT("IPC-2581"), []() -> PCB_IO *{ return new PCB_IO_IPC2581;})
static PCB_IO_MGR::REGISTER_PLUGIN registerPcadPlugin(PCB_IO_MGR::PCAD, wxT("P-Cad"), []() -> PCB_IO *{ return new PCB_IO_PCAD;})
static PCB_IO_MGR::REGISTER_PLUGIN registerEaglePlugin(PCB_IO_MGR::EAGLE, wxT("Eagle"), []() -> PCB_IO *{ return new PCB_IO_EAGLE;})
static PCB_IO_MGR::REGISTER_PLUGIN registerAltiumCircuitStudioPlugin(PCB_IO_MGR::ALTIUM_CIRCUIT_STUDIO, wxT("Altium Circuit Studio"), []() -> PCB_IO *{ return new PCB_IO_ALTIUM_CIRCUIT_STUDIO;})
static PCB_IO_MGR::REGISTER_PLUGIN registerAltiumDesignerPlugin(PCB_IO_MGR::ALTIUM_DESIGNER, wxT("Altium Designer"), []() -> PCB_IO *{ return new PCB_IO_ALTIUM_DESIGNER;})
static PCB_IO_MGR::REGISTER_PLUGIN registerCadstarArchivePlugin(PCB_IO_MGR::CADSTAR_PCB_ARCHIVE, wxT("CADSTAR PCB Archive"), []() -> PCB_IO *{ return new PCB_IO_CADSTAR_ARCHIVE;})
static PCB_IO_MGR::REGISTER_PLUGIN registerLegacyPlugin(PCB_IO_MGR::LEGACY, wxT("Legacy"), []() -> PCB_IO *{ return new PCB_IO_KICAD_LEGACY;})
static PCB_IO_MGR::REGISTER_PLUGIN registerFabmasterPlugin(PCB_IO_MGR::FABMASTER, wxT("Fabmaster"), []() -> PCB_IO *{ return new PCB_IO_FABMASTER;})
static PCB_IO_MGR::REGISTER_PLUGIN registerAltiumCircuitMakerPlugin(PCB_IO_MGR::ALTIUM_CIRCUIT_MAKER, wxT("Altium Circuit Maker"), []() -> PCB_IO *{ return new PCB_IO_ALTIUM_CIRCUIT_MAKER;})
#define FMT_NOTFOUND
Definition: sch_io_mgr.cpp:45
Register a plugin.
Definition: pcb_io_mgr.h:147
Definition of file extensions used in Kicad.