7 Replies Latest reply on Nov 12, 2015 5:12 AM by 99anger

    How do I create an adaptive trend by clicking on a value?

    kevinhelec

      Hi Guys,

       

      Newbie with VBA here. I'm trying to create a display with multiple tag values that I would like to populate in a static trend (in the bottom right hand corner). The code would have to clear the previous data before populating with the newly clicked value trend. Also will the option to ctrl-click multiple values to trend would be great. I've tried a few different bits of code but none successful.

       

      Cheers

        • Re: How do I create an adaptive trend by clicking on a value?
          bshang

          Hi Kevin,

           

          The document below may be helpful.

          PI ProcessBook VBA Scripts: Click Value Trend

           

          Attached is also a sample PDI that does something similar. You'll need to reset to another server and tags. It will show a trend of the tag whose value symbol is selected. You can also select multiple value symbols to show more than one trace. Here is the VBA code for it.

          Dim MySymbols As SelectedSymbols
          Dim MySymbol As Symbol
          Dim MySymbolType As Long
          Dim PITagname As String
          Dim traceCount As Integer
          
          Sub AddTagsToTrend()
          
          'Remove traces
          
          traceCount = Trend1.traceCount
          For N = 1 To traceCount
          Trend1.RemoveTrace (1)
          Next N
          
          Set MySymbols = ThisDisplay.SelectedSymbols
          'Check to see how many are selected.
          If MySymbols.Count = 0 Then
          'If the count is zero, user clicked display background, do nothing.
          Exit Sub
          End If
          
          For N = 1 To MySymbols.Count
          'Attach to each individual symbol object.
          Set MySymbol = MySymbols.Item(N)
          'Get the symbols type, ie. Trend, Line, Value, etc..
          MySymbolType = MySymbol.Type
          
          'If symbol object is type 7, by definition its a value object.
          If MySymbolType = 7 Then
          
          'Get the tagname and trend it.
          PITagname = MySymbol.GetTagName(1)
          Trend1.AddTrace (PITagname)
          
          End If
          Next N
          
          End Sub
          
          Private Sub Value1_Click(ByVal lvarX As Long, ByVal lvarY As Long)
          Call AddTagsToTrend
          End Sub
          
          Private Sub Value2_Click(ByVal lvarX As Long, ByVal lvarY As Long)
          Call AddTagsToTrend
          End Sub
          
          Private Sub Value3_Click(ByVal lvarX As Long, ByVal lvarY As Long)
          Call AddTagsToTrend
          End Sub
          

           

           

          The following Youtube videos from Learning are also very helpful.

          OSIsoft: Introduction to ProcessBook scripting with VBA. v1.3 - YouTube

            • Re: How do I create an adaptive trend by clicking on a value?
              kevinhelec

              Thank you worked great! Just finally got the chance to try.

              • Re: How do I create an adaptive trend by clicking on a value?
                99anger

                Hello shang

                I have used your code in my prcessbook

                But when I click the value symbol, if there is already trace related to the symbol then error occure "specified tag already exists"

                I want to add trace unless existing trace of  symbol was been

                How to modify your code? Please

                  • Re: How do I create an adaptive trend by clicking on a value?
                    knightk

                    Try looping through each tag and check if the tag already exists (I don't know any other way of doing this because once the tags are added to a trend you need to refer to them by their ID within the trend, not tag name). You can then alter the code so if it already exists it removes the value from the trend for a neat show/hide feature.

                      • Re: How do I create an adaptive trend by clicking on a value?
                        99anger

                        Thanks

                        But when I tried to with next vba code, I get failure with same message "specified tag already exists"

                         

                        Private Sub Value1_Click(ByVal lvarX As Long, ByVal lvarY As Long) 

                        For n =1 to Trend1. TraceCount

                        If Trend1.GetTagName(n) <>Value1.GetTagName Then

                        Call AddTagsToTrend 

                        Else Exit Sub

                        End If

                        Next

                        End Sub 

                         

                        What's wrong?

                        And can you help me full code that you recommand

                          • Re: How do I create an adaptive trend by clicking on a value?
                            dng

                            Hi Lim,

                             

                            You can add a function to find out if the tag has already been added to the trend as follow (see TagExist function). Only add the tag to the trend if TagExist returns false.

                             

                            Dim MySymbols As SelectedSymbols
                            Dim MySymbol As Symbol
                            Dim MySymbolType As Long
                            Dim PITagname As String
                            Dim traceCount As Integer
                            
                            Sub AddTagsToTrend()
                            
                            'Remove traces
                            traceCount = Trend1.traceCount
                            For N = 1 To traceCount
                                Trend1.RemoveTrace (1)
                            Next N
                            
                            Set MySymbols = ThisDisplay.SelectedSymbols
                            'Check to see how many are selected.
                            If MySymbols.Count = 0 Then
                            'If the count is zero, user clicked display background, do nothing.
                            Exit Sub
                            End If
                            
                            For N = 1 To MySymbols.Count
                                'Attach to each individual symbol object.
                                Set MySymbol = MySymbols.Item(N)
                                'Get the symbols type, ie. Trend, Line, Value, etc..
                                MySymbolType = MySymbol.Type
                                
                                'If symbol object is type 7, by definition its a value object.
                                If MySymbolType = 7 Then
                                
                                    'Get the tagname and trend it.
                                    PITagname = MySymbol.GetTagName(1)
                                    
                                    If TagExist(PITagname) = False Then
                                        Trend1.AddTrace (PITagname)
                                    End If
                                
                                End If
                            Next N
                            End Sub
                            
                            
                            Function TagExist(tagname As String) As Boolean
                                Dim IsAdded As Boolean
                                Dim i As Integer
                                IsAdded = False
                                i = 1
                                While i <= Trend1.traceCount And IsAdd = False
                                    If Trend1.GetTagName(i) = PITagname Then
                                        IsAdded = True
                                    End If
                                    i = i + 1
                                Wend
                                TagExist = IsAdded
                            End Function
                            
                            Private Sub Value1_Click(ByVal lvarX As Long, ByVal lvarY As Long)
                            Call AddTagsToTrend
                            End Sub
                            
                            Private Sub Value2_Click(ByVal lvarX As Long, ByVal lvarY As Long)
                            Call AddTagsToTrend
                            End Sub
                            
                            Private Sub Value3_Click(ByVal lvarX As Long, ByVal lvarY As Long)
                            Call AddTagsToTrend
                            End Sub