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
48
49#define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
50#define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
51
52
53// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
54// and code size. Until then, use the simplest method:
55
56// This implementation is one of two which could be done.
57// The other one would cater to DLL/DSO's. But since it would be nearly
58// impossible to link a KICAD type DLL/DSO right now without pulling in all
59// ::Draw() functions, I forgo that option temporarily.
60
61// Some day it may be possible to have some built in AND some DLL/DSO
62// plugins coexisting.
63
64
66{
67 // This implementation is subject to change, any magic is allowed here.
68 // The public IO_MGR API is the only pertinent public information.
69
70 return PLUGIN_REGISTRY::Instance()->Create( aFileType );
71}
72
73
74const wxString PCB_IO_MGR::ShowType( PCB_FILE_T aType )
75{
76 const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
77
78 for( const auto& plugin : plugins )
79 {
80 if ( plugin.m_type == aType )
81 {
82 return plugin.m_name;
83 }
84 }
85
86 return wxString::Format( _( "UNKNOWN (%d)" ), aType );
87}
88
89
91{
92 const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
93
94 for( const auto& plugin : plugins )
95 {
96 if ( plugin.m_name == aType )
97 {
98 return plugin.m_type;
99 }
100 }
101
102 return PCB_FILE_T( -1 );
103}
104
105
106// The KIWAY_PLAYER::OpenProjectFiles() API knows nothing about plugins, so
107// determine how to load the BOARD here
109{
110 const auto& plugins = PCB_IO_MGR::PLUGIN_REGISTRY::Instance()->AllPlugins();
111
112 for( const auto& plugin : plugins )
113 {
114 bool isKiCad = plugin.m_type == PCB_IO_MGR::KICAD_SEXP || plugin.m_type == PCB_IO_MGR::LEGACY;
115
116 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
117 continue;
118
119 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
120 continue;
121
122 IO_RELEASER<PCB_IO> pi( plugin.m_createFunc() );
123
124 if( pi->CanReadBoard( aFileName ) )
125 return plugin.m_type;
126 }
127
129}
130
131
133{
134 const auto& plugins = PCB_IO_MGR::PLUGIN_REGISTRY::Instance()->AllPlugins();
135
136 for( const auto& plugin : plugins )
137 {
138 bool isKiCad = plugin.m_type == PCB_IO_MGR::KICAD_SEXP || plugin.m_type == PCB_IO_MGR::LEGACY;
139
140 if( ( aCtl & KICTL_KICAD_ONLY ) && !isKiCad )
141 continue;
142
143 if( ( aCtl & KICTL_NONKICAD_ONLY ) && isKiCad )
144 continue;
145
146 IO_RELEASER<PCB_IO> pi( plugin.m_createFunc() );
147
148 if( pi->CanReadLibrary( aLibPath ) )
149 return plugin.m_type;
150 }
151
153}
154
155
156BOARD* PCB_IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aAppendToMe,
157 const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
158 PROGRESS_REPORTER* aProgressReporter )
159{
160 IO_RELEASER<PCB_IO> pi( PluginFind( aFileType ) );
161
162 if( pi ) // test pi->plugin
163 {
164 pi->SetProgressReporter( aProgressReporter );
165 return pi->LoadBoard( aFileName, aAppendToMe, aProperties, aProject );
166 }
167
168 THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
169}
170
171
172void PCB_IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard,
173 const STRING_UTF8_MAP* aProperties )
174{
175 IO_RELEASER<PCB_IO> pi( PluginFind( aFileType ) );
176
177 if( pi )
178 {
179 pi->SaveBoard( aFileName, aBoard, aProperties ); // virtual
180 return;
181 }
182
183 THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
184}
185
186
187bool PCB_IO_MGR::ConvertLibrary( STRING_UTF8_MAP* aOldFileProps, const wxString& aOldFilePath,
188 const wxString& aNewFilePath )
189{
191
192 if( oldFileType == PCB_IO_MGR::FILE_TYPE_NONE )
193 return false;
194
195
196 IO_RELEASER<PCB_IO> oldFilePI( PCB_IO_MGR::PluginFind( oldFileType ) );
198 wxArrayString fpNames;
199 wxFileName newFileName( aNewFilePath );
200
201 if( newFileName.HasExt() )
202 {
203 wxString extraDir = newFileName.GetFullName();
204 newFileName.ClearExt();
205 newFileName.SetName( "" );
206 newFileName.AppendDir( extraDir );
207 }
208
209 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
210 return false;
211
212 try
213 {
214 bool bestEfforts = false; // throw on first error
215 oldFilePI->FootprintEnumerate( fpNames, aOldFilePath, bestEfforts, aOldFileProps );
216
217 for ( const wxString& fpName : fpNames )
218 {
219 std::unique_ptr<const FOOTPRINT> fp( oldFilePI->GetEnumeratedFootprint( aOldFilePath, fpName, aOldFileProps ) );
220 kicadPI->FootprintSave( aNewFilePath, fp.get() );
221 }
222
223 }
224 catch( ... )
225 {
226 return false;
227 }
228
229 return true;
230}
231
232
233// These text strings are "truth" for identifying the plugins. If you change the spellings,
234// you will obsolete library tables, so don't do it. Additions are OK.
235
236// clang-format off
239 wxT( "KiCad" ),
240 []() -> PCB_IO* { return new PCB_IO_KICAD_SEXPR; } );
241
244 wxT( "Legacy" ),
245 []() -> PCB_IO* { return new PCB_IO_KICAD_LEGACY; } );
246
247// Keep non-KiCad plugins in alphabetical order
248
251 wxT( "Altium Circuit Maker" ),
252 []() -> PCB_IO* { return new PCB_IO_ALTIUM_CIRCUIT_MAKER; } );
253
256 wxT( "Altium Circuit Studio" ),
257 []() -> PCB_IO* { return new PCB_IO_ALTIUM_CIRCUIT_STUDIO; } );
258
261 wxT( "Altium Designer" ),
262 []() -> PCB_IO* { return new PCB_IO_ALTIUM_DESIGNER; } );
263
266 wxT( "CADSTAR PCB Archive" ),
267 []() -> PCB_IO* { return new PCB_IO_CADSTAR_ARCHIVE; } );
268
271 wxT( "Eagle" ),
272 []() -> PCB_IO* { return new PCB_IO_EAGLE; } );
273
276 wxT( "EasyEDA / JLCEDA Std" ),
277 []() -> PCB_IO* { return new PCB_IO_EASYEDA; });
278
281 wxT( "EasyEDA / JLCEDA Pro" ),
282 []() -> PCB_IO* { return new PCB_IO_EASYEDAPRO; });
283
286 wxT( "Fabmaster" ),
287 []() -> PCB_IO* { return new PCB_IO_FABMASTER; } );
288
291 wxT( "GEDA/Pcb" ),
292 []() -> PCB_IO* { return new PCB_IO_GEDA; } );
293
296 wxT( "P-Cad" ),
297 []() -> PCB_IO* { return new PCB_IO_PCAD; } );
298
301 wxT( "Solidworks PCB" ),
302 []() -> PCB_IO* { return new PCB_IO_SOLIDWORKS; } );
303
306 wxT( "IPC-2581" ),
307 []() -> PCB_IO* { return new PCB_IO_IPC2581; } );
308// clang-format on
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
Works with Eagle 6.x XML board files and footprints to implement the Pcbnew #PLUGIN API or a portion ...
Definition: pcb_io_eagle.h:132
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:114
const std::vector< ENTRY > & AllPlugins() const
Definition: pcb_io_mgr.h:127
static PLUGIN_REGISTRY * Instance()
Definition: pcb_io_mgr.h:93
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:65
static void Save(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aBoard, const STRING_UTF8_MAP *aProperties=nullptr)
Write either a full aBoard to a storage file in a format that this implementation knows about,...
Definition: pcb_io_mgr.cpp:172
static bool ConvertLibrary(STRING_UTF8_MAP *aOldFileProps, const wxString &aOldFilePath, const wxString &aNewFilePath)
Convert a schematic symbol library to the latest KiCad format.
Definition: pcb_io_mgr.cpp:187
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:90
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:76
@ 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 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:108
static BOARD * Load(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aAppendToMe=nullptr, const STRING_UTF8_MAP *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:156
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:132
static const wxString ShowType(PCB_FILE_T aFileType)
Return a brief name for a plugin given aFileType enum.
Definition: pcb_io_mgr.cpp:74
A base class that BOARD loading and saving plugins should derive from.
Definition: pcb_io.h:72
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition: project.h:62
A name/value tuple with unique names and optional values.
#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:77
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
Definition: kiway_player.h:76
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:146
Definition of file extensions used in Kicad.