Creating a New VI from a Template Using VI Scripting

Requires: VI Scripting

Although you can programmatically create complex VIs one object at a time by using the New VI Object function and the Terminal:Connect Wire method, that approach requires complex and expansive scripting code that is time-consuming to write and difficult to read. It is often easier to write a template VI that closely approximates the VI you want to create. You then can use VI Scripting to create a copy of the template VI and edit small parts of the copied code to achieve your scripting goals.

What to Use

This VI Scripting task requires the following objects:

New VI Close Reference
 Add  Find
 Add  Find

What to Do

Before you start: Open the Creating New VI From Template VI in the labview\examples\Application Control\VI Scripting\Creating VIs directory for an example that illustrates each step of the following procedure.

 Open Example

Expand the procedure for more information about this specific example.

  1. Design a template VI that approximates the form of the desired target VI as closely as possible.

    Include the following components in the template design:

    Example Details

    In the example, the template illustrates a design that includes an array input, a numeric output, a Placeholder subVI, and the wiring required to connect these objects required. At run time, a scripting VI can replace the Placeholder subVI with a subVI that performs the user-specified computation.

    Note Note  The placeholder subVI purposely has an identical connector pane pattern to both of the possible replacement subVIs. Matching connector pane patterns enables the scripting VI to easily replace the placeholder subVI without rewiring its terminals. If the placeholder object in a template VI has a different connector pane than the replacement object, LabVIEW might not wire data to the replacement object as desired.
  2. Use a New VI function to create a copy of the template VI by wiring the path to the template VI to the function.

    You can use a path control, a path constant, or the VI:VI Path property of a reference to your template VI to specify the path of the template VI.

    Example Details

    In the example, the VI:VI Path property provides the path to the template VI.

  3. Obtain references to the objects that you want to modify in the template VI. The most common way to obtain a reference to an object in a template VI is to use the Open VI Object Reference function.

    Example Details

    In the example, the scripting VI must replace the Placeholder subVI with a subVI that performs the operation that the user of the scripting VI specifies at run time. Because the Placeholder subVI has a unique label, the Open VI Object Reference function can obtain a reference to that subVI.

  4. Inspect or modify the referenced objects to achieve your scripting goal.

    Refer to the list of common scripting tasks for more information about ways to edit a VI using VI Scripting.

    Example Details

    In the example, the scripting VI uses the Replace method to replace the Placeholder subVI with a subVI capable of performing the user-specified computation task. Based on the value of the Operation radio button at run time, a Case structure selects whether to replace the placeholder subVI with an addition subVI or a multiplication subVI.

  5. Use the Close Reference function to close each open object reference when you are finished using it.

    Example Details

    In the example, the scripting VI closes the reference to the replacement subVI as well as the reference to the target VI. However, the scripting VI does not close the reference to the original placeholder subVI because the Replace method invalidates that reference.

Caveats and Recommendations

Additional Examples

Refer to the Creating New VI From Scratch VI in the labview\examples\Application Control\VI Scripting\Creating VIs directory for an example of creating a new VI one object at a time. Notice the complexity of this example and the number of references required to complete the task without using a template.

 Open example  Find related examples