Book a Demo

Author Topic: Add or delete an element from a diagram  (Read 15253 times)

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Add or delete an element from a diagram
« on: December 26, 2015, 08:32:01 am »
Hi,

I have an element that i want to add to a specific diagram through the API.
And I have an element that I want to remove from the diagram.
I could not find an API that does that.
Does anyone know how this can be achieved?

Thanks,
Tzafrir

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add or delete an element from a diagram
« Reply #1 on: December 26, 2015, 05:54:32 pm »
You have to create a new diagram object on the diagram
Code: [Select]
EA.Diagram.DiagramObjects.AddNew()and then set the elementID of that diagramObject to the elelementID of the element you want to add.

Removing is the same principle, remove the DiagramObject using
Code: [Select]
EA.Diagram.DiagramObjects.DeleteAt()
Geert

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Re: Add or delete an element from a diagram
« Reply #2 on: December 27, 2015, 06:21:17 pm »
I tried to use those APIs, but none of them work as I expect. I am probably missing something here.

I will use a sample code to be more clear:

For the AddNew API:
EA.Diagram diagram = repository.GetCurrentDiagram();
EA.DiagramObject diagramObj = diagram.DiagramObjects.AddNew("ABC", "Class");
diagramObj.ElementID = someElement.ElementID;
diagram.Update();

For the delete API:
short id = (short)someElement.ElementID;
diagram.DiagramObjects.DeleteAt(id,false);
(This is throwing an exception on an element that is on the diagram)

I think I used them both properly, can you assist me with what is wrong?

Tzafrir




Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add or delete an element from a diagram
« Reply #3 on: December 27, 2015, 06:48:21 pm »
You forgot to save the DiagramObject using Update()

Geert

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Re: Add or delete an element from a diagram
« Reply #4 on: December 28, 2015, 07:33:58 am »
 :( I know it should be simple but I am missing something basic here:

For the Add method all I want to do is get an element and put it on the diagram

this simplify what I wrote:
private addToDiagram(EA.Repository repository,EA.Element someElement)
{
   EA.Diagram diagram = repository.GetCurrentDiagram();
    EA.DiagramObject a = diagram.DiagramObjects.AddNew(someElement.Name, someElement.Type);
    a.ElementID = selected.ElementID;
    a.Update(); //The update is throwing an exception
    diagram.Update();
}
In debugger I see the element Id is getting updated, but the Update throws an exception.


For the delete function, all it should do is get element an element which is on the diagram and delete it:
private deleteFromDiagram(EA.Repository repository,EA.Element someElement)
{
  EA.Diagram diagram = repository.GetCurrentDiagram();
  short id = (short)selected.ElementID;
  diagram.DiagramObjects.DeleteAt(id, true); //this line throws an exception for a valid element
  diagram.Update();
}
Again, what is wrong with this delete?

 Tzafrir

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add or delete an element from a diagram
« Reply #5 on: December 28, 2015, 03:13:07 pm »

Geert

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Re: Add or delete an element from a diagram
« Reply #6 on: December 29, 2015, 05:35:26 am »
Sorry for the confusing sample, in my code I use someElement, this was a typo when I created the mail
Anyway I changed the addNew to get the position and still getting this exception:

private addToDiagram(EA.Repository repository,EA.Element someElement)
{
   EA.Diagram diagram = repository.GetCurrentDiagram();
   EA.DiagramObject a = (EA.DiagramObject) diagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "");
   a.ElementID = someElement.ElementID;
   a.Update(); //The update is throwing an exception
   diagram.Update();
}

For the delete function, i saw that the element was deleted by using the count method but the diagram was not updated in model (with the update and refresh methods)
private deleteFromDiagram(EA.Repository repository,EA.Element someElement)
{
  EA.Diagram diagram = repository.GetCurrentDiagram();
 
  int i = diagram.DiagramObjects.Count; //2
  diagram.DiagramObjects.DeleteAt(1, true); //delete the second element in diagram
   i = diagram.DiagramObjects.Count; //1
   diagram.DiagramObjects.Refresh();
   diagram.Update();
}

The sample code I saw in the forum looks pretty much like mine, but something is still missing...

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add or delete an element from a diagram
« Reply #7 on: December 29, 2015, 06:31:48 am »
You have to reload the diagram before you see any changes, and you don't need the refresh() operation. (you hardly ever need Refresh())

You might need to lose the diagram.update() as well. That might undo the delete you just did on the diagramObjects collection.

As for the exception, it might help if you post the actual exception

Geert

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Re: Add or delete an element from a diagram
« Reply #8 on: December 30, 2015, 03:55:03 am »
Hi,

Add element to diagram was resolved but the requested element for deletion is not deleted from diagram.
What I see is:
diagram.DiagramObjects.DeleteAt(index,true) does delete the element,
as mentioned above i can see the size of diagramObjects is reduced by 1.
I removed the diagram.DiagramObjects.Refresh().
and run it with and without diagram.Update() API call.
both cases element stays on the diagram.

I also run a test that add a new element to a diagram and it is shown when closing and opening the diagram.
(So diagramObjects are getting refreshed but without the deletion)
Any other ideas?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add or delete an element from a diagram
« Reply #9 on: December 30, 2015, 07:05:54 am »
I'm not sure what you are doing wrong, but this bit of VBScript removes a single DiagramObject each time it is executed.
I added the ReloadDiagram so you can see it without having to close/reopen the diagram.

Code: [Select]
sub main
dim currentDiagram as EA.Diagram
set currentDiagram = Repository.GetCurrentDiagram()
if currentDiagram.DiagramObjects.Count > 0 then
currentDiagram.DiagramObjects.DeleteAt 0,true
end if
Repository.ReloadDiagram(currentDiagram.DiagramID)
end sub

main

Geert

tzafrir

  • EA User
  • **
  • Posts: 127
  • Karma: +0/-0
    • View Profile
Re: Add or delete an element from a diagram
« Reply #10 on: December 30, 2015, 08:45:05 am »
Thanks :)
The repository.ReloadDiagram(diagram.DiagramID) solved all my issues

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add or delete an element from a diagram
« Reply #11 on: December 30, 2015, 03:04:57 pm »
Thanks :)
The repository.ReloadDiagram(diagram.DiagramID) solved all my issues
That means it worked before too, you just didn't see it.

Geert