Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: MatthiasVDE on February 11, 2020, 08:35:09 pm

Title: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 11, 2020, 08:35:09 pm
When I create a diagramobject, it automatically adjusts its size (right and bottom parameters) to the actual size of the diagramobject, for example a class.

Code: [Select]
dim messageRootObject as EA.DiagramObject
set messageRootObject = messageDiagram.DiagramObjects.AddNew("l=10;r=;t=20;b=")
messageRootObject.ElementID = element.ElementID
messageRootObject.Update()
messageDiagram.DiagramObjects.Refresh()
Repository.ReloadDiagram(messageDiagram.DiagramID)

But this is not saved to the database, it's not possible to get the actual size of a diagramobject.
This displays always '0'.
Quote
Session.Output messageRootObject.right
Session.Output messageRootObject.bottom
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: Paolo F Cantoni on February 11, 2020, 09:11:04 pm
When I create a diagramobject, it automatically adjusts its size (right and bottom parameters) to the actual size of the diagramobject, for example a class.

Code: [Select]
dim messageRootObject as EA.DiagramObject
set messageRootObject = messageDiagram.DiagramObjects.AddNew("l=10;r=;t=20;b=")
messageRootObject.ElementID = element.ElementID
messageRootObject.Update()
messageDiagram.DiagramObjects.Refresh()
Repository.ReloadDiagram(messageDiagram.DiagramID)

But this is not saved to the database, it's not possible to get the actual size of a diagramobject.
This displays always '0'.
Quote
Session.Output messageRootObject.right
Session.Output messageRootObject.bottom
If I understand you correctly, Matthias,
You've stumbled upon one of the first "Gotchas" of diagramobject processing.
The API returns, NOT the size of the object on the (live) diagram, but the size of the object as it was last saved with a diagram save.  So UNTIL you save the diagram with the object, you'll see the behaviour you documented.

As qwerty would say, Sparxian logic...

Paolo
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 11, 2020, 09:13:15 pm
When I create a diagramobject, it automatically adjusts its size (right and bottom parameters) to the actual size of the diagramobject, for example a class.

Code: [Select]
dim messageRootObject as EA.DiagramObject
set messageRootObject = messageDiagram.DiagramObjects.AddNew("l=10;r=;t=20;b=")
messageRootObject.ElementID = element.ElementID
messageRootObject.Update()
messageDiagram.DiagramObjects.Refresh()
Repository.ReloadDiagram(messageDiagram.DiagramID)

But this is not saved to the database, it's not possible to get the actual size of a diagramobject.
This displays always '0'.
Quote
Session.Output messageRootObject.right
Session.Output messageRootObject.bottom
If I understand you correctly, Matthias,
You've stumbled upon one of the first "Gotchas" of diagramobject processing.
The API returns, NOT the size of the object on the (live) diagram, but the size of the object as it was last saved with a diagram save.  So UNTIL you save the diagram with the object, you'll see the behaviour you documented.

As qwerty would say, Sparxian logic...

Paolo
And how do I save that?
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: qwerty on February 11, 2020, 09:34:56 pm
Save it. Re-reload the object. Read the size.

q.
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 11, 2020, 09:38:03 pm
Save it. Re-reload the object. Read the size.

q.

I did this:
Code: [Select]
messageRootObject.Update()
messageDiagram.DiagramObjects.Refresh()
Repository.ReloadDiagram(messageDiagram.DiagramID)
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: peterc on February 11, 2020, 09:47:02 pm
Don't you also need a Repository.SaveDiagram(messageDiagram.DiagramID) before you reload it?
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 11, 2020, 09:51:42 pm
Code: [Select]
dim l, r, t, b
l = "10"
r = ""
t = "20"
b = ""
dim messageRootObject as EA.DiagramObject
set messageRootObject = messageDiagram.DiagramObjects.AddNew("l="&l&";r="&r&";t="&t&";b="&b, "")
messageRootObject.ElementID = element.ElementID
messageRootObject.Update()
messageDiagram.DiagramObjects.Refresh()
Repository.SaveDiagram(messageDiagram.DiagramID)
Repository.ReloadDiagram(messageDiagram.DiagramID)

