Opening and activating existing configurations

To activate a previously created configuration, an instance of the TwinCAT XAE has to be created, the configuration has to be loaded and activated.

Procedure

The ProgId "VisualStudio.DTE.10.0" is used to create an instance of Visual Studio. Via Visual Studios DTE object a full control of Visual Studio is possible. The Procedure to create the ITcSysManager interface (the 'sysMan' instance here) is described in the chapter Accessing TwinCAT Configurations.

Sample (CSharp):

Please note, that, for this sample, you need to add both a reference to "Microsoft Developer Environment 10.0" and "Beckhoff TwinCAT XAE Base" to your project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE100;
using System.IO;
using TCatSysManagerLib;

namespace ActivatePreviousConfiguration
{
class Program
{
static void Main(string[] args)
{
Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
EnvDTE.DTE dte = (EnvDTE.DTE)System.Activator.CreateInstance(t);
dte.SuppressUI = false;
dte.MainWindow.Visible = true;

EnvDTE.Solution sol = dte.Solution;
sol.Open(@"C:\Temp\SolutionFolder\MySolution1\MySolution1.sln");

EnvDTE.Project pro = sol.Projects.Item(1);

ITcSysManager sysMan = pro.Object;

sysMan.ActivateConfiguration();
sysMan.StartRestartTwinCAT();
}
}
}

Sample (PowerShell):

$prjDir = "C:\tmp\TestSolution\"
$prjName = "TestSolution.sln"
$prjPath = $prjDir += $prjName
$dte = new-object -com VisualStudio.DTE.10.0
$dte.SuppressUI = $false
$dte.MainWindow | %{$_.gettype().InvokeMember("Visible","SetProperty",$null,$_,$true)}

$sln = $dte.Solution
$sln.Open($prjPath)

$project = $sln.Projects.Item(1)
$systemManager = $project.Object

$systemManager.ActivateConfiguration()
$systemManager.StartRestartTwinCAT()

Sample (VBScript):

dim dte,sln,proj,sysMan 
set dte = CreateObject("VisualStudio.DTE.10.0")
set sln = dte.Solution
call sln.Open("C:\SolutionFolder\MySolution1.sln")
set proj = sln.Projects(1)
set sysMan = proj.Object
call sysMan.ActivateConfiguration
call sysMan.StartRestartTwinCAT