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 <cirilo.bernardo@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <iostream>
25 #include <sstream>
26 #include <wx/log.h>
27 #include <wx/translation.h>
28 
30 
31 #define PLUGIN_CLASS_3D "PLUGIN_3D"
32 #define PLUGIN_3D_MAJOR 1
33 #define PLUGIN_3D_MINOR 0
34 #define PLUGIN_3D_PATCH 0
35 #define PLUGIN_3D_REVISION 0
36 
37 
39 {
40  ok = false;
45  m_canRender = NULL;
46  m_load = NULL;
47 
48  return;
49 }
50 
51 
53 {
54  Close();
55 
56  return;
57 }
58 
59 
60 bool KICAD_PLUGIN_LDR_3D::Open( const wxString& aFullFileName )
61 {
62  m_error.clear();
63 
64  if( ok )
65  Close();
66 
67  if( !open( aFullFileName, PLUGIN_CLASS_3D ) )
68  {
69  if( m_error.empty() )
70  {
71  std::ostringstream ostr;
72  ostr << "Failed to open plugin '" << aFullFileName.ToUTF8() << "'";
73  m_error = ostr.str();
74  }
75 
76  #ifdef DEBUG
77  std::ostringstream ostr;
78  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
79  ostr << " * [INFO] failed on file " << aFullFileName.ToUTF8() << "\n";
80  ostr << " * [INFO] error: " << m_error;
81  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
82  #endif
83 
84  return false;
85  }
86 
87  // the version checks passed and the base KICAD_PLUGIN functions have been linked;
88  // now we link the remaining functions expected by PLUGIN_3D and confirm that the
89  // plugin is loaded
95  LINK_ITEM( m_load, PLUGIN_3D_LOAD, "Load" );
96 
97  #ifdef DEBUG
98  bool fail = false;
99 
100  if( !m_getNExtensions )
101  {
102  std::ostringstream ostr;
103  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
104  wxString errmsg = "incompatible plugin (missing function 'GetNExtensions')";
105  ostr << errmsg.ToUTF8() << "\n";
106  ostr << "'" << aFullFileName.ToUTF8() << "'";
107  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
108  fail = true;
109  }
110 
111  if( !m_getModelExtension )
112  {
113  if( !fail )
114  {
115  std::ostringstream ostr;
116  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
117  wxString errmsg = "incompatible plugin (missing function 'GetModelExtension')";
118  ostr << errmsg.ToUTF8() << "\n";
119  ostr << "'" << aFullFileName.ToUTF8() << "'";
120  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
121  fail = true;
122  }
123  else
124  {
125  std::ostringstream ostr;
126  wxString errmsg = "missing function 'GetModelExtension'";
127  ostr << errmsg.ToUTF8();
128  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
129  }
130  }
131 
132  if( !m_getNFilters )
133  {
134  if( !fail )
135  {
136  std::ostringstream ostr;
137  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
138  wxString errmsg = "incompatible plugin (missing function 'GetNFilters')";
139  ostr << errmsg.ToUTF8() << "\n";
140  ostr << "'" << aFullFileName.ToUTF8() << "'";
141  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
142  fail = true;
143  }
144  else
145  {
146  std::ostringstream ostr;
147  wxString errmsg = "missing function 'GetNFilters'";
148  ostr << errmsg.ToUTF8();
149  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
150  }
151  }
152 
153  if( !m_getFileFilter )
154  {
155  if( !fail )
156  {
157  std::ostringstream ostr;
158  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
159  wxString errmsg = "incompatible plugin (missing function 'GetFileFilter')";
160  ostr << errmsg.ToUTF8() << "\n";
161  ostr << "'" << aFullFileName.ToUTF8() << "'";
162  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
163  fail = true;
164  }
165  else
166  {
167  std::ostringstream ostr;
168  wxString errmsg = "missing function 'GetFileFilter'";
169  ostr << errmsg.ToUTF8();
170  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
171  }
172  }
173 
174  if( !m_canRender )
175  {
176  if( !fail )
177  {
178  std::ostringstream ostr;
179  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
180  wxString errmsg = "incompatible plugin (missing function 'CanRender')";
181  ostr << errmsg.ToUTF8() << "\n";
182  ostr << "'" << aFullFileName.ToUTF8() << "'";
183  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
184  fail = true;
185  }
186  else
187  {
188  std::ostringstream ostr;
189  wxString errmsg = "missing function 'CanRender'";
190  ostr << errmsg.ToUTF8();
191  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
192  }
193  }
194 
195  if( !m_load )
196  {
197  if( !fail )
198  {
199  std::ostringstream ostr;
200  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
201  wxString errmsg = "incompatible plugin (missing function 'Load')";
202  ostr << errmsg.ToUTF8() << "\n";
203  ostr << "'" << aFullFileName.ToUTF8() << "'";
204  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
205  }
206  else
207  {
208  std::ostringstream ostr;
209  wxString errmsg = "missing function 'Load'";
210  ostr << errmsg.ToUTF8();
211  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
212  }
213  }
214 
215  #endif
216 
218  || !m_getFileFilter || !m_canRender || !m_load )
219  {
220  Close();
221 
222  std::ostringstream ostr;
223  ostr << "Failed to open plugin '" << aFullFileName.ToUTF8() << "'; missing functions";
224  m_error = ostr.str();
225 
226  return false;
227  }
228 
229  ok = true;
230  return true;
231 }
232 
233 
235 {
236  #ifdef DEBUG
237  if( ok )
238  {
239  std::ostringstream ostr;
240  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
241  ostr << " * [INFO] closing plugin";
242  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
243  }
244  #endif
245 
246  ok = false;
251  m_canRender = NULL;
252  m_load = NULL;
253  close();
254 
255  return;
256 }
257 
258 
259 void KICAD_PLUGIN_LDR_3D::GetLoaderVersion( unsigned char* Major, unsigned char* Minor,
260  unsigned char* Patch, unsigned char* Revision ) const
261 {
262  if( Major )
263  *Major = PLUGIN_3D_MAJOR;
264 
265  if( Minor )
266  *Minor = PLUGIN_3D_MINOR;
267 
268  if( Patch )
269  *Patch = PLUGIN_3D_PATCH;
270 
271  if( Revision )
272  *Revision = PLUGIN_3D_REVISION;
273 
274  return;
275 }
276 
277 
278 // these functions are shadows of the 3D Plugin functions from 3d_plugin.h
280 {
281  m_error.clear();
282 
283  if( !ok && !reopen() )
284  {
285  if( m_error.empty() )
286  m_error = "[INFO] no open plugin / plugin could not be opened";
287 
288  return 0;
289  }
290 
291  if( NULL == m_getNExtensions )
292  {
293  m_error = "[BUG] GetNExtensions is not linked";
294 
295  #ifdef DEBUG
296  std::ostringstream ostr;
297  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
298  ostr << " * " << m_error;
299  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
300  #endif
301 
302  return 0;
303  }
304 
305  return m_getNExtensions();
306 }
307 
308 
309 char const* KICAD_PLUGIN_LDR_3D::GetModelExtension( int aIndex )
310 {
311  m_error.clear();
312 
313  if( !ok && !reopen() )
314  {
315  if( m_error.empty() )
316  m_error = "[INFO] no open plugin / plugin could not be opened";
317 
318  return NULL;
319  }
320 
321  if( NULL == m_getModelExtension )
322  {
323  m_error = "[BUG] GetModelExtension is not linked";
324 
325  #ifdef DEBUG
326  std::ostringstream ostr;
327  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
328  ostr << " * " << m_error;
329  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
330  #endif
331 
332  return NULL;
333  }
334 
335  return m_getModelExtension( aIndex );
336 }
337 
338 
340 {
341  m_error.clear();
342 
343  if( !ok && !reopen() )
344  {
345  if( m_error.empty() )
346  m_error = "[INFO] no open plugin / plugin could not be opened";
347 
348  return 0;
349  }
350 
351  if( NULL == m_getNFilters )
352  {
353  m_error = "[BUG] GetNFilters is not linked";
354 
355  #ifdef DEBUG
356  std::ostringstream ostr;
357  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
358  ostr << " * " << m_error;
359  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
360  #endif
361 
362  return 0;
363  }
364 
365  return m_getNFilters();
366 }
367 
368 
369 char const* KICAD_PLUGIN_LDR_3D::GetFileFilter( int aIndex )
370 {
371  m_error.clear();
372 
373  if( !ok && !reopen() )
374  {
375  if( m_error.empty() )
376  m_error = "[INFO] no open plugin / plugin could not be opened";
377 
378  return NULL;
379  }
380 
381  if( NULL == m_getFileFilter )
382  {
383  m_error = "[BUG] GetFileFilter is not linked";
384 
385  #ifdef DEBUG
386  std::ostringstream ostr;
387  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
388  ostr << " * " << m_error;
389  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
390  #endif
391 
392  return NULL;
393  }
394 
395  return m_getFileFilter( aIndex );
396 }
397 
398 
400 {
401  m_error.clear();
402 
403  if( !ok && !reopen() )
404  {
405  if( m_error.empty() )
406  m_error = "[INFO] no open plugin / plugin could not be opened";
407 
408  return false;
409  }
410 
411  if( NULL == m_canRender )
412  {
413  m_error = "[BUG] CanRender is not linked";
414 
415  #ifdef DEBUG
416  std::ostringstream ostr;
417  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
418  ostr << " * " << m_error;
419  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
420  #endif
421 
422  return false;
423  }
424 
425  return m_canRender();
426 }
427 
428 
429 SCENEGRAPH* KICAD_PLUGIN_LDR_3D::Load( char const* aFileName )
430 {
431  m_error.clear();
432 
433  if( !ok && !reopen() )
434  {
435  if( m_error.empty() )
436  m_error = "[INFO] no open plugin / plugin could not be opened";
437 
438  return NULL;
439  }
440 
441  if( NULL == m_load )
442  {
443  m_error = "[BUG] Load is not linked";
444 
445  #ifdef DEBUG
446  std::ostringstream ostr;
447  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
448  ostr << " * " << m_error;
449  wxLogTrace( MASK_PLUGINLDR, "%s\n", ostr.str().c_str() );
450  #endif
451 
452  return NULL;
453  }
454 
455  return m_load( aFileName );
456 }
PLUGIN_3D_CAN_RENDER m_canRender
Definition: pluginldr3D.h:59
bool(* PLUGIN_3D_CAN_RENDER)(void)
Definition: pluginldr3D.h:46
void Close(void) override
Function Close cleans up and closes/unloads the plugin.
PLUGIN_3D_GET_MODEL_EXTENSION m_getModelExtension
Definition: pluginldr3D.h:56
#define MASK_PLUGINLDR
Definition: pluginldr.h:37
#define PLUGIN_3D_REVISION
Definition: pluginldr3D.cpp:35
bool Open(const wxString &aFullFileName) override
Function Open opens a plugin of the given class, performs version compatibility checks,...
Definition: pluginldr3D.cpp:60
std::string m_error
Definition: pluginldr.h:73
SCENEGRAPH * Load(char const *aFileName)
int GetNExtensions(void)
PLUGIN_3D_GET_N_EXTENSIONS m_getNExtensions
Definition: pluginldr3D.h:55
char const *(* PLUGIN_3D_GET_MODEL_EXTENSION)(int aIndex)
Definition: pluginldr3D.h:40
bool open(const wxString &aFullFileName, const char *aPluginClass)
Function open opens a plugin of the specified class and links the extensions required by kicad_plugin...
Definition: pluginldr.cpp:54
char const * GetFileFilter(int aIndex)
PLUGIN_3D_GET_N_FILTERS m_getNFilters
Definition: pluginldr3D.h:57
#define NULL
PLUGIN_3D_GET_FILE_FILTER m_getFileFilter
Definition: pluginldr3D.h:58
char const * GetModelExtension(int aIndex)
#define PLUGIN_CLASS_3D
Definition: pluginldr3D.cpp:31
SCENEGRAPH *(* PLUGIN_3D_LOAD)(char const *aFileName)
Definition: pluginldr3D.h:48
bool reopen(void)
Function reopen reopens a plugin and returns true on success.
Definition: pluginldr.cpp:298
int(* PLUGIN_3D_GET_N_EXTENSIONS)(void)
Definition: pluginldr3D.h:38
void GetLoaderVersion(unsigned char *Major, unsigned char *Minor, unsigned char *Revision, unsigned char *Patch) const override
Function GetLoaderVersion returns the version information of the Plugin Loader for plugin compatibili...
#define PLUGIN_3D_PATCH
Definition: pluginldr3D.cpp:34
#define PLUGIN_3D_MAJOR
Definition: pluginldr3D.cpp:32
virtual ~KICAD_PLUGIN_LDR_3D()
Definition: pluginldr3D.cpp:52
void close(void)
Function close nullifies internal pointers in preparation for closing the plugin.
Definition: pluginldr.cpp:284
#define LINK_ITEM(funcPtr, funcType, funcName)
Definition: pluginldr.h:41
char const *(* PLUGIN_3D_GET_FILE_FILTER)(int aIndex)
Definition: pluginldr3D.h:44
#define PLUGIN_3D_MINOR
Definition: pluginldr3D.cpp:33
int(* PLUGIN_3D_GET_N_FILTERS)(void)
Definition: pluginldr3D.h:42
PLUGIN_3D_LOAD m_load
Definition: pluginldr3D.h:60