Compiling PLC projects

Individual projects within a TwinCAT solution can be compiled selectively using SolutionBuild.BuildProject without having to rebuild the entire solution. Before calling BuildProject, the target platform must be set to the desired configuration using ConfigurationManager.ActiveTargetPlatform. The result can be retrieved via SolutionBuild.LastBuildInfo: 0 indicates success; any other value indicates an error.

Requirements

PowerShell sample

$sln = $dte.Solution
$sysMan = $sln.Projects.Item(1).Object

# Compile SysMan project using the SolutionBuild interface
$sysManProject = $dte.Solution.Projects.Item(1)
$sysManProjectPath = $sysManProject.FullName
$sysMan.ConfigurationManager.ActiveTargetPlatform = "TwinCAT RT (x64)"
$sln.SolutionBuild.BuildProject("Release|TwinCAT RT (x64)", $sysManProjectPath, $true)

# Compile SysMan-Project with BuildTargetPlatform command
$sysMan.BuildTargetPlatform("TwinCAT RT (x64)") 

# Compile PLC project (path to .plcproj file)
$plcProjectPath = "C:\Projects\MyProject\PLC1\PLC1.plcproj"
$sysMan.ConfigurationManager.ActiveTargetPlatform = "TwinCAT RT (x64)"
$sln.SolutionBuild.BuildProject("Release|TwinCAT RT (x64)", $plcProjectPath, $true)

# Check result (0 = success)
$lastBuildInfo = $sln.SolutionBuild.LastBuildInfo
if ($lastBuildInfo -ne 0) {
    Write-Error "Build fehlgeschlagen"
}

C# sample

EnvDTE.Solution sln = dte.Solution;

string plcProjectPath = @"C:\Projects\MyProject\PLC1\PLC1.plcproj";

systemManager.ConfigurationManager.ActiveTargetPlatform = "TwinCAT RT (x64)";
// Third parameter: true = wait until build is complete
sln.SolutionBuild.BuildProject("Release|TwinCAT RT (x64)", plcProjectPath, true);

int result = sln.SolutionBuild.LastBuildInfo; // 0 = success
Compiling PLC projects 1:

The first parameter is the build configuration, such as "Release|TwinCAT RT (x64)" or "Debug|TwinCAT RT (x64)". The third parameter, true, blocks the call until the build is complete. LastBuildInfo != 0 indicates an error. The exact error messages are listed in the DTE's error list.

Compiling PLC projects 2:

Before starting a build via the DTE's SolutionBuild interface, you must use System Manager to set the target platform to the appropriate configuration. Otherwise, a clean build is not possible. The official TwinCAT Platform Matrix provides a complete overview of the supported target platforms (operating system, processor, PE/ELF format).