How to add a pen to a Data Analysis Trend using a script

Introduction

Hello and welcome to a new guide on how to best use Movicon.NExT™.

In this technote, we are going to create an example to show you how to add and change a pen in the Data Analysis Trend object using a script.

Example

In this example we shall define three tags to link to the Demo driver offered by the Movicon.NExT platform.
These three tags will be added to the trends using a script associated with a button.
Each trend will have their own screen with buttons and Display objects to display the tag’s value in realtime.

How to insert and configure the driver

After having started up the Movicon.NExT editor and created, or loaded, a project, insert the Demo driver by following these steps below:

How to create the tag

Now that the driver has been added, we shall proceed with creating the tags that will be needed for simulation purposes:

Once the Counter tag has been created, create another two tags, and configure them in this way:

Name Random
Model Type Variable
Data Type Int32
Demo Type Random
Minimum Value 0
Maximum Value 100
Station Station0

Name Triangle
model Type Variable
Data Type Int32
Demo Type Triangular Wave/Up Down counter
Minimum Value 0
Maximum Value 100
Delta Value 1
Station Station0

When you have finished configuring the tags, click on the Save All icon located on the Movicon.NExT editor’s toolbar at the top to save the project.

How to create the Data Logger

Now that the driver and tags have been instantiated, we can go ahead and define the Data Logger to assign the tags to. To do this, open the I/O Data Server:

Assign the Counter, Random and Triangle tags to the Data Logger you have just created:

How to create the screen

Now, we will need to create a screen:

Once the screen has been created, you can then change the project's properties by inserting the screen as the project's Main screen which will show when the project starts up. To do this:

Once you have finished configuring the project startup page, click on the Save All icon located on the toolbar at the top to save the project.

Now, open the Main screen and click on the Toolbox tab, which is located on the far right of the editor window, next to the properties window, to open the Toolbox library provided by Movicon.NExT.
Drag three Display objects, three Standard Metro Style button, and a Data Analysis Trend object on screen. These are found in the Display, Buttons\Standard and Trends folders, respectively.

Assign the Counter tag to one of the Display objects in this way:

Repeat this procedure to assign the Random and Triangle tags to the other two object.

Click on the Save All icon located on the Movicon.NExT editor’s toolbar at the top to save the project.

Data Analysis Trend

At this point, we can configure one of the buttons by changing its Text property with the Set Counter as Pen string. Once you have entered this string, confirm the change, and make sure the button is still selected and open the Script tab. Once the Script tab opens, insert the reference to the DataAnalisysRTControl.dll. To do this:

Now that the reference to the Data Analysis Trend dll has been inserted, copy and paste the following script in the button:

'#Language "WWB.NET"

'This script use reference: <Movicon.NExT Folder>\Toolbox\DataAnalisysRTControl.dll
'Script to insert into a button
Public Sub Control_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Control.Click

    Dim myInterval As System.TimeSpan
    Dim fElement As System.Windows.FrameworkElement
    Dim daControl As DataAnalisysRTControl.DataAnalisysRT
    Dim newPen As DataAnalisysRTControl.SerieData
    Dim DataLoggerOrHistorianName As String
    Dim UseDataLogger As Boolean
    Dim DBColumnName As String
    Dim tagName As String
    Dim penName As String
    Dim penType As Integer
    Dim myScaleMin As Double
    Dim myScaleMax As Double
    Dim i As Integer

    Try

         fElement = Document.MapScreenEntities("DataAnalysisRT").Element
         daControl = TryCast(fElement, DataAnalisysRTControl.DataAnalisysRT)

         'Remove all pens from the trend
         If daControl.GetPensNumber > 0 Then
         For i = 0 To daControl.GetPensNumber
              daControl.RemovePen(i)
         DoEvents
         Next
    End If

    'Initializing pen
    DataLoggerOrHistorianName = "DL_1sec"
    UseDataLogger = True
    tagName = "Counter"
    DBColumnName = "Counter"
    penName = "Counter"
    penType = 1
    myScaleMin = -1
    myScaleMax = 101

    'Add pen
    newPen = daControl.AddPen(penName,tagName,UseDataLogger, System.Windows.Media.Color.FromRgb(255,0,0), DataLoggerOrHistorianName, myScaleMin, myScaleMax, False, penType)

    'Specify how to identify the tag into the database (mandatory!)
     newPen.tagName = DBColumnName

    'Update pen properties
    If Not IsNothing(newPen) Then
         newPen.thickness = 2
         newPen.showaxis = True
         newPen.isVisible = True
    daControl.ReloadPens()
    End If

    'Set Time interval
    myInterval = New System.TimeSpan(0,1,0)
    daControl.SetTimeRange(myInterval)
    daControl.ReloadData()

    Catch ex As System.Exception
         MsgBox(ex.Message)
    Finally
         daControl = Nothing
         fElement = Nothing
    End Try

End Sub

After this has been done, the code will delete all the Trend’s pens, if any, and then add the Counter tag alone with its parameters.
Repeat this procedure for the other two buttons by changing the text to Set Random as Pen and Set Triangle as Pen. Also change the tag reference in the script from Counter to Random and Triangle respectively.

Once the buttons have been configured, select the Data Analysis Trend object, and change its name to DataAnalysisRT. To do this, select the object and:

Runtime Test

Test everything out in runtime mode by clicking on the Start Runtime icon.
Once the application is started and the synoptic appears, and as soon as the project has been fully loaded and the driver started working, click on the Set Pen buttons to assign the specific pen to the trend.