Author Topic: SOLVED Updating Tagged Values from an Add In  (Read 5765 times)

ZuluTen

  • EA User
  • **
  • Posts: 56
  • Karma: +0/-0
    • View Profile
SOLVED Updating Tagged Values from an Add In
« on: July 17, 2014, 04:36:32 am »
I'm having trouble getting some of my Add In code to work, which should, whenever a new element is dragged and dropped from the Toolbox, automatically populate a Tagged Value called 'Analyst' with the name of the user.
The error message is :
EA_OnPostNewElement: Public member 'Analyst' on type IDualCollection' not found
This I assume means it can't find the Tagged Value but I even ran a simple diagnostic (the commented out Message Boxes) from the Add In and it's definitely present.
 
Ultimately I also want to populate other fields, such as Date, but let's not try to run too soon.
The code is as follows:
Code: [Select]
   Function EA_OnPostNewElement(ByVal Repository As EA.Repository, ByVal Info As EA.EventProperties) As Object

        Dim intPointer As Integer
        Dim objElement As Object
        Dim objTaggedValue As Object
        Dim strReport As String
        Dim strNumericOnly As String
        Dim strTodaysDate As String = String.Format("{0:dd/MM/yyyy}", DateTime.Now)
        Dim strUser As String = Environment.UserName
        Dim nonAlphaChars As New System.Text.RegularExpressions.Regex("[^0-9]")

        strReport = EventPropertiesToString(Info) 'get the element ID
        strNumericOnly = nonAlphaChars.Replace(strReport, String.Empty) 'remove all non numeric characters
        intPointer = CInt(strNumericOnly) 'and ensure that it's an integer

        objElement = Repository.GetElementByID(intPointer)
        'MsgBox(objElement.Name) this instruction used to prove that the appropriate element can be found
        'in the repository

        'For Each objTaggedValue In objElement.TaggedValues
        '    MsgBox(objTaggedValue.Name)
        'Next
        objElement.TaggedValues.Analyst = strUser

        EA_OnPostNewElement = Nothing
    End Function

I also want to do a simple check to ensure that an 'empty' Tagged Value is present in the collection for each element, and if not then add it, so I'd be most grateful for a clue how to do that.

I found the following from a Sparx publication to create a new Tagged Value but I was again getting error messages, and I'm uncertain whether AddNew is applicable to Tagged Value collections...

(I assume that 'Appended' is the Name of the Tagged Value, and 'Change' is the desired value, but maybe I've misunderstood the intent)

Code: [Select]
'TaggedValues
o = element.TaggedValues.AddNew("Appended","Change")
If not o.Update Then
Console.WriteLine("TaggedValues error:" + o.GetLastError())
End if
element.TaggedValues.Refresh
For idx = 0 to element.TaggedValues.Count -1
o = element.TaggedValues.GetAt(idx)
Console.WriteLine(o.Name)
If(o.Name="Appended") Then
If bDel Then element.TaggedValues.Delete (idx)
End if
Next

Many thanks in advance for any help received.
« Last Edit: July 18, 2014, 06:09:37 pm by ZuluTen »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Updating Tagged Values from an Add In
« Reply #1 on: July 17, 2014, 08:29:27 am »
Code: [Select]
objElement.TaggedValues.Analyst = strUserwon't work. You need to iterate through the TaggedValues collection and compare for
Code: [Select]
objElement.TaggedValues.Name == strUser or
Code: [Select]
objElement.TaggedValues.Name == "Analyst" &&
objElement.TaggedValues.Notes == strUser


(You likely need to adapt for VB syntax)

q.
« Last Edit: July 17, 2014, 08:30:32 am by qwerty »

ZuluTen

  • EA User
  • **
  • Posts: 56
  • Karma: +0/-0
    • View Profile
Re: Updating Tagged Values from an Add In
« Reply #2 on: July 17, 2014, 03:59:42 pm »
Thanks for that.
The code now works perfectly,and reads:
Code: [Select]
For Each objTaggedValue In objElement.TaggedValues
       If objTaggedValue.Name = "Analyst" Then
           objTaggedValue.Value = strUser
           objTaggedValue.update()
       End If
Next

I've tried using both "Value" and "Notes", though the latter from what I understand relates only to when a tagged value content is of memo size...

Also, I now know how to add a Tagged Value:
Code: [Select]
objNewTaggedValue = objElement.TaggedValues.AddNew("tagged_Value_Name", "Tagged_Value_Contents")
objNewTaggedValue.Update()


Now 'm also wondering: when a Tagged Value is defined as part of an MDG it is shown in the GUI as what might best be described as:
Element.Properties.MDG_Name.TaggedValue
Is there a way of recreating that programmatically? Is it just another collection under element, I wonder?
« Last Edit: July 17, 2014, 05:24:55 pm by ZuluTen »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Updating Tagged Values from an Add In
« Reply #3 on: July 17, 2014, 06:36:28 pm »
The TVs from MDGs have some extra data in t_xref and it's a bit of black magic behind that. You might be able to recognize them. Just create a MDG stereo in an empty repos  and look into t_xref.

q.

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +54/-3
    • View Profile
Re: Updating Tagged Values from an Add In
« Reply #4 on: July 18, 2014, 09:04:23 am »
Quote
Now 'm also wondering: when a Tagged Value is defined as part of an MDG it is shown in the GUI as what might best be described as:
Element.Properties.MDG_Name.TaggedValue
Is there a way of recreating that programmatically?
When tagged values are defined for a stereotype in a UML Profile, don't add them manually. Instead, either apply the stereotype to the element and its tagged values will be added automatically, or if the stereotype is already applied then "synchronize" the stereotype to add in any missing tagged values.
« Last Edit: July 18, 2014, 09:05:03 am by KP »
The Sparx Team
[email protected]

ZuluTen

  • EA User
  • **
  • Posts: 56
  • Karma: +0/-0
    • View Profile
Re: Updating Tagged Values from an Add In
« Reply #5 on: July 18, 2014, 03:45:33 pm »
Quote
When tagged values are defined for a stereotype in a UML Profile, don't add them manually. Instead, either apply the stereotype to the element and its tagged values will be added automatically, or if the stereotype is already applied then "synchronize" the stereotype to add in any missing tagged values.

That makes sense, but can synchronisation be triggered programmatically?

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +54/-3
    • View Profile
Re: Updating Tagged Values from an Add In
« Reply #6 on: July 18, 2014, 04:32:53 pm »
You can synchronize a single element using the method EA.Element.SynchTaggedValues(). Otherwise, you can synch all items (elements, connectors, attributes, etc) with the same stereotype using the undocumented CustomCommand.
« Last Edit: July 18, 2014, 04:33:44 pm by KP »
The Sparx Team
[email protected]

ZuluTen

  • EA User
  • **
  • Posts: 56
  • Karma: +0/-0
    • View Profile
Re: Updating Tagged Values from an Add In
« Reply #7 on: July 18, 2014, 06:10:28 pm »
Quote
You can synchronize a single element using the method EA.Element.SynchTaggedValues(). Otherwise, you can synch all items (elements, connectors, attributes, etc) with the same stereotype using the undocumented CustomCommand.

Great. Many thanks.