Samples | System 6.3.10 Determining the file path of a TwinCAT project | Determining the file path of a TwinCAT project This chapter shows two ways to determine the file path of a TwinCAT project: path to the .tsproj file of a TwinCAT solution via the DTE Project object. path to the .plcproj file for individual PLC projects via ProduceXml on the PLC node. Path to the .tsproj file via DTE SolutionBuild.BuildProject requires the file path of the TwinCAT project. This can be determined using the FileName property of the DTE Project object. C#:
EnvDTE.DTE dte = (EnvDTE.DTE)Marshal.GetActiveObject("TcXaeShell.DTE.17.0"); foreach (EnvDTE.Project project in dte.Solution.Projects) { // FileName returns the full path to the .tsproj file string projectPath = project.FileName; Console.WriteLine(projectPath); // Output e.g.: C:\Projects\MyProject\MyProject.tsproj } PowerShell:
$dte = [Runtime.InteropServices.Marshal]::GetActiveObject("TcXaeShell.DTE.17.0") foreach ($project in $dte.Solution.Projects) { $projectPath = $project.FileName Write-Host $projectPath } # For use with SolutionBuild.BuildProject $dte.Solution.SolutionBuild.BuildProject( "Debug|TwinCAT RT (x64)", # ConfigurationName $projectPath, # ProjectUniqueName (FileName or UniqueName) $true # WaitForBuildToFinish ) Path to the .plcproj file via ProduceXml The path to the .plcproj file for a PLC project is not directly accessible via DTE. Instead, the PLC node TIPC is iterated over, and the ProjectPath value is read from the XML of each PLC child object.
ITcSmTreeItem plcNode = sysMan.LookupTreeItem("TIPC"); // Dictionary [key = project name | value = project file path] Dictionary projectPaths = []; int padWidth = plcNode.ChildCount.ToString().Length; int maxNameLength = Enumerable.Range(1, plcNode.ChildCount).Max(i => plcNode.Child[i].Name.Length); // Iterate through all PLC projects and extract their project paths and names for (int i = 1; i { var child = plcNode.Child[i]; var doc = XDocument.Parse(child.ProduceXml()); string projectPath = doc.Descendants("ProjectPath").FirstOrDefault()?.Value ?? "Unknown"; projectPaths.Add(child.Name, projectPath); Console.WriteLine($"{i.ToString().PadLeft(padWidth)}: Project Name:
{child.Name.PadRight(maxName Length)}, Project Path: {projectPath}"); } 6.3.11 Finding targets using broadcast search | Finding targets using broadcast search You can search the network for TwinCAT target systems on the route node via ConsumeXml , either generally for all devices on the network or specifically by IP address or hostname. The result is then retrieved via ProduceXml . TE1000 211 Version: 1.7.0