Skip to content

Commit

Permalink
fixes 0003486: Improve VRML export for more complex structures
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Oct 21, 2018
1 parent c950d1f commit a8b7390
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions src/Gui/SoFCDB.cpp
Expand Up @@ -237,13 +237,66 @@ const std::string& Gui::SoFCDB::writeNodesToString(SoNode * root)
return cReturnString;
}

SoNode* replaceSwitches(SoNodeList* children, SoGroup* parent)
{
if (!children) {
return parent;
}

for (int i=0; i<children->getLength(); i++) {
SoNode* node = (*children)[i];
if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
if (node->getTypeId().isDerivedFrom(SoSwitch::getClassTypeId())) {
SoSwitch* group = static_cast<SoSwitch*>(node);
int which = group->whichChild.getValue();
if (which == SO_SWITCH_NONE)
continue;
SoGroup* newParent = new SoGroup();
SoNodeList c;
if (which >= 0) {
c.append(group->getChild(which));
}
else {
// SO_SWITCH_INHERIT or SO_SWITCH_ALL
for (int i=0; i<group->getNumChildren(); i++)
c.append(group->getChild(i));
}

replaceSwitches(&c, newParent);
parent->addChild(newParent);
}
else {
SoGroup* newParent = static_cast<SoGroup*>(node->getTypeId().createInstance());
replaceSwitches(node->getChildren(), newParent);
parent->addChild(newParent);
}
}
else {
parent->addChild(node);
}
}

return parent;
}

SoNode* replaceSwitchesInSceneGraph(SoNode* node)
{
if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
return replaceSwitches(node->getChildren(), new SoSeparator);
}

return node;
}

bool Gui::SoFCDB::writeToVRML(SoNode* node, const char* filename, bool binary)
{
SoNode* noSwitches = replaceSwitchesInSceneGraph(node);
noSwitches->ref();
SoVRMLAction vrml2;
vrml2.setOverrideMode(true);
vrml2.apply(node);
vrml2.apply(noSwitches);
SoToVRML2Action tovrml2;
tovrml2.apply(node);
tovrml2.apply(noSwitches);
SoVRMLGroup* vrmlRoot = tovrml2.getVRML2SceneGraph();
vrmlRoot->setInstancePrefix(SbString("o"));
vrmlRoot->ref();
Expand All @@ -252,7 +305,8 @@ bool Gui::SoFCDB::writeToVRML(SoNode* node, const char* filename, bool binary)

// restore old settings
vrml2.setOverrideMode(false);
vrml2.apply(node);
vrml2.apply(noSwitches);
noSwitches->unref();

Base::FileInfo fi(filename);
if (binary) {
Expand Down

0 comments on commit a8b7390

Please sign in to comment.