Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: GrahamL on June 25, 2020, 01:30:00 am

Title: Delete all package elements
Post 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
Title: Re: Delete all package elements
Post by: qwerty on June 25, 2020, 01:41:40 am
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.
Title: Re: Delete all package elements
Post by: Uffe on June 25, 2020, 01:45:29 am
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
Title: Re: Delete all package elements
Post by: Geert Bellekens on June 25, 2020, 01:59:08 am
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
Code: [Select]
dim j
for j = package.Elements.Count -1 to 0 step -1
package.Elements.DeleteAt j , false
next

Example C#
Code: [Select]
            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
Title: Re: Delete all package elements
Post by: qwerty on June 25, 2020, 02:05:18 am
The refresh at the end of the Delete-All-Loop is quite, hmmm. There's nothing left to refresh.

q.