ModifyAttributes Method (PIUnitBatch object)

             

 

The ModifyAttributes method changes the values of the passed attributes in the corresponding object on the Server.  The method provides a more efficient method to make multiple modifications then setting the attributes individually. Desired modifications are specified in the passed NamedValues collection where the each name is a property name and the value contains the new property value.

 

Syntax

object.ModifyAttributes nvsAttributes, hnvsErrors

  

The ModifyAttributes method syntax has these parts:

Part

Description

object

An object expression that evaluates to a PIUnitBatch.

nvsAttributes

NamedValues collection of attribute names and new values used to modify the object.

hnvsErrors

NamedValues collection of errors returned if there are problems.

 

Trappable Errors

In addition to generic errors (such as Out of Memory), the following errors may occur:

Error

Description

pseMODIFYATTRIBUTES

There was an error with ModifyAttributes. Check the errors (hnvsErrors) collection

 

Errors Collection Errors

Here are some of the errors that can be returned in the errors collection (hnvsErrors). The name of the NamedValue in the collection is the attribute name passed. The value is the error code:

Error

Description

pseATTRIBUTENOTVALID

The attribute name passed wasn't valid.

pseINVALIDNAME

Name of the object is invalid.

 

Example

Here is an example demonstrating how to use ModifyAttributes. To build this example:

1. Create a new Visual Basic project and add a reference to the PISDK Library.

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

 

'
' This program adds a PIUnitBatch with a PISubBatch to the module database.
' Then it adds a PIBatch to the PIBatch database.
' Then it adds a PITransferRecord to the PITransferRecord database with the PIBatch
' and the PIUnitBatch as its source and destination.
' Then it uses ModifyAttributes to modify each of them.
'
Option Explicit
Dim g_UniqueNameCounter As Long

Private Sub Form_Load()
Dim piServer As Server
Dim myUnit As PIModule
Dim myHeadingSet As PIHeadingSet
Dim myHeading1 As PIHeading
Dim myHeading2 As PIHeading
Dim myBatch As PIBatch
Dim myUnitBatch As PIUnitBatch
Dim mySubBatch As PISubBatch
Dim myTxRec As PITransferRecord
Dim nameStr As String
Dim batchIDStr As String
Dim proceedureNameStr As String
Dim productStr As String
Dim recipeStr As String
Dim startTimeStr As String
Dim endTimeStr As String
Dim sourceStr As String
Dim propStr As String
Dim subPropStr As String
Dim valStr As String
Dim destStr As String
Dim headingStr As String
Dim nvsModAttr As NamedValues
Dim nvsErrors As NamedValues
Dim startTime As New PITime
Dim endTime As New PITime
Dim nvsPropData As NamedValues
Dim nvsProperties As NamedValues
Dim nvsSubPropData As NamedValues
Dim nvsSubProperties As NamedValues
'
' Intialize unique name counter
'
g_UniqueNameCounter = 1 ' intialize unique name counter
'
' Get the default server
'
Set piServer = PISDK.Servers.DefaultServer
'
' Add a module to the root collection with a unique name
'
Set myUnit = piServer.PIModuleDB.PIModules.Add(GenUniqueName())
myUnit.IsPIUnit = True ' make it a unit so we can add unit batches to it
'
' Add a PIHeadingSet and two PIHeadings so we'll have them for the PISubBatch
'
Set myHeadingSet = piServer.PIModuleDB.PIHeadingSets.Add(GenUniqueName(), "PISubBatch HeadingSet")
'
' Add two PIHeadings to the PIHeadingSet. One for when we add the PISubBatch and another
' to use when we modify it.
'
Set myHeading1 = myHeadingSet.PIHeadings.Add(GenUniqueName(), "Heading 1", 1)
Set myHeading2 = myHeadingSet.PIHeadings.Add(GenUniqueName(), "Heading 2", 2)
'
' Add a PIBatch with a unique name
'
endTime.SetToCurrent
startTime.UTCSeconds = endTime.UTCSeconds - 1 ' 1 second batch
Set myBatch = piServer.PIBatchDB.Add(GenUniqueName, "Product", "Recipe", startTime, endTime)
'
' Add a PIUnitBatch with a unique name. Use the same start and end time as the PIBatch
'
Set myUnitBatch = myUnit.AddPIUnitBatch(GenUniqueName(), "Product", startTime, endTime)
'
' Add a PISubBatch to the PIUnitBatch. Use the same start and end time as the PIBatch
'
Set mySubBatch = myUnitBatch.PISubBatches.Add(GenUniqueName(), myHeading1, startTime, endTime)
'
' Add a PITransferRecord. Use the same start and end time as the PIBatch
'
Set myTxRec = piServer.PIBatchDB.PITransferRecordDB.Add(myBatch, myUnitBatch, startTime, endTime)
'
' Get ModifyAttributes strings once
'
nameStr = piServer.PIModuleDB.AttributeName(mbaName)
batchIDStr = piServer.PIModuleDB.AttributeName(mbaBatchID)
proceedureNameStr = piServer.PIModuleDB.AttributeName(mbaProcedureName)
productStr = piServer.PIModuleDB.AttributeName(mbaProduct)
recipeStr = piServer.PIModuleDB.AttributeName(mbaRecipe)
startTimeStr = piServer.PIModuleDB.AttributeName(mbaStartTime)
endTimeStr = piServer.PIModuleDB.AttributeName(mbaEndTime)
propStr = piServer.PIModuleDB.AttributeName(mbaPIProperties) ' both of these use the same atrribute name
subPropStr = piServer.PIModuleDB.AttributeName(mbaPIProperties) ' both of these use the same atrribute name
valStr = piServer.PIModuleDB.AttributeName(mbaValue)
headingStr = piServer.PIModuleDB.AttributeName(mbaPIHeading)
sourceStr = piServer.PIModuleDB.AttributeName(mbaSource)
destStr = piServer.PIModuleDB.AttributeName(mbaDestination)
'
' Change the start and end times
'
startTime.UTCSeconds = startTime.UTCSeconds + 1
endTime.UTCSeconds = endTime.UTCSeconds + 1
'
' Modify the PIBatch, including adding some PIProperty's
'
Set nvsModAttr = New NamedValues
nvsModAttr.Add batchIDStr, ("New Batch ID")
nvsModAttr.Add productStr, "New Product"
nvsModAttr.Add recipeStr, "New Recipe"
nvsModAttr.Add startTimeStr, startTime
nvsModAttr.Add endTimeStr, endTime
Set nvsProperties = New NamedValues ' this NamedValues collection will hold all root properties
Set nvsPropData = New NamedValues
nvsPropData.Add valStr, 1
nvsProperties.Add "Prop1 No SubProps", nvsPropData ' first property doesn't have any sub properties

