'Enterprise Architect Python Scripting - Add an element to a Diagram

I am trying to write a python script so that I can add an element to a diagram in Sparx Enterprise Architect.

TestDiagEl=TestDiag.DiagramObjects.AddNew("l=10;r=110;t=-20;b=-80","")
TestDiagEl.ElementID=TestEl.ElementID
TestDiagEl.Update
TestDiag.Update
eaRep.ReloadDiagram(TestDiag.DiagramID)

It doesn't seem to work, what am I doing wrong?

EDIT It seems that @geert was right, additionally i didnt add () after Update.



Solution 1:[1]

When writing code you should know what each line of code does, and why you are writing it.

In this case

TestDiagEl=TestDiag.DiagramObjects.AddNew("l=10;r=110;t=-20;b=-80","")

Creates a new diagramObject in memory

TestDiagEl.ElementID=TestEl.ElementID

Sets the elementID of the diagramObject to the elementID of my element

TestDiagEl.Update

Save the diagramObject to the database

TestDiag.Update

Save the current diagram in memory to the database

eaRep.ReloadDiagram(TestDiag.DiagramID)

Get the diagramDetails from the database and show them in the GUI

One problem is the TestDiag.Update. Since your diagram in memory doesn't know about the new DiagramObject yet, you are effectively undoing the addition of the new DiagramObject. Remove that line, and all should be OK.

Another problem is the parameter you pass to the DiagramObjects.AddNew method. The top and bottom values should be positive as you can see in the provided documentation So:

TestDiagEl=TestDiag.DiagramObjects.AddNew("l=10;r=110;t=20;b=80","")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1