Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: IanG on November 06, 2015, 10:59:24 pm
-
Hello all,
I'm trying to build an add-in which allows the user to create a tagged value through a windows form. The user selects the package and then the element which will have the next tagged value.
The code compiles fine, EA and the add-in run fine but the new tagged value simply does get added. Can anyone see anything wrong in my approach? I've played around with the refresh and the update but no luck so
Thanks in advance.
Ian
string SelectedElement = treeEA.SelectedNode.Text;
EA.Package aPackage;
aPackage = EA_Repository.GetTreeSelectedPackage();
foreach (EA.Package thePackage in aPackage.Packages)
{
foreach (EA.Element theElements in thePackage.Elements)
{
if (SelectedElement == theElements.Name)
{
theElements.TaggedValues.AddNew("MyTagName", "MyTagValue");
theElements.TaggedValues.Refresh();
theElements.Update();
}
}
}
-
Hi Ian,
Collection.AddNew() returns the object that's been created, in this case a TaggedValue, and you need to call Update() on that or it will never get written to the database. You're refreshing the collection, good, and updating the element (not sure if you actually need to do that in this case), but you never update the newly created tagged value.
There's a sample in the help under Automation and Scripting -- Enterprise Architect Object Model -- Reference -- Code Samples -- Element Extras.
Cheers,
/Uffe
-
Hi Ian,
your code has just two small mistakes.
The first has been answered by Uffe.
The AddNew-function returns an object which will be your new tagged-value.
The second is, that you have not to call the Update-function of collection. You have to call the Update-function of your new tagged value.
Also the parameter of the AddNew-function are wrong. The first parameter is the name of the tagged-value (this is correct) but he second parameter is the type of your tagged-value not the value.
When I change your code, it should be corrected look like this
EA.TaggedValue newValue=(EA.TaggedValue)theElements.TaggedValues.AddNew("MyTagName", "string");
newValue.Value="MyTagValue";
newValue.Update()
theElements.TaggedValues.Refresh();
Jan
-
Excellent - that works great! :)
Thank you both so much for your help.
Cheers,
Ian