3 Replies Latest reply on Dec 13, 2012 6:30 PM by mikeloria

    AF Attribute Min/Max in VBA code

    mikeloria

      I have a customer who wants to determine the min and max values for an AF attribute, given a timerange in VBA. The purpose is to use this for setting the trend scale. The customer does not wish to use autscale because he wishes to have a single scale for the trend. The user plans to look at all the mins and max's for each attribute that is added through VBA and use the value that best encomappases all.

       

      1) Can this be done without the COM AFSDK wrapper? We wish to avoid having to install and maintain the wrapper on multiple machines in multiple areas. 

       

      2) Is there an alternative?

       

      Thank you in advance for your considerations in this matter.

        • Re: AF Attribute Min/Max in VBA code
          Asle Frantzen

          Hi Mike

           

          Since I'm such a nice guy, here you go:

           
          Private WithEvents myCtxtHndlr As ContextHandler
          
          Private Sub Display_Open()
              Set myCtxtHndlr = Application.ContextHandlers("E")
          End Sub
          
          Private Sub myCtxtHndlr_ContextChanged(FromDisplay As Display, FromContextHandler As ContextHandler)
              
              Dim afTreePath As String
              afTreePath = myCtxtHndlr.CurrentContext(FromDisplay)  ' Context string
          
              Dim UpperValue As Variant
              Dim LowerValue As Variant
              
              UpperValue = CreateAFValue(afTreePath, "trend_upper")
              LowerValue = CreateAFValue(afTreePath, "trend_lower")
              
              Call Set_Trend_Scale(CDbl(LowerValue), CDbl(UpperValue), "*")
          End Sub
          
          
          Sub Set_Trend_Timerange(days As Double)
              ' The generic set time range routine. Number of days is
              ' passed to this routine. example a 10 comes "*-10d","*"
              
              Dim sym As Symbol
              For Each sym In Symbols
                  If sym.Type = 10 Then      'Trend symbol found, set time range.
                      sym.SetTimeRange "*-" & days & "d", "*"
                  End If
              Next
          
          End Sub
          
          Sub Set_Trend_Scale(lower As Double, upper As Double, trendname As String)
              ' Update trend scale
              ' scale = lower to upper
              ' trendname = name of trend or "*" for all trends
          
              Dim sym As Symbol
              Dim myTrend As trend
              
              For Each sym In Symbols
                  If sym.Type = 10 Then      'Trend symbol found, set time range.
                      Set myTrend = sym
                      If (trendname = "*" Or trendname = myTrend.TrendTitle) Then
                          Success = myTrend.SetTraceScale(lower, upper)
                      End If
                  End If
              Next
          End Sub
          
          
          Function CreateAFValue(ByVal myElement As String, ByVal myAttribute As String) As Variant
              
              ' Dimension variables.
              Dim oValue As Value
              Dim sElement As String
              Dim sAttribute As String
              Dim valueName As String
              Dim Found As Boolean
              Dim mySymbol As Long
              
              ' Initialize variables.
              Found = False
              sAttribute = myAttribute
              valueName = "AFVal_" + myAttribute
              sElement = myElement
          
              ' Search to see if our tag already exists in the processbook display
              For mySymbol = 1 To ThisDisplay.Symbols.Count Step 1
                  If ThisDisplay.Symbols(mySymbol).Name = valueName Then
                      Found = True
                  End If
              Next mySymbol
          
              
              Dim vrDate As Variant
              Dim vrStatus As Variant
              Dim MyValue As Variant
          
              ' If it exists, use it to get the value from PI. Else, create a new tag value object
              If (Found) Then
                  Set oValue = ThisDisplay.Symbols.Item(valueName)
                  oValue.SetTagName ("AF2." + sElement + "|" + sAttribute)
              Else
                  Set oValue = ThisDisplay.Symbols.Add(pbSymbolValue)
                  
                  With oValue
                      .Name = valueName
                         
                      'set the position, size and visible properties
                      .Top = 14950
                      .Left = -14700
                      .Width = 1000
                      .Height = 750
                      .Visible = False
                      
                      .SetTagName ("AF2." + sElement + "|" + sAttribute)
                  End With
              End If
          
              CreateAFValue = oValue.GetValue(vrDate, vrStatus)
          End Function
          

           

           

          (The snippet gets the max and min for the trend from AF attributes. If it's not exactly what you need you can probably use the code as a starting point)

          • Re: AF Attribute Min/Max in VBA code
            dhollebeek

            Autoscale works for trends even if they are set to single scale, but I'll assume it doesn't work the way they want it to.  If the attribute is already plotted on a trend, then you can do:

             
            Dim i as integer
            Dim value as variant
            Dim time as variant
            Dim status as variant
            
            MyTrend.CurrentTrace = 1
            for i = 1 to MyTrend.TraceValuesCount
              value = MyTrend.GetTraceValue i, time, status
              ' check for min or max value here ...
            next i
            

             And then set the trend scales accordingly.