fixes 0003486: Improve VRML export for more complex structures

This commit is contained in:
wmayer
2018-10-21 20:53:19 +02:00
parent 8a421b8e2e
commit 2a9d119167

View File

@@ -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();
@@ -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) {