Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: vrieg on August 06, 2024, 06:01:02 pm
-
Hello,
I would like to delete multiple elements at once using EA API.
Due to some reason the diagram does not get updated after deleting die diagram object.
Do I miss a command. I tries already with GPT assistants but I could not figure it out.
Is there a DiagramRefresh or something like this needed?
'iterate through the connected elements
'iterate through the list and add them to the diagram if this has not yet done
dim key0, DiagramObj
dim notConnectedElements
set notConnectedElements = CreateObject("Scripting.Dictionary")
for each DiagramObj in diagram.DiagramObjects
dim elementToDelete
elementToDelete=true
dim currentElement
set currentelement = Repository.GetElementByID(DiagramObj.ElementID)
'iterate through all elements that dont have a connection to rootElement and that are not the rootElement istel
for each key0 in connectedElements.Keys
if ( DiagramObj.ElementID = key0 and connectedElements.Exists(DiagramObj.ElementID)) then
elementToDelete=false
exit for
end if
next
if elementToDelete and DiagramObj.ElementID <> rootElement.ElementID then
DeleteParts rootElement, currentElement
' Delete the diagram objects associated with the element
DeleteDiagramObjects diagram, currentElement
rootElement.Update()
Repository.ReloadDiagram(diagram.DiagramID)
'Repository.ReloadDiagram(diagram.DiagramID)
end if
next
Sub DeleteDiagramObjects(diagram, elementToDelete)
' Iterate through the diagram objects and delete those associated with the element
Dim j
For j = diagram.DiagramObjects.Count -1 to 0 step -1
Dim diagramObj
Set diagramObj = diagram.DiagramObjects.GetAt(j)
' Check if the diagram object is associated with the element to be deleted
If diagramObj.ElementID = elementToDelete.ElementID Then
' Delete the diagram object
diagram.DiagramObjects.Delete(j)
exit for
End If
Next
Repository.ReloadDiagram(diagram.DiagramID)
End Sub
-
Are you sure the diagramobjects get deleted?
If they are, the diagram should be refreshed after doing a ReloadDiagram.
I suspect the script never reaches the delete part for some reason.
Geert
-
Looking at the code presented, I mis the DIM diagram and corresponding assignment.
Also after changing something of a collection, the collection should be refreshed to commit the changes (diagram.DiagramObjects.Refresh()).
-
the collection should be refreshed to commit the changes (diagram.DiagramObjects.Refresh()).
That is nonsense. The Refresh() will actually do the opposite. It will get the data from the database and into the collection in memory.
Geert
-
Ok, sorry, I missed the Update() step.
See https://sparxsystems.com/enterprise_architect_user_guide/16.1/add-ins___scripting/collection.html (https://sparxsystems.com/enterprise_architect_user_guide/16.1/add-ins___scripting/collection.html):
It is important to realize that when the 'AddNew' function is called, the item is not automatically added to the current collection. The typical steps are:
- Call AddNew to add a new item
- Modify the item as required
- Call Update on the item to save it to the database
- Call Refresh on the collection to include it in the current set
Delete is the same; until Refresh is called, the collection still contains a reference to the deleted item, which should not be called.
-
It has been a mixture of both. Some whitespace of the VBA script at the line ending causing the deletion loop not to be entered and the missing update.
But with Geert's EA-Matic it seems to work now :)