Book a Demo

Author Topic: Cut/paste an element in the project tree  (Read 4855 times)

damien

  • EA User
  • **
  • Posts: 41
  • Karma: +0/-0
    • View Profile
Cut/paste an element in the project tree
« on: May 19, 2008, 04:56:04 pm »
Hello,

I would know how cut and paste an element form a package to a another package in an addin. The only way i have found to do it, is added a new element to the package.elements collection and copy all properties from the source element to the target element.

Someone can explain to me how can i do it, please

Thanks you
Bye

damien

  • EA User
  • **
  • Posts: 41
  • Karma: +0/-0
    • View Profile
Re: Cut/paste an element in the project tree
« Reply #1 on: May 20, 2008, 01:38:54 am »
up

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Cut/paste an element in the project tree
« Reply #2 on: May 20, 2008, 08:13:40 am »
From an add-in you can set the PackageID, call Update() and possibly Repository.AdviseElementChange().  I haven't checked, but if you have nested elements you may also need to do the same thing for them.
« Last Edit: May 20, 2008, 08:14:29 am by simonm »

Frank Horn

  • EA User
  • **
  • Posts: 535
  • Karma: +1/-0
    • View Profile
Re: Cut/paste an element in the project tree
« Reply #3 on: May 20, 2008, 04:59:08 pm »
Quote
From an add-in you can set the PackageID, call Update() and possibly Repository.AdviseElementChange()

This will move an element, not copy it. EA automation lacks a clone method (and generally EA is weak when it comes to cloning).

I can think of only two ways to clone an element: either creating a new one and copying all properties (meaning quite a lot of code: all properties, tagged values, stereotypes and what not), or using xmi export:

1. Write a recursive function which yields the path (array of child element indices) from the selected element's parent package to the selected element.
2. Export parent package to a temp file.
3. Import temp file to a new package with the strip GUIDs option.
4. Delete temp file.
5. Get cloned element using path from step 1.
6. Move cloned element to destination package (set PackageID).
7. Delete imported package.

Not nice, but at least there's no danger of forgetting to copy some properties or having to change the code when a new EA version introduces new properties. And there may be properties which are not accessible via automation at all.

damien

  • EA User
  • **
  • Posts: 41
  • Karma: +0/-0
    • View Profile
Re: Cut/paste an element in the project tree
« Reply #4 on: May 20, 2008, 05:07:20 pm »
Thanks you very much it works
no need to Repository.AdviseElementChange()