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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/filename.h>
22#include <wx/uri.h>
23
24#include <config.h>
25#include <board.h>
26#include <footprint.h>
27#include <kiway_player.h>
30#include <pcb_io/pcb_io_mgr.h>
31
54#include <reporter.h>
56
57
58#define FMT_UNIMPLEMENTED _( "Plugin '%s' does not implement the '%s' function." )
59#define FMT_NOTFOUND _( "Plugin type '%s' is not found." )
60
61
62// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
63// and code size. Until then, use the simplest method:
64
65// This implementation is one of two which could be done.
66// The other one would cater to DLL/DSO's. But since it would be nearly
67// impossible to link a KICAD type DLL/DSO right now without pulling in all
68// ::Draw() functions, I forgo that option temporarily.
69
70// Some day it may be possible to have some built in AND some DLL/DSO
71// plugins coexisting.
72
73
75{
76 // This implementation is subject to change, any magic is allowed here.
77 // The public IO_MGR API is the only pertinent public information.
78
79 return PLUGIN_REGISTRY::Instance()->Create( aFileType );
80}
81
82
83const wxString PCB_IO_MGR::ShowType( PCB_FILE_T aType )
84{
85 if( aType == PCB_IO_MGR::NESTED_TABLE )
87
88 const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
89
90 for( const auto& plugin : plugins )
91 {
92 if ( plugin.m_type == aType )
93 return plugin.m_name;
94 }
95
96 return wxString::Format( _( "UNKNOWN (%d)" ), aType );
97}
98
99
101{
104
105 const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
106
107 for( const auto& plugin : plugins )
108 {
109 if( plugin.m_name.CmpNoCase( aType ) == 0 )
110 return plugin.m_type;
111 }
112
114}
115
116
118{
119 switch( aFileType )
120 {
121 case ALLEGRO:
122 return true;
123
124 default:
125 return false;
126 }
127}
128
129
131{
132 switch( aFileType )
133 {
135 case EAGLE:
136 case EASYEDA:
137 case EASYEDAPRO:
138 case EASYEDAPRO_V3:
139 case GEDA_PCB:
140 case ALTIUM_DESIGNER:
143 case ALLEGRO:
144 return true;
145
146 default:
147 return false;
148 }
149}
150
151
152// The KIWAY_PLAYER::OpenProjectFiles() API knows nothing about plugins, so
153// determine how to load the BOARD here
155{
156 const auto& plugins = PCB_IO_MGR::PLUGIN_REGISTRY::Instance()->AllPlugins();
157
158 for( const auto& plugin : plugins )
159 {
160 bool isKiCad = plugin.m_type == PCB_IO_MGR::KICAD_SEXP || plugin.m_type == PCB_IO_MGR::LEGACY;
161
162 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
163 continue;
164
165 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
166 continue;
167
168 IO_RELEASER<PCB_IO> pi( plugin.m_createFunc() );
169
170 if( pi->CanReadBoard( aFileName ) )
171 return plugin.m_type;
172 }
173
175}
176
177
179{
181
182 if( parser.Parse( aLibPath.ToStdString() ).has_value() )
183 return NESTED_TABLE;
184
185 const auto& plugins = PCB_IO_MGR::PLUGIN_REGISTRY::Instance()->AllPlugins();
186
187 for( const auto& plugin : plugins )
188 {
189 bool isKiCad = plugin.m_type == PCB_IO_MGR::KICAD_SEXP || plugin.m_type == PCB_IO_MGR::LEGACY;
190
191 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
192 continue;
193
194 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
195 continue;
196
197 IO_RELEASER<PCB_IO> pi( plugin.m_createFunc() );
198
199 if( pi->CanReadLibrary( aLibPath ) )
200 return plugin.m_type;
201 }
202
204}
205
206
207std::unique_ptr<BOARD> PCB_IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName,
208 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject,
209 PROGRESS_REPORTER* aProgressReporter )
210{
211 IO_RELEASER<PCB_IO> pi( FindPlugin( aFileType ) );
212
213 if( pi ) // test pi->plugin
214 {
215 pi->SetProgressReporter( aProgressReporter );
216 return pi->LoadBoard( aFileName, aProperties, aProject );
217 }
218
219 THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
220}
221
222
223void PCB_IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD& aBoard,
224 const std::map<std::string, UTF8>* aProperties )
225{
226 IO_RELEASER<PCB_IO> pi( FindPlugin( aFileType ) );
227
228 if( pi )
229 {
230 pi->SaveBoard( aFileName, aBoard, aProperties ); // virtual
231 return;
232 }
233
234 THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
235}
236
237
238bool PCB_IO_MGR::ConvertLibrary( const std::map<std::string, UTF8>& aOldFileProps, const wxString& aOldFilePath,
239 const wxString& aNewFilePath, REPORTER* aReporter )
240{
242
243 if( oldFileType == PCB_IO_MGR::FILE_TYPE_NONE )
244 return false;
245
246 // A nested library table has no plugin to enumerate it; reject it before dereferencing
247 // the null plugin below.
248 if( oldFileType == PCB_IO_MGR::NESTED_TABLE )
249 return false;
250
251 IO_RELEASER<PCB_IO> oldFilePI( PCB_IO_MGR::FindPlugin( oldFileType ) );
253
254 if( !oldFilePI || !kicadPI )
255 return false;
256
257 wxArrayString fpNames;
258 wxFileName newFileName( aNewFilePath );
259
260 if( newFileName.HasExt() )
261 {
262 wxString extraDir = newFileName.GetFullName();
263 newFileName.ClearExt();
264 newFileName.SetName( "" );
265 newFileName.AppendDir( extraDir );
266 }
267
268 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
269 return false;
270
271 try
272 {
273 bool bestEfforts = false; // throw on first error
274 oldFilePI->FootprintEnumerate( fpNames, aOldFilePath, bestEfforts, &aOldFileProps );
275 std::map<std::string, UTF8> props { { "skip_cache_validation", "" } };
276
277 for ( const wxString& fpName : fpNames )
278 {
279 std::unique_ptr<const FOOTPRINT> fp( oldFilePI->GetEnumeratedFootprint( aOldFilePath, fpName,
280 &aOldFileProps ) );
281
282 try
283 {
284 kicadPI->FootprintSave( aNewFilePath, fp.get(), &props );
285 }
286 catch( ... )
287 {
288 // Footprints that cannot be saved are just skipped. This is not see
289 // as a fatal error.
290 // this can be just a illegal filename used for the footprint
291 if( aReporter )
292 {
293 aReporter->Report( wxString::Format( "Footprint \"%s\" can't be saved. Skipped",
294 fpName ),
296 }
297 }
298 }
299 }
300 catch( IO_ERROR& io_err )
301 {
302 if( aReporter )
303 {
304 aReporter->Report( wxString::Format( "Library '%s' Convert err: \"%s\"",
305 aOldFilePath,
306 io_err.What() ),
308 }
309
310 return false;
311 }
312 catch( ... )
313 {
314 return false;
315 }
316
317 return true;
318}
319
320
321// These text strings are "truth" for identifying the plugins. If you change the spellings,
322// you will obsolete library tables, so don't do it. Additions are OK.
323
324// clang-format off
327 wxT( "KiCad" ),
328 []() -> PCB_IO* { return new PCB_IO_KICAD_SEXPR; } );
329
332 wxT( "Legacy" ),
333 []() -> PCB_IO* { return new PCB_IO_KICAD_LEGACY; } );
334
335// Keep non-KiCad plugins in alphabetical order
336
339 wxT( "Allegro" ),
340 []() -> PCB_IO* { return new PCB_IO_ALLEGRO; } );
341
344 wxT( "Altium Circuit Maker" ),
345 []() -> PCB_IO* { return new PCB_IO_ALTIUM_CIRCUIT_MAKER; } );
346
349 wxT( "Altium Circuit Studio" ),
350 []() -> PCB_IO* { return new PCB_IO_ALTIUM_CIRCUIT_STUDIO; } );
351
354 wxT( "Altium Designer" ),
355 []() -> PCB_IO* { return new PCB_IO_ALTIUM_DESIGNER; } );
356
359 wxT( "CADSTAR PCB Archive" ),
360 []() -> PCB_IO* { return new PCB_IO_CADSTAR_ARCHIVE; } );
361
364 wxT( "Eagle" ),
365 []() -> PCB_IO* { return new PCB_IO_EAGLE; } );
366
369 wxT( "EasyEDA / JLCEDA Std" ),
370 []() -> PCB_IO* { return new PCB_IO_EASYEDA; });
371
374 wxT( "EasyEDA / JLCEDA Pro" ),
375 []() -> PCB_IO* { return new PCB_IO_EASYEDAPRO; });
376
379 wxT( "EasyEDA / JLCEDA Pro v3" ),
380 []() -> PCB_IO* { return new PCB_IO_EASYEDAPRO_V3; });
381
384 wxT( "Fabmaster" ),
385 []() -> PCB_IO* { return new PCB_IO_FABMASTER; } );
386
389 wxT( "gEDA / Lepton EDA" ),
390 []() -> PCB_IO* { return new PCB_IO_GEDA; } );
391
394 wxT( "P-Cad" ),
395 []() -> PCB_IO* { return new PCB_IO_PCAD; } );
396
399 wxT( "Solidworks PCB" ),
400 []() -> PCB_IO* { return new PCB_IO_SOLIDWORKS; } );
401
404 wxT( "IPC-2581" ),
405 []() -> PCB_IO* { return new PCB_IO_IPC2581; } );
406
409 wxT( "ODB++" ),
410 []() -> PCB_IO* { return new PCB_IO_ODBPP; } );
411
414 wxT( "PADS" ),
415 []() -> PCB_IO* { return new PCB_IO_PADS(); } );
416
419 wxT( "Sprint Layout" ),
420 []() -> PCB_IO* { return new PCB_IO_SPRINT_LAYOUT; } );
421
424 wxT( "DipTrace" ),
425 []() -> PCB_IO* { return new PCB_IO_DIPTRACE; } );
426
429 wxT( "Protel Autotrax" ),
430 []() -> PCB_IO* { return new PCB_IO_AUTOTRAX; } );
431
434 wxT( "PADS Binary" ),
435 []() -> PCB_IO* { return new PCB_IO_PADS_BINARY(); } );
436// clang-format on
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
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:59
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:119
const std::vector< ENTRY > & AllPlugins() const
Definition pcb_io_mgr.h:132
static PLUGIN_REGISTRY * Instance()
Definition pcb_io_mgr.h:98
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 bool ImportPopulatesProjectSettings(PCB_FILE_T aFileType)
Return true when importing aFileType writes netclasses, rules or other settings that belong to the pr...
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:52
@ KICAD_SEXP
S-expression Pcbnew file format.
Definition pcb_io_mgr.h:54
@ GEDA_PCB
Geda PCB file formats.
Definition pcb_io_mgr.h:66
@ ALTIUM_DESIGNER
Definition pcb_io_mgr.h:59
@ LEGACY
Legacy Pcbnew file formats prior to s-expression.
Definition pcb_io_mgr.h:55
@ ALTIUM_CIRCUIT_MAKER
Definition pcb_io_mgr.h:57
@ ALTIUM_CIRCUIT_STUDIO
Definition pcb_io_mgr.h:58
@ CADSTAR_PCB_ARCHIVE
Definition pcb_io_mgr.h:60
@ PCB_FILE_UNKNOWN
0 is not a legal menu id on Mac
Definition pcb_io_mgr.h:53
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_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 std::unique_ptr< BOARD > Load(PCB_FILE_T aFileType, const wxString &aFileName, 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 GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a footprint library's libPath.
static bool ImportGeneratesProjectLibrary(PCB_FILE_T aFileType)
Return true when importing aFileType should materialize a project footprint library.
static const wxString ShowType(PCB_FILE_T aFileType)
Return a brief name for a plugin given aFileType enum.
PCB I/O plugin for importing the native PADS Layout binary .pcb format, distinct from the ASCII forma...
A base class that BOARD loading and saving plugins should derive from.
Definition pcb_io.h:76
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition project.h:63
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
#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 registerEasyEDAProV3Plugin(PCB_IO_MGR::EASYEDAPRO_V3, wxT("EasyEDA / JLCEDA Pro v3"), []() -> PCB_IO *{ return new PCB_IO_EASYEDAPRO_V3;})
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 registerPadsBinaryPlugin(PCB_IO_MGR::PADS_BINARY, wxT("PADS Binary"), []() -> PCB_IO *{ return new PCB_IO_PADS_BINARY();})
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:151
Definition of file extensions used in Kicad.