Requires: VI Scripting
When you create a new VI from a template, you usually want to make small modifications to the code that LabVIEW copied from the template into the new VI. To make these modifications, you must obtain references to the objects in the new VI that you want to inspect or modify. After you obtain these object references, you can wire them to Property Nodes or Invoke Nodes to get or set information about the referenced objects. The easiest way to obtain references to objects in the new VI is to use the Open VI Object Reference function. However, to use this function, the desired objects in the new VI must have unique labels. If the desired objects do not have unique labels, you must use a different, more complex procedure to obtain references for these unlabeled or unknown objects.
What to Use
This VI Scripting task requires the following objects:
What to Do
Before you start: Open the Obtaining Known Object Reference VI in the labview\examples\Application Control\VI Scripting\Finding and Modifying Objects directory for an example that illustrates each step of the following procedure.
Expand the procedure for more information about this specific example.
- Obtain a reference to the target VI that contains the object with which you want to interact.
Determine the identifying information of the object whose reference you want to obtain.
- Class type—Every front panel or block diagram object that you can use with VI Scripting is a member of a VI Server class, so the reference for a given object is of a corresponding class. Refer to the anatomies of VIs to determine the class name of the object you want to access. Use the Class Specifier Constant to indicate the class of object for the Open VI Object Reference function.
In the example, the Edit Me control is a member of the Numeric class.
- Owner—Every object on both the front panel and the block diagram has an owner. In the simplest case, the owner of an object is either the front panel or the block diagram of a VI. However, if an object resides within a substructure such as a loop, a Case structure, a sequence structure, or a cluster, that substructure owns the object.
In the example, the Edit Me control resides on the front panel of the target VI, so the front panel of the target VI owns the control. Thus, the scripting VI uses the VI:Front Panel property to obtain the reference to the front panel of the target VI.
||Note The Control and ControlTerminal classes are often confused. A ControlTerminal object is the block diagram representation for the corresponding front panel Control object. Thus, the front panel of the target VI owns the Edit Me Control object, and the block diagram of the target VI owns the associated Edit Me ControlTerminal object. You can change the numeric representation of a Control object but not of a ControlTerminal object.
- Label—This is the name assigned to the desired object in the target VI.
In the example, the label of the control to change is Edit Me.
- Use an Open VI Object Reference function to obtain a reference to the desired object by wiring the identifying information from step 2 to the function.
- Get or set information about the referenced object by wiring the object refnum output of the Open VI Object Reference function to a Property Node or an Invoke Node.
In the example, the scripting VI uses the Numeric:Representation property to set the numeric representation of the Edit Me control.
- Use the Close Reference function to close each open object reference when you are finished using it.
Caveats and Recommendations
- Display block diagram labels at least once—The Open VI Object Reference function cannot locate objects whose labels have never been displayed. Because block diagram objects do not display a label by default, you must manually display the label for any block diagram object that you intend to find using the Open VI Object Reference function. To display the label of an object when the label is invisible, right-click the object and select Visible Items»Label. After you display the label at least once, the Open VI Object Reference function can find the associated object even if you make the label invisible again.
- Create unique labels—LabVIEW does not enforce unique labels for objects on the block diagram. By default, most objects that are members of the same class have the same label. For example, if you add two For Loops to a block diagram, each one has a label of For Loop. If you use the Open VI Object Reference function on an object that shares a label with another object, LabVIEW returns a reference to the first object that it finds. Assigning unique labels to each object ensures that LabVIEW returns the desired reference.
- Close all references that you open—An excessive number of open references negatively impacts the performance of a VI. Read about closing VI and object references for more performance details.
Refer to the Creating New VI From Template VI in the labview\examples\Application Control\VI Scripting\Creating VIs directory for an example of obtaining references in a known VI.