KiCad PCB EDA Suite
vrml.cpp File Reference
#include "plugins/3d/3d_plugin.h"
#include "plugins/3dapi/ifsg_all.h"
#include "richio.h"
#include "vrml1_base.h"
#include "vrml2_base.h"
#include "wrlproc.h"
#include "x3d.h"
#include <clocale>
#include <wx/filename.h>
#include <wx/stdpaths.h>
#include <wx/string.h>
#include <wx/wfstream.h>
#include <wx/log.h>
#include <decompress.hpp>

Go to the source code of this file.

Classes

struct  FILE_DATA
 
class  LOCALESWITCH
 

Macros

#define PLUGIN_VRML_MAJOR   1
 
#define PLUGIN_VRML_MINOR   3
 
#define PLUGIN_VRML_PATCH   2
 
#define PLUGIN_VRML_REVNO   2
 

Functions

const char * GetKicadPluginName (void)
 Function GetKicadPluginName returns the name of the plugin instance; for example IDFv3. More...
 
void GetPluginVersion (unsigned char *Major, unsigned char *Minor, unsigned char *Patch, unsigned char *Revision)
 Function GetPluginVersion retrieves the version of the instantiated plugin for informational purposes. More...
 
int GetNExtensions (void)
 Function GetNExtensions. More...
 
char const * GetModelExtension (int aIndex)
 Function GetModelExtension. More...
 
int GetNFilters (void)
 Function GetNFilters. More...
 
char const * GetFileFilter (int aIndex)
 Function GetFileFilter. More...
 
bool CanRender (void)
 Function CanRender. More...
 
SCENEGRAPHLoadVRML (const wxString &aFileName, bool useInline)
 
SCENEGRAPHLoadX3D (const wxString &aFileName)
 
SCENEGRAPHLoad (char const *aFileName)
 reads a model file and creates a generic display structure More...
 

Variables

const wxChar *const traceVrmlPlugin = wxT( "KICAD_VRML_PLUGIN" )
 Flag to enable VRML plugin trace output. More...
 
static struct FILE_DATA file_data
 

Macro Definition Documentation

◆ PLUGIN_VRML_MAJOR

#define PLUGIN_VRML_MAJOR   1

Definition at line 52 of file vrml.cpp.

◆ PLUGIN_VRML_MINOR

#define PLUGIN_VRML_MINOR   3

Definition at line 53 of file vrml.cpp.

◆ PLUGIN_VRML_PATCH

#define PLUGIN_VRML_PATCH   2

Definition at line 54 of file vrml.cpp.

◆ PLUGIN_VRML_REVNO

#define PLUGIN_VRML_REVNO   2

Definition at line 55 of file vrml.cpp.

Function Documentation

◆ CanRender()

bool CanRender ( void  )

Function CanRender.

Returns
true if the plugin can render a model, that is the Load() function is implemented

Definition at line 143 of file vrml.cpp.

144 {
145  // this plugin supports rendering of IDF component outlines
146  return true;
147 }

◆ GetFileFilter()

char const* GetFileFilter ( int  aIndex)

Function GetFileFilter.

Returns
the file filter string for the given index

Definition at line 134 of file vrml.cpp.

135 {
136  if( aIndex < 0 || aIndex >= int( file_data.filters.size() ) )
137  return nullptr;
138 
139  return file_data.filters[aIndex].c_str();
140 }
static struct FILE_DATA file_data
char const * filters[NFILS]

References file_data, and FILE_DATA::filters.

◆ GetKicadPluginName()

const char* GetKicadPluginName ( void  )

Function GetKicadPluginName returns the name of the plugin instance; for example IDFv3.

This string may be used to check for name conflicts or to display informational messages about loaded plugins. This method must be implemented in specific instantiations of a plugin class.

Returns
is the NULL-terminated UTF-8 string representing the plugin name

Definition at line 66 of file vrml.cpp.

67 {
68  return "PLUGIN_3D_VRML";
69 }

◆ GetModelExtension()

char const* GetModelExtension ( int  aIndex)

Function GetModelExtension.

Parameters
aIndexis the extension to return; valid values are 0 to GetNExtensions() - 1.
Returns
the requested extension or a null string if aIndex was invalid.

Definition at line 119 of file vrml.cpp.

120 {
121  if( aIndex < 0 || aIndex >= int( file_data.extensions.size() ) )
122  return nullptr;
123 
124  return file_data.extensions[aIndex].c_str();
125 }
static struct FILE_DATA file_data
char const * extensions[NEXTS]

References FILE_DATA::extensions, and file_data.

◆ GetNExtensions()

int GetNExtensions ( void  )

Function GetNExtensions.

Returns
the number of extensions supported by the plugin

Definition at line 113 of file vrml.cpp.

114 {
115  return file_data.extensions.size();
116 }
static struct FILE_DATA file_data
char const * extensions[NEXTS]

References FILE_DATA::extensions, and file_data.

◆ GetNFilters()

