13.3. Writing an Open Inventor file

Inventor scene graphs can be written to a file in either ASCII or binary format. SoWriteAction SoWriteAction SoWriteAction is used for writing scene graphs to files. An instance of this class contains an instance of SoOutput SoOutput SoOutput , which by default writes to stdout in ASCII format. The getOutput() method returns a pointer to the SoOutput SoOutput SoOutput . Other methods for SoOutput SoOutput SoOutput 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:

SoWriteAction myAction;
FILE *fp;

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

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

  
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:

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

myAction.GetOutput().OpenFile("filename.iv");
myAction.GetOutput().SetBinary(false);
myAction.Apply(root);
myAction.GetOutput().CloseFile();
  
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 SoWriteAction SoWriteAction 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{ 
       }
    	}
}