Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: GrahamL on June 25, 2020, 01:30:00 am
-
Hi
I want to delete all elements in a package from an add
This is what I am doing
var elements = contextPackage.Elements;
for (short i = 0; i < elements.Count; ++i)
{
elements.Delete(i);
elements.Refresh();
}
contextPackage.Update();
But i seems not to work
What is the correct way?
Thanks
-
Delete from top to bottom and in all cases remove the Refresh. Or (to make it slower) delete the first and refresh until it's empty.
q.
-
Hi,
Your exit condition depends on the size of a collection you're whittling down. This gets tricky.
The simple solution is to loop backwards, and only refresh after the end of the loop.
/Uffe
-
I do as Uffe says, loop from back to front.
Not really necessary if you don't refresh, but a "best practice" anyway
Example vbscript
dim j
for j = package.Elements.Count -1 to 0 step -1
package.Elements.DeleteAt j , false
next
Example C#
for (int i = this.elementWrapperOwner.wrappedElement.Constraints.Count - 1; i >= 0; i--)
{
var currentConstraint = (global::EA.Constraint)this.elementWrapperOwner.wrappedElement.Constraints.GetAt(short.Parse(i.ToString()));
if (this.name == currentConstraint.Name)
{
this.elementWrapperOwner.wrappedElement.Constraints.DeleteAt(short.Parse(i.ToString()), false);
}
}
Geert
-
The refresh at the end of the Delete-All-Loop is quite, hmmm. There's nothing left to refresh.
q.