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

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

The getPart() Method

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:

xf = (SoTransform *) myKit->getPart("transform", TRUE);
  
xf = (SoTransform) myKit.GetPart("transform", true);
  
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

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:

myOtherKit->setPart("transform", xf);
    
myOtherKit.SetPart("transform", xf);
    
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:

myOtherKit->setPart("transform", NULL);
    
myOtherKit.SetPart("transform", null);
    
myOtherKit.setPart("transform", null);
    

To change the “shape” in SoShapeKit SoShapeKit SoShapeKit from the default cube to a cone:

myShape->setPart("shape", new SoCone);
    
myShape.SetPart("shape", new SoCone());
    
myShape.setPart("shape", new SoCone());
    

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