Author Topic: Deleting multiple elements from a diagram using API  (Read 3859 times)

vrieg

  • EA User
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Deleting multiple elements from a diagram using API
« 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

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Deleting multiple elements from a diagram using API
« Reply #1 on: August 06, 2024, 07:04:58 pm »
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

ea0522

  • EA User
  • **
  • Posts: 134
  • Karma: +5/-0
    • View Profile
Re: Deleting multiple elements from a diagram using API
« Reply #2 on: August 06, 2024, 07:08:16 pm »
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()).

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Deleting multiple elements from a diagram using API
« Reply #3 on: August 06, 2024, 09:11:08 pm »
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

ea0522

  • EA User
  • **
  • Posts: 134
  • Karma: +5/-0
    • View Profile
Re: Deleting multiple elements from a diagram using API
« Reply #4 on: August 06, 2024, 09:35:44 pm »
Ok, sorry, I missed the Update() step.

See https://sparxsystems.com/enterprise_architect_user_guide/16.1/add-ins___scripting/collection.html:

Quote
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.

vrieg

  • EA User
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: Deleting multiple elements from a diagram using API
« Reply #5 on: August 12, 2024, 07:10:36 pm »
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 :)