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