int GetNFilters ( void  )

Function GetNFilters.

Returns
the number of file filters

Definition at line 128 of file vrml.cpp.

129 {
130  return file_data.filters.size();
131 }
static struct FILE_DATA file_data
char const * filters[NFILS]

References file_data, and FILE_DATA::filters.

◆ GetPluginVersion()

void GetPluginVersion ( unsigned char *  Major,
unsigned char *  Minor,
unsigned char *  Patch,
unsigned char *  Revision 
)

Function GetPluginVersion retrieves the version of the instantiated plugin for informational purposes.

Do not confuse this with GetClassVersion which is used to determine API compatibility.

Parameters
Majorwill hold the Plugin Major version
Minorwill hold the Plugin Minor version
Patchwill hold the Plugin Patch level
Revisionwill hold the Plugin Revision

Definition at line 72 of file vrml.cpp.

74 {
75  if( Major )
76  *Major = PLUGIN_VRML_MAJOR;
77 
78  if( Minor )
79  *Minor = PLUGIN_VRML_MINOR;
80 
81  if( Patch )
82  *Patch = PLUGIN_VRML_PATCH;
83 
84  if( Revision )
85  *Revision = PLUGIN_VRML_REVNO;
86 
87  return;
88 }
#define PLUGIN_VRML_MAJOR
Definition: vrml.cpp:52
#define PLUGIN_VRML_PATCH
Definition: vrml.cpp:54
#define PLUGIN_VRML_MINOR
Definition: vrml.cpp:53
#define PLUGIN_VRML_REVNO
Definition: vrml.cpp:55

References PLUGIN_VRML_MAJOR, PLUGIN_VRML_MINOR, PLUGIN_VRML_PATCH, and PLUGIN_VRML_REVNO.

◆ Load()

SCENEGRAPH* Load ( char const *  aFileName)

reads a model file and creates a generic display structure

Parameters
aFileNameis the full path of the model file
Returns
a SCENEGRAPH pointer to the display structure if the model was successfully loaded and NULL if there is no rendering support for the model or there were problems reading the model

Definition at line 319 of file vrml.cpp.

320 {
321  if( nullptr == aFileName )
322  return nullptr;
323 
324  wxString fname = wxString::FromUTF8Unchecked( aFileName );
325 
326  if( !wxFileName::FileExists( fname ) )
327  return nullptr;
328 
329  LOCALESWITCH switcher;
330 
331  SCENEGRAPH* scene = nullptr;
332  wxString ext = wxFileName( fname ).GetExt();
333 
334  if( ext == "x3d" || ext == "X3D" )
335  scene = LoadX3D( fname );
336  else
337  scene = LoadVRML( fname, true );
338 
339  return scene;
340 }
SCENEGRAPH * LoadX3D(const wxString &aFileName)
Definition: vrml.cpp:309
SCENEGRAPH * LoadVRML(const wxString &aFileName, bool useInline)
Definition: vrml.cpp:170
Define the basic data set required to represent a 3D model.
Definition: scenegraph.h:44

References LoadVRML(), and LoadX3D().

Referenced by CADSTAR_ARCHIVE_PARSER::PART::DEFINITION::PIN::Parse().

◆ LoadVRML()

SCENEGRAPH* LoadVRML ( const wxString &  aFileName,
bool  useInline 
)

Definition at line 170 of file vrml.cpp.

