Example Code

Creating and Populating a Workspace Using the TestStand API

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • Teststand

Code and Documents

Attachment

Overview

This example demonstrates the TestStand API calls required to generate workspace files programatically, including the following tasks:

 

  • Creating a new workspace, project, and sequence file
  • Defining the relationships between the files
  • Saving the changes

 

Description

In order to manipulate the contents of TestStand workspaces using the TestStand API, you use the WorkspaceObject API. First, create a workspace and project file using the PropertyObjectFile API:

 

Create a new workspace file

 

Locals.WorkspaceRef = RunState.Engine.NewWorkspaceFile(),
Locals.WorkspaceRef.AsPropertyObjectFile.Path = "C:\\myWorkspace.tsw",
Locals.WorkspaceRef.AsPropertyObjectFile.WriteFile(WriteFileFormat_Current)

 

Create a new project file

 

Locals.ProjectRef = RunState.Engine.NewPropertyObjectFile(FileType_ProjectFile),
Locals.ProjectRef.AsPropertyObjectFile.Path = "C:\\myProject.tpj",
Locals.ProjectRef.AsPropertyObjectFile.WriteFile(WriteFileFormat_Current)

 

Before adding any files to a workspace, you must first add a project file.  Use the NewFile and InsertObject methods with the RootWorkspaceObject property to insert a new project file:

 

Adding a project file to a workspace

 

//create a new workspace object using the path to the project file.  All items in a workspace are defined as a workspaceObject.
Locals.projWSObject = Locals.WorkspaceRef.AsWorkspaceFile.RootWorkspaceObject.NewFile(Locals.ProjectRef.AsPropertyObjectFile.Path), //insert the workspace object into the top level workspace object.
Locals.WorkspaceRef.AsWorkspaceFile.RootWorkspaceObject.InsertObject(Locals.projWSObject,0)

You can add other files or folders into the project using a similar approach:

 

Adding a folder to a Project in a workspace

 

Locals.FolderWSObject = Locals.projWSObject.AsWorkspaceObject.NewFolder(Locals.Name),
Locals.projWSObject.AsWorkspaceObject.InsertObject(Locals.FolderWSObject ,0),

 

Adding a file to a folder in a workspace

 

Locals.SeqFileWSObject = Locals.projWSObject.AsWorkspaceObject.NewFile(Locals.SeqFileRef.AsPropertyObjectFile.Path),
Locals.projWSObject.AsWorkspaceObject.InsertObject(Locals.SeqFileWSObject,0)

 

Finally, save the project file through the workspace API.  NOTE: The changes will not persist if you save the files using the property object API

 

Saving the project file

 

Locals.projWSObject.AsWorkspaceObject.ProjectFile.WriteFile(WriteFileFormat_Current)

 

Saving project file using Incorrect approach (files added to the project will not persist)

 

Locals.ProjectRef.AsPropertyObjectFile.WriteFile(WriteFileFormat_Current)

You should, however, use the PropertyObjectFile API to save the workspace file:

 

Saving the workspace

 

Locals.WorkspaceRef.AsPropertyObjectFile.IncChangeCount(),
Locals.WorkspaceRef.AsPropertyObjectFile.WriteFile(WriteFileFormat_Current)

 

Hardware and Software Requirements

Generate and Populate Workspace - TS 2014.seq

TestStand 2014 or Compatible 

 

Generate and Populate Workspace - TS 2017.seq

TestStand 2017 or Compatible

 

Steps to Implement or Execute Code

  1. Open the attached example sequence file.
  2. Run the example using Execute » Run MainSequence
  3. Enter a file name and directory when prompted.
  4. Once the example completes execution, open the newly created workspace file in the directory you specified. 
  5. Verify that the workspace contains a project, folder, and sequence file.

 

 

National Instruments
Applications Engineer

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
TucsonDave
Member
Member
on

Be careful with the "Adding a file to a folder in a workspace" step.  I do not think this adds a file to a folder, just to the project.  Also, Locals.SeqFileRef is not defined.  This can be any number of objects.  The important point is that it represents an existing file.  Note that NewFile does not create the file, it creates an object that references a file.  Therefore, the file must be created elsewhere and its path passed to NewFile.