Book a Demo

Author Topic: Duplicate Tagged Values via Inheritence  (Read 3267 times)

Paul.Marks

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Duplicate Tagged Values via Inheritence
« on: June 02, 2011, 03:58:17 pm »
Hi,

I'm trying to script up a process to sum up the values of specific tags through inheritence. My code is working but my collection does not contain the duplicated tags.

Example: I have 3 elements linked togeather each with a tag called "risk" with a value of 10. I want to get the total of 30 but duplicates are being excluded from the collection so I just get the value of 10.

Any help greatly appricated as the documentation is not highlighting where I'm going wrong.

Code: [Select]
Option Explicit

!INC Local Scripts.EAConstants-VBScript

Sub ElementRatingSystem()
      
      ' Show the script output window
      Repository.EnsureOutputVisible "Script"
      
      ' Get the currently selected element in the tree to work on
      Dim theElement as EA.Element
      Set theElement = Repository.GetTreeSelectedObject()
      
      If Not theElement Is Nothing And theElement.ObjectType = otElement Then
            
            Dim i
            
            Session.Output( "Element Roll-Up Rating System" )
            Session.Output( "=======================================" )
            Session.Output( "Working on element '" & theElement.Name & "' (Type=" & theElement.Type & _
            ", ID=" & theElement.ElementID & ")" )
            
            ' ==================================================
            ' MANAGE ELEMENT TAGGED VALUES
            ' ==================================================
            Session.Output( "**********************Tagged Values************************" )
            
            ' Add a new tag
            Dim tags as EA.Collection
            Set tags = theElement.TaggedValues
            
            Dim tagsEx as EA.Collection
            Set tagsEx = theElement.TaggedValuesEx
            
            Dim x
            Dim y
            Dim r
            
            Dim newTag as EA.TaggedValue
            Set newTag = Nothing
            
            ' List all Inherited element tags
            y = tagsEx.Count
            Session.Output " Total Tags: " & y
            For i = 0 To tagsEx.Count - 1
                  
                  Dim currentTagEx as EA.TaggedValue
                  Set currentTagEx = tagsEx.GetAt( i )
                  
                  Session.Output( i & " Inherited Tagged Value: " & currentTagEx.Name & " = " & currentTagEx.Value )
                  
                  ' Sum all tags except any Ratings Tags
                  If currentTagEx.Name <> "Rating" Then
                        ' Check for Numeric Value
                        If IsNumeric(currentTagEx.Value) = True Then
                              x = CInt(currentTagEx.Value) + x
                        End If
                  End If
                  
            Next
            
            Session.Output " ** Updating Tags ** "
            ' Update Tags
            For i = 0 To tags.Count - 1
                  Dim currentTag as EA.TaggedValue
                  Set currentTag = tags.GetAt( i )
                  If currentTag.Name = "Rating" Then
                        currentTag.Value = x
                        currentTag.Update
                        Session.Output( "    Recalulated: " & currentTag.Name & " " & currentTag.Value)
                        r = True
                        Exit For
                  End If
            Next
            
            If r <> True Then
                  Session.Output " Rating = "      & x
                  if x >= 0 then
                  Set newTag = tags.AddNew( "Rating", x )
                  newTag.Update()
                  Session.Output( "    Added new Tag: " & newTag.Name & " " & newTag.Value)
                  end if
            End If
            
            'Refresh Collection
            tags.Refresh()            
            Set tags = Nothing
            Set tagsEx = Nothing
            Set x = Nothing
            Set y = Nothing
            Set r = Nothing
                        
            Session.Output( "Done!" )
            
      Else
            ' No item selected in the tree, or the item selected was not an element
            MsgBox( "This script requires an element be selected in the Project Browser." & vbCrLf & _
            "Please select an element in the Project Browser and try again." )
      End If
      
      
End Sub

ElementRatingSystem