From 51f3a02f736051dd1e1d494b3bf7ccd624836365 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 1 Mar 2012 11:33:31 +0000 Subject: [PATCH] From Luc Frauciel, "I've modified dxf writer : - correction to writeFace : the fourth point was defined with an incorrect code (http://www.autodesk.com/techpubs/autocad/acad2000/dxf/3dface_dxf_06.htm) - if no layer name was found, an empty string was used, with is incorrect according to dxf specifications and was rejected by Autodesk DWG TrueView - the plugin was writting polygons and triangles as LINE, as if PolygonMode GL_LINE was active, and didn't use 3DFACE primitive. I changed this behaviour to write 3DFACE as default, and LINE when PolygonMode GL_LINE is active. when reading back the file with osg, the result is now consistent with the source Tested with osg plugin, FME (Safe software), Autodesk DWG TrueView " --- src/osgPlugins/dxf/DXFWriterNodeVisitor.cpp | 113 +++++++++++--------- src/osgPlugins/dxf/DXFWriterNodeVisitor.h | 5 +- 2 files changed, 67 insertions(+), 51 deletions(-) diff --git a/src/osgPlugins/dxf/DXFWriterNodeVisitor.cpp b/src/osgPlugins/dxf/DXFWriterNodeVisitor.cpp index 94d549258..ee037d582 100644 --- a/src/osgPlugins/dxf/DXFWriterNodeVisitor.cpp +++ b/src/osgPlugins/dxf/DXFWriterNodeVisitor.cpp @@ -14,6 +14,7 @@ #include +#include #include #include "DXFWriterNodeVisitor.h" @@ -61,13 +62,15 @@ class ValueVisitor : public osg::ValueVisitor { class DxfPrimitiveIndexWriter : public osg::PrimitiveIndexFunctor { public: - DxfPrimitiveIndexWriter(std::ostream& fout,osg::Geometry* geo,const Layer &layer,AcadColor &acad,const osg::Matrix& m = osg::Matrix::identity()) : + DxfPrimitiveIndexWriter(std::ostream& fout,osg::Geometry* geo,const Layer &layer,AcadColor &acad, + const osg::Matrix& m = osg::Matrix::identity(),bool writeTriangleAs3DFace = true) : osg::PrimitiveIndexFunctor(), _fout(fout), _geo(geo), _layer(layer), _acad(acad), - _m(m) + _m(m), + _writeTriangleAs3DFace(writeTriangleAs3DFace) { } @@ -91,52 +94,53 @@ class DxfPrimitiveIndexWriter : public osg::PrimitiveIndexFunctor { _fout < _indexCache; - osg::Geometry* _geo; + osg::Geometry* _geo; Layer _layer; AcadColor _acad; // needed to lookup new colors - osg::Matrix _m; + osg::Matrix _m; + + bool _writeTriangleAs3DFace; }; @@ -443,7 +449,9 @@ std::string DXFWriterNodeVisitor::getLayerName(const std::string& defaultvalue) break; } } - + //empty layer name is forbidden + if (layerName.empty()) layerName = "0"; + return layerName; } @@ -467,7 +475,12 @@ std::string DXFWriterNodeVisitor::getLayerName(const std::string& defaultvalue) void DXFWriterNodeVisitor::processStateSet(osg::StateSet* ss) { // anything to do if no material/texture? - // could detect polygon mode and output in that form? + + osg::PolygonMode * pm = dynamic_cast(ss->getAttribute(osg::StateAttribute::POLYGONMODE)); + if (pm) + { + if (pm->getMode(osg::PolygonMode::FRONT)==osg::PolygonMode::LINE) _writeTriangleAs3DFace = false; + } } void DXFWriterNodeVisitor::processGeometry(osg::Geometry* geo, osg::Matrix& m) @@ -501,7 +514,7 @@ void DXFWriterNodeVisitor::processGeometry(osg::Geometry* geo, osg::Matrix& m) for(unsigned int i = 0; i < geo->getNumPrimitiveSets(); ++i) { osg::PrimitiveSet* ps = geo->getPrimitiveSet(i); - DxfPrimitiveIndexWriter pif(_fout, geo,_layer,_acadColor,m); + DxfPrimitiveIndexWriter pif(_fout, geo,_layer,_acadColor,m,_writeTriangleAs3DFace); ps->accept(pif); } } else { diff --git a/src/osgPlugins/dxf/DXFWriterNodeVisitor.h b/src/osgPlugins/dxf/DXFWriterNodeVisitor.h index 86758b3ac..d8725f710 100644 --- a/src/osgPlugins/dxf/DXFWriterNodeVisitor.h +++ b/src/osgPlugins/dxf/DXFWriterNodeVisitor.h @@ -168,7 +168,8 @@ class DXFWriterNodeVisitor: public osg::NodeVisitor { osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), _fout(fout), _currentStateSet(new osg::StateSet()), - _firstPass(true) + _firstPass(true), + _writeTriangleAs3DFace(true) { @@ -271,6 +272,8 @@ class DXFWriterNodeVisitor: public osg::NodeVisitor { std::vector _layers; bool _firstPass; Layer _layer; + + bool _writeTriangleAs3DFace; AcadColor _acadColor;