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