Book a Demo

Author Topic: Deleting an Element with a script  (Read 6461 times)

Arnoud_B

  • EA User
  • **
  • Posts: 76
  • Karma: +0/-0
    • View Profile
Deleting an Element with a script
« on: July 08, 2021, 07:59:46 pm »
I have an Element which appears in my search and I even have other elements that have a relationship with it but it is no longer i my project browser and it is also not shown in the view the relationship sort of ends in a void  :-[.
So I cannot delete it using the GUI.
So I thought of writing a very small script to delete it anyway. I find the Element but you cannot delete an object in SparxEA you should delete it from the Collection that holds it, but which collection holds the elements?

I have found the element like this:
   var elementToDelete as EA.Element;
   elementToDelete = Repository.GetElementByGuid("{D913995C-5E5D-48da-B91D-C9E03356B389}");
   Session.Output("found object: " + elementToDelete.Name);

However I fail to find how to get rid of it.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Deleting an Element with a script
« Reply #1 on: July 08, 2021, 08:04:10 pm »
Before doing any of that I would run a project integrity check. That might just solve if for you.

Now in order to delete an element you have to get it's owner.
That owner is either another element (element.ParentID), or a package (element.PackageID if ParentID = 0)

Then loop the owner.Elements untill you get the index of your element, and do a owner.Elements.DeleteAt()

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Deleting an Element with a script
« Reply #2 on: July 08, 2021, 11:16:14 pm »
Almost. If the element is packed inside an element it will not be found with the package.elements but only via its parent element's collection. I for myself would use a repository.execute, but that's nothing for the faint hearted. Nor for the careless...

q.

Arnoud_B

  • EA User
  • **
  • Posts: 76
  • Karma: +0/-0
    • View Profile
Re: Deleting an Element with a script
« Reply #3 on: August 27, 2021, 07:17:03 pm »
Hi Geert,
Thanks again for the good direction. I ran the integrity check and indeed there my item was noted as corrupt. I repaired it (had to run twice to get the second and related object also found and repaired) and now my connector that ended somewhere in the void is nicely attached again the the element which I could just delete with the normal UI. Learned something again today!