VI scripting provides powerful tools for automating common development tasks. Scripting is the backbone of LabVIEW QuickDrop, allowing developers to create macros that can be invoked using keyboard shortcuts.
This blog describes how to use LabVIEW VI scripting to automate the duplication of a VI and its references into a new VI.
Over years of LabVIEW development, DMC has created a powerful and flexible state machine architecture that is well-suited for industrial applications due to its framework of States, Events, and Actions. Each state machine has its own associated typedef Enums for States, Events, and Actions. Learn more about DMC's State Machine.
Many of DMC’s LabVIEW applications start from the “Template” state machine VI. To start from the Template, we used to copy the Template state machine and its associated CTL files into a new folder, rename the VI and the CTLs, and then open the new VI and point LabVIEW to the “missing” files that were renamed using the new name.
This process was simple, but tedious, especially on complicated projects that used multi-state machine architectures.
For a recent FedEx project, I decided to create a tool that uses VI scripting to automatically copy a template state machine to a new folder, rename the VI and CTLs, replace all references to the “template” CTL files with the newly created CTL files, and add the new VI to the project.
All the user has to do is enter the file path to the template they wish to copy (the default location of the Template is automatically populated), and enter a name for the new state machine.
My tool for automatically duplicating a VI consists of the following actions. VI scripting is used in steps 3, 5, and 6:
- Create new folder – easy using the “Create Folder” VI.
- Copy and rename file – easy using “Copy” VI, replacing the old “Template” name with the user-specified name. The one trick at this step is that I also copy the original CTL files with the Template name so that the new VI can be opened by the scripting VIs without searching for missing CTLs.
- Replace Template CTLs
- Use the VI scripting Traverse for GObjects VI to find all references to the template CTL files, and replace the CTLs with the newly created files.
- The code above: checks the name of each typedef enum and cluster. If the name matches the name of one of the Template files that needs to be replaced, it is replaced with the corresponding new file. The replacement is performed using a VI Scripting Invoke Node.
- Repeat for the front panel. This time we search for controls and indicators that match the Template CTLs we want to replace:
- After the front panel and block diagram replacements are complete, save the new VI. Now this VI has no references to the template CTLs.
- Delete original Template CTLs.
- Resolve conflicts
- Since the VI script modifies files on disk while the project is open, the replacements can result in conflicts in the project.
- Never fear, VI scripting can find and resolve conflicts. My tool recursively browses project items to find those that are in conflict.
- To resolve the conflict, my tool deletes the conflicting file and re-adds the file to the project.
- Add the new state machine to the project.
- I made this step an option that the user can deselect from the front panel. If you’re using an auto-populating folder, this step can cause problems. It’s best to leave the auto-populating folder alone.
- Edit the new state machine icon.
- I found the LabVIEW Icon API here: ..\vi.lib\LabVIEW Icon API\Launch Icon Editor.vi. This makes it convenient to edit the icons of newly-created VIs so that you don’t end up with lots of VIs with the template icon.
- Notify the user that the duplication process is complete
This duplication tool has helped us save a lot of time when creating new VIs from our template, and it also reduces the possibility of making a mistake in the duplication process. The VI scripting principles I used to develop this tool could be used for any process that involves manipulating VI files on disk or in the project.
Some ideas include:
- Batch renaming of files in a folder, class, or library
- Creating a VI based on a typedef data type (perhaps a functional global variable)
- Copying a VI and its dependencies
Happy VI scripting!
Learn more about DMC's LabVIEW Programming.