NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot add a sequence to a project using API

I am trying to create a TestStand app completely with the API.  I have been able to create the workspace and add a project.  However, I cannot figure out how to add a sequence file to the project.  Below is my code.  The last few lines are how I tried to add the sequence file, but got an exception.
 
How do I add a sequence file to a project?
 
WorkspaceFile workspace = _testStandEngine.NewWorkspaceFile();
PropertyObjectFile workspaceFile = workspace.AsPropertyObjectFile();
workspaceFile.Path = @"C:\Released\TestStand\TestApp\TestApp.tsw";
workspaceFile.WriteFile(WriteFileFormat.WriteFileFormat_Current);

// create project file
PropertyObjectFile projectFile = _testStandEngine.NewPropertyObjectFile(PropertyObjectFileTypes.FileType_ProjectFile);
projectFile.Path = @"C:\Released\TestStand\TestApp\MyProject\MyProject.tpj";
projectFile.WriteFile(WriteFileFormat.WriteFileFormat_Current);                

// add project to workspace
WorkspaceObject project = workspace.RootWorkspaceObject.NewFile(projectFile.Path);
workspace.RootWorkspaceObject.InsertObject(project, 0);

project.ProjectFile.WriteFile(WriteFileFormat.WriteFileFormat_Current);

// sequence file

PropertyObjectFile sequenceFile = _testStandEngine.NewPropertyObjectFile(PropertyObjectFileTypes.FileType_SequenceFile);
sequenceFile.Path = @"C:\Released\TestStand\TestApp\MyProject\Tests.seq";
sequenceFile.WriteFile(WriteFileFormat.WriteFileFormat_Current);

//*** throws an exception:
//*** "Attempting to insert non-project file into workspace."
WorkspaceObject sequence = workspace.RootWorkspaceObject.NewFile(sequenceFile.Path);

project.InsertObject(sequence, 0);

workspace.AsPropertyObjectFile().WriteFile(WriteFileFormat.WriteFileFormat_Current);
 
0 Kudos
Message 1 of 6
(2,738 Views)

You can't add sequence files to a workspace.  You have to add them to a project.  It's been a while since I've played with this but I think you need to do something like this:

projectAsObject = workspacefile.FindWorkSpaceObject(project path)

seqFileObject = projectAsObject.NewFile(SequenceFilePath)

projectAsObject.InsertObject(seqFileObject)

 

Again, been a while so I'm not quite sure on all the details.

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
0 Kudos
Message 2 of 6
(2,208 Views)

I believe I sorted out the details of your code, but still cannot add a sequence to a project.  I am able to create a sequence file and populate it, but just can't add it to my project.  See my revised code below.

 

I use a SequenceFile object since it seems the most straightforward for adding properties.  Note that to call InsertObject I had to call NewFile to get a WorkspaceObject object for my sequence file.  Also note that I sprinkled Save and Write methods around thinking that it might resolve my issue.  Perhaps I am not saving the 'correct' way or at the right time.

 

SequenceFile sequenceFile = _testStandEngine.NewSequenceFile();

Sequence newSequence = _testStandEngine.NewSequence();
newSequence.Name = "MyNewSequence";
PropertyObject locals = newSequence.Locals;
locals.NewSubProperty("MyBool", PropertyValueTypes.PropValType_Boolean, false, "", 0);
locals.NewSubProperty("MyNumber", PropertyValueTypes.PropValType_Number, false, "", 0);
sequenceFile.InsertSequenceEx(0, newSequence);

sequenceFile.Save(@"C:\Released\TestStand\TestApp\MyProject\Tests.seq");

WorkspaceObject projectAsObject = workspace.FindWorkspaceObject(projectFile.Path);
WorkspaceObject sequenceFileAsObject = projectAsObject.NewFile(@"C:\Released\TestStand\TestApp\MyProject\Tests.seq");
projectAsObject.InsertObject(sequenceFileAsObject, 0);

// save?
projectFile.WriteFile(WriteFileFormat.WriteFileFormat_Current);                                

// save everything
workspace.AsPropertyObjectFile().WriteFile(WriteFileFormat.WriteFileFormat_Current);

 

0 Kudos
Message 3 of 6
(2,200 Views)

projectFile.IncChangeCount()

projectFile.SaveFileIfModified(False)

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
0 Kudos
Message 4 of 6
(2,197 Views)

You might need to do AsPropertyObjectFile() for the projectFile before calling IncChangeCount and SaveFileIfModified methods.

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
0 Kudos
Message 5 of 6
(2,194 Views)

I finally got it.  jiggawax's comments helped.  I found the article (https://forums.ni.com/t5/Example-Programs/Creating-and-Populating-a-Workspace-Using-the-TestStand-AP...) on which I based my code to be incomplete.  The complete solution is below.

 

My biggest problem was that it wasn't clear that WorkspaceObject.NewFile does not create a file, it only creates an object associated with a file.  For a sequence file, you must create the sequence file (various ways), save it, and then pass NewFile the path to the file.

 

const string ProjectFileName = @"C:\Released\TestStand\TestApp\MyProject\MyProject.tpj";
const string SequenceFileName = @"C:\Released\TestStand\TestApp\MyProject\Tests.seq";
const string WorkspaceFileName = @"C:\Released\TestStand\TestApp\TestApp.tsw";

// create workspace file
WorkspaceFile workspace = _testStandEngine.NewWorkspaceFile();
workspace.AsPropertyObjectFile().Path = WorkspaceFileName;
workspace.AsPropertyObjectFile().WriteFile(WriteFileFormat.WriteFileFormat_Current);

// create project file
PropertyObjectFile projectFile = _testStandEngine.NewPropertyObjectFile(PropertyObjectFileTypes.FileType_ProjectFile);
projectFile.Path = ProjectFileName;
projectFile.WriteFile(WriteFileFormat.WriteFileFormat_Current);

// add project to workspace
WorkspaceObject projectObject = workspace.RootWorkspaceObject.NewFile(projectFile.Path);
workspace.RootWorkspaceObject.InsertObject(projectObject, 0);

// create sequence and add a variable to Locals
Sequence newSequence = _testStandEngine.NewSequence();
newSequence.Name = "MySequence";
PropertyObject locals = newSequence.Locals;
locals.NewSubProperty("MyNumber", PropertyValueTypes.PropValType_Number, false, "", 0);

// create sequence file, add sequence, and save
SequenceFile sequenceFile = _testStandEngine.NewSequenceFile();
sequenceFile.InsertSequenceEx(0, newSequence);
sequenceFile.Save(SequenceFileName);

// create WorkspaceObject for sequence file and insert it into project
WorkspaceObject sequenceObject = projectObject.NewFile(sequenceFile.Path);
projectObject.InsertObject(sequenceObject, 0);

// save the project file
projectObject.ProjectFile.WriteFile(WriteFileFormat.WriteFileFormat_Current);

// save the workspace
workspace.AsPropertyObjectFile().IncChangeCount();
workspace.AsPropertyObjectFile().WriteFile(WriteFileFormat.WriteFileFormat_Current);

 

0 Kudos
Message 6 of 6
(2,176 Views)