KiCad PCB EDA Suite
pluginldr3D.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) 2015 Cirilo Bernardo <[email protected]>
5 * Copyright (C) 2021 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 <iostream>
26#include <sstream>
27#include <wx/log.h>
28#include <wx/translation.h>
29
31
32#define PLUGIN_CLASS_3D "PLUGIN_3D"
33#define PLUGIN_3D_MAJOR 1
34#define PLUGIN_3D_MINOR 0
35#define PLUGIN_3D_PATCH 0
36#define PLUGIN_3D_REVISION 0
37
38
40{
41 ok = false;
42 m_getNExtensions = nullptr;
43 m_getModelExtension = nullptr;
44 m_getNFilters = nullptr;
45 m_getFileFilter = nullptr;
46 m_canRender = nullptr;
47 m_load = nullptr;
48
49 return;
50}
51
52
54{
55 Close();
56
57 return;
58}
59
60
61bool KICAD_PLUGIN_LDR_3D::Open( const wxString& aFullFileName )
62{
63 m_error.clear();
64
65 if( ok )
66 Close();
67
68 if( !open( aFullFileName, PLUGIN_CLASS_3D ) )
69 {
70 if( m_error.empty() )
71 {
72 std::ostringstream ostr;
73 ostr << "Failed to open plugin '" << aFullFileName.ToUTF8() << "'";
74 m_error = ostr.str();
75 }
76
77 wxLogTrace( tracePluginLoader, wxT( "%s:%s:%d\n"
78 " * [INFO] failed on file '%s'\n"
79 " * [INFO] error: " ),
80 __FILE__, __FUNCTION__, __LINE__, aFullFileName.ToUTF8(), m_error );
81
82 return false;
83 }
84
85 // the version checks passed and the base KICAD_PLUGIN functions have been linked;
86 // now we link the remaining functions expected by PLUGIN_3D and confirm that the
87 // plugin is loaded
94
95#ifdef DEBUG
96 bool fail = false;
97
98 if( !m_getNExtensions )
99 {
100 wxLogTrace( tracePluginLoader,
101 wxT( "%s:%s:%d\n"
102 "incompatible plugin (missing function 'GetNExtensions')" ),
103 __FILE__, __FUNCTION__, __LINE__ );
104
105 fail = true;
106 }
107
109 {
110 if( !fail )
111 {
112 wxLogTrace( tracePluginLoader,
113 wxT( "%s:%s:%d\n"
114 "incompatible plugin (missing function 'GetModelExtension')" ),
115 __FILE__, __FUNCTION__, __LINE__ );
116
117 fail = true;
118 }
119 else
120 {
121 wxLogTrace( tracePluginLoader, wxT( "missing function 'GetModelExtension'" ) );
122 }
123 }
124
125 if( !m_getNFilters )
126 {
127 if( !fail )
128 {
129 wxLogTrace( tracePluginLoader,
130 wxT( "%s:%s:%d\n"
131 "incompatible plugin (missing function 'GetNFilters')" ),
132 __FILE__, __FUNCTION__, __LINE__ );
133
134 fail = true;
135 }
136 else
137 {
138 wxLogTrace( tracePluginLoader, wxT( "missing function 'GetNFilters'" ) );
139 }
140 }
141
142 if( !m_getFileFilter )
143 {
144 if( !fail )
145 {
146 wxLogTrace( tracePluginLoader,
147 wxT( "%s:%s:%d\n"
148 "incompatible plugin (missing function 'GetFileFilter')" ),
149 __FILE__, __FUNCTION__, __LINE__ );
150
151 fail = true;
152 }
153 else
154 {
155 wxLogTrace( tracePluginLoader, wxT( "missing function 'GetFileFilter'" ) );
156 }
157 }
158
159 if( !m_canRender )
160 {
161 if( !fail )
162 {
163 wxLogTrace( tracePluginLoader,
164 wxT( "%s:%s:%d\n"
165 "incompatible plugin (missing function 'CanRender')" ),
166 __FILE__, __FUNCTION__, __LINE__ );
167
168 fail = true;
169 }
170 else
171 {
172 wxLogTrace( tracePluginLoader, wxT( "missing function 'CanRender'" ) );
173 }
174 }
175
176 if( !m_load )
177 {
178 if( !fail )
179 {
180 wxLogTrace( tracePluginLoader,
181 wxT( "%s:%s:%d\n"
182 "incompatible plugin (missing function 'Load')" ),
183 __FILE__, __FUNCTION__, __LINE__ );
184 }
185 else
186 {
187 wxLogTrace( tracePluginLoader, wxT( "missing function 'Load'" ) );
188 }
189 }
190
191#endif
192
195 {
196 Close();
197
198 std::ostringstream ostr;
199 ostr << "Failed to open plugin '" << aFullFileName.ToUTF8() << "'; missing functions";
200 m_error = ostr.str();
201
202 return false;
203 }
204
205 ok = true;
206 return true;
207}
208
209
211{
212#ifdef DEBUG
213 if( ok )
214 {
215 wxLogTrace( tracePluginLoader, wxT( "%s:%s:%d\n"
216 " * [INFO] closing plugin" ),
217 __FILE__, __FUNCTION__, __LINE__ );
218 }
219#endif
220
221 ok = false;
222 m_getNExtensions = nullptr;
223 m_getModelExtension = nullptr;
224 m_getNFilters = nullptr;
225 m_getFileFilter = nullptr;
226 m_canRender = nullptr;
227 m_load = nullptr;
228 close();
229
230 return;
231}
232
233
234void KICAD_PLUGIN_LDR_3D::GetLoaderVersion( unsigned char* Major, unsigned char* Minor,
235 unsigned char* Patch, unsigned char* Revision ) const
236{
237 if( Major )
238 *Major = PLUGIN_3D_MAJOR;
239
240 if( Minor )
241 *Minor = PLUGIN_3D_MINOR;
242
243 if( Patch )
244 *Patch = PLUGIN_3D_PATCH;
245
246 if( Revision )
247 *Revision = PLUGIN_3D_REVISION;
248
249 return;
250}
251
252
254{
255 m_error.clear();
256
257 if( !ok && !reopen() )
258 {
259 if( m_error.empty() )
260 m_error = "[INFO] no open plugin / plugin could not be opened";
261
262 return 0;
263 }
264
265 if( nullptr == m_getNExtensions )
266 {
267 m_error = "[BUG] GetNExtensions is not linked";
268
269 wxLogTrace( tracePluginLoader, wxT( "%s:%s:%d\n"
270 "%s" ),
271 __FILE__, __FUNCTION__, __LINE__, m_error );;
272
273 return 0;
274 }
275
276 return m_getNExtensions();
277}
278
279
281{
282 m_error.clear();
283
284 if( !ok && !reopen() )
285 {
286 if( m_error.empty() )
287 m_error = "[INFO] no open plugin / plugin could not be opened";
288
289 return nullptr;
290 }
291
292 if( nullptr == m_getModelExtension )
293 {
294 m_error = "[BUG] GetModelExtension is not linked";
295
296 wxLogTrace( tracePluginLoader, wxT( "%s:%s:%d\n"
297 "%s" ),
298 __FILE__, __FUNCTION__, __LINE__, m_error );;
299
300 return nullptr;
301 }
302
303 return m_getModelExtension( aIndex );
304}
305
306
308{
309 m_error.clear();
310
311 if( !ok && !reopen() )
312 {
313 if( m_error.empty() )
314 m_error = "[INFO] no open plugin / plugin could not be opened";
315
316 return 0;
317 }
318
319 if( nullptr == m_getNFilters )
320 {
321 m_error = "[BUG] GetNFilters is not linked";
322
323 wxLogTrace( tracePluginLoader, wxT( "%s:%s:%d\n"
324 "%s" ),
325 __FILE__, __FUNCTION__, __LINE__, m_error );;
326
327 return 0;
328 }
329
330 return m_getNFilters();
331}
332
333
334char const* KICAD_PLUGIN_LDR_3D::GetFileFilter( int aIndex )
335{
336 m_error.clear();
337
338 if( !ok && !reopen() )
339 {
340 if( m_error.empty() )
341 m_error = "[INFO] no open plugin / plugin could not be opened";
342
343 return nullptr;
344 }
345
346 if( nullptr == m_getFileFilter )
347 {
348 m_error = "[BUG] GetFileFilter is not linked";
349
350 wxLogTrace( tracePluginLoader, wxT( "%s:%s:%d\n"
351 "%s" ),
352 __FILE__, __FUNCTION__, __LINE__, m_error );;
353
354 return nullptr;
355 }
356
357 return m_getFileFilter( aIndex );
358}
359
360
362{
363 m_error.clear();
364
365 if( !ok && !reopen() )
366 {
367 if( m_error.empty() )
368 m_error = "[INFO] no open plugin / plugin could not be opened";
369
370 return false;
371 }
372
373 if( nullptr == m_canRender )
374 {
375 m_error = "[BUG] CanRender is not linked";
376
377 wxLogTrace( tracePluginLoader, wxT( "%s:%s:%d\n"
378 "%s" ),
379 __FILE__, __FUNCTION__, __LINE__, m_error );;
380
381 return false;
382 }
383
384 return m_canRender();
385}
386
387
388SCENEGRAPH* KICAD_PLUGIN_LDR_3D::Load( char const* aFileName )
389{
390 m_error.clear();
391
392 if( !ok && !reopen() )
393 {
394 if( m_error.empty() )
395 m_error = "[INFO] no open plugin / plugin could not be opened";
396
397 return nullptr;
398 }
399
400 if( nullptr == m_load )
401 {
402 m_error = "[BUG] Load is not linked";
403
404 wxLogTrace( tracePluginLoader, wxT( "%s:%s:%d\n"
405 "%s" ),
406 __FILE__, __FUNCTION__, __LINE__, m_error );;
407
408 return nullptr;
409 }
410
411 return m_load( aFileName );
412}
bool Open(const wxString &aFullFileName) override
Open a plugin of the given class, performs version compatibility checks, and links all required funct...
Definition: pluginldr3D.cpp:61
PLUGIN_3D_GET_FILE_FILTER m_getFileFilter
Definition: pluginldr3D.h:82
SCENEGRAPH * Load(char const *aFileName)
char const * GetFileFilter(int aIndex)
PLUGIN_3D_GET_N_FILTERS m_getNFilters
Definition: pluginldr3D.h:81
int GetNExtensions(void)
virtual ~KICAD_PLUGIN_LDR_3D()
Definition: pluginldr3D.cpp:53
void GetLoaderVersion(unsigned char *Major, unsigned char *Minor, unsigned char *Revision, unsigned char *Patch) const override
Return the version information of the Plugin Loader for plugin compatibility checking.
PLUGIN_3D_LOAD m_load
Definition: pluginldr3D.h:84
PLUGIN_3D_CAN_RENDER m_canRender
Definition: pluginldr3D.h:83
PLUGIN_3D_GET_MODEL_EXTENSION m_getModelExtension
Definition: pluginldr3D.h:80
PLUGIN_3D_GET_N_EXTENSIONS m_getNExtensions
Definition: pluginldr3D.h:79
char const * GetModelExtension(int aIndex)
void Close(void) override
Clean up and closes/unloads the plugin.
bool open(const wxString &aFullFileName, const char *aPluginClass)
Open a plugin of the specified class and links the extensions required by kicad_plugin.
Definition: pluginldr.cpp:63
std::string m_error
Definition: pluginldr.h:134
void close(void)
Nullify internal pointers in preparation for closing the plugin.
Definition: pluginldr.cpp:261
bool reopen(void)
Reopen a plugin.
Definition: pluginldr.cpp:275
Define the basic data set required to represent a 3D model.
Definition: scenegraph.h:45
const wxChar *const tracePluginLoader
Flag to enable plugin loader trace output.
Definition: pluginldr.cpp:40
#define PLUGIN_CLASS_3D
Definition: pluginldr3D.cpp:32
#define PLUGIN_3D_REVISION
Definition: pluginldr3D.cpp:36
#define PLUGIN_3D_PATCH
Definition: pluginldr3D.cpp:35
#define PLUGIN_3D_MAJOR
Definition: pluginldr3D.cpp:33
#define PLUGIN_3D_MINOR
Definition: pluginldr3D.cpp:34
int(* PLUGIN_3D_GET_N_EXTENSIONS)(void)
Definition: pluginldr3D.h:39
char const *(* PLUGIN_3D_GET_MODEL_EXTENSION)(int aIndex)
Definition: pluginldr3D.h:41
char const *(* PLUGIN_3D_GET_FILE_FILTER)(int aIndex)
Definition: pluginldr3D.h:45
SCENEGRAPH *(* PLUGIN_3D_LOAD)(char const *aFileName)
Definition: pluginldr3D.h:49
bool(* PLUGIN_3D_CAN_RENDER)(void)
Definition: pluginldr3D.h:47
int(* PLUGIN_3D_GET_N_FILTERS)(void)
Definition: pluginldr3D.h:43
#define LINK_ITEM(funcPtr, funcType, funcName)
Definition: pluginldr.h:45