2 Replies Latest reply on Mar 25, 2011 5:32 PM by dlunasin

    Code Finds & Replaces tags on all symbols EXCEPT XYPlot

    kpotter

      I have VBA code that does a find/replace on all references in a display.  For example, lets say you have Pump 91A, with several tags, all containing "91A" in their name.  The code can change every "91A" to "91B", or whatever replacement you need.  It also works on AF References (Yes, I know about AF Relative displays, which will make all of this obsolete, but I still need this.)  I'm relying on "SetTagName" which exists for every symbol EXCEPT XYPlot.  I submitted a suggestion to tech support to add this (they ultimately said they will) and they said I can code around it by reading each tag, adding the new tag, then removing the old.  They gave me the example below, but it's not preserving settings such as:  Multiple Y Scales, Data Retreval Methods, Plot Time.  If anyone has code that does this, that would be most awesome.  Thank you!

       

      Sub ChangeXYPlotTagDynamically()
       
      Dim xySym As XYPlot
      Dim xydef As XYDefinition
      Dim myXYtags As XYTags
      Dim Datasource As String
      Dim TagName As String
      Dim maxScale As Double
      Dim minScale As Double
      Dim maxConfig As pbMinMaxEnum
      Dim minConfig As pbMinMaxEnum
      Dim scaleCnt As Integer

      Set xySym = ThisDisplay.XYPlot1
      Set xydef = xySym.GetDefinition
      Set myXYtags = xydef.Tags
                     
      scaleCnt = myXYtags.Count 'STATIC VALUE!!!
                     
      For counter = 1 To myXYtags.Count
          'first tag in the collection will be an old tag each time through the loop

         
          'get tagname
          Datasource = myXYtags(1).TagName
          TagName = Datasource + "123" 'This temporarily replaces the FindReplace function
         
          'Used so when new tag is created it is set to the proper config
          Dim isXtag As Boolean
         
          isXtag = myXYtags(1).isXtag
        
         'Preserve the scale if set to absolute value
         'Max and min are enumerations
          maxConfig = myXYtags(counter).Max
          minConfig = myXYtags(counter).Min
         
          If (maxConfig = pbScaleAbsolute) Then
             maxScale = CDbl(myXYtags(counter).AbsoluteMax)
          End If
         
          If (minConfig = pbScaleAbsolute) Then
             minScale = CDbl(myXYtags(counter).AbsoluteMin)
          End If
         
          xydef.Tags.Add TagName, isXtag
         
          xydef.Tags.Remove (1)
         
          'Since the newly added tag will always be the last one in the list, set the scaleCnt to the Count of tags
          myXYtags(scaleCnt).SetScaleConfiguration False, minConfig, minScale, maxConfig, maxScale

      Next counter

      xySym.SetDefinition xydef
           
      End Sub

        • Re: Code Finds & Replaces tags on all symbols EXCEPT XYPlot
          dhollebeek

          The trick is to first loop through all the xyplot tags and collect all the tag's settings into a list so that you can re-apply them when you re-add the tags.  ERD does this, but that code is in C#, and I really can't post that code here anyway.  The settings you need to preserve are: TagName, IsXTag, RetrievalMethod, AutorangeMin, AutorangeMax, AbsoluteMin, AbsoluteMax, StartTime, EndTime, and TimeInterval.

            • Re: Code Finds & Replaces tags on all symbols EXCEPT XYPlot
              dlunasin

              Below is modified version of the code I originally sent. Hope it works for you.

               
              Sub ChangeXYPlotTagDynamically()
               
              Dim xySym As XYPlot
              Dim xydef As XYDefinition
              Dim myXYtags As XYTags
              Dim Datasource As String
              Dim TagName As String
              Dim isXtag As Boolean
              
              Dim maxScale As Double
              Dim minScale As Double
              Dim maxConfig As pbMinMaxEnum
              Dim minConfig As pbMinMaxEnum
              Dim startTime As String
              Dim endTime As String
              Dim interval As String
              Dim RetMethod As pbRetrievalMethod
              
              Dim tagIndex As Integer
              Dim scaleCnt As Integer
              
              Set xySym = ThisDisplay.XYPlot1
              Set xydef = xySym.GetDefinition
              Set myXYtags = xydef.Tags
                              
              scaleCnt = myXYtags.Count 'STATIC VALUE: this is used for the index of all newly added tags
              tagIndex = 1 'Static Value: so code can be reused for other purpose if needed
                              
              For counter = 1 To myXYtags.Count
                 
                  'get tagname
                  Datasource = myXYtags(tagIndex).TagName
                  TagName = Datasource + "123"
                  
                  'Preserve the scale and other configurations
                  'Used so when new tag is created it is set to the proper config
                  isXtag = myXYtags(tagIndex).isXtag
                  maxConfig = myXYtags(tagIndex).Max
                  minConfig = myXYtags(tagIndex).Min
                  startTime = myXYtags(tagIndex).startTime
                  endTime = myXYtags(tagIndex).endTime
                  interval = myXYtags(tagIndex).TimeInterval
                  RetMethod = myXYtags(tagIndex).RetrievalMethod
                         
                  '***Check case for scales***
                  If (maxConfig = pbScaleAbsolute) Then
                     maxScale = CDbl(myXYtags(tagIndex).AbsoluteMax)
                  End If
                  
                  If (minConfig = pbScaleAbsolute) Then
                     minScale = CDbl(myXYtags(tagIndex).AbsoluteMin)
                  End If
                  
                  If (maxConfig = pbScaleAutorange) Then
                      maxScale = Str(myXYtags(tagIndex).AutorangeMin)
                  End If
                  
                  If (minConfig = pbScaleAutorange) Then
                      minScale = Str(myXYtags(tagIndex).AutorangeMin)
                  End If
                      
                  '***Add the newly named tag***
                  xydef.Tags.Add TagName, isXtag
                  
                  '***Remove the old tag***
                  xydef.Tags.Remove (tagIndex)
                  
                  '***set the saved retrieval method***
                  myXYtags(scaleCnt).SetRetrievalMethod RetMethod
                      
                  '***newly added tag will always be the last one in the list***
                  myXYtags(scaleCnt).SetScaleConfiguration False, minConfig, minScale, maxConfig, maxScale
                  
                  '***set the timerange once based on the x tag***
                  If isXtag Then
                      myXYtags(scaleCnt).SetTimeRange False, startTime, endTime, interval
                  End If
                  
              Next counter
              
              '***save everything back to the symbol***
              xySym.SetDefinition xydef
                    
              End Sub