ExportVRML.cpp
Go to the documentation of this file.
1 #include "ExportVRML.h"
2 
4 
5 #include <Inventor/actions/SoWriteAction.h>
6 #include <Inventor/actions/SoToVRML2Action.h>
7 #include <Inventor/nodes/SoFile.h>
8 #include <Inventor/nodes/SoSeparator.h>
9 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
10 
11 namespace armarx::viz::coin
12 {
13 
14 static SoGroup* convertSoFileChildren(SoGroup* orig)
15 {
16  if (!orig)
17  {
18  return new SoGroup;
19  }
20 
21  SoGroup* storeResult;
22 
23  if (orig->getTypeId() == SoSeparator::getClassTypeId())
24  {
25  storeResult = new SoSeparator;
26  }
27  else
28  {
29  storeResult = new SoGroup;
30  }
31 
32  storeResult->ref();
33 
34  if (orig->getTypeId().isDerivedFrom(SoGroup::getClassTypeId()))
35  {
36  // process group node
37  for (int i = 0; i < orig->getNumChildren(); i++)
38  {
39  SoNode* n1 = orig->getChild(i);
40 
41  if (n1->getTypeId().isDerivedFrom(SoGroup::getClassTypeId()))
42  {
43  // convert group
44  SoGroup* n2 = (SoGroup*)n1;
45  SoGroup* gr1 = convertSoFileChildren(n2);
46  storeResult->addChild(gr1);
47  }
48  else if (n1->getTypeId() == SoFile::getClassTypeId())
49  {
50  // really load file!!
51  SoFile* fn = (SoFile*)n1;
52  SoGroup* fileChildren;
53  fileChildren = fn->copyChildren();
54  storeResult->addChild(fileChildren);
55  }
56  else
57  {
58  // just copy child node
59  storeResult->addChild(n1);
60  }
61  }
62  }
63 
64  storeResult->unrefNoDelete();
65  return storeResult;
66 }
67 
68 void exportToVRML(SoNode* node, std::string const& exportFilePath)
69 {
70  SoOutput* so = new SoOutput();
71  if (!so->openFile(exportFilePath.c_str()))
72  {
73  ARMARX_ERROR << "Could not open file " << exportFilePath << " for writing.";
74  return;
75  }
76 
77  so->setHeaderString("#VRML V2.0 utf8");
78 
79  SoGroup* n = new SoGroup;
80  n->ref();
81  n->addChild(node);
82  SoGroup* newVisu = convertSoFileChildren(n);
83  newVisu->ref();
84 
85  SoToVRML2Action tovrml2;
86  tovrml2.apply(newVisu);
87  SoVRMLGroup* newroot = tovrml2.getVRML2SceneGraph();
88  newroot->ref();
89  SoWriteAction wra(so);
90  wra.apply(newroot);
91  newroot->unref();
92 
93  so->closeFile();
94 
95  newVisu->unref();
96  n->unref();
97 }
98 
99 }
armarx::viz::coin::exportToVRML
void exportToVRML(SoNode *node, std::string const &exportFilePath)
Definition: ExportVRML.cpp:68
so
linux gnu libIceDB so
Definition: CMakeLists.txt:7
ExportVRML.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::viz::coin
Definition: ElementVisualizer.cpp:11
Logging.h