9 Replies Latest reply on Jul 16, 2010 9:02 AM by hanyong

    VBA - Script to move from AF to AF2

    wpurrer

      Hello !

       


      Has anyone have an VBA - Script to replace all AF with AF2 .. tags 

       

      thx.

        • Re: VBA - Script to move from AF to AF2

          Just to make sure we are on the same page, you are trying to migrate dynamic symbols (e.g. bar, value, trend) that are configured to read data from AF 1.x using the AF Modeler Add-in, to reading AF 2.x data using the embeded AF 2.x Data Set - correct?

           

          If that's the case, then are you looking into something like this?

          Select Case sym.Type
              Case pbSymbolBar
                  Set bar = sym
                  tag = bar.GetTagName(1)
                  If InStr(1, tag, "AF:", vbTextCompare) > 0 Then
                      bar.SetTagName Replace(tag, "AF.", "AF2.", , , vbTextCompare)
                  End If
              Case pbSymbolValue
                  Set value = sym
                  tag = value.GetTagName(1)
                  If InStr(1, tag, "AF.", vbTextCompare) > 0 Then
                      value.SetTagName Replace(tag, "AF.", "AF2.", , , vbTextCompare)
                  End If
              Case pbSymbolTrend
                  Set trend = sym
                  For i = 1 To trend.TraceCount
                     tag = trend.GetTagName(i)
                      If InStr(1, tag, "AF:", vbTextCompare) > 0 Then
                          trend.SetTagName i, Replace(tag, "AF.", "AF2.", , , vbTextCompare)
                      End If
                  Next
              End Select
          Next
            • Re: VBA - Script to move from AF to AF2
              hanyong

              A correction to what Steve had proposed, other than replacing the prefix "AF." with "AF2." you need to change the part of the path as well. Just an example, when reading from the same path the configuration for AF dataset and AF2 dataset is as follows:

               

              AF Dataset path: AF.\\DEVPI\demo\NuGreen\Houston\Milling Process\Equipment\H400\Fuel Gas Flow
              AF2 Dataset path: AF2.\\DEVPI\demo\NuGreen\Houston\Milling Process\Equipment\H400|Fuel Gas Flow

               

              Other than the prefix, AF2 dataset uses '|' as delimiter before the attribute (Fuel Gas Flow in this example), whereas AF dataset uses '\'.

               

              I would suggest to split the path and reconstructing it. The code uploaded here, because I used modification of a Split function from here to split the string into a string array to do achieve this.

                • Re: VBA - Script to move from AF to AF2
                  wpurrer

                  Thanks you both for the answer !

                  • Re: VBA - Script to move from AF to AF2

                    Good catch, Han Yong! Thanks for pointing out the correction and coming up with the code to address this!

                      • Re: VBA - Script to move from AF to AF2
                        wpurrer

                        Hi, togther i tried it out => but it doesn't work:

                         

                        Bug a) no grouped Symbols

                         

                        Bug b) it mixes up the attributes.

                          • Re: VBA - Script to move from AF to AF2

                            Wolfgang Purrer

                            but it doesn't work:
                            Let's try and clarify this...

                             

                            Wolfgang Purrer

                            Bug a) no grouped Symbols
                            Well, the sample code that Han Yong and I provided is not an "official" code from OSIsoft, and obviously does not come with any guarantee of some sort - including its exhaustiveness. As an example, you are also missing XY Plots and Batch Trends in there. You can easily take care of "Grouped Symbols" by looking for Symbol.Type = pbSymbolComposite and then drilling into its "ConnectedSymbols" (a collection of Symbol object). Please do not hesitate to initiate a new thread if you need help on dealing with composite symbols specifically.

                             

                            Wolfgang Purrer

                            Bug b) it mixes up the attributes.
                            What do you mean by that?

                              • Re: VBA - Script to move from AF to AF2
                                hanyong

                                Hi Wolfgang

                                 

                                Steve Pilon

                                the sample code that Han Yong and I provided is not an "official" code from OSIsoft, and obviously does not come with any guarantee of some sort - including its exhaustiveness

                                I must admit, I am guity for not testing this extensively.

                                 

                                Wolfgang Purrer

                                Bug b) it mixes up the attributes.

                                I think it is a mistake in the code for updating the Trend's trace tag, i missed a required change for the SetTagName call at the end of the pbSymbolTrend case, so instead of updating the tag in the trend, it is updating something else. (Quite obvious that i copied & paste)

                                 

                                The following changes should make it work:

                                Case pbSymbolTrend
                                    Set Trend = sym
                                    For i = 1 To Trend.TraceCount
                                        Tag = Trend.GetTagName(i)
                                        If InStr(1, Tag, "AF.", vbTextCompare) > 0 Then
                                            atrWords = Split(Tag, "\")
                                            ' replace AF. with AF2.
                                            newpath = "AF2."
                                            CountWords = UBound(atrWords) - LBound(atrWords) + 1
                                            ' append the remaining path, except the attribute
                                            For j = 1 To CountWords - 2
                                                newpath = newpath & "\" & atrWords(j)
                                            Next j
                                            ' append "|" and the attribute
                                            newpath = newpath & "|" & atrWords(CountWords - 1)
                                            ' This is the part that is wrong in the original sample
                                            Trend.SetTagName i, newpath
                                        End If
                                    Next
                                 

                                 

                                I have update the sample code that i posted as well, you can get it from here. As Steve mentioned, not all kinds of symbols and all scenarios has been taken into consideration yet. If you need help on a specific type of symbol, you can put up a post on it =)

                                 

                                I'll try to give code that works for grouped symbols and multistate (I think this should be quite commonly used as well). Don't want to rush it through and make more mistakes like the one above. 

                                  • Re: VBA - Script to move from AF to AF2
                                    wpurrer

                                    Thanks => i know.

                                     

                                     

                                     

                                    I changed the vba - script that it works for me. (composite symbols )

                                     

                                     

                                    Sub replaceAF()
                                        Dim sym As Variant
                                        Dim index As Integer
                                        Dim j As Integer
                                        Dim atrWords() As String
                                        Dim newpath As String
                                        Dim CountWords As Long
                                       
                                       
                                        For index = 1 To ThisDisplay.Symbols.Count
                                            Set sym = ThisDisplay.Symbols(index)
                                            ' only included Bar and Value symbol as an example here.
                                            replacesym (sym)
                                            Next index
                                    End Sub

                                    Public Sub replacesym(ByVal sym As Symbol)


                                    Select Case sym.Type
                                            
                                                      
                                                Case pbSymbolBar
                                                    Set Bar = sym
                                                    tag = Bar.GetTagName(1)
                                                    If InStr(1, tag, "AF.", vbTextCompare) > 0 Then
                                                   
                                                        newpath = replace(tag)
                                                       
                                                        Bar.SetTagName newpath
                                                    End If
                                                Case pbSymbolValue
                                                    Set Value = sym
                                                    tag = Value.GetTagName(1)
                                                    If InStr(1, tag, "AF.", vbTextCompare) > 0 Then
                                                       
                                                        newpath = replace(tag)

                                                        Value.SetTagName newpath
                                                    End If
                                                Case pbSymbolTrend
                                                    Set Trend = sym
                                                    For i = 1 To Trend.TraceCount
                                                        tag = Trend.GetTagName(i)
                                                        If InStr(1, tag, "AF.", vbTextCompare) > 0 Then
                                                          
                                                             newpath = replace(tag)

                                                            ' Set new path
                                                            Trend.SetTagName i, newpath
                                                        End If
                                                    Next
                                                            
                                            Case pbSymbolBGroup
                                                   
                                                   ' MsgBox "pSymbolBGroup " + sym.Name + " " + Str(sym.SymbolAttachments.Count)
                                                   
                                            Case pbSymbolComposite
                                                   
                                                     Dim sComp As Composite
                                                     Dim newSym As Symbol
                                                     Set sComp = sym
                                                    
                                                     For i = 1 To sComp.GroupedSymbols.Count
                                                    
                                                       Set newSym = sComp.GroupedSymbols(i)
                                                       replacesym newSym
                                                    
                                                     Next
                                                                     
                                                       'MsgBox "psymbolComposite " + sym.Name
                                      
                                             End Select
                                               
                                    End Sub
                                    Public Function replace(ByVal tag As String) As String

                                     atrWords = Split(tag, "\")
                                                            ' replace AF. with AF2.
                                                            newpath = "AF2."
                                                            CountWords = UBound(atrWords) - LBound(atrWords) + 1
                                                            ' append the remaining path, except the attribute
                                                            For j = 1 To CountWords - 2
                                                                newpath = newpath & "\" & atrWords(j)
                                                            Next j
                                                            ' append "|" and the attribute
                                                            newpath = newpath & "|" & atrWords(CountWords - 1)
                                                           
                                                           
                                                            replace = newpath

                                    End Function


                                    Public Function Split(ByVal InputText As String, _
                                             Optional ByVal Delimiter As String) As Variant


                                        Dim strReplacedText As String
                                        Dim intIndex As Integer


                                        strReplacedText = Trim(replace(InputText, _
                                             vbTab, " "))
                                       
                                        Do While InStr(strReplacedText, "  ")
                                            strReplacedText = replace(strReplacedText, _
                                                "  ", " ")
                                        Loop

                                        If Len(Delimiter) = 0 Then
                                            Split = VBA.Split(strReplacedText)
                                        Else
                                            Split = VBA.Split(strReplacedText, Delimiter)
                                        End If

                                    End Function