Session.Output messageRootObject.right
Session.Output messageRootObject.bottom

The output stays '0', but if you take a look on the diagram, the left and bottom aren't 0 so I don't get it why it always return '0'
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: Geert Bellekens on February 11, 2020, 10:00:46 pm
Remove messageDiagram.DiagramObjects.Refresh(), you don't need that here.

Also, try filling in all parameters (l, r, t, b)

Geert
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 11, 2020, 10:08:31 pm
Remove messageDiagram.DiagramObjects.Refresh(), you don't need that here.

Also, try filling in all parameters (l, r, t, b)

Geert

Assume that I have a class element, with 50 attributes. When I create this diagramobject with parameters, left=10, right=50, top=20, bottom=50 the diagramobject will be displayed with a larger width then 50 - 10 (right - left) and with a larger height then 50 - 20 (bottom - top) because Sparx EA auto-resizes the diagramobject to the minimal size to display everything.

My question is, how can I get the positioning of the diagramobject, after the auto-resize that Sparx EA did?
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: Uffe on February 11, 2020, 10:31:17 pm
My question is, how can I get the positioning of the diagramobject, after the auto-resize that Sparx EA did?
You need to retrieve the DiagramObject from the database. The one you got from AddNew is not kept up-to-date by the API.

So after your call to messageDiagram.DiagramObjects.Refresh() (reloading the list of DiagramObjects from the database), you need to traverse messageDiagram.DiagramObjects, locate the one whose .ElementID matches element.ElementID and use that. You can reuse your messageRootObject variable for this, but you need to read new data into it.

HTH,


/Uffe
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 11, 2020, 10:33:21 pm
My question is, how can I get the positioning of the diagramobject, after the auto-resize that Sparx EA did?
You need to retrieve the DiagramObject from the database. The one you got from AddNew is not kept up-to-date by the API.

So after your call to messageDiagram.DiagramObjects.Refresh() (reloading the list of DiagramObjects from the database), you need to traverse messageDiagram.DiagramObjects, locate the one whose .ElementID matches element.ElementID and use that. You can reuse your messageRootObject variable for this, but you need to read new data into it.

HTH,


/Uffe

So I can't do this without querying the database?
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: Geert Bellekens on February 11, 2020, 11:32:59 pm
Be careful. Sometimes reloading stuff via Refresh() is not enough.
Sometimes you need to actually get the object from the repository again using Repository.GetxxxByID or something similar.

Geert
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 12, 2020, 12:10:06 am
When I look at the database, the right and bottom for the diagramobjects is also '0'. And when I drag it a little in EA, and look then in the database, then I see the correct right and bottom as displayed on the diagram.

EDIT:

By first doing Repository.OpenDiagram(messageDiagram.DiagramID) before saving, I can see the correct right and bottom in the database. But stil no correct right and bottom in the object model.
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: qwerty on February 12, 2020, 01:07:19 am
I also tried that myself and saw the same effect. Initially storing a DO leaves bottom/right zero. So EA will calculate the size depending on the defaults. These can be stored in EA's guts or in the shape script (which ever takes over). Since there is no operation to retrieve the default size your only solutions: a) send a feature request or b) forget it. Probably you should go for c) think about some other solution.

q.
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 12, 2020, 01:29:19 am
This works:

Code: [Select]
dim messageRootObject as EA.DiagramObject
set messageRootObject = messageDiagram.DiagramObjects.AddNew("l="&l&";r="&r&";t="&t&";b="&b, "")
messageRootObject.ElementID = element.ElementID
messageRootObject.Update()
Repository.OpenDiagram(messageDiagram.DiagramID)
Repository.SaveDiagram(messageDiagram.DiagramID)
Repository.ReloadDiagram(messageDiagram.DiagramID)
messageDiagram.DiagramObjects.Refresh()

