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
- A TwinCAT solution containing at least one PLC project is open.
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
![]() | The first parameter is the build configuration, such as |
![]() | Before starting a build via the DTE's |
