Author Topic: DiagramObjects.AddNew doesn't add a new diagramobject  (Read 4061 times)

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
DiagramObjects.AddNew doesn't add a new diagramobject
« 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
« Last Edit: May 13, 2022, 05:38:16 pm by FSXManu »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #1 on: May 13, 2022, 05:38:04 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?

Cheers
Manuel
Manuel,

If you post the code you are using we could maybe help.
Without the code it's just guessing.

Geert

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #2 on: May 13, 2022, 05:39:00 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?

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

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #3 on: May 13, 2022, 05:49:21 pm »
Code: [Select]
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.

Code: [Select]
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

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #4 on: May 13, 2022, 06:04:42 pm »
Code: [Select]
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.

Code: [Select]
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.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #5 on: May 13, 2022, 06:16:15 pm »

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 for an example script that adds elements to a diagram.

Geert

FSXManu

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #6 on: May 13, 2022, 06:32:14 pm »

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

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #7 on: May 13, 2022, 06:36:01 pm »
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

philchudley

  • EA User
  • **
  • Posts: 744
  • Karma: +21/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #8 on: May 13, 2022, 11:15:21 pm »
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);
}
Models are great!
Correct models are even greater!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #9 on: May 13, 2022, 11:35:43 pm »
Hi Phil,

I think the following lines are not necessary:

Code: [Select]
   diagramLegend.left = left;
   diagramLegend.top = top;
Should have been already taken care of the geometry string you passed as parameter to AddNew

Code: [Select]
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

philchudley

  • EA User
  • **
  • Posts: 744
  • Karma: +21/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Re: DiagramObjects.AddNew doesn't add a new diagramobject
« Reply #10 on: May 13, 2022, 11:58:56 pm »
Thanks for feedback Geert, removed the lines you suggested and all works just fine.

Thanks

Phil
Models are great!
Correct models are even greater!