Create SubVI

From LabVIEW Wiki
Jump to: navigation, search

The Create SubVI function is a feature that enhances code modularity, readability, and reuse by allowing developers to encapsulate a group of block diagram elements into a separate, reusable VI. This function is essential for managing complex applications, as it helps to break down large VIs into smaller, manageable parts.

In LabVIEW, a VI can become complex and hard to navigate as its functionality expands. The Create SubVI function addresses this issue by enabling developers to select a portion of the block diagram and create a new VI from that selection. The newly created SubVI can then be used in the original VI as a single node, significantly simplifying the block diagram and making the code easier to understand and maintain.

How to Use Create SubVI

To create a SubVI in LabVIEW, follow these steps:

  1. Select Block Diagram Elements: In your main VI, select the block diagram elements (controls, indicators, functions, and wires) that you want to encapsulate into a SubVI.
  2. Create SubVI: Select the Edit menu->Create SubVI. LabVIEW automatically generates the SubVI, creating appropriate connector pane terminals for the controls and indicators involved in the selection.
  3. Edit SubVI (Optional): Double-click the newly created SubVI to open and edit it as needed. You might want to modify its front panel, block diagram, icon, or connector pane to better suit your needs.
  4. Reuse SubVI: You can now use this SubVI in other parts of your application or in other projects, just like any other VI.

Best Practices

  • Define Clear Inputs and Outputs: Ensure that the selected block diagram elements have clearly defined inputs and outputs for the SubVI.
  • Use Descriptive Names: Give your SubVI a descriptive name and customize its icon to reflect its functionality, making it easier to identify and use.
  • Organize Connector Pane: Arrange the connector pane terminals in a logical order and group related terminals to improve usability.
  • Document SubVIs: Provide documentation for the SubVI, including a description of its purpose, inputs, outputs, and any important behavior or requirements.

Benefits of Using SubVIs

  • Code Reuse: SubVIs can be used across multiple projects, promoting code reuse and reducing development time.
  • Simplified Debugging: Debugging a complex VI becomes easier when it's broken down into smaller, more manageable SubVIs.
  • Improved Readability: Replacing clusters of block diagram elements with SubVIs makes the main VI's block diagram cleaner and easier to read.
  • Enhanced Maintenance: Changes made to a SubVI automatically propagate to all instances of that SubVI, simplifying maintenance.

Extending the Create SubVI Behavior

Do the following to add extra scripting code that is executed after the built-in "Create SubVI" code has run:

  1. Open this VI located at: [LabVIEW 20xx]\resource\plugins\CreateSubVI\Additional Actions Template.vi
  2. Save a copy of the VI here: [LabVIEW 20xx]\resource\plugins\CreateSubVI\CreateSubVI_AdditionalActions.vi
  3. Add scripting code to this copy of the VI to do whatever you want to the created subVI.

Replacing the Create SubVI Behavior

Would you rather replace the behavior rather than extend it? Then do the following:

  1. Make a Backup: Copy [LabVIEW]\resource\plugins\lv_modifyNewSubVI.vi and name it lv_modifyNewSubVI.bak or something similar.
  2. In the original lv_modifyNewSubVI.vi, you can remove the code on its diagram, and write your scripting code to modify the new subVI.
    Here is a description of the inputs/outputs of the VI:
    VI Refnum - A VI reference to the newly created subVI.
    Fail? - If true, LabVIEW assumes an error occurred during your scripting, does not commit any of your scripting changes, and instead creates the subVI in the same way it did in LabVIEW 2010 and previous...full conpane, weirdly-named controls, etc.
    Caller Connections in/out - This is the most complicated part. The input Caller Connections array defines numeric indices for the controls/indicators on the (full) subVI conpane as it is created. For example, if you create a subVI with one input and one output, then the array might have the values [0,1]. You can then programmatically inspect the conpane to see which items are connected to which indices...let's say for this example, the control is index 0 and the indicator is index 1. For the output Caller Connections array, you must specify which indices on the new conpane those same objects are now wired to. So for example, if you change to a 4x2x2x4 conpane, and you place the indicator at index 0 and the control at index 11 (those are the top corners of 4x2x2x4), you would output [11,0] as the output Caller Connections. If you want to learn more about how this works, check out [LabVIEW]\resource\plugins\CreateSubVI\Calculate New Conpane Array.vi, which is the VI that figures this out for the current shipping Create SubVI technique.
  3. If you ever want to revert to the shipping functionality, restore the .bak copy of the VI.

Testing Your Changes to Create SubVI

These VIs do not stay in memory after the Create SubVI operation is completed. So debugging can be done in the usual manner (i.e. with breakpoints, etc.) for scripting code. After editing and saving, simply test it by selecting the Edit menu->Create SubVI menu item again.