PISubBatches / PISubBatch / PISubBatchList Example

Option Explicit

Private Sub Command1_Click()
   Dim module As PIModule
   Dim startTime As New PITime
   Dim endTime As New PITime
   Dim unitBatch As PIUnitBatch
   Dim subBatch1 As PISubBatch
   Dim subBatch2 As PISubBatch
   Dim subSubBatch1 As PISubBatch
   Dim subSubBatch2 As PISubBatch
   Dim subHeading1 As PIHeading
   Dim subHeading2 As PIHeading
   Dim subHeadingSet1 As PIHeadingSet


   On Error Resume Next
   Set module = PISDK.Servers.DefaultServer.PIModuleDB.PIModules.Add("PISubBatchesTest")
   Set module = PISDK.Servers.DefaultServer.PIModuleDB.PIModules.Item("PISubBatchesTest")
   module.IsPIUnit = True  ' make it a unit
   Set subHeadingSet1 = PISDK.Servers.DefaultServer.PIModuleDB.PIHeadingSets.Add("PISubBatchesTest", "")
   Set subHeadingSet1 = PISDK.Servers.DefaultServer.PIModuleDB.PIHeadingSets.Item("PISubBatchesTest")
   Set subHeading1 = subHeadingSet1.PIHeadings.Add("SubHeading1", "Level1", 1)
   Set subHeading1 = subHeadingSet1.PIHeadings.Item("SubHeading1")
   Set subHeading2 = subHeadingSet1.PIHeadings.Add("SubHeading2", "Level2", 2)
   Set subHeading2 = subHeadingSet1.PIHeadings.Item("SubHeading2")
   On Error GoTo 0
   endTime.SetToCurrent
   startTime = endTime - 1    ' 1 second batch to avoid overlapping problems
   Set unitBatch = module.AddPIUnitBatch("UB1", "Product", startTime, endTime)
   Set subBatch1 = unitBatch.PISubBatches.Add("SB1", subHeading1, startTime, endTime)
   Set subBatch2 = subBatch1.PISubBatches.Add("SB1.1", subHeading2, startTime, endTime)
   Set subBatch1 = unitBatch.PISubBatches.Add("SB2", subHeading1, startTime, endTime)
   Set subBatch2 = subBatch1.PISubBatches.Add("SB2.1", subHeading2, startTime, endTime)
'
'  Load into ListBox
'
   List1.Clear
   LoadSubBatches unitBatch.PISubBatches

End Sub

Private Sub LoadSubBatches(SubBatches As PISubBatches)
   Dim tempStr As String
   Dim subBatch As PISubBatch
   Dim subBatchList As New PISubBatchList    ' test sub batch list

   For Each subBatch In SubBatches
      subBatchList.Insert subBatch  ' insert each item in the SubBatchList
      tempStr = subBatch.Name & " " & subBatch.PIHeading.Name & " " & subBatch.startTime.LocalDate _
               & " " & subBatch.endTime.LocalDate & " " & subBatch.UniqueID
      List1.AddItem tempStr
' Note the recursion. The subbatches collection will be empty when no more exist
      LoadSubBatches subBatch.PISubBatches
   Next subBatch
'
'  Verify that the count of SubBatches and subBatchList is the same
'
   If (SubBatches.Count <> subBatchList.Count) Then
      MsgBox "Count mismatch between PISubBatches collection and PISubBatchList collection."
   End If
End Sub
Enabling Operational Intelligence