Sparx Systems Forum
Enterprise Architect => General Board => Topic started by: Alexander.Golyshkin 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
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.
-
Find it in a packages elements collection and use its delete operation.
q.
-
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
-
Workable Py code below
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()
-
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.
-
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...
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()
-
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
-
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
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