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

Revision 8578, 4.0 kB (checked in by robert, 5 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
Line 
1#include "ReaderWriterIV.h"
2
3// OSG headers
4#include <osg/Notify>
5#include <osgDB/FileUtils>
6#include <osgDB/FileNameUtils>
7
8// Inventor headers
9#include <Inventor/SoDB.h>
10#include <Inventor/SoInteraction.h>
11#include <Inventor/nodekits/SoNodeKit.h>
12#include <Inventor/nodes/SoSeparator.h>
13#include <Inventor/actions/SoWriteAction.h>
14#include <Inventor/actions/SoCallbackAction.h>
15
16#ifdef __COIN__
17#include <Inventor/VRMLnodes/SoVRMLImageTexture.h>
18#endif
19
20#include "ConvertFromInventor.h"
21#include "GroupSoLOD.h"
22#include "ConvertToInventor.h"
23
24
25// Register with Registry to instantiate the inventor reader.
26REGISTER_OSGPLUGIN(Inventor, ReaderWriterIV)
27
28ReaderWriterIV::ReaderWriterIV()
29{
30    supportsExtension("iv","Inventor format");
31    supportsExtension("wrl","VRML world file");
32}
33
34// Read file and convert to OSG
35osgDB::ReaderWriter::ReadResult
36ReaderWriterIV::readNode(const std::string& file,
37                         const osgDB::ReaderWriter::Options* options) const
38{
39    std::string ext = osgDB::getLowerCaseFileExtension(file);
40    if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
41
42    std::string fileName = osgDB::findDataFile( file, options );
43    if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
44
45    osg::notify(osg::INFO) << "osgDB::ReaderWriterIV::readNode() Reading file " 
46                           << fileName.data() << std::endl;
47   
48    // Initialize Inventor
49    SoDB::init();
50    SoNodeKit::init();
51    SoInteraction::init();
52   
53
54    // Initial GroupSoLOD node
55    GroupSoLOD::initClass();
56
57#ifdef __COIN__
58    // Disable delayed loading of VRML textures
59    SoVRMLImageTexture::setDelayFetchURL(FALSE);
60#endif
61
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    {
79        rootIVNode->ref();
80        // Convert the inventor scenegraph to an osg scenegraph and return it
81        ConvertFromInventor convertIV;
82        ReadResult result = convertIV.convert(rootIVNode);
83        rootIVNode->unref();
84        return result;
85    }
86
87    return ReadResult::FILE_NOT_HANDLED;
88}
89
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.