Sparx Systems Forum

Enterprise Architect => General Board => Topic started by: yonatan.lehman on January 25, 2022, 02:49:51 am

Title: Cloning elements
Post by: yonatan.lehman 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





 
Title: Re: Cloning elements
Post by: Geert Bellekens on January 25, 2022, 03:50:03 am
Code: [Select]
destElement.PackageID = destPackage.PackageID;
destElement.Update();

Geert
Title: Re: Cloning elements
Post by: yonatan.lehman 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)



Title: Re: Cloning elements
Post by: Geert Bellekens 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
Title: Re: Cloning elements
Post by: qwerty 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.