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:
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.
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:
All objects that you do not want to create programmatically one at a time
All connections that you do not want to create programmatically from scratch
Placeholder objects for objects that you want to specify at run time. Placeholder objects allow you data to wire the terminals of the placeholder in the template rather than programmatically creating those connections from scratch in the scripting VI.
Visible labels on all objects that you want to be able to locate or modify with the scripting VI
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 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.
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.
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.
Inspect or modify the referenced objects to achieve your scripting goal.
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.
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
Create an intermediate subVI that gathers and returns the desired references—For VI scripting applications that make many modifications to a template, you can clarify your code by separating the code that gathers references into a subVI. To create a separate subVI that returns the references you want to modify, perform steps 2-4 in a new subVI rather than in your top-level scripting VI. Then the top-level scripting VI can call the new subVI and make modifications with the returned references.
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.
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.