Skip to content

Create HMI-project programmatically by using EnvDTE

The information in this post are usable with:

  • VisualStudio 2013, 2015, 2017, 2019
  • TE2000-HMI-Engineering 1.10.*.* (Download) and future releases
  • Sources of this post: GitHub Repository

If you whish to create a HMI-project programmatically EnvDTE is the key for success. Keep in mind, this is just one way of several other possible approaches. In this post we only focus C# but other programming languages are usable as well.

Here, we will add a VisualStudio Command [Online] with the help of a VisualStudio Extension [Online]. The result is shown in the following screenshot. A new command to create a HMI-project is added to “File / New” named “Create HMI Project”.

In fact, programmatically we call the VisualStudio project creation process directly by using VisualStudio’s automation interface, called DTE interface (EnvDTE and EnvDTE.DTE). Nice introduction of this can be read here. Behind the scenes we just call AddFromTemplate(String, String, String, Boolean).

The only things you have to know are the project template’s category and project template’s name. For our goal in this post the following values are required and they map directly to the entries of the “New Project”-dialog as seen in the following screenshot.

public const string DefaultHmiTplCategory = "TwinCAT HMI";
public const string DefaultHmiProjectTplName = "TwinCAT HMI Project";

By clicking “Create HMI Project” as shown in the first screenshot, Execute(object sender, EventArgs e) is called, the template install directory is queried, and in the result a new HMI-project is created. The implementation of Execute is as easy as possible, any special error-handling is implemented in helper methods. Just have a look into the full implementation within the GitHub repository.

private void Execute(object sender, EventArgs e)
{
  ThreadHelper.ThrowIfNotOnUIThread();

  var dte = (DTE2)Package.GetGlobalService(typeof(SDTE));
  var projectBaseName = "HmiProject";
  var slnDirPath = @"C:\temp";

  EnsureSingleProject(dte, projectBaseName, slnDirPath, DefaultHmiProjectTplName);
}

To execute the command, load the solution CreateProjectWithDte.sln, click <F5> and check the new menu command.

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.