13.3. Writing an Open Inventor file

Inventor scene graphs can be written to a file in either ASCII or binary format. SoWriteAction( C++ | Java | .NET ) is used for writing scene graphs to files. An instance of this class contains an instance of SoOutput( C++ | Java | .NET ), which by default writes to stdout in ASCII format. The getOutput() method returns a pointer to the SoOutput( C++ | Java | .NET ). Other methods for SoOutput( C++ | Java | .NET ) include the following:

openFile()

opens and writes to a file rather than to stdout.

setFilePointer()

explicitly sets the pointer to the file to write to.

closeFile()

closes the file opened with openFile(). The file is closed automatically when the action is destroyed.

setBinary()

writes the file in binary format if TRUE; writes the file in ASCII if FALSE (the default).

setBuffer()

writes to a buffer in memory rather than to a file.

For example, to write in binary to an already open file pointed to by fp:


C++
SoWriteAction myAction;
FILE *fp;

myAction.getOutput()->setBinary(TRUE);
myAction.getOutput()->setFilePointer(fp);
myAction.apply(root);
       

.NET
SoWriteAction myAction = new SoWriteAction();

myAction.GetOutput().SetBinary(true);
myAction.GetOutput().OpenFile("myFile.iv");
myAction.Apply(root);
myAction.GetOutput().CloseFile();

  

Java
SoWriteAction myAction = new SoWriteAction();

myAction.getOutput().setBinary(true);
myAction.getOutput().openFile("myFile.iv");
myAction.apply(root);
myAction.getOutput().closeFile();

To write in ASCII to a named file:


C++
SoWriteAction myAction;
   
myAction.getOutput()->openFile("myFile.iv");
myAction.getOutput()->setBinary(FALSE);
myAction.apply(root);
myAction.getOutput()->closeFile();
       

.NET
SoWriteAction myAction = new SoWriteAction();

myAction.GetOutput().OpenFile("filename.iv");
myAction.GetOutput().SetBinary(false);
myAction.Apply(root);
myAction.GetOutput().CloseFile();
  

Java
SoWriteAction myAction = new SoWriteAction();

myAction.getOutput().openFile("filename.iv");
myAction.getOutput().setBinary(false);
myAction.apply(root);
myAction.getOutput().closeFile();
       

See Chapter 12, Importing data for a complete description of the Inventor file format. Here is an example of the output of SoWriteAction( C++ | Java | .NET ) for a subgraph:

#Inventor V2.0 ascii
Separator {
	   Separator {
       		Transform {
 			         scaleFactor 1 2 1
       		}
       		Material {
          			ambientColor .2 .2 .2
          			diffuseColor .6 .6 .6
          			specularColor .5 .5 .5
 			         shininess .5
       		}
       		Cube{ 
       }
    	}
}