Tuesday, August 2, 2011

How to Customize Edit > Create SubVI in LabVIEW 2011

LabVIEW 2011 was announced at the NI Week keynote today, and there are plenty of places you can learn about all the great new features. In this blog post, I'll describe how to customize one of the best new features...the improvements to the Edit > Create SubVI gesture in the LabVIEW IDE.

You may recall that last year, Stephen and I solicited the LabVIEW Community to vote up (or 'kudo' up, I guess) the Create a proper connector pane when doing Edit -> Create subVI idea posted by Yair on the Idea Exchange. You all responded in an impressive way, and we were able to add the feature in LabVIEW 2011.  Specifically, we now create a subVI with the proper connector pane, properly-named error terminals in the bottom corners, properly-named refnum/class terminals in the top corners, and an organized front panel.

But that's not all!  Thanks to Stephen, the code that modifies the created subVI is all VI-based.  So if you want to customize the created subVI in some way with your own scripting VIs, there are two ways to do it. Here's how:

Providing Additional Create SubVI Functionality
Do you like the way we create the subVI, but you want to perform additional actions on it?  If so, then follow these steps:
  1. Open this VI: [LabVIEW]\resource\plugins\CreateSubVI\Additional Actions Template.vi
  2. Save a copy of the VI here: [LabVIEW]\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.  Some ideas I've had are:
    • Perform a Diagram Cleanup on the created subVI's diagram
    • Wrap the created subVI's diagram in an error case structure
    • Programmatically launch the Icon Editor on the created subVI
Completely Replacing Our Create SubVI Functionality
Would you rather define your own behavior for how the subVI is created?  If so, then follow these steps.
  1. Make a copy of  [LabVIEW]\resource\plugins\lv_modifyNewSubVI.vi and name it lv_modifyNewSubVI.bak.
  2. In the original lv_modifyNewSubVI.vi, you can remove the code on its diagram, and write your own 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 originally 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.
I should also point out that these VIs are not kept in memory after a Create SubVI operation...so it's very easy to debug (via breakpoints, etc.) any plugins you write, as you can easily modify your plugin code and test it by performing the Edit > Create SubVI  gesture again.

I would like to eventually post some plugins of both types to the NI Community.  In the meantime, please let me know if you've written some plugins to augment (or replace) the Create SubVI functionality...I'd love to see what y'all can come up with!