Exporting PLC code in PLCopen format
ITcPlcOpenImportExport2.PlcOpenExport2 allows you to export individual POUs or entire subtrees of a PLC project as PLCopen XML. The ITcPlcOpenImportExport2 interface is obtained by casting the PLC project Tree Item. The elements to be exported are addressed using a project-specific selector. The bSubTree parameter controls whether only the specified elements are exported, or whether all child elements are also exported.
Requirements
- TwinCAT XAE is installed and started.
- A DTE connection to the target instance is active.
-
ITcSysManageris initialized in an STA thread. - A PLC project is included in the solution.
- The
ITcPlcOpenImportExport2interface is available starting with TwinCAT Build 4019.
C# sample
The selector is project-specific and uses ; as a separator, such as "POUs.fb1;POUs.fb2". Using bSubTree = true exports all sub-elements of the selected element, including methods, actions, transitions, and properties. Without this flag, only the explicitly listed elements are exported.
ITcSmTreeItem plcProject = sysManager.LookupTreeItem(@"TIPC^PLC1^PLC1 Project");
ITcPlcOpenImportExport2 exportInterface = (ITcPlcOpenImportExport2)plcProject;
// Export a single POU
exportInterface.PlcOpenExport2(
@"C:\Export\MyProg.xml", // output file
"POUs.MyProg", // selection (project-relative, ';' as separator)
false // false = only listed elements, not recursive
);
// Export selected POUs
exportInterface.PlcOpenExport2(
@"C:\Export\MyProg.xml", // output file
"POUs.MyProg;DUTs.MyDUT", // selection (project-relative, ';' as separator)
false // false = only listed elements, not recursive
);
// Export entire POUs folder recursively
exportInterface.PlcOpenExport2(
@"C:\Export\AllPOUs.xml",
"POUs",
true // true = entire subtree including methods, actions, transitions, properties
);
PowerShell sample
The PowerShell sample calls PlcOpenExport2 with the same three parameters as the C# sample.
$plcProject = $sysManager.LookupTreeItem(@"TIPC^PLC1^PLC1 Project");
# Export a single POU
$plcProject.PlcOpenExport2(
'C:\Export\MyProg.xml', # output file
'POUs.MyProg', # selection (project-relative,';' as separator)
$false # false = only listed elements, not recursive
);
# Export selected POUs
$plcProject.PlcOpenExport2(
'C:\Export\MyProg.xml',# output file
'POUs.MyProg;DUTs.MyDUT',# selection (project-relative,';' as separator)
$false # false = only listed elements, not recursive
);
# Export entire POUs folder recursively
$plcProject.PlcOpenExport2(
'C:\Export\AllPOUs.xml',
'POUs',
$true # true = entire subtree including methods, actions, transitions, properties
);