Author Topic: Cloning elements  (Read 1356 times)

yonatan.lehman

  • EA User
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
Cloning elements
« on: January 25, 2022, 02:49:51 am »
I need to programmatically move a Component from one Package to another Package.
(equivalent to Cut and Paste)

Searching around I see that there is now an Element Clone method but I could not find out how to use it.

I can see that I can add an Element to a package by doing
 
     var destElement = destPackage.Elements.AddNew(srcElement.Name, srcElement.Type);

  but how do I set the destination Element attributes ?

I can clone an Element within its parent package by doing
    var destElement = srcElement.Clone();

But how do I then move it to destPackage ?

Thanks





 

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 11812
  • Karma: +457/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Cloning elements
« Reply #1 on: January 25, 2022, 03:50:03 am »
Code: [Select]
destElement.PackageID = destPackage.PackageID;
destElement.Update();

Geert

yonatan.lehman

  • EA User
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
Re: Cloning elements
« Reply #2 on: January 25, 2022, 10:40:36 pm »
Hi Geert
Thanks!

Does that mean that after the assignment to PackageID and the Update then element will now be in destPackage.Elements?

 i.e. the clone is equivalent to to :
var destElement = destPackage.Elements.AddNew(srcElement.Name, srcElement.Type);
destElement.CopyAllContent(srcElement)
(Where CopyAllContent is an API I invented)




Geert Bellekens

  • EA Guru
  • *****
  • Posts: 11812
  • Karma: +457/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Cloning elements
« Reply #3 on: January 25, 2022, 10:46:10 pm »
Hi Geert
Thanks!

Does that mean that after the assignment to PackageID and the Update then element will now be in destPackage.Elements?

 i.e. the clone is equivalent to to :
var destElement = destPackage.Elements.AddNew(srcElement.Name, srcElement.Type);
destElement.CopyAllContent(srcElement)
(Where CopyAllContent is an API I invented)
Yes, but you might need a destPackage.Elements.Refresh() before it actually shows up in that collection.

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 12776
  • Karma: +362/-295
  • I'm no guru at all
    • View Profile
Re: Cloning elements
« Reply #4 on: January 25, 2022, 11:07:57 pm »
Just as a side note: you are talking about moving. Copy and clone are something very different.

q.