16.7. Other Methods: getPart() and setPart()

Two other useful methods of SoBaseKit() are getPart() and setPart().

The getPart() method returns the requested node (part):

getPart(partName, makeIfNeeded);

If makeIfNeeded is TRUE and no node is present, the node is created. In addition, if any extra nodes are needed to connect the node to the top node (“this”) of the node kit, those nodes are created as well.

For example:


C++
xf = (SoTransform *) myKit->getPart("transform", TRUE);
  

.NET
xf = (SoTransform) myKit.GetPart("transform", true);
  

Java
xf = (SoTransform) myKit.getPart("transform", true);
  

looks for “transform” and either returns it (if found) or makes it (if not found). It then assigns this node to xf. If you specify FALSE for makeIfNeeded and the node kit has no “transform” yet, the method returns NULL without creating the node. If the catalog for the type of node kit you are using does not have an entry for a part named “transform,” the getPart() method returns NULL.

The setPart() method inserts the given node as a new part in the node kit:

setPart(partName, node);

If extra nodes are required to connect the part into the node-kit structure, those nodes are created as well. For example, suppose you want another node kit to share the transform node (xf) created in the previous example:


C++
myOtherKit->setPart("transform", xf);
    

.NET
myOtherKit.SetPart("transform", xf);
    

Java
myOtherKit.setPart("transform", xf);
    

If the given node is not derived from the type of that part, as described in the node-kit catalog, the part will not be set. If you have linked with the debugging library, an error message will print.

To delete the transform node entirely, use a NULL argument for the node pointer:


C++
myOtherKit->setPart("transform", NULL);
    

.NET
myOtherKit.SetPart("transform", null);
    

Java
myOtherKit.setPart("transform", null);
    

To change the “shape” in SoShapeKit( C++ | Java | .NET ) from the default cube to a cone:


C++
myShape->setPart("shape", new SoCone);
    

.NET
myShape.SetPart("shape", new SoCone());
    

Java
myShape.setPart("shape", new SoCone());
    

And, of course, setPart() will do nothing if there is no part with the specified name in the catalog.