Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: ZuluTen 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:
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)
'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.
-
objElement.TaggedValues.Analyst = strUser
won't work. You need to iterate through the TaggedValues collection and compare forobjElement.TaggedValues.Name == strUser
or objElement.TaggedValues.Name == "Analyst" &&
objElement.TaggedValues.Notes == strUser
(You likely need to adapt for VB syntax)
q.
-
Thanks for that.
The code now works perfectly,and reads:
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:
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?
-
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.
-
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.
-
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?
-
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 (http://www.sparxsystems.com/cgi-bin/yabb/YaBB.cgi?num=1397142183/2#2).
-
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 (http://www.sparxsystems.com/cgi-bin/yabb/YaBB.cgi?num=1397142183/2#2).
Great. Many thanks.