| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/Registry> |
|---|
| 13 | #include <osgDB/ReadFile> |
|---|
| 14 | #include <osgDB/FileUtils> |
|---|
| 15 | #include <osgDB/FileNameUtils> |
|---|
| 16 | |
|---|
| 17 | #include <map> |
|---|
| 18 | #include <iostream> |
|---|
| 19 | #include <utility> |
|---|
| 20 | |
|---|
| 21 | #include <string> |
|---|
| 22 | #include <sstream> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | |
|---|
| 25 | #include "dxfFile.h" |
|---|
| 26 | #include "DXFWriterNodeVisitor.h" |
|---|
| 27 | |
|---|
| 28 | using namespace osg; |
|---|
| 29 | using namespace osgDB; |
|---|
| 30 | using namespace std; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | class ReaderWriterdxf : public osgDB::ReaderWriter |
|---|
| 34 | { |
|---|
| 35 | public: |
|---|
| 36 | ReaderWriterdxf() |
|---|
| 37 | { |
|---|
| 38 | supportsExtension("dxf","Autodesk DXF format"); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | virtual const char* className() { return "Autodesk DXF Reader/Writer"; } |
|---|
| 42 | virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options*) const; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | virtual WriteResult writeObject(const osg::Object& obj,const std::string& fileName,const Options* options=NULL) const |
|---|
| 46 | { |
|---|
| 47 | const osg::Node* node = dynamic_cast<const osg::Node*>(&obj); |
|---|
| 48 | if (node) |
|---|
| 49 | return writeNode(*node, fileName, options); |
|---|
| 50 | else |
|---|
| 51 | return WriteResult(WriteResult::FILE_NOT_HANDLED); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | virtual WriteResult writeObject(const osg::Object& obj,std::ostream& fout,const Options* options=NULL) const |
|---|
| 56 | { |
|---|
| 57 | const osg::Node* node = dynamic_cast<const osg::Node*>(&obj); |
|---|
| 58 | if (node) |
|---|
| 59 | return writeNode(*node, fout, options); |
|---|
| 60 | else |
|---|
| 61 | return WriteResult(WriteResult::FILE_NOT_HANDLED); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | virtual WriteResult writeNode(const osg::Node& node,std::ostream& fout,const Options* =NULL) const |
|---|
| 65 | { |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | DXFWriterNodeVisitor nv(fout); |
|---|
| 69 | |
|---|
| 70 | (const_cast<osg::Node*>(&node))->accept(nv); |
|---|
| 71 | |
|---|
| 72 | if ( nv.writeHeader(node.getBound()) ) { |
|---|
| 73 | (const_cast<osg::Node*>(&node))->accept(nv); |
|---|
| 74 | nv.writeFooter(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | return WriteResult(WriteResult::FILE_SAVED); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | virtual WriteResult writeNode(const osg::Node& node,const std::string& fileName,const Options* options =NULL) const |
|---|
| 81 | { |
|---|
| 82 | if (!acceptsExtension(osgDB::getFileExtension(fileName))) |
|---|
| 83 | return WriteResult(WriteResult::FILE_NOT_HANDLED); |
|---|
| 84 | |
|---|
| 85 | osgDB::ofstream f(fileName.c_str()); |
|---|
| 86 | |
|---|
| 87 | if (!f.is_open() ) { |
|---|
| 88 | return WriteResult(WriteResult::ERROR_IN_WRITING_FILE); |
|---|
| 89 | } |
|---|
| 90 | DXFWriterNodeVisitor nv(f); |
|---|
| 91 | |
|---|
| 92 | (const_cast<osg::Node*>(&node))->accept(nv); |
|---|
| 93 | |
|---|
| 94 | if ( nv.writeHeader(node.getBound()) ) { |
|---|
| 95 | (const_cast<osg::Node*>(&node))->accept(nv); |
|---|
| 96 | nv.writeFooter(); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | return WriteResult(WriteResult::FILE_SAVED); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | protected: |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | REGISTER_OSGPLUGIN(dxf, ReaderWriterdxf) |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | osgDB::ReaderWriter::ReadResult |
|---|
| 111 | ReaderWriterdxf::readNode(const std::string& filename, const osgDB::ReaderWriter::Options* options) const |
|---|
| 112 | { |
|---|
| 113 | std::string ext = osgDB::getFileExtension(filename); |
|---|
| 114 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | if (options) { |
|---|
| 118 | bool useAccuracy=false; |
|---|
| 119 | double maxError=0.0; |
|---|
| 120 | bool improveAccuracyOnly=false; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | std::string optionsstring=options->getOptionString(); |
|---|
| 124 | |
|---|
| 125 | size_t accstart=optionsstring.find("Accuracy("); |
|---|
| 126 | if (accstart>=0) { |
|---|
| 127 | const char* start=optionsstring.c_str() + accstart + strlen("Accuracy("); |
|---|
| 128 | if (sscanf(start,"%lf",&maxError)==1) useAccuracy=true; |
|---|
| 129 | } |
|---|
| 130 | if (useAccuracy) { |
|---|
| 131 | |
|---|
| 132 | if (optionsstring.find("ImproveAccuracyOnly") != std::string::npos) { |
|---|
| 133 | improveAccuracyOnly=true; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | dxfEntity::getRegistryEntity("ARC")->setAccuracy(true,maxError,improveAccuracyOnly); |
|---|
| 138 | dxfEntity::getRegistryEntity("CIRCLE")->setAccuracy(true,maxError,improveAccuracyOnly); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | dxfFile df(filename); |
|---|
| 145 | if (df.parseFile()) { |
|---|
| 146 | |
|---|
| 147 | osg::Group* osg_top = df.dxf2osg(); |
|---|
| 148 | return (osg_top); |
|---|
| 149 | } |
|---|
| 150 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 151 | } |
|---|