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 The 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 <footprint.h>
30#include <kiway_player.h>
33#include <pcb_io/pcb_io_mgr.h>
34
55#include <reporter.h>
57
58
59#define FMT_UNIMPLEMENTED _( "Plugin '%s' does not implement the '%s' function." )
60#define FMT_NOTFOUND _( "Plugin type '%s' is not found." )
61
62
63// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
64// and code size. Until then, use the simplest method:
65
66// This implementation is one of two which could be done.
67// The other one would cater to DLL/DSO's. But since it would be nearly
68// impossible to link a KICAD type DLL/DSO right now without pulling in all
69// ::Draw() functions, I forgo that option temporarily.
70
71// Some day it may be possible to have some built in AND some DLL/DSO
72// plugins coexisting.
73
74
76{
77 // This implementation is subject to change, any magic is allowed here.
78 // The public IO_MGR API is the only pertinent public information.
79
80 return PLUGIN_REGISTRY::Instance()->Create( aFileType );
81}
82
83
84const wxString PCB_IO_MGR::ShowType( PCB_FILE_T aType )
85{
86 if( aType == PCB_IO_MGR::NESTED_TABLE )
88
89 const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
90
91 for( const auto& plugin : plugins )
92 {
93 if ( plugin.m_type == aType )
94 return plugin.m_name;
95 }
96
97 return wxString::Format( _( "UNKNOWN (%d)" ), aType );
98}
99
100
102{
105
106 const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
107
108 for( const auto& plugin : plugins )
109 {
110 if( plugin.m_name.CmpNoCase( aType ) == 0 )
111 return plugin.m_type;
112 }
113
115}
116
117
118// The KIWAY_PLAYER::OpenProjectFiles() API knows nothing about plugins, so
119// determine how to load the BOARD here
121{
122 const auto& plugins = PCB_IO_MGR::PLUGIN_REGISTRY::Instance()->AllPlugins();
123
124 for( const auto& plugin : plugins )
125 {
126 bool isKiCad = plugin.m_type == PCB_IO_MGR::KICAD_SEXP || plugin.m_type == PCB_IO_MGR::LEGACY;
127
128 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
129 continue;
130
131 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
132 continue;
133
134 IO_RELEASER<PCB_IO> pi( plugin.m_createFunc() );
135
136 if( pi->CanReadBoard( aFileName ) )
137 return plugin.m_type;
138 }
139
141}
142
143
145{
147
148 if( parser.Parse( aLibPath.ToStdString() ).has_value() )
149 return NESTED_TABLE;
150
151 const auto& plugins = PCB_IO_MGR::PLUGIN_REGISTRY::Instance()->AllPlugins();
152
153 for( const auto& plugin : plugins )
154 {
155 bool isKiCad = plugin.m_type == PCB_IO_MGR::KICAD_SEXP || plugin.m_type == PCB_IO_MGR::LEGACY;
156
157 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
158 continue;
159
160 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
161 continue;
162
163 IO_RELEASER<PCB_IO> pi( plugin.m_createFunc() );
164
165 if( pi->CanReadLibrary( aLibPath ) )
166 return plugin.m_type;
167 }
168
170}
171
172
173BOARD* PCB_IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aAppendToMe,
174 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject,
175 PROGRESS_REPORTER* aProgressReporter )
176{
177 IO_RELEASER<PCB_IO> pi( FindPlugin( aFileType ) );
178
179 if( pi ) // test pi->plugin
180 {
181 pi->SetProgressReporter( aProgressReporter );
182 return pi->LoadBoard( aFileName, aAppendToMe, aProperties, aProject );
183 }
184
185 THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
186}
187
188
189void PCB_IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard,
190 const std::map<std::string, UTF8>* aProperties )
191{
192 IO_RELEASER<PCB_IO> pi( FindPlugin( aFileType ) );
193
194 if( pi )
195 {
196 pi->SaveBoard( aFileName, aBoard, aProperties ); // virtual
197 return;
198 }
199
200 THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
201}
202
203
204bool PCB_IO_MGR::ConvertLibrary( const std::map<std::string, UTF8>& aOldFileProps,
205 const wxString& aOldFilePath, const wxString& aNewFilePath,
206 REPORTER* aReporter )
207{
209
210 if( oldFileType == PCB_IO_MGR::FILE_TYPE_NONE )
211 return false;
212
213 IO_RELEASER<PCB_IO> oldFilePI( PCB_IO_MGR::FindPlugin( oldFileType ) );
215 wxArrayString fpNames;
216 wxFileName newFileName( aNewFilePath );
217
218 if( newFileName.HasExt() )
219 {
220 wxString extraDir = newFileName.GetFullName();
221 newFileName.ClearExt();
222 newFileName.SetName( "" );
223 newFileName.AppendDir( extraDir );
224 }
225
226 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
227 return false;
228
229 try
230 {
231 bool bestEfforts = false; // throw on first error
232 oldFilePI->FootprintEnumerate( fpNames, aOldFilePath, bestEfforts, &aOldFileProps );
233 std::map<std::string, UTF8> props { { "skip_cache_validation", "" } };
234
235 for ( const wxString& fpName : fpNames )
236 {
237 std::unique_ptr<const FOOTPRINT> fp( oldFilePI->GetEnumeratedFootprint( aOldFilePath, fpName,
238 &aOldFileProps ) );
239
240 try
241 {
242 kicadPI->FootprintSave( aNewFilePath, fp.get(), &props );
243 }
244 catch( ... )
245 {
246 // Footprints that cannot be saved are just skipped. This is not see
247 // as a fatal error.
248 // this can be just a illegal filename used for the footprint
249 if( aReporter )
250 aReporter->Report( wxString::Format( "Footprint \"%s\" can't be saved. Skipped",
251 fpName ),
253 }
254 }
255 }
256 catch( IO_ERROR& io_err )
257 {
258 if( aReporter )
259 aReporter->Report( wxString::Format( "Library '%s' Convert err: \"%s\"",
260 aOldFilePath, io_err.What() ),
262 return false;
263 }
264 catch( ... )
265 {
266 return false;
267 }
268
269 return true;
270}
271
272
273// These text strings are "truth" for identifying the plugins. If you change the spellings,
274// you will obsolete library tables, so don't do it. Additions are OK.
275
276// clang-format off
279 wxT( "KiCad" ),
280 []() -> PCB_IO* { return new PCB_IO_KICAD_SEXPR; } );
281
284 wxT( "Legacy" ),
285 []() -> PCB_IO* { return new PCB_IO_KICAD_LEGACY; } );
286
287// Keep non-KiCad plugins in alphabetical order
288
291 wxT( "Allegro" ),
292 []() -> PCB_IO* { return new PCB_IO_ALLEGRO; } );
293
296 wxT( "Altium Circuit Maker" ),
297 []() -> PCB_IO* { return new PCB_IO_ALTIUM_CIRCUIT_MAKER; } );
298
301 wxT( "Altium Circuit Studio" ),
302 []() -> PCB_IO* { return new PCB_IO_ALTIUM_CIRCUIT_STUDIO; } );
303
306 wxT( "Altium Designer" ),
307 []() -> PCB_IO* { return new PCB_IO_ALTIUM_DESIGNER; } );
308
311 wxT( "CADSTAR PCB Archive" ),
312 []() -> PCB_IO* { return new PCB_IO_CADSTAR_ARCHIVE; } );
313
316 wxT( "Eagle" ),
317 []() -> PCB_IO* { return new PCB_IO_EAGLE; } );
318
321 wxT( "EasyEDA / JLCEDA Std" ),
322 []() -> PCB_IO* { return new PCB_IO_EASYEDA; });
323
326 wxT( "EasyEDA / JLCEDA Pro" ),
327 []() -> PCB_IO* { return new PCB_IO_EASYEDAPRO; });
328
331 wxT( "Fabmaster" ),
332 []() -> PCB_IO* { return new PCB_IO_FABMASTER; } );
333
336 wxT( "gEDA / Lepton EDA" ),
337 []() -> PCB_IO* { return new PCB_IO_GEDA; } );
338
341 wxT( "P-Cad" ),
342 []() -> PCB_IO* { return new PCB_IO_PCAD; } );
343
346 wxT( "Solidworks PCB" ),
347 []() -> PCB_IO* { return new PCB_IO_SOLIDWORKS; } );
348
351 wxT( "IPC-2581" ),
352 []() -> PCB_IO* { return new PCB_IO_IPC2581; } );
353
356 wxT( "ODB++" ),
357 []() -> PCB_IO* { return new PCB_IO_ODBPP; } );
358
361 wxT( "PADS" ),
362 []() -> PCB_IO* { return new PCB_IO_PADS(); } );
363
366 wxT( "Sprint Layout" ),
367 []() -> PCB_IO* { return new PCB_IO_SPRINT_LAYOUT; } );
368
371 wxT( "DipTrace" ),
372 []() -> PCB_IO* { return new PCB_IO_DIPTRACE; } );
373
376 wxT( "Protel Autotrax" ),
377 []() -> PCB_IO* { return new PCB_IO_AUTOTRAX; } );
378// clang-format on
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > Parse(const std::filesystem::path &aPath)
static const wxString TABLE_TYPE_NAME
Read-only importer for Protel Autotrax (.PCB, "PCB FILE 4") and Easytrax (.PCB, "PCB FILE 5") layout ...
Works with Eagle 6.x XML board files and footprints to implement the Pcbnew #PLUGIN API or a portion ...
A #PLUGIN derivation for saving and loading Geda PCB files.
Definition pcb_io_geda.h:63
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:121
const std::vector< ENTRY > & AllPlugins() const
Definition pcb_io_mgr.h:134
static PLUGIN_REGISTRY * Instance()
Definition pcb_io_mgr.h:100
static bool ConvertLibrary(const std::map< std::string, UTF8 > &aOldFileProps, const wxString &aOldFilePath, const wxString &aNewFilePath, REPORTER *aReporter)
Convert a schematic symbol library to the latest KiCad format.
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...
static PCB_FILE_T EnumFromStr(const wxString &aFileType)
Return the PCB_FILE_T from the corresponding plugin type name: "kicad", "legacy", etc.
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
@ KICAD_SEXP
S-expression Pcbnew file format.
Definition pcb_io_mgr.h:58
@ GEDA_PCB
Geda PCB file formats.
Definition pcb_io_mgr.h:69
@ ALTIUM_DESIGNER
Definition pcb_io_mgr.h:63
@ LEGACY
Legacy Pcbnew file formats prior to s-expression.
Definition pcb_io_mgr.h:59
@ ALTIUM_CIRCUIT_MAKER
Definition pcb_io_mgr.h:61
@ ALTIUM_CIRCUIT_STUDIO
Definition pcb_io_mgr.h:62
@ CADSTAR_PCB_ARCHIVE
Definition pcb_io_mgr.h:64
@ PCB_FILE_UNKNOWN
0 is not a legal menu id on Mac
Definition pcb_io_mgr.h:57
static PCB_IO * FindPlugin(PCB_FILE_T aFileType)
Return a #PLUGIN which the caller can use to import, export, save, or load design documents.
static PCB_FILE_T FindPluginTypeFromBoardPath(const wxString &aFileName, int aCtl=0)
Return a plugin type given a path for a board file.
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,...
static PCB_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a footprint library's libPath.
static const wxString ShowType(PCB_FILE_T aFileType)
Return a brief name for a plugin given aFileType enum.
A base class that BOARD loading and saving plugins should derive from.
Definition pcb_io.h:79
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition project.h:66
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:75
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:104
#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)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define KICTL_KICAD_ONLY
chosen file is from KiCad according to user
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
Pcbnew PCB_IO for DipTrace binary .dip board files.
Geda PCB file plugin definition file.
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 registerPadsPlugin(PCB_IO_MGR::PADS, wxT("PADS"), []() -> PCB_IO *{ return new PCB_IO_PADS();})
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 / Lepton EDA"), []() -> 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 registerDipTracePlugin(PCB_IO_MGR::DIPTRACE, wxT("DipTrace"), []() -> PCB_IO *{ return new PCB_IO_DIPTRACE;})
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;})
static PCB_IO_MGR::REGISTER_PLUGIN registerAutotraxPlugin(PCB_IO_MGR::AUTOTRAX, wxT("Protel Autotrax"), []() -> PCB_IO *{ return new PCB_IO_AUTOTRAX;})
static PCB_IO_MGR::REGISTER_PLUGIN registerSprintLayoutPlugin(PCB_IO_MGR::SPRINT_LAYOUT, wxT("Sprint Layout"), []() -> PCB_IO *{ return new PCB_IO_SPRINT_LAYOUT;})
static PCB_IO_MGR::REGISTER_PLUGIN registerAllegroPlugin(PCB_IO_MGR::ALLEGRO, wxT("Allegro"), []() -> PCB_IO *{ return new PCB_IO_ALLEGRO;})
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
#define FMT_NOTFOUND
Register a plugin.
Definition pcb_io_mgr.h:153
Definition of file extensions used in Kicad.