Book a Demo

Author Topic: DELETE an elemenent using script API  (Read 23550 times)

Alexander.Golyshkin

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
DELETE an elemenent using script API
« on: March 25, 2024, 10:29:21 pm »
Is there possible to DELETE an element from REPO having an Id or GUID? I didn't find any API related to it, could you help?

Also I just tried to use SQL query like below

Code: [Select]
DELETE from t_object WHERE ea_guid='{3D986D35-80C0-4ebb-91A9-43A728357B38}' OR object_id=12138
but looks like it doesn't work at all. The expected result is deleted element by pressing Ctrl+DEL.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: DELETE an elemenent using script API
« Reply #1 on: March 25, 2024, 11:02:09 pm »
Find it in a packages elements collection and use its delete operation.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DELETE an elemenent using script API
« Reply #2 on: March 25, 2024, 11:51:55 pm »
Deleting something in EA is always the same.
Find the owner, loop through the collection, and use the DeleteAt method

So for an element, use package.Elements.DeleteAt or Element.Elements.DeleteAt

For an attribute use Element.Attributes.DeleteAt, etc...

Geert

Alexander.Golyshkin

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: DELETE an elemenent using script API
« Reply #3 on: March 26, 2024, 02:36:04 am »
Workable Py code below

Code: [Select]
   def _deleteElement( self, aElement: CDispatch ):
      pkg = self._engine.getRepo().getPackageByID( aElement.packageID )

      for index, element in enumerate( pkg.Elements ):
         if aElement.ElementGUID == element.ElementGUID:
            pkg.Elements.Delete( index )
            LOGGER.info( f"Removed Unused EA Element {element.ElementGUID}." )
            break

      pkg.Update()

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: DELETE an elemenent using script API
« Reply #4 on: March 26, 2024, 06:03:06 am »
Actually you would do an repo.getelementby id or guid, load its parent package similarly and remove it from its package's element collection. Eventually you have an element stuck in another. So tha parent is not a package.

q.

Alexander.Golyshkin

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: DELETE an elemenent using script API
« Reply #5 on: March 26, 2024, 08:05:21 pm »
Yes, thnx for suggestion above, the following code also is workable, but nay cause EA internal App Error due missing parentID for some DELETING elements...

Code: [Select]
   def _deleteElement( self, aElement: CDispatch ):
      parentElem = self._engine.getRepo().GetElementByID( aElement.ParentID )
      isFound: bool = False

      for index, element in enumerate( parentElem.Elements ):
         if aElement.ElementID == element.ElementID:
            LOGGER.info( f"Removed Unused EA Element {element.ElementGUID}." )
            parentElem.Elements.Delete( index )
            isFound = True
            break

      if isFound:
         parentElem.Update()
« Last Edit: March 26, 2024, 08:14:37 pm by Alexander.Golyshkin »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DELETE an elemenent using script API
« Reply #6 on: March 26, 2024, 08:14:20 pm »
two remarks:

This code won't work if your element doesn't have an element as owner, but a package, so you'll have to check parentID first, and package second.
If you are using a non-strong types language, you can use the same code for both package or parentElement as they both have the same Elements collection.

also, parentElem.Update() is not needed at all, so you can delete the whole isFound logic.

Geert

Alexander.Golyshkin

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: DELETE an elemenent using script API
« Reply #7 on: March 26, 2024, 08:24:53 pm »
two remarks:

This code won't work if your element doesn't have an element as owner, but a package, so you'll have to check parentID first, and package second.
If you are using a non-strong types language, you can use the same code for both package or parentElement as they both have the same Elements collection.

also, parentElem.Update() is not needed at all, so you can delete the whole isFound logic.

Geert

Yes, thnx, I have fixed comment above about EA exception with following

Code: [Select]
   def _deleteElement( self, aElement: CDispatch ):
      try:
         parentElem = self._engine.getRepo().GetElementByID( aElement.ParentID )
      except Exception as e:
         LOGGER.error( e )
         parentElem = self._engine.getRepo().getPackageByID( aElement.packageID )

      for index, element in enumerate( parentElem.Elements ):
         if aElement.ElementID == element.ElementID:
            LOGGER.info( f"Removed Unused EA Element {element.ElementGUID}." )
            parentElem.Elements.Delete( index )
            break