このコンテンツは設定された言語で表示できません。

このコンテンツは表示可能な別の言語で表示されています。 お使いのブラウザに翻訳機能がある場合はご利用ください。

Using VI Snippets to Store and Share Reusable Code Sections

Updated Oct 31, 2023

Environment

Software

  • LabVIEW

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.

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
 

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 1 - The frame with the dotted line lets you know that the image is a VI Snippet
Note:
This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
 

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 2 - 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

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.


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.
 

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. 
 

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. 

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

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 3 - Structures pre-populated with common code segments
Note:
This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
 

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

Figure 5 - Code that commonly goes together
 

Figure 6 - Preconfigured VI calls with input values already wired
 

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

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