Sparx Systems Forum
Enterprise Architect => General Board => Topic started by: tzafrir 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
-
You have to create a new diagram object on the diagram
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
EA.Diagram.DiagramObjects.DeleteAt()
Geert
-
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
-
You forgot to save the DiagramObject using Update()
Geert
-
:( 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
-
- If you pass someElement as parameter you shouldn't use selected.ElementID later on
- DiagramObjects.AddNew is a bit special. You should pass a position string as the name, and an empty string for Type see http://sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/addandmanagediagrams.html (http://sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/addandmanagediagrams.html)
- Collection.DeleteAdd() expects the index of the element in the collection, not the ID of the element (you also made the same someElement/selected mistake here too)
Geert
-
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...
-
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
-
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?
-
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.
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
-
Thanks :)
The repository.ReloadDiagram(diagram.DiagramID) solved all my issues
-
Thanks :)
The repository.ReloadDiagram(diagram.DiagramID) solved all my issues
That means it worked before too, you just didn't see it.
Geert