Creating and handling TwinCAT Analytics projects

The TwinCAT Automation Interface provides methods and properties to create and access TwinCAT Measurement projects. The following chapter describes how some basic tasks can be solved with a TwinCAT project and includes information on the following topics:

Creating a TwinCAT Measurement project

TwinCAT Measurement is a global "container" that can host one or more measurement projects, e.g. a TwinCAT Scope configuration. Similar to a regular TwinCAT configuration, each project is first described with the aid of a template file. This template file is used when adding a new project to the solution, which can be done by calling the AddFromTemplate() method from the Visual Studio DTE object. Note that this procedure is the same when adding a regular TwinCAT project. The following code snippet assumes that you have already acquired a DTE instance and created a Visual Studio solution, as shown in our article about accessing TwinCAT configurations. A reference to the DTE instance is stored in the "dte" object.

EnvDTE.Project scopeProject = dte.Solution.AddFromTemplate(template, destination, name);

Creating, accessing and handling a TwinCAT Analytics configuration

A TwinCAT Analytics project represents an analysis configuration. This means that all elements inserted into the project are subject to the same analysis settings. You can add an analytics project via the Automation Interface by specifying the appropriate "TwinCAT Analytics Project" template when adding a project using the AddFromTemplate() method, as described above.

The EnvDTE.Project element thus added can then be mapped to the IMeasurementAnalyticsProject interface, which provides the following methods:

int StartAnalytics()

Starts the analysis process.

int StopAnalytics();

Stops the analysis process.

int AddReferencedScope();

Adds an instance of a TwinCAT Scope project to the solution and links the analytics elements.

The TwinCAT Scope instance can be adapted with the IMeasurementScope interface via Automation Interface.

int ChangeName(string name);

Method of changing the project name.

int ShowControl();

Brings the Microsoft Visual Studio© Editor to the foreground to display the Analytics project.

int CloseControl();

Closes the editor.

int GetAvailableModules(out Hashtable modules);

Fills a hash table with the data from the analytics engine modules provided.

The hash table might look like this:

{02040109-0000-0000-f000-000000000064} "Min Max Avg 1Ch"

{02040103-0000-0000-f000-000000000064} "Edge Counter OnOff 2Ch"

{02040102-0000-0000-f000-000000000064} "Edge Counter OnOff 1Ch"

{02040101-0000-0000-f000-000000000064} "Edge Counter 1Ch"

The GUIDs are necessary for later generation of the analysis functions.

int AddNetwork(out ProjectItem item, string name = "");

Adds a network to the Analytics project and issues the instance of the EnvDTE.ProjectItem with which you can continue.

The object EnvDTE.ProjectItem.Object can be mapped to the IMeasurementAnalyticsNetwork interface.

Creating, accessing and handling networks

A TwinCAT Analytics network represents the level at which function blocks are created, managed and visualized.

A network can contain multiple instances of different functions, as well as other networks as so-called sub-networks.

Each network is represented within a Microsoft Visual Studio© Editor instance, so that the internal docking mechanisms can be used to simultaneously represent different networks.

The IMeasurementAnalyticsNetwork interface defines the following methods:

int ShowControl();

Brings the Microsoft Visual Studio© Editor to the foreground to display the Analytics network.

int CloseControl();

Closes the editor.

int AddFunction(out ProjectItem item, Guid guid, string name = "");

Adds an analysis function to the network.

The function is determined by the corresponding GUID, which can be selected via the interface method IMeasurementAnalyticsProject.GetAvailableModules(out Hashtable modules).

The instance of EnvDTE.ProjectItem.Object can be mapped to the IMeasurementAnalyticsFunction interface.

int AddNetwork(out ProjectItem item, string name = "");

The object EnvDTE.ProjectItem.Object can be mapped to the IMeasurementAnalyticsNetwork interface.

int ChangeName(string name);

Changes the name of the network.

int AddNetworkTemplate(out ProjectItem item, string path);

Adds a network from a template. The template must be specified at file level.

The object EnvDTE.ProjectItem.Object can be mapped to the IMeasurementAnalyticsNetwork interface.

Creating, accessing and handling functions

A TwinCAT Analytics Function represents the level of the analysis function.

Each function has its own input and output variables as well as various configuration parameters.

The IMeasurementAnalyticsFunction interface defines the following methods:

int ShowControl();

Brings the Microsoft Visual Studio© Editor to the foreground to display the Analytics project.

int CloseControl();

Closes the editor.

int ChangeName(string name);

Changes the name of the function.

int SetInputVariable(string input, int inputIndex);

Sets the input variable to the specified index of the function on the input string.

The input string must be in XML format as an example from the TargetBrowser.

TargetBrowserExportInfo

To obtain the XML-formatted string, you can simply drag and drop the desired variable from the target browser into a text editor.

Please note that the text editor must be started as an administrator if the target browser host (e.g. Visual Studio) has also been started as an administrator.

No interface is required to adjust the parameters of the function.

To do this, you can simply search the EnvDTE-Properties list of the EnvDTE.ProjectItem.Properties object for the EnvDTE.Property and set its value.