4.8. Using New Action Classes

Like nodes, action classes must be initialized before any instances can be constructed. Also note that before you can add a method to the method list, you must initialize both the action class and the node class involved.

Example 4-3 reads in a scene graph from the file volume.iv and applies the GetVolumeAction action to it, printing the resulting volume.

Example 4.3.  PrintVolume.c++

#include <Inventor/SoDB.h>
#include <Inventor/SoInput.h>
#include <Inventor/SoInteraction.h>
#include <Inventor/nodes/SoSeparator.h>

// Header file for new action class
#include "GetVolumeAction.h"

main()
{
   // Initialize Inventor
   SoInteraction::init();

   // Initialize the new action class
   GetVolumeAction::initClass();

   // Open the file and read the scene
   SoInput      myInput;
   SoSeparator  *root;
   if (! myInput.openFile("volume.iv")) {
     fprintf(stderr, "Can't open \"volume.iv\" for reading\n");
     return 1;
   }
   root = SoDB::readAll(&myInput);
   if (root == NULL) {
     printf("Couldn't read scene from \"volume.iv\"\n");
     return 2;
   }
   root->ref();

   // Compute the volume: apply a GetVolumeAction to the root
   GetVolumeAction va;
   va.apply(root);

   // Print the result
   printf("Total volume = %g\n", va.getVolume());

   return 0;
}