Creating Prototypes by Coding
Prototypes are meant to be designed graphically using Views Studio. In some cases, however, you may need to create prototypes or to modify existing prototypes from a C++ program. This section explains how you can create prototypes by coding in C++ instead of designing them with Views Studio.
Creating a New Prototype
A prototype is represented by an instance of the
IlvPrototype class. To create a new prototype, use the following constructor:
IlvPrototype* proto = new IlvPrototype("myPrototype");
Adding Graphic Nodes
The first step is to define the graphic appearance of the prototype. This is done by adding nodes containing graphic objects. For this, you create instances of the
IlvGraphicNode class and add them to the prototype using the
addNode method.
IlvLabel* label = new IlvLabel(display, 100, 100, "Hello");
IlvGraphicNode* node = new IlvGraphicNode(label, "label", IlTrue);
proto->addNode(node);
The
IlvGraphicNode constructor has three parameters:
An
IlvGraphic: the graphic object to include in the prototype.
A string: the name of the node.
A Boolean: specifies whether a local transformer should be associated with the graphic node. (See the
IlvGraphicNode class in the
Views Prototypes Reference Manual for details.)
You must give different names to the graphic nodes of your prototype if you need to reference them in accessor parameters.
Adding Subgroups
You can create hierarchical objects by adding a subgroup to your prototype. To do this, you must add a node that is an instance of the
IlvSubGroupNode class. This subgroup can be an
IlvGroup that you build yourself by adding graphic nodes to it, or it can be an instance of another prototype:
// Add a sub-group:
IlvGroup* subgroup = new IlvGroup("subgroup");
IlvLine* line1 = new IlvLine(display, IlvPoint(100, 100),
IlvPoint(200, 200));
subgroup->addNode(new IlvGraphicNode(line1, "line1"));
IlvLine* line2 = new IlvLine(display, IlvPoint(100, 200),
IlvPoint(200, 100));
subgroup->addNode(new IlvGraphicNode(line2, "line2"));
proto->addNode(new IlvSubGroupNode(subgroup));
// Add a prototype instance as a sub-group:
IlvPrototype* proto = IlvLoadPrototype("samples.pump", display);
IlvProtoInstance* instance = proto->clone();
proto->addNode(new IlvSubGroupNode(instance));
Adding Accessor Objects
Once you have “drawn” your prototype by adding graphic objects to it, you can define its properties and specify the effect of changing these properties. To do this, you add accessor objects to your prototype. Accessor objects are instances of subclasses of
IlvUserAccessor.
To add an accessor object to your prototype, create an instance of the appropriate subclass of
IlvUserAccessor and call the
addAccessor method. For example, the following code adds two accessor objects to a prototype: an
IlvValueAccessor that stores a value and an
IlvConditionAccessor that tests a condition and changes a attribute according to the result.
proto->addAccessor(new IlvValueAccessor("v", IlvValueFloatType));
proto->addAccessor(new IlvConditionAccessor("v", IlvValueFloatType,
display,
IlvConditionAccessor::IlvCondGreaterThan,
"100",
"label.label",
"Greater than 100",
"Smaller than 100"));
See the section
Predefined AccessorsPredefined Accessors and the
Views Prototypes Reference Manual for a complete description of each accessor class.
Adding the Prototype to a Library
Prototypes must be stored in a prototype library so that they can be saved and reloaded later.
To create a new prototype library, use the
IlvProtoLibrary class:
IlvProtoLibrary* protoLib = new IlvProtoLibrary(display,
"myLib",
"/usr/home/myhome/protos");
A prototype library stores its prototypes in a file system directory ("/usr/home/myhome/protos" in the previous example). You can change this directory later using the setPath method.
To add your prototype to the new library, call the addPrototype method:
protoLib->addPrototype(proto);
Saving the Prototype
To save your prototype, call the
IlvAbstractProtoLibrary::save method:
myLib->save(0, IlTrue);
The first parameter is an optional output stream where the library description file is saved. Set it to 0 so that the description file is saved to its default location ("/usr/home/myhome/protos/myLib.ipl" in the previous example). The second parameter is set to IlTrue to specify that all the prototypes must be saved.
Published date: 05/24/2022
Last modified date: 02/24/2022