root/OpenSceneGraph/trunk/src/osgPlugins/Inventor/ReaderWriterIV.cpp @ 8578

Revision 8578, 4.0 kB (checked in by robert, 6 years ago)

Converted plugins to use the new supportsExtension()/supportsOptions/supportsProtocl() methods
to help enable better querying of supported features

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[2225]1#include "ReaderWriterIV.h"
2
3// OSG headers
4#include <osg/Notify>
[2759]5#include <osgDB/FileUtils>
[2225]6#include <osgDB/FileNameUtils>
7
8// Inventor headers
[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// Register with Registry to instantiate the inventor reader.
[7076]26REGISTER_OSGPLUGIN(Inventor, ReaderWriterIV)
[2225]27
28ReaderWriterIV::ReaderWriterIV()
29{
[8578]30    supportsExtension("iv","Inventor format");
31    supportsExtension("wrl","VRML world file");
[2225]32}
33
34// Read file and convert to OSG
35osgDB::ReaderWriter::ReadResult
[2501]36ReaderWriterIV::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    // Initialize Inventor
[3920]49    SoDB::init();
50    SoNodeKit::init();
[2225]51    SoInteraction::init();
[3920]52   
[2225]53
54    // Initial GroupSoLOD node
55    GroupSoLOD::initClass();
56
[7512]57#ifdef __COIN__
[6543]58    // Disable delayed loading of VRML textures
59    SoVRMLImageTexture::setDelayFetchURL(FALSE);
[6544]60#endif
[6543]61
[2225]62    // Open the file
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    // Create the inventor scenegraph from the file
72    SoSeparator* rootIVNode = SoDB::readAll(&input);
73
74    // Close the file
75    input.closeFile();
76
77    if (rootIVNode)
78    {
[3393]79        rootIVNode->ref();
[2225]80        // Convert the inventor scenegraph to an osg scenegraph and return it
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
91osgDB::ReaderWriter::WriteResult
92ReaderWriterIV::writeNode(const osg::Node& node, const std::string& fileName,
93                          const osgDB::ReaderWriter::Options* options) const
94{
95    // accept extension
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    // Initialize Inventor
104    SoInteraction::init();
105
106    // Convert OSG graph to Inventor graph
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    // Change prefix according to VRML spec:
116    // Node names must not begin with a digit, and must not contain spaces or
117    // control characters, single or double quote characters, backslashes, curly braces,
118    // the sharp (#) character, the plus (+) character or the period character.
119    if (useVRML1)
120      SoBase::setInstancePrefix("_");
121
122    // Write Inventor graph to file
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}
Note: See TracBrowser for help on using the browser.