171 {
172  FILE_LINE_READER* modelFile = nullptr;
173  SCENEGRAPH* scene = nullptr;
174  wxString filename = aFileName;
175  wxFileName tmpfilename;
176 
177  if( aFileName.Upper().EndsWith( "WRZ" ) )
178  {
179  wxFFileInputStream ifile( aFileName );
180  tmpfilename = wxFileName( aFileName );
181 
182  tmpfilename.SetPath( wxStandardPaths::Get().GetTempDir() );
183  tmpfilename.SetExt( "WRL" );
184 
185  wxFileOffset size = ifile.GetLength();
186 
187  if( size == wxInvalidOffset )
188  return nullptr;
189 
190  {
191  wxFFileOutputStream ofile( tmpfilename.GetFullPath() );
192 
193  if( !ofile.IsOk() )
194  return nullptr;
195 
196  char *buffer = new char[size];
197 
198  ifile.Read( buffer, size);
199  std::string expanded;
200 
201  try
202  {
203  expanded = gzip::decompress( buffer, size );
204  }
205  catch(...)
206  {
207  delete[] buffer;
208  return nullptr;
209  }
210 
211  delete[] buffer;
212 
213  ofile.Write( expanded.data(), expanded.size() );
214  ofile.Close();
215  }
216 
217  filename = tmpfilename.GetFullPath();
218  }
219 
220  try
221  {
222  // set the max char limit to 8MB; if a VRML file contains
223  // longer lines then perhaps it shouldn't be used
224  modelFile = new FILE_LINE_READER( filename, 0, 8388608 );
225  }
226  catch( IO_ERROR & )
227  {
228  wxLogError( wxS( " * " ) + _( "[INFO] load failed: input line too long\n" ) );
229  return nullptr;
230  }
231 
232 
233  // VRML file processor
234  WRLPROC proc( modelFile );
235 
236  // Cleanup our temporary file
237  if( tmpfilename.IsOk() )
238  wxRemoveFile( tmpfilename.GetFullPath() );
239 
240  if( proc.GetVRMLType() == WRLVERSION::VRML_V1 )
241  {
242  wxLogTrace( traceVrmlPlugin, " * [INFO] Processing VRML 1.0 file" );
243 
244  WRL1BASE* bp = new WRL1BASE;
245 
246  if( !bp->Read( proc ) )
247  {
248  wxLogTrace( traceVrmlPlugin, " * [INFO] load failed" );
249  }
250  else
251  {
252  wxLogTrace( traceVrmlPlugin, " * [INFO] load completed" );
253 
254  scene = (SCENEGRAPH*)bp->TranslateToSG( nullptr, nullptr );
255  }
256 
257  delete bp;
258  }
259  else
260  {
261  wxLogTrace( traceVrmlPlugin, " * [INFO] Processing VRML 2.0 file" );
262 
263  WRL2BASE* bp = new WRL2BASE;
264 
265  // allow Inline{} files to be included
266  bp->SetEnableInline( true );
267 
268  if( !bp->Read( proc ) )
269  {
270  wxLogTrace( traceVrmlPlugin, " * [INFO] load failed" );
271  }
272  else
273  {
274  wxLogTrace( traceVrmlPlugin, " * [INFO] load completed" );
275 
276  // for now we recalculate all normals per-vertex per-face
277  scene = (SCENEGRAPH*)bp->TranslateToSG( nullptr );
278  }
279 
280  delete bp;
281  }
282 
283  if( nullptr != modelFile )
284  delete modelFile;
285 
286  // DEBUG: WRITE OUT VRML2 FILE TO CONFIRM STRUCTURE
287 #if ( defined( DEBUG_VRML1 ) && DEBUG_VRML1 > 3 ) \
288  || ( defined( DEBUG_VRML2 ) && DEBUG_VRML2 > 3 )
289  if( scene )
290  {
291  wxFileName fn( wxString::FromUTF8Unchecked( aFileName ) );
292  wxString output;
293 
294  if( proc.GetVRMLType() == VRML_V1 )
295  output = wxT( "_vrml1-" );
296  else
297  output = wxT( "_vrml2-" );
298 
299  output.append( fn.GetName() );
300  output.append( wxT(".wrl") );
301  S3D::WriteVRML( output.ToUTF8(), true, (SGNODE*)(scene), true, true );
302  }
303 #endif
304 
305  return scene;
306 }
Represent the top node of a VRML1 model.
Definition: vrml1_base.h:45
bool Read(WRLPROC &proc)
Definition: vrml2_base.cpp:163
bool Read(WRLPROC &proc)
Definition: vrml1_base.cpp:77
The base class of all Scene Graph nodes.
Definition: sg_node.h:74
SGLIB_API bool WriteVRML(const char *filename, bool overwrite, SGNODE *aTopNode, bool reuse, bool renameNodes)
Function WriteVRML writes out the given node and its subnodes to a VRML2 file.
Definition: ifsg_api.cpp:76
void SetEnableInline(bool enable)
Definition: vrml2_base.cpp:85
A LINE_READER that reads from an open file.
Definition: richio.h:172
SGNODE * TranslateToSG(SGNODE *aParent, WRL1STATUS *sp) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
Definition: vrml1_base.cpp:529
#define _(s)
The top node of a VRML2 model.
Definition: vrml2_base.h:59
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition: vrml.cpp:63
Define the basic data set required to represent a 3D model.
Definition: scenegraph.h:44
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
Definition: vrml2_base.cpp:911

References _, WRLPROC::GetVRMLType(), WRL1BASE::Read(), WRL2BASE::Read(), WRL2BASE::SetEnableInline(), traceVrmlPlugin, WRL1BASE::TranslateToSG(), WRL2BASE::TranslateToSG(), VRML_V1, and S3D::WriteVRML().

Referenced by WRL2BASE::GetInlineData(), and Load().

◆ LoadX3D()

SCENEGRAPH* LoadX3D ( const wxString &  aFileName)

Definition at line 309 of file vrml.cpp.

310 {
311  SCENEGRAPH* scene = nullptr;
312  X3DPARSER model;
313  scene = model.Load( aFileName );
314 
315  return scene;
316 }
Definition: x3d.h:36
SCENEGRAPH * Load(const wxString &aFileName)
Definition: x3d.cpp:46
Define the basic data set required to represent a 3D model.
Definition: scenegraph.h:44

References X3DPARSER::Load().

Referenced by Load().

Variable Documentation

◆ file_data

struct FILE_DATA file_data
static