3.12. Node Types

Inventor provides runtime type-checking through the SoType( C++ ) class. Use the getTypeId() method on an instance to obtain the SoType( C++ ) for that instance. Runtime type-checking is available for most Inventor classes, including nodes, engines, actions, details, and events.

The SoType( C++ ) class has methods that enable you to find the parent class of a type (getParent()), to create an instance of a particular type (createInstance()), and to obtain an SbName( C++ ) for the class type (getName()). For example, the following code returns a name, such as

Material or Group, which you could then use to print some information about the node:


C++
node->getTypeId().getName();
  

The following two statements both return the SoType( C++ ) for an SoMaterial( C++ | Java | .NET ) node (the first is more efficient):


C++
// (1)
SoMaterial::getClassTypeId();
// (2)
SoType::fromName("Material");

To determine whether an instance is of a particular type, use the == operator, as follows:


C++
if (myNode->getTypeId() == SoGroup::getClassTypeId())
      // Is this an SoGroup?

To determine whether an instance is of the same type or derived from a particular class, use the isOfType() method or the SoType::derivedFrom() method (the two methods have the same effects):


C++
// (1)
if (myNode->isOfType(SoGroup::getClassTypeId()))
   // Is this an SoGroup, SoSeparator, SoSwitch, and so on
      
// (2)
if (myNode->getTypeId().isDerivedFrom(
      SoGroup::getClassTypeId()))

Also see the description in Chapter 8, Applying Actions of the SoSearchAction( C++ | Java | .NET ), which allows you to search the scene graph for nodes of a particular type, or derived from a type.