dim newMessageRootObject as EA.DiagramObject
for each newMessageRootObject in messageDiagram.DiagramObjects
if newMessageRootObject.ElementID = element.ElementID then
set messageRootObject = newMessageRootObject
end if
next
Session.Output messageRootObject.right
Session.Output messageRootObject.bottom
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: qwerty on February 12, 2020, 02:01:06 am
What do you mean with "this works"? Now you're acutally supplying b/r which you initially didn't.

q.
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 12, 2020, 02:21:26 am
What do you mean with "this works"? Now you're acutally supplying b/r which you initially didn't.

q.

It prints the correct right and bottom values, as displayed on the diagram.

         Session.Output messageRootObject.right
         Session.Output messageRootObject.bottom
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: qwerty on February 12, 2020, 02:59:07 am
Well, again: your OP left bottom/right empty. Here you seem to supply them.

q.
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 12, 2020, 06:33:24 pm
Well, again: your OP left bottom/right empty. Here you seem to supply them.

q.

No I didn't.

Recap:
When you add an element to a diagram (diagramobject), you need to provide left, right, top, bottom. But when the element (for example a class) is too large (many attributes and long names), and you provide right = 20 and bottom = 40, then the real size of the element overrides the values that you provide for right and bottom, this is the same effect as when you do not provide anything. When you take a look on the diagram, you will see that the right and bottom values are different than those you provide (or didn't provide if you left it blanco).

But:
When you read the values right and bottom of the diagramObject, you will determine that they are equal to those you provide (or didn't provide if you left it blanco).

Right?

To solve this problem, I did the following:

Code: [Select]

dim l, r, t, b
l = "20"
r = "" --> If I put 30 here, the effect on the diagram is the same
t = "20 "
b = "" --> If I put 40 here, the effect on the diagram is the same               
dim messageRootObject as EA.DiagramObject
set messageRootObject = messageDiagram.DiagramObjects.AddNew("l="&l&";r="&r&";t="&t&";b="&b, "")
messageRootObject.ElementID = element.ElementID
--> the element is a class with a long name and many attributes

Code: [Select]
messageRootObject.Update()
Repository.OpenDiagram(messageDiagram.DiagramID)

--> look at the diagram, the right and bottom are not "zero", neither "30" or "40" if you provided this. This is because the element has long names and too many attributes for those right and bottom values.

When you read the values right and bottom from your diagramobject at this point, you will see 'zero' or '30' or '40', but not the values as displayed on your diagram.
But if you want to read them, you need the following workaround, otherwise it doesn't work.

Code: [Select]
Repository.SaveDiagram(messageDiagram.DiagramID)
--> First open it before you can save it

Code: [Select]
Repository.ReloadDiagram(messageDiagram.DiagramID)
messageDiagram.DiagramObjects.Refresh()
--> This code is also needed, otherwise I doesn't work.

Code: [Select]

dim newMessageRootObject as EA.DiagramObject
for each newMessageRootObject in messageDiagram.DiagramObjects
     if newMessageRootObject.ElementID = element.ElementID then
set messageRootObject = newMessageRootObject
     end if
next
--> This is also necessary

Code: [Select]

Session.Output messageRootObject.right
Session.Output messageRootObject.bottom
Now you see the values, based on the real size as displayed on the diagram.
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: qwerty on February 12, 2020, 07:45:15 pm
Now I see. Another weird EA hack indeed. I also tried parts of that but not in the exact combination (I wasn't that desperate as you ;-). I'll play around a bit more later...

q.
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: qwerty on February 12, 2020, 08:37:56 pm
Interestingly for me it does not work (most likely a version issue). I noticed that the size of the newly created diagram object varies depending on the use of save/reload and its order. Well, as long as it works for you... For me I'll close Pandorra's jar.

q.
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: MatthiasVDE on February 12, 2020, 08:42:03 pm
Interestingly for me it does not work (most likely a version issue). I noticed that the size of the newly created diagram object varies depending on the use of save/reload and its order. Well, as long as it works for you... For me I'll close Pandorra's jar.

q.

I use 15.1
Title: Re: Left, right, top and bottom properties of DiagramObject
Post by: qwerty on February 12, 2020, 09:40:22 pm
I'm on 13.5. Who knows what happened.

q.