Author Topic: Move EmbeddedElement from one Element to another  (Read 12824 times)

Viking

  • EA User
  • **
  • Posts: 398
  • Karma: +2/-2
    • View Profile
Move EmbeddedElement from one Element to another
« on: June 11, 2020, 01:54:00 am »
Hello,

I want to move an EmbeddedElement from one Element (“Old Element”) to another (“New Element”). In an object-oriented way I would have added the already existing EmbeddedElement to the EmbeddedElements of the New Element. But there is only a AddNew method without ID or object. So AddNew creates a new EmbeddedElement (although it is already there) and I replace the ID with the ID of the existing EmbeddedElement. Is that correct or is there a better way?

V.

Viking

  • EA User
  • **
  • Posts: 398
  • Karma: +2/-2
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #1 on: June 11, 2020, 02:05:32 am »
I found this thread: https://sparxsystems.com/forums/smf/index.php?topic=3960.0

Seams that I have to set a ParentID instead of an ElementID. I will try.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #2 on: June 11, 2020, 02:09:13 am »
Hello,


AddNew() creates a new object and adds it to a Collection. The Collection acts as a factory. So calling AddNew() in order to move something is wrong.

All model constituent classes in the API have an attribute specifying their owner, eg Package.ParentID, Attribute.ParentID, TaggedValue.ElementID.
In order to move anything from one Collection to another, you don't manipulate the Collections, but change the owner reference in the object you want to move.

So simply change the EmbeddedElement.ParentID from OldElement.ElementID to NewElement.ElementID, then call EmbeddedElement.Update() to write the changes to the database.

Note that the EA API will not automatically update any references. So after you've made the change, your OldElement.EmbeddedElements and NewElement.EmbeddedElements are both out of sync and need to be .Refresh()ed.

HTH,


/Uffe
My theories are always correct, just apply them to the right reality.

Viking

  • EA User
  • **
  • Posts: 398
  • Karma: +2/-2
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #3 on: June 11, 2020, 03:11:45 am »
Hello,
AddNew() creates a new object and adds it to a Collection. The Collection acts as a factory. So calling AddNew() in order to move something is wrong.
All model constituent classes in the API have an attribute specifying their owner, eg Package.ParentID, Attribute.ParentID, TaggedValue.ElementID.
In order to move anything from one Collection to another, you don't manipulate the Collections, but change the owner reference in the object you want to move.
So simply change the EmbeddedElement.ParentID from OldElement.ElementID to NewElement.ElementID, then call EmbeddedElement.Update() to write the changes to the database.
Note that the EA API will not automatically update any references. So after you've made the change, your OldElement.EmbeddedElements and NewElement.EmbeddedElements are both out of sync and need to be .Refresh()ed.
HTH, /Uffe

Many thanks, @Uffe.

I do not have to move the realtionships, right?

qwerty

  • EA Guru
  • *****
  • Posts: 13545
  • Karma: +395/-300
  • I'm no guru at all
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #4 on: June 11, 2020, 03:20:18 am »
No, you don't. Connectors are bound to elements, irrelevant where they are (whether under a package or another element).

q.

Viking

  • EA User
  • **
  • Posts: 398
  • Karma: +2/-2
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #5 on: June 11, 2020, 03:29:33 am »
No, you don't. Connectors are bound to elements, irrelevant where they are (whether under a package or another element).
q.

Many thanks @Uffe and @Qwerty. It works :-)

But now another question: I moved the Element to another place on the Diagram by script. The EmbeddedElement does not move. But when I move the Element by hand, the EmbeddedElement moves to its Element. I updated the diagram, reloaded it, updated the diagramobjects, etc.

Do i have to move the EmbeddedElement as well?
« Last Edit: June 11, 2020, 03:38:02 am by Viking »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13067
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Move EmbeddedElement from one Element to another
« Reply #6 on: June 11, 2020, 03:34:29 am »
Yes, you'll have to move the embedded element as well.

