Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Ribeye on April 27, 2022, 03:41:49 am
-
I am successfully assigning stereotypes to elements I am creating in C#.
When it comes to setting values of tags that the stereotype has I am failing.
I copied code from https://sparxsystems.com/forums/smf/index.php?topic=6097.0 but it is creating new tags, not setting the tag values of the stereotype.
I cant find example code anywhere.
Here is that I have come to.
// https://sparxsystems.com/forums/smf/index.php?topic=6097.0
public static bool addTaggedValue(EA.Element elementParam, string tagName, string value)
{
TaggedValue tag = elementParam.TaggedValuesEx.GetByName(tagName);
if (tag == null)
{
TaggedValue element = elementParam.TaggedValuesEx.AddNew(tagName, value);
element.Value = value;
element.Update(); //must be executed in order to save new tagged value
elementParam.Update(); // is it needed?
elementParam.TaggedValues.Refresh();
String debugString = element.FQName; // always ""
return true;
}
tag.Value = value;
tag.Update();
elementParam.Update(); // is it needed?
elementParam.TaggedValues.Refresh();
return false;
}
-
I would not use the GetByName since you never know which one EA returns. Names are not unique so there might exist more than one. Use your own wrapper to find the correct TV.
elementParam.Update() is not needed since you do not change the element, and the collection for the TVs can not be changed. You can only handle it by its operations (addnew/delete).
How do you know that it creates new TVs each time?
q.
-
Also, use the regular TaggedValues collection, not the TaggedValuesEx collection.
And the Refresh() is not needed.
Geert
-
Thanks for the replies, they do help.
For anyone else looking to answer this question:
I came to believe the unique name of a tag is "profileName::stereotypeName::tagName"
So my method for adding / updated the tag of an EA.Element is:
// https://sparxsystems.com/forums/smf/index.php?topic=6097.0
public static bool addTaggedValue(EA.Element elementParam, string profileName, string stereotypeName, string tagName, string value)
{
String tagId = profileName + "::" + stereotypeName + "::" + tagName;
TaggedValue tag = elementParam.TaggedValues.GetByName(tagId);
if (tag == null)
{
TaggedValue element = elementParam.TaggedValues.AddNew(tagName, value);
element.Value = value;
element.Update(); //must be executed in order to save new tagged value
return true;
}
tag.Value = value;
tag.Update();
return false;
}
For EA.Attribute and other EA.element types I needed another version with exactly the same code but replacing EA.Element with EA.Attribute. Could not find a way to generalise EA.Element and EA.Property!
-
For EA.Attribute and other EA.element types I needed another version with exactly the same code but replacing EA.Element with EA.Attribute. Could not find a way to generalise EA.Element and EA.Property!
It would as well be hard to generalize wheel and potato. They have nothing in common...
q.
-
Hmm, are you trying to add tagged values to a stereotyped element, that should already have the tagged value because it's defined in the stereotype?
In that case you don't need to add it manually, but simply synchronize the stereotype. This will add all the missing tagged values automatically.
You can do that from a toolbox, or using a script
Here's a script that does exactly that: https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Tools/UML%20Profile/Synchronize%20Steretoypes.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Tools/UML%20Profile/Synchronize%20Steretoypes.vbs)
Geert
-
For EA.Attribute and other EA.element types I needed another version with exactly the same code but replacing EA.Element with EA.Attribute. Could not find a way to generalise EA.Element and EA.Property!
If you're using a dynamic language as JScript/JavaScript you can do that. Take care, EA.Element is a type, EA.Attribute, EA.Connector, EA.Package are different types from automation perspective.
Using C# you can use dynamic as parameter type for your object.
Example in JScript:
// holds for everything that has an TaggedValues collection
function addTaggedValue(object, tagName, tagValue) {
if (typeof(object.TaggedValues) === 'undefined')
return null;
var tag = null;
for (var index = 0; index < object.TaggedValues.Count; index++) {
var candidate = object.TaggedValues.GetAt(index);
if (candidate.Property == tagName) {
tag = candidate;
break;
}
}
if (tag == null) {
tag = object.TaggedValues.AddNew(tagName, "");
}
tag.Value = tagValue;
tag.Update();
return tag;
}