Use VI Snippet to Store and Share Reusable Code Sections

Publish Date: Sep 10, 2013 | 43 Ratings | 3.79 out of 5 |  PDF

Overview

VI Snippets are a new way to store, share and reuse small portions of LabVIEW code. VI Snippets combine the portability of a screenshot with the functionality of a VI file by embedding LabVIEW code in a standard PNG image. When you drag a VI Snippet PNG image onto the block diagram it will drop the snippet as graphical code that can be edited and used in your application.

Table of Contents

  1. Related Links

Background

Traditionally, you could share LabVIEW code in one of two ways; as a .VI file, or as a screenshot of the block diagram.  A VI contains functional, reusable code, but you must have LabVIEW installed to see the block diagram.  Screenshots are easily embedded in documents, forum posts and emails, but they require that you recreate the code from the image, which may contain items like case structures that obscure some of the diagram from view.

Creating a VI Snippet

Figure 1 - Drag and drop a VI Snippet onto the block diagram and instantly edit code

To create a VI Snippet, select a portion (or all) of your block diagram and select Edit»Create VI Snippet from Selection.  Then choose where you would like to save your VI Snippet.

The PNG file you save is a screenshot of your selected code with LabVIEW code embedded within.  All VI Snippets have a border and icon to indicate that you can drag them into LabVIEW to add the code to your Block Diagram.

Figure 2 - The frame with the dotted line lets you know that the image is a VI Snippet

You can now share your VI Snippet online anywhere images are allowed including blogs, NI Discussion Forums, and the NI User Community. 

Adding a VI Snippet to Your Block Diagram

To add a VI Snippet to your block diagram, drag the image from file manager (such as Windows Explorer) or Internet Explorer onto a Block Diagram Window.

Figure 3 - VI Snippets make sharing code on the community easy

Generally dragging and dropping an image file onto your block diagram will add an image decoration to your block diagram.  VI Snippets are special PNG images that have LabVIEW code in addition to the regular image data.  When you drop a VI Snippet onto the block diagram you get the depicted code added to your VI.

Tips and Tricks for using VI Snippet

1.    Keep Frequently Used Code (or Other Block Diagram Elements) in Snippet Form

One time saving tip is to create a folder to save VI Snippets in locally.  Whenever you come across a piece of code you find yourself recreating many times save it as a reusable VI Snippet.  You can create VI Snippets from portions of code that would have broken run arrows or empty structures that would not work well as SubVIs.  As VI Snippets contain VI information they retain anything on the block diagram such as comments, decorations, and data stored in constants.

2.    Use VI Snippets When Asking for Help or Helping Others

If you are asking for help on the support forums, use a VI Snippet to post just the section of code you are working on.  This way everyone visiting the forums can see your code and potentially weigh in with suggestions without having to download and open your VI.   If you are answering someone else’s question a Snippet is both visible and useable – making it easier for many people to learn from your example.

3.    Be Mindful of SubVIs

You can create VI Snippets that reference SubVIs but your end users need to have the SubVIs on their machines to be able to use the code.  VI Snippets only contain information from the block diagram you create them from.  If your code segment contains a SubVI, the SubVI is not embedded in the VI Snippet. 

4.    Use Your Computer Desktop as an Intermediary If You are Dragging from Firefox or Google Chrome

Firefox and other browsers behave differently to Internet Explorer in the way they handle dragged images. When an image is dragged from Internet Explorer, it is on the clipboard, which allows LabVIEW to easily convert the image metadata into code. Alternatively, if you drag from Firefox or Chrome directly to LabVIEW you will get the URL of the image rather than the code you expected.  An easy way to get around this is to drag the image to your desktop or a folder first, and then drag the image from your desktop to LabVIEW.  Note that LabVIEW 2013 allows users to drag VI snippets directly from Chrome, or Firefox to the block diagram if the snippet is stored at a URL that starts in http: and ends in .PNG.

Keeping a local copy of useful VI Snippets will also make it easy to find them again in the future.

5.    Image Editors Will Remove Embedded VI Data

Image editing programs that do not understand the extra data embedded in the VI Snippet will remove the VI information if you edit the PNG file.  This will prevent the code from dropping in LabVIEW.  To avoid this happening, refrain from editing VI Snippets outside of LabVIEW.  If you want to add comments or annotations, add them to your block diagram before creating the VI Snippet.

Snippet Examples

Here are some examples of potential time-savers. 

Figure 4 - Structures pre-populated with common code segments

Figure 5- Templates for common types of VIs (note the black "undefined" wire)

Figure 6- Code that commonly goes together

Figure 7- Preconfigured VI calls with input values already wired

 

Figure 8- Common data sets such as sample input data used during debugging

Figure 9- Customize comments once and quickly add to multiple VIs

1. Related Links

To see everything that's new in LabVIEW, visit ni.com/labview/whatsnew

To try this and other new features, download LabVIEW to evaluate it for free

Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit