Opening and activating the existing configuration

To activate an existing TwinCAT configuration, three steps are performed: A DTE instance is created, the solution is opened, and then ActivateConfiguration and StartRestartTwinCAT are called on the ITcSysManager object.

The ITcSysManager object is obtained from the first project in the solution. The ProgID determines which version of Visual Studio is launched. References to “Microsoft Developer Environment” and “Beckhoff TwinCAT XAE Base” must be added to the project.

Visual Studio version

ProgID

Visual Studio 2022

VisualStudio.DTE.17.0

Visual Studio 2019

VisualStudio.DTE.16.0

Visual Studio 2017

VisualStudio.DTE.15.0

TcXaeShell 2022

TcXaeShell.DTE.17.0

C# sample

Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.17.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();

PowerShell sample

$prjDir = "C:\tmp\TestSolution\"
$prjName = "TestSolution.sln"
$prjPath = $prjDir += $prjName
$dte = new-object -com VisualStudio.DTE.17.0
$dte.SuppressUI = $false
$dte.MainWindow.Visible = $true

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

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

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