Skip to content

First steps with the TwinCAT HMI Automation Interface

As you already know, TwinCAT itself can be completly controlled by use of an automation interface and enables one to create and manipulate TwinCAT XAE configurations via programming / scripting code [Link to Beckhoff, Link to Documentation (PDF)]. TwinCAT HMI provides similar entry points to create / manage HMI projects.

To come in touch with the TwinCAT HMI Automation Interface it is only required to have an installed VisualStudio 2017/2019 or TwinCAT XAE Shell with the latest TwinCAT HMI [Link to Product]

Open VisualStudio, create a new HMI-project and open NuGet’ Package Management Console:

In case if you are not familar with the DTE interface of VisualStudio, just check this Microsoft documentation [Link]. But I have to admit that the TwinCAT HMI Automation Interface is NOT totally based on that DTE architecture. The TwinCAT HMI Automation Interface USES the DTE but it is NOT BASED on the DTE.

In this post we just try to show how the TwinCAT HMI Automation Interface can be retrieved with less code.

One big advantage of the opened “Package Manager Console” is, that the memory map of VisualStudio and it’s loaded solution / project is injected into the console and can be used and modified. Just enter the following small piece of code:

$dte.solution

As you can see, the HMI project is listed and can be used / modified with that $dte variable. All available projects within the solution will be listed. In case more projects are added, you have to select your individual project, e.g. $dte.solution.Projects[1]. As well, if you like to work with a specific project, you have to use that accessor. For example, iterate over all first-level project items just use:

Foreach($p in $dte.solution.projects[1].ProjectItems) { $p.Name }

Query TwinCAT HMI Automation Interface…

Based on the previous information, enter the following command and as a result you will get the concrete interface instance to work with HMI projects:

([TcHmiAutomation.ITcHmiAutomation]$dte.GetObject("Beckhoff.TcHmi.1.12")).GetType()

“Beckhoff.TcHmi.1.12” is the automation identifier for which the TwinCAT HMI Automation Interface is registered for. Just check other examples on GitHub [Link to some search results]. GetObject()is used to obtain properties and methods within the DTE memory foodprint [Link to Microsoft documentation]. [TcHmiAutomation.ITcHmiAutomation]is a type cast to the concrete TwinCAT HMI Automation Interface which is available after TwinCAT HMI installation and is directly available within this scenario (because the console is loaded and embedded in VisualStudio, for other scenarios the handling is slightly different, check later Blog posts).

To get more information about that interface:

([TcHmiAutomation.ITcHmiAutomation]$dte.GetObject("Beckhoff.TcHmi.1.12")) | Get-Member

Query TcHmiAutomation.ITcHmiProject instance…

For working with a specific HMI project, query an interface for that:

([TcHmiAutomation.ITcHmiAutomation]$dte.GetObject("Beckhoff.TcHmi.1.12")).GetHmiProject("TcHmiProject1") | Get-Member

As a result you will get a large list of methods and properties which are free for use (short form):

Name                          MemberType Definition                                                                                      
----                          ---------- ----------                                                                                      
[[shortted]]
AddView                       Method     TcHmiAutomation.ITcHmiFile
Build                         Method     bool Build(string solutionConfiguration, bool updateUi)
Clean                         Method     bool Clean(bool updateUi)                              
GetFileInstance               Method     TcHmiAutomation.ITcHmiFile GetFileInstance()                                                    
GetFilePermissionInstance     Method     TcHmiAutomation.ITcHmiFilePermission GetFilePermissionInstance()
GetHmiFile                    Method     TcHmiAutomation.ITcHmiFile GetHmiFile(string relativePath)                                      
[[shortted]]

Few more examples…

Build a HMI project:

PM> ([TcHmiAutomation.ITcHmiAutomation]$dte.GetObject("Beckhoff.TcHmi.1.12")).GetHmiProject("TcHmiProject1").Build()
True
PM>

Add a new view:

PM> $view = ([TcHmiAutomation.ITcHmiAutomation]$dte.GetObject("Beckhoff.TcHmi.1.12")).GetHmiProject("TcHmiProject1").AddView("HelloWorld")
PM> $view.Name
HelloWorld.view
PM> 

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.