Creating and handling C++ projects and modules
This chapter explains in-depth how to create, access and handle TwinCAT C++ projects. The following list shows all chapters in this article:
- General information about C++ projects
- Creating new C++ projects
- Creating new module within a C++ project
- Opening existing C++ projects
- Creating module instances
- Calling TMC Code Generator
- Calling Publish Modules command
- Setting C++ Project Properties
- Building project
General information about C++ projects
C++ projects are specified by their so-called project template, which are used by the “TwinCAT C++ Project Wizard”. Inside a project multiple modules could be defined by module templates, which are used by the “TwinCAT Class Wizard”.
TwinCAT-defined templates are documented in the Section C++ / Wizards.
The customer could define own templates, which is documented at the corresponding sub-section if C++ Section / Wizards.
Create C++ projects
To create a new C++ project using the Automation Interface, you must navigate to the C++ node and then execute the CreateChild() method with the appropriate template file as a parameter.
Code snippet (C#):
ITcSmTreeItem cpp = systemManager.LookupTreeItem("TIXC");
ITcSmTreeItem cppProject = cpp.CreateChild("NewCppProject", 0, "", pathToTemplateFile);
Code snippet (Powershell):
$cpp = $systemManager.LookupTreeItem("TIXC")
$newProject = $cpp.CreateChild("NewCppProject", 0, "", $pathToTemplateFile)
To instantiate a driver project, use "TcVersionedDriverWizard" as pathToTemplateFile.
Creating new module within a C++ project
Within a C++ project usually a TwinCAT Module Wizard is used to let the wizard create a module by a template.
Code snippet (C#):
ITcSmTreeItem cppModule = cppProject.CreateChild("NewModule", 1, "", pathToTemplateFile);
Code snippet (Powershell):
$cppModule = $cppProject.CreateChild("NewModule", 0, "", $pathToTemplateFile);
As example for instantiating a Cyclic IO module project please use "TcModuleCyclicCallerWizard " as pathToTemplateFile.
Opening existing C++ projects
To open an existing C++-Project via Automation Interface, you need to navigate to the C++ node and then execute the CreateChild() method with the path to the corresponding C++ project file as a parameter.
You can use three different values as SubType:
- 0: Copy project to solution directory
- 1: Move project to solution directory
- 2: Use original project location (specify “” as NameOfProject parameter)
Basically, these values represent the functionalities (Yes, No, Cancel) from the following MessageBox in TwinCAT XAE:
In place of the template file you need to use the path to the C++ project (to its vcxproj file) that needs to be added. As an alternative, you can also use a C++ project archive (tczip file).
Code snippet (C#):
ITcSmTreeItem cpp = systemManager.LookupTreeItem("TIXC");
ITcSmTreeItem newProject = cpp.CreateChild("NameOfProject", 1, "", pathToProjectOrTczipFile);
Code snippet (Powershell):
$cpp = $systemManager.LookupTreeItem("TIXC")
$newProject = $cpp.CreateChild("NameOfProject", 1, "", $pathToProjectOrTczipFile)
Please note that C++ projects can’t be renamed, thus the original project name needs to be specified. (cmp. Renaming TwinCAT C++ projects)
Creating module instances
TcCOM Modules could be created at the System -> TcCOM Modules node. Please see documentation there.
The same procedure could also be applied to the C++ project node to add TcCOM instances at that place ($newProject
at the code on top of this page.).
Calling TMC Code Generator
TMC Code generator could be called to generate C++ code after changes at the TMC file of the C++ project.
Code snippet (C#):
string startTmcCodeGenerator = @"<?xml version=""1.0"" encoding=""UTF-16""?>
<TreeItem>
<CppProjectDef>
<Methods>
<StartTmcCodeGenerator>
<Active>true</Active>
</StartTmcCodeGenerator>
</Methods>
</CppProjectDef>
</TreeItem>";
cppProject.ConsumeXml(startTmcCodeGenerator);
Code snippet (Powershell):
$startTmcCodeGenerator = @"<?xml version=""1.0"" encoding=""UTF-16""?>
<TreeItem>
<CppProjectDef>
<Methods>
<StartTmcCodeGenerator>
<Active>true</Active>
</StartTmcCodeGenerator>
</Methods>
</CppProjectDef>
</TreeItem>"
$cppProject.ConsumeXml($startTmcCodeGenerator)
Calling Publish Modules command
Publishing includes building the project for all platforms. The compiled module will be provided for Export like described in the Module-Handling section of C++.
Code snippet (C#):
string publishModules = @"<?xml version=""1.0"" encoding=""UTF-16""?>
<TreeItem>
<CppProjectDef>
<Methods>
<PublishModules>
<Active>true</Active>
</PublishModules>
</Methods>
</CppProjectDef>
</TreeItem>";
cppProject.ConsumeXml(publishModules);
Code snippet (Powershell):
$publishModules = @"<?xml version=""1.0"" encoding=""UTF-16""?>
<TreeItem>
<CppProjectDef>
<Methods>
<PublishModules>
<Active>true</Active>
</PublishModules>
</Methods>
</CppProjectDef>
</TreeItem>"
$cppProject.ConsumeXml($publishModules)
Setting C++ Project Properties
C++ projects provide different options for the build and deployment process.
These are settable by the Automation Interface.
Code snippet (C#):
string projProps = @"<?xml version=""1.0"" encoding=""UTF-16""?>
<TreeItem>
<CppProjectDef>
<BootProjectEncryption>Target</BootProjectEncryption>
<TargetArchiveSettings>
<SaveProjectSources>false</SaveProjectSources>
</TargetArchiveSettings>
<FileArchiveSettings>
<SaveProjectSources>false</SaveProjectSources>
</FileArchiveSettings>
</CppProjectDef>
</TreeItem>";
cppProject.ConsumeXml(projProps);
Code snippet (Powershell):
$projProps = @"<?xml version=""1.0"" encoding=""UTF-16""?>
<TreeItem>
<CppProjectDef>
<BootProjectEncryption>Target</BootProjectEncryption>
<TargetArchiveSettings>
<SaveProjectSources>false</SaveProjectSources>
</TargetArchiveSettings>
<FileArchiveSettings>
<SaveProjectSources>false</SaveProjectSources>
</FileArchiveSettings>
</CppProjectDef>
</TreeItem>"
$cppProject.ConsumeXml($projProps)
For the BootProjectEncryption the values “None” and “Target” are valid.
Both other settings are “false” and “true” values.
Building project
To build the project or solution you can use the corresponding classes and methods of the Visual Studio API, which are documented here.