diff --git a/src/Gui/SoFCDB.cpp b/src/Gui/SoFCDB.cpp index 782aaaee5..87a4541d2 100644 --- a/src/Gui/SoFCDB.cpp +++ b/src/Gui/SoFCDB.cpp @@ -23,9 +23,15 @@ #include "PreCompiled.h" #ifndef _PreComp_ +# include +# include # include #endif +#include +#include +#include + #include "SoFCDB.h" #include "SoFCColorBar.h" #include "SoFCColorLegend.h" @@ -37,6 +43,7 @@ #include "SoFCUnifiedSelection.h" #include "SoFCSelectionAction.h" #include "SoFCInteractiveElement.h" +#include "SoFCUnifiedSelection.h" #include "SoFCVectorizeSVGAction.h" #include "SoFCVectorizeU3DAction.h" #include "SoAxisCrossKit.h" @@ -200,3 +207,72 @@ const std::string& Gui::SoFCDB::writeNodesToString(SoNode * root) free(buffer); return cReturnString; } + +bool Gui::SoFCDB::writeToVRML(SoNode* node, const char* filename, bool binary) +{ + SoVRMLAction vrml2; + vrml2.apply(node); + SoToVRML2Action tovrml2; + tovrml2.apply(node); + SoVRMLGroup* vrmlRoot = tovrml2.getVRML2SceneGraph(); + vrmlRoot->ref(); + std::string buffer = SoFCDB::writeNodesToString(vrmlRoot); + vrmlRoot->unref(); // release the memory as soon as possible + + Base::FileInfo fi(filename); + if (binary) { + // We want to write compressed VRML but Coin 2.4.3 doesn't do it even though + // SoOutput::getAvailableCompressionMethods() delivers a string list that + // contains 'GZIP'. setCompression() was called directly after opening the file, + // returned TRUE and no error message appeared but anyway it didn't work. + // Strange is that reading GZIPped VRML files works. + // So, we do the compression on our own. + Base::ofstream str(fi, std::ios::out | std::ios::binary); + zipios::GZIPOutputStream gzip(str); + + if (gzip) { + gzip << buffer; + gzip.close(); + return true; + } + } + else { + Base::ofstream str(fi, std::ios::out); + + if (str) { + str << buffer; + str.close(); + return true; + } + } + + return false; +} + +bool Gui::SoFCDB::writeToFile(SoNode* node, const char* filename, bool binary) +{ + bool ret = false; + Base::FileInfo fi(filename); + + // Write VRML V2.0 + if (fi.hasExtension("wrl") || fi.hasExtension("vrml") || fi.hasExtension("wrz")) { + // If 'wrz' is set then force compression + if (fi.hasExtension("wrz")) + binary = true; + + ret = SoFCDB::writeToVRML(node, filename, binary); + } + else if (fi.hasExtension("iv")) { + // Write Inventor in ASCII + std::string buffer = SoFCDB::writeNodesToString(node); + Base::ofstream str(Base::FileInfo(filename), std::ios::out); + + if (str) { + str << buffer; + str.close(); + ret = true; + } + } + + return ret; +} diff --git a/src/Gui/SoFCDB.h b/src/Gui/SoFCDB.h index cc7b3a6a2..9e9d0a82c 100644 --- a/src/Gui/SoFCDB.h +++ b/src/Gui/SoFCDB.h @@ -41,6 +41,9 @@ public: static void finish(); /// helper to apply a SoWriteAction to a node and write it to a string static const std::string& writeNodesToString(SoNode * root); + static bool writeToVRML(SoNode* node, const char* filename, bool binary); + // Write to VRML or Inventor file + static bool writeToFile(SoNode* node, const char* filename, bool binary); }; } // namespace Gui diff --git a/src/Gui/View3DInventorViewer.cpp b/src/Gui/View3DInventorViewer.cpp index 62db9d913..27f42d622 100644 --- a/src/Gui/View3DInventorViewer.cpp +++ b/src/Gui/View3DInventorViewer.cpp @@ -91,7 +91,6 @@ #include #include #include -#include #include "View3DInventorViewer.h" #include "ViewProviderDocumentObject.h" @@ -1130,48 +1129,7 @@ bool View3DInventorViewer::dumpToFile(SoNode* node, const char* filename, bool b bool ret = false; Base::FileInfo fi(filename); - // Write VRML V2.0 - if (fi.hasExtension("wrl") || fi.hasExtension("vrml") || fi.hasExtension("wrz")) { - // If 'wrz' is set then force compression - if (fi.hasExtension("wrz")) - binary = true; - - SoVRMLAction vrml2; - vrml2.apply(node); - SoToVRML2Action tovrml2; - tovrml2.apply(node); - SoVRMLGroup* vrmlRoot = tovrml2.getVRML2SceneGraph(); - vrmlRoot->ref(); - std::string buffer = SoFCDB::writeNodesToString(vrmlRoot); - vrmlRoot->unref(); // release the memory as soon as possible - - if (binary) { - // We want to write compressed VRML but Coin 2.4.3 doesn't do it even though - // SoOutput::getAvailableCompressionMethods() delivers a string list that - // contains 'GZIP'. setCompression() was called directly after opening the file, - // returned TRUE and no error message appeared but anyway it didn't work. - // Strange is that reading GZIPped VRML files works. - // So, we do the compression on our own. - Base::ofstream str(fi, std::ios::out | std::ios::binary); - zipios::GZIPOutputStream gzip(str); - - if (gzip) { - gzip << buffer; - gzip.close(); - ret = true; - } - } - else { - Base::ofstream str(fi, std::ios::out); - - if(str) { - str << buffer; - str.close(); - ret = true; - } - } - } - else if (fi.hasExtension("idtf") || fi.hasExtension("svg")) { + if (fi.hasExtension("idtf") || fi.hasExtension("svg")) { int ps=4; QColor c = Qt::white; std::auto_ptr vo; @@ -1198,15 +1156,8 @@ bool View3DInventorViewer::dumpToFile(SoNode* node, const char* filename, bool b out->closeFile(); } else { - // Write Inventor in ASCII - std::string buffer = SoFCDB::writeNodesToString(node); - Base::ofstream str(Base::FileInfo(filename), std::ios::out); - - if (str) { - str << buffer; - str.close(); - ret = true; - } + // Try VRML and Inventor format + ret = SoFCDB::writeToFile(node, filename, binary); } return ret;