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