diff --git a/source/RobotAPI/components/ArViz/CMakeLists.txt b/source/RobotAPI/components/ArViz/CMakeLists.txt index 16442d23b8acbb12446de1f288c3f73dec39092c..1903438e67d826f68527078545f09ca595b5216e 100644 --- a/source/RobotAPI/components/ArViz/CMakeLists.txt +++ b/source/RobotAPI/components/ArViz/CMakeLists.txt @@ -29,6 +29,7 @@ set(SOURCES Coin/Visualizer.cpp Coin/RegisterVisualizationTypes.cpp + Coin/ExportVRML.cpp Introspection/ElementJsonSerializers.cpp Introspection/exceptions.cpp @@ -66,6 +67,7 @@ set(HEADERS Coin/VisualizationObject.h Coin/Visualizer.h + Coin/ExportVRML.h # Client Client/Layer.h diff --git a/source/RobotAPI/components/ArViz/Coin/ExportVRML.cpp b/source/RobotAPI/components/ArViz/Coin/ExportVRML.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bdc64d2f1aa2690996fee29d550e458c2a3db73 --- /dev/null +++ b/source/RobotAPI/components/ArViz/Coin/ExportVRML.cpp @@ -0,0 +1,99 @@ +#include "ExportVRML.h" + +#include <ArmarXCore/core/logging/Logging.h> + +#include <Inventor/actions/SoWriteAction.h> +#include <Inventor/actions/SoToVRML2Action.h> +#include <Inventor/nodes/SoFile.h> +#include <Inventor/nodes/SoSeparator.h> +#include <Inventor/VRMLnodes/SoVRMLGroup.h> + +namespace armarx::viz::coin +{ + +static SoGroup* convertSoFileChildren(SoGroup* orig) +{ + if (!orig) + { + return new SoGroup; + } + + SoGroup* storeResult; + + if (orig->getTypeId() == SoSeparator::getClassTypeId()) + { + storeResult = new SoSeparator; + } + else + { + storeResult = new SoGroup; + } + + storeResult->ref(); + + if (orig->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) + { + // process group node + for (int i = 0; i < orig->getNumChildren(); i++) + { + SoNode* n1 = orig->getChild(i); + + if (n1->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) + { + // convert group + SoGroup* n2 = (SoGroup*)n1; + SoGroup* gr1 = convertSoFileChildren(n2); + storeResult->addChild(gr1); + } + else if (n1->getTypeId() == SoFile::getClassTypeId()) + { + // really load file!! + SoFile* fn = (SoFile*)n1; + SoGroup* fileChildren; + fileChildren = fn->copyChildren(); + storeResult->addChild(fileChildren); + } + else + { + // just copy child node + storeResult->addChild(n1); + } + } + } + + storeResult->unrefNoDelete(); + return storeResult; +} + +void exportToVRML(SoNode* node, std::string const& exportFilePath) +{ + SoOutput* so = new SoOutput(); + if (!so->openFile(exportFilePath.c_str())) + { + ARMARX_ERROR << "Could not open file " << exportFilePath << " for writing."; + return; + } + + so->setHeaderString("#VRML V2.0 utf8"); + + SoGroup* n = new SoGroup; + n->ref(); + n->addChild(node); + SoGroup* newVisu = convertSoFileChildren(n); + newVisu->ref(); + + SoToVRML2Action tovrml2; + tovrml2.apply(newVisu); + SoVRMLGroup* newroot = tovrml2.getVRML2SceneGraph(); + newroot->ref(); + SoWriteAction wra(so); + wra.apply(newroot); + newroot->unref(); + + so->closeFile(); + + newVisu->unref(); + n->unref(); +} + +} diff --git a/source/RobotAPI/components/ArViz/Coin/ExportVRML.h b/source/RobotAPI/components/ArViz/Coin/ExportVRML.h new file mode 100644 index 0000000000000000000000000000000000000000..85758493f8008e98dc4a4389a6de0b2c64b456dd --- /dev/null +++ b/source/RobotAPI/components/ArViz/Coin/ExportVRML.h @@ -0,0 +1,12 @@ +#pragma once + +#include <Inventor/nodes/SoNode.h> + +#include <string> + +namespace armarx::viz::coin +{ + + void exportToVRML(SoNode* node, std::string const& exportFilePath); + +} diff --git a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp index 764277bc58d4c0d21dd347197092e172c2aac4a7..01ed16c36643b4967e913f0849093fe890f42d56 100644 --- a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp +++ b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp @@ -1,24 +1,20 @@ #include "Visualizer.h" -#include "VisualizationRobot.h" -#include "VisualizationObject.h" +#include "ExportVRML.h" #include <ArmarXCore/core/logging/Logging.h> #include <ArmarXCore/util/CPPUtility/GetTypeString.h> -#include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h> -#include <VirtualRobot/Visualization/VisualizationFactory.h> - -#include <Inventor/nodes/SoUnits.h> -#include <Inventor/actions/SoWriteAction.h> -#include <Inventor/actions/SoToVRML2Action.h> -#include <Inventor/VRMLnodes/SoVRMLGroup.h> - - -#include <thread> +#include <Inventor/SoPath.h> namespace armarx::viz { +namespace coin +{ + void clearRobotCache(); + void clearObjectCache(); +} + struct CoinVisualizerWrapper : IceUtil::Shared { class CoinVisualizer* this_; @@ -625,34 +621,6 @@ namespace armarx::viz void CoinVisualizer::exportToVRML(const std::string& exportFilePath) { - - - SoOutput* so = new SoOutput(); - if (!so->openFile(exportFilePath.c_str())) - { - ARMARX_ERROR << "Could not open file " << exportFilePath << " for writing." << std::endl; - return; - } - - so->setHeaderString("#VRML V2.0 utf8"); - - SoGroup* n = new SoGroup; - n->ref(); - n->addChild(root); - SoGroup* newVisu = VirtualRobot::CoinVisualizationFactory::convertSoFileChildren(n); - newVisu->ref(); - - SoToVRML2Action tovrml2; - tovrml2.apply(newVisu); - SoVRMLGroup* newroot = tovrml2.getVRML2SceneGraph(); - newroot->ref(); - SoWriteAction wra(so); - wra.apply(newroot); - newroot->unref(); - - so->closeFile(); - - newVisu->unref(); - n->unref(); + coin::exportToVRML(root, exportFilePath); } }