Geert

Viking

  • EA User
  • **
  • Posts: 398
  • Karma: +2/-2
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #7 on: June 11, 2020, 03:41:22 am »
Yes, you'll have to move the embedded element as well.
Geert

Curiously it does know its position. Why does the EmbeddedElement move to the correct position when I change the diagram by hand, but it does absolutly not via script. Is it a bug or a feature?

qwerty

  • EA Guru
  • *****
  • Posts: 13545
  • Karma: +395/-300
  • I'm no guru at all
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #8 on: June 11, 2020, 04:50:18 am »
Well, by hand it goes to the hand coded part of EA that makes these things. When you use the automation you have to care for them by yourself. Looks harder, but you can make funny things via automation you can't do manually.

q.

Viking

  • EA User
  • **
  • Posts: 398
  • Karma: +2/-2
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #9 on: June 11, 2020, 04:10:34 pm »
Well, by hand it goes to the hand coded part of EA that makes these things. When you use the automation you have to care for them by yourself. Looks harder, but you can make funny things via automation you can't do manually.
q.

Yes, maybe. But it takes some programming effort to move the EmbeddedElements accordingly.

Many thanks to @Uffe, @Geert and @qwerty.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #10 on: June 11, 2020, 05:30:20 pm »
It might be worth sending in a feature request to ask that the diagram refresh function (in the GUI) should check for embedded elements which are outside of their parent.

/U
My theories are always correct, just apply them to the right reality.

Viking

  • EA User
  • **
  • Posts: 398
  • Karma: +2/-2
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #11 on: June 17, 2020, 08:00:43 pm »
Hi,
I want to move a DiagramObject to another place on the diagram. Die Code changes the left-coordinate from 603 to 229, which is fine. But after Update(), it changes the coordinate again (to 513). I did this often, but not with EmbeddedElements. Is Update() to a DiagramObject wrong? But I use that often for DiagramObjects. What is wrong? V.

Code: [Select]
Debug.WriteLine("Element.Name: " + EmbeddedElement.Name + " " + ElementDiagramObject.left); // which is 603
moveHorizonal = 374;
ElementDiagramObject.left = ElementDiagramObject.left - moveHorizonal;
Debug.WriteLine("ElementOfNewElement.Name: " + EmbeddedElement.Name + " " + ElementDiagramObject.left); // 229
ElementDiagramObject.Update();
Debug.WriteLine("ElementOfNewElement.Name: " + EmbeddedElement.Name + " " + ElementDiagramObject.left); // now 513

qwerty

  • EA Guru
  • *****
  • Posts: 13545
  • Karma: +395/-300
  • I'm no guru at all
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #12 on: June 17, 2020, 08:26:53 pm »
It's basically correct. IIRC when moving embedded elements you need to calculate exactly (!) the border position. Else EA will do some "auto-correct".

q.

Viking

  • EA User
  • **
  • Posts: 398
  • Karma: +2/-2
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #13 on: June 17, 2020, 08:50:24 pm »
It's basically correct. IIRC when moving embedded elements you need to calculate exactly (!) the border position. Else EA will do some "auto-correct". q.

Many thanks, qwerty. I will try.

Do you know, if both borders need to have to same coordinates. Currently I position the Embedded Element in analogy to the old position.

qwerty

  • EA Guru
  • *****
  • Posts: 13545
  • Karma: +395/-300
  • I'm no guru at all
    • View Profile
Re: Move EmbeddedElement from one Element to another
« Reply #14 on: June 17, 2020, 09:39:55 pm »
Sorry, I don't remember (too long ago). Anyhow, you just need to experiment with one horizontal and one veritcal edge of the outer element. Best you move a port manually and look which coordinates EA gives it in comparison to its embedding element. IIRC a port has 15x15 size and so you have a 7 offset. No idea how to cope with ports that have adjustable size.

q.