Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: FSXManu on May 13, 2022, 05:30:35 pm
-
Hi
I'm trying to create a DiagramObject with the API but for some reason it won't turn up in the Database and just won't save.
I tested it with the basic sample given on the Documentation (https://sparxsystems.com/enterprise_architect_user_guide/14.0/automation/the_addnew_function.html) but it still won't work. Any idea what I am doing wrong?
Edit: attached my code I tried:
diaObj = diagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "")
diaObj.Update()
and also
diagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "")
diagram.DiagramObjects.Update()
Cheers
Manuel
-
Hi
I'm trying to create a DiagramObject with the API but for some reason it won't turn up in the Database and just won't save.
I tested it with the basic sample given on the Documentation (https://sparxsystems.com/enterprise_architect_user_guide/14.0/automation/the_addnew_function.html) but it still won't work. Any idea what I am doing wrong?
Cheers
Manuel
Manuel,
If you post the code you are using we could maybe help.
Without the code it's just guessing.
Geert
-
Hi
I'm trying to create a DiagramObject with the API but for some reason it won't turn up in the Database and just won't save.
I tested it with the basic sample given on the Documentation (https://sparxsystems.com/enterprise_architect_user_guide/14.0/automation/the_addnew_function.html) but it still won't work. Any idea what I am doing wrong?
Cheers
Manuel
Manuel,
If you post the code you are using we could maybe help.
Without the code it's just guessing.
Geert
Hi Geert
Yes attached it i posted it before I attached it but I modified it now. Sorry for that.
Manuel
-
diaObj = diagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "")
diaObj.Update()
The problem is that you now have a diagramObject that doesn't represent anything.
DiagramObject is nothing more then the link between a diagram and an Element. It says: This element is shown on this diagram at location x with properties y
So you'll have to set the elementID on the diagramObject before saving.
diaObj = diagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "")
diaObj.ElementID = myElement.ElementID
diaObj.Update()
There's no deed to call Update() on the DiagramObjects collection.
Geert
-
diaObj = diagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "")
diaObj.Update()
The problem is that you now have a diagramObject that doesn't represent anything.
DiagramObject is nothing more then the link between a diagram and an Element. It says: This element is shown on this diagram at location x with properties y
So you'll have to set the elementID on the diagramObject before saving.
diaObj = diagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "")
diaObj.ElementID = myElement.ElementID
diaObj.Update()
There's no deed to call Update() on the DiagramObjects collection.
Geert
I see. I tried that now but for some reason I still can't see a new row in the t_diagramobjects table.
I thought maybe I need to call update on the diagram.DiagramObjects because the Documentation of the Collection Class says "Updates the current Collection object after modification or appending a new item. " for the Update call. But I will get an error saying Update doesn't work on <Unknown> so I assume it doesn't know that this is a collection.
-
I see. I tried that now but for some reason I still can't see a new row in the t_diagramobjects table.
You may be undoing your changes then by saving the diagram or something like that.
Make sure you don't do anything else after the diagramObject.Update() method and then look in the database.
See https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Projects/Project%20M/Diagram%20Group/Add%20defaults%20from%20template%20diagram.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Projects/Project%20M/Diagram%20Group/Add%20defaults%20from%20template%20diagram.vbs) for an example script that adds elements to a diagram.
Geert
-
I see. I tried that now but for some reason I still can't see a new row in the t_diagramobjects table.
You may be undoing your changes then by saving the diagram or something like that.
Make sure you don't do anything else after the diagramObject.Update() method and then look in the database.
See https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Projects/Project%20M/Diagram%20Group/Add%20defaults%20from%20template%20diagram.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Projects/Project%20M/Diagram%20Group/Add%20defaults%20from%20template%20diagram.vbs) for an example script that adds elements to a diagram.
Geert
Thank you! It finally worked. It didn't show up until I also assigned diaObj.Sequence a value and then it worked. Thank you very much
Manuel
-
It didn't show up until I also assigned diaObj.Sequence a value and then it worked.
That can't be it. I've almost never set the sequence value on diagramObjects.
Geert
-
Here is the function that adds a legend element to a diagram, but could work for any element. I did reload the diagram to display the element (legend). The code is:
function AddLegendToDiagram(diagram, legend) {
// Assign diagram as EA.Diagram and legend as EA.Element for code complete
var theDiagram as EA.Diagram;
var theLegend as EA.Element;
theDiagram = diagram;
theLegend = legend;
// Values for top left coords of legend, height and width
var left = 22;
var top = -17; // must be negative
var width = 150;
var height = 171;
var right = left + width;
var bottom = -top + height; // negate the top so the height math is correct
var geometry = "l=" + left + ";" +
"r=" + right +";" +
"t=" + top + ";" +
"b=" + bottom + ";";
var diagramLegend as EA.DiagramObject;
diagramLegend = theDiagram.DiagramObjects.AddNew(geometry,"");
diagramLegend.ElementID = theLegend.ElementID;
diagramLegend.left = left;
diagramLegend.top = top;
diagramLegend.Update();
theDiagram.DiagramObjects.Refresh();
Repository.ReloadDiagram(theDiagram.DiagramID);
}
-
Hi Phil,
I think the following lines are not necessary:
diagramLegend.left = left;
diagramLegend.top = top;
Should have been already taken care of the geometry string you passed as parameter to AddNew
theDiagram.DiagramObjects.Refresh();
Only needed if you wanted to iterate the DiagramObjects collection in memory afterwards without reloading the diagram object from the database.
Geert
-
Thanks for feedback Geert, removed the lines you suggested and all works just fine.
Thanks
Phil