| [2225] | 1 | #include "ReaderWriterIV.h" |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | #include <osg/Notify> |
|---|
| [2759] | 5 | #include <osgDB/FileUtils> |
|---|
| [2225] | 6 | #include <osgDB/FileNameUtils> |
|---|
| 7 | |
|---|
| 8 | |
|---|
| [3920] | 9 | #include <Inventor/SoDB.h> |
|---|
| [2225] | 10 | #include <Inventor/SoInteraction.h> |
|---|
| [3920] | 11 | #include <Inventor/nodekits/SoNodeKit.h> |
|---|
| [2225] | 12 | #include <Inventor/nodes/SoSeparator.h> |
|---|
| [7348] | 13 | #include <Inventor/actions/SoWriteAction.h> |
|---|
| 14 | #include <Inventor/actions/SoCallbackAction.h> |
|---|
| [6544] | 15 | |
|---|
| [7512] | 16 | #ifdef __COIN__ |
|---|
| [6543] | 17 | #include <Inventor/VRMLnodes/SoVRMLImageTexture.h> |
|---|
| [6544] | 18 | #endif |
|---|
| [2225] | 19 | |
|---|
| 20 | #include "ConvertFromInventor.h" |
|---|
| 21 | #include "GroupSoLOD.h" |
|---|
| [7348] | 22 | #include "ConvertToInventor.h" |
|---|
| [2225] | 23 | |
|---|
| [7348] | 24 | |
|---|
| [2225] | 25 | |
|---|
| [7076] | 26 | REGISTER_OSGPLUGIN(Inventor, ReaderWriterIV) |
|---|
| [2225] | 27 | |
|---|
| 28 | ReaderWriterIV::ReaderWriterIV() |
|---|
| 29 | { |
|---|
| [8578] | 30 | supportsExtension("iv","Inventor format"); |
|---|
| 31 | supportsExtension("wrl","VRML world file"); |
|---|
| [2225] | 32 | } |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | osgDB::ReaderWriter::ReadResult |
|---|
| [2501] | 36 | ReaderWriterIV::readNode(const std::string& file, |
|---|
| [3694] | 37 | const osgDB::ReaderWriter::Options* options) const |
|---|
| [2225] | 38 | { |
|---|
| [2501] | 39 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 40 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| [2225] | 41 | |
|---|
| [3691] | 42 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| [2501] | 43 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 44 | |
|---|
| [2225] | 45 | osg::notify(osg::INFO) << "osgDB::ReaderWriterIV::readNode() Reading file " |
|---|
| 46 | << fileName.data() << std::endl; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| [3920] | 49 | SoDB::init(); |
|---|
| 50 | SoNodeKit::init(); |
|---|
| [2225] | 51 | SoInteraction::init(); |
|---|
| [3920] | 52 | |
|---|
| [2225] | 53 | |
|---|
| 54 | |
|---|
| 55 | GroupSoLOD::initClass(); |
|---|
| 56 | |
|---|
| [7512] | 57 | #ifdef __COIN__ |
|---|
| [6543] | 58 | |
|---|
| 59 | SoVRMLImageTexture::setDelayFetchURL(FALSE); |
|---|
| [6544] | 60 | #endif |
|---|
| [6543] | 61 | |
|---|
| [2225] | 62 | |
|---|
| 63 | SoInput input; |
|---|
| 64 | if (!input.openFile(fileName.data())) |
|---|
| 65 | { |
|---|
| 66 | osg::notify(osg::WARN) << "osgDB::ReaderWriterIV::readIVFile() " |
|---|
| 67 | << "Cannot open file " << fileName << std::endl; |
|---|
| 68 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | SoSeparator* rootIVNode = SoDB::readAll(&input); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | input.closeFile(); |
|---|
| 76 | |
|---|
| 77 | if (rootIVNode) |
|---|
| 78 | { |
|---|
| [3393] | 79 | rootIVNode->ref(); |
|---|
| [2225] | 80 | |
|---|
| 81 | ConvertFromInventor convertIV; |
|---|
| [3393] | 82 | ReadResult result = convertIV.convert(rootIVNode); |
|---|
| 83 | rootIVNode->unref(); |
|---|
| 84 | return result; |
|---|
| [2225] | 85 | } |
|---|
| 86 | |
|---|
| 87 | return ReadResult::FILE_NOT_HANDLED; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| [7348] | 90 | |
|---|
| 91 | osgDB::ReaderWriter::WriteResult |
|---|
| 92 | ReaderWriterIV::writeNode(const osg::Node& node, const std::string& fileName, |
|---|
| 93 | const osgDB::ReaderWriter::Options* options) const |
|---|
| 94 | { |
|---|
| 95 | |
|---|
| 96 | std::string ext = osgDB::getLowerCaseFileExtension(fileName); |
|---|
| 97 | if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 98 | bool useVRML1 = !isInventorExtension(osgDB::getFileExtension(fileName)); |
|---|
| 99 | |
|---|
| 100 | osg::notify(osg::INFO) << "osgDB::ReaderWriterIV::writeNode() Writing file " |
|---|
| 101 | << fileName.data() << std::endl; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | SoInteraction::init(); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | ConvertToInventor osg2iv; |
|---|
| 108 | osg2iv.setVRML1Conversion(useVRML1); |
|---|
| 109 | (const_cast<osg::Node*>(&node))->accept(osg2iv); |
|---|
| 110 | SoNode *ivRoot = osg2iv.getIvSceneGraph(); |
|---|
| 111 | if (ivRoot == NULL) |
|---|
| 112 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 113 | ivRoot->ref(); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | if (useVRML1) |
|---|
| 120 | SoBase::setInstancePrefix("_"); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | SoOutput out; |
|---|
| 124 | out.setHeaderString((useVRML1) ? "#VRML V1.0 ascii" : "#Inventor V2.1 ascii"); |
|---|
| 125 | if (!out.openFile(fileName.c_str())) |
|---|
| 126 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 127 | SoWriteAction wa(&out); |
|---|
| 128 | wa.apply(ivRoot); |
|---|
| 129 | ivRoot->unref(); |
|---|
| 130 | |
|---|
| 131 | return WriteResult::FILE_SAVED; |
|---|
| 132 | } |
|---|