Opening and activating existing configurations

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

Procedure

The ProgId "VisualStudio.DTE.10.0" is used to create an instance of Microsoft Visual Studio®. Full control over Microsoft Visual Studio® is possible via the Visual Studio DTE object. The procedure for creating the ITcSysManager interface (here the'sysMan' instance) is described in the chapter Accessing TwinCAT configurations.

Example (CSharp):

Please note that for this example you have to add a reference to "Microsoft Developer Environment 10.0" as well as to "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 systemManager = pro.Object as ITcSysManager;

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

Example (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()

Example (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