IPointList2 Example

 

 

This example, when Run button is clicked, connects to the specified server, gets a PointList of at least 12 tags based on user defined criteria and  creates three (3) separate PointLists in various ways from the original list.

The form contains 2 user selectable items:

- Server (name, default = localhost)

- Tag search criteria ("where clause") that is passed to GetPoints call (this example only works with PI3 servers)

The ListBox in middle can be shown unsorted or sorted alphabetically (ascending/descending).
The TexBox below the ListBox shows the PointList index of the selected item (PIPoint). The index will change according to sort order (click an item to select it).

 

Build this Server example as follows:

1. Create a new project and add a reference to the PISDK and PISDKCommon libraries.

2. Add the visual elements as shown in the picture. Make sure the numbered element names (ListBoxes, TextBoxes) match those shown. Make sure to add the Run-button first so that its name will be Command1. Add an OptionButton and then make it a control array with three elements (top one in index 0).

3. Set the MultiLine property of the textbox "Text2" to True. The tag names in this box must be separated by linefeed.

3. Cut and paste the code below into the project.

 

Option Explicit

' Notes: this example uses 12 tags and relies on user entering
' a tagsearch search criteria that returns at least that many tags

Dim srv As Server ' server object
Dim tsPointList As PointList ' list returned by GetPoints
Dim pList As PointList ' Primary PointList
Dim i2Pl As IPointList2 ' IPointList2 interface for primary list
Dim pList2 As PointList ' secondary PointList
Dim i2Pl2 As IPointList2 ' IPointList2 interface for secondary list
Dim pList3 As PointList ' third PointList
Dim i2Pl3 As IPointList2 ' IPointList2 interface for third list

' Form is loading...set defaults
Private Sub Form_Load()
    Text1.Text = PISDK.Servers.DefaultServer.Name ' default server
    Text2.Text = "s*" ' default tagmask
End Sub

' Run button pressed.
' Create multiple pointlists in various ways
Private Sub Command1_Click()
    On Error GoTo err

    Set srv = PISDK.Servers(Text1.Text)
    srv.Open ' open server

    Set tsPointList = srv.GetPoints("tag='" & Text2.Text & "'") ' get some points
    If tsPointList Is Nothing Then GoTo notags
    If tsPointList.Count < 20 Then GoTo notags
    Set pList = New PointList ' always create a new lists
    Set pList2 = New PointList
    Set pList3 = New PointList

    Set i2Pl = pList ' IPointlist2 interfaces
    Set i2Pl2 = pList2
    Set i2Pl3 = pList3

    ' Create pointlists in various ways
    createList1
    createList2
    createList3
    ' make all views unsorted so we can see the real order
    updateList pList, List1 ' show list1
    updateList pList2, List2 ' show list2
    updateList pList3, List3 ' show list3
    Command2.Enabled = True ' enable remove button

    Exit Sub
notags:
    MsgBox "Need at least 12 tags from tagsearch - try with different tag mask"
    Exit Sub
err:
    MsgBox "Error in call:" & err.Description
End Sub

' Remove "List2" items from List3
Private Sub Command2_Click()
    On Error GoTo errhdlr
    i2Pl3.Remove pList2(1) ' remove PIPoint
    i2Pl3.Remove pList2(2).PathName ' remove PIPoint by path
    i2Pl3.Remove pList2(3) ' remove PIPoint

    updateList pList3, List3 ' update list3
    Command2.Enabled = False ' disable remove button
    Exit Sub
errhdlr:
    MsgBox "Remove failed, err=" & err.Number & ", " & err.Description
End Sub

' List2 item clicked. Get the list index of the item (IndexOf)
Private Sub List2_Click()
    Dim idxByPath As Long, idxByObj As Long
    Dim pPoint As PIPoint

    On Error GoTo errhdlr
    idxByPath = i2Pl2.IndexOf(List2.Text) ' get index by pathname
    Set pPoint = pList2.Item(List2.Text)
    idxByObj = i2Pl2.IndexOf(pPoint) ' get index by PIPoint obj
    If idxByPath <> idxByObj Then
        MsgBox "IndexOf failed"
    Else
        Text3.Text = idxByPath
    End If
    Exit Sub
errhdlr:
    MsgBox "IndexOf failed, err=" & err.Number & ", " & err.Description
End Sub

' Sorting for List2 has changed - update the list accordingly
Private Sub Option1_Click(index As Integer)
    If index = 0 Then
        i2Pl2.Sort soAscending
    ElseIf index = 1 Then
        i2Pl2.Sort soDescending
    Else
        i2Pl2.Sort soUnordered
    End If
    updateList pList2, List2 ' update the view
End Sub

' create list1 - this demostrates the use of addItems
Private Sub createList1()
    Dim pPoints(1) As Object
    Dim tagNames(1) As String

    On Error GoTo errhdlr
    i2Pl.AddItems tsPointList(1) ' add a single tag
    i2Pl.AddItems tsPointList(2).PathName ' add by path
    Set pPoints(0) = tsPointList(3)
    Set pPoints(1) = tsPointList(4)
    i2Pl.AddItems pPoints ' add array (of PIPoint objects)
    tagNames(0) = tsPointList(5).PathName
    tagNames(1) = tsPointList(6).PathName
    i2Pl.AddItems tagNames ' add array (of paths)
    Exit Sub
errhdlr:
    MsgBox "AddItems failed, err=" & err.Number & ", " & err.Description
End Sub
 

' Demonstrates the use of InsertAt and Sorting - list will be unsorted
Private Sub createList2()
    On Error GoTo errhdlr
    i2Pl2.Sorting = soUnordered ' set to unsorted so insertAt will work
    i2Pl2.InsertAt tsPointList(8), 1 ' insert first item
    i2Pl2.InsertAt tsPointList(9).PathName, 2 ' insert second item
    i2Pl2.InsertAt tsPointList(10), 1 ' make this the first item
    Option1(2).Value = True
    Exit Sub
errhdlr:
    MsgBox "InsertAt failed, err=" & err.Number & ", " & err.Description
End Sub

' demonstrates the use of adding a pointlist
' this is almost like a merge but will not accept dumplicate tags
Private Sub createList3()
    On Error GoTo errhdlr
    i2Pl3.AddItems tsPointList(11) ' add a single tag
    i2Pl3.AddItems tsPointList(12) ' add a single tag
    i2Pl3.AddItems pList2 ' add a pointlist
    Option1(2).Value = True
    Exit Sub
errhdlr:
    MsgBox "AddItems failed, err=" & err.Number & ", " & err.Description
End Sub

' Update a listbox with tagnames
Private Sub updateList(pList As PointList, list As ListBox)
    Dim lIndex As Integer

    list.Clear ' clear previous results
    For lIndex = 1 To pList.Count
        list.AddItem pList.Item(lIndex).PathName
    Next

End Sub

Enabling Operational Intelligence