Set nvsPropData = New NamedValues
nvsPropData.Add valStr, 2
Set nvsSubPropData = New NamedValues
nvsSubPropData.Add valStr, 3
Set nvsSubProperties = New NamedValues
nvsSubProperties.Add "SubProp3", nvsSubPropData
Set nvsSubPropData = New NamedValues
nvsSubPropData.Add valStr, 4
nvsSubProperties.Add "SubProp4", nvsSubPropData

nvsPropData.Add subPropStr, nvsSubProperties
nvsProperties.Add "Prop2 with SubProps", nvsPropData ' second property has a 2 sub properties
nvsModAttr.Add propStr, nvsProperties
'
' Display the BatchID before and after the edit. If you want to look at all the parameters
' before and after, then run this program in the debugger and put the object in the watch window
'
MsgBox "PIBatch BatchID before ModifyAttributes: " & myBatch.BatchID, vbOKOnly, "PIBatch"
myBatch.ModifyAttributes nvsModAttr, nvsErrors
MsgBox "PIBatch BatchID after ModifyAttributes: " & myBatch.BatchID, vbOKOnly, "PIBatch"
'
' Modify the UnitBatch
'
Set nvsModAttr = New NamedValues
nvsModAttr.Add batchIDStr, ("New UnitBatch ID")
nvsModAttr.Add productStr, "New Product"
nvsModAttr.Add proceedureNameStr, "Procedure Name 1"
nvsModAttr.Add startTimeStr, startTime
nvsModAttr.Add endTimeStr, endTime
'
' Display the BatchID before and after the edit. If you want to look at all the parameters
' before and after, then run this program in the debugger and put the object in the watch window
'
MsgBox "PIUnitBatch BatchID before ModifyAttributes: " & myUnitBatch.BatchID, vbOKOnly, "PIUnitBatch"
myUnitBatch.ModifyAttributes nvsModAttr, nvsErrors
MsgBox "PIUnitBatch BatchID after ModifyAttributes: " & myUnitBatch.BatchID, vbOKOnly, "PIUnitBatch"
'
' Modify the SubBatch
'
Set nvsModAttr = New NamedValues
nvsModAttr.Add nameStr, ("New SubBatch Name")
nvsModAttr.Add headingStr, myHeading2 ' change the PIHeading for this SubBatch
nvsModAttr.Add startTimeStr, startTime
nvsModAttr.Add endTimeStr, endTime
'
' Display the name before and after the edit. If you want to look at all the parameters
' before and after, then run this program in the debugger and put the object in the watch window
'
MsgBox "PISubBatch Name before ModifyAttributes: " & mySubBatch.Name, vbOKOnly, "PISubBatch"
mySubBatch.ModifyAttributes nvsModAttr, nvsErrors
MsgBox "PISubBatch Name after ModifyAttributes: " & mySubBatch.Name, vbOKOnly, "PISubBatch"
'
' Modify the TransferRecord
'
Set nvsModAttr = New NamedValues
nvsModAttr.Add sourceStr, myUnitBatch ' swap source and destination
nvsModAttr.Add destStr, myBatch
nvsModAttr.Add startTimeStr, startTime
nvsModAttr.Add endTimeStr, endTime
'
' Display the source object type before and after the edit.
' If you want to look at all the parameters before and after, then run this program in the
' debugger and put the object in the watch window
'
MsgBox "Source before ModifyAttributes: " & TypeName(myTxRec.Source), vbOKOnly, "PITransferRecord"
myTxRec.ModifyAttributes nvsModAttr, nvsErrors
MsgBox "Source after ModifyAttributes: " & TypeName(myTxRec.Source), vbOKOnly, "PITransferRecord"

End Sub

Function GenUniqueName() As String
'
' This function generates a unique name to avoid name collisions
'
Dim curTim As New PITime
Dim curDate As Date

curTim.SetToCurrent
curDate = curTim.LocalDate
GenUniqueName = Format(curDate, "yymmddhhmmss") & "-" & g_UniqueNameCounter
g_UniqueNameCounter = g_UniqueNameCounter + 1 ' increment this to make sure we get unique names

End Function


 

Enabling Operational Intelligence