Book a Demo

Author Topic: Copy an element  (Read 6476 times)

Heidi Thomson

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Copy an element
« on: January 18, 2012, 10:03:04 pm »
Hi,

Is there a way to create a copy of an element using the automation element?  What I am trying to do is copy hyperlinks from one diagram into another but as I want to change the location of the link, I want a new element and not the same element.

Alternatively, how do I add a new hyperlink element to a diagram? I'm assuming there is a property that I'm not setting but can't find it at the moment. (hence trying to copy an existing one and change it).

Thanks,
Heidi

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Copy an element
« Reply #1 on: January 18, 2012, 10:35:27 pm »
see here: http:// http://www.sparxsystems.com/cgi-bin/yabb/YaBB.cgi?num=1326778856

I guess a copy via automation must be done "manually". I.e.: use add operations.

q.

Luis J. Lobo

  • EA User
  • **
  • Posts: 252
  • Karma: +0/-0
  • IT Consultant
    • View Profile
Re: Copy an element
« Reply #2 on: January 19, 2012, 09:29:41 pm »
Hiperlinks are not Elements, but DiagramObjects.

The Element object has the .Clone() method.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Copy an element
« Reply #3 on: January 20, 2012, 05:31:14 am »
Quote
The Element object has the .Clone() method.
Definitely more comfortable than using add and copying all properties...

q.

Luis J. Lobo

  • EA User
  • **
  • Posts: 252
  • Karma: +0/-0
  • IT Consultant
    • View Profile
Re: Copy an element
« Reply #4 on: January 20, 2012, 05:33:15 am »
Sorry!! the ".Clone()" method belongs to the Package object, not Element object.

In fact, when I want to clone an object, I do the following:

1. Create a Package named "Temp"
2. Move source element to "Temp" package
3. Clone the "Temp" package
4. Rename the cloned package to "Cloned Element"
5. Get the source element out of the "Temp" package
6. Delete "Temp" package.

When the process finish, I get a package called "Cloned Element" which contains the cloned element inside.
« Last Edit: January 20, 2012, 05:39:15 am by Deiser »

Luis J. Lobo

  • EA User
  • **
  • Posts: 252
  • Karma: +0/-0
  • IT Consultant
    • View Profile
Re: Copy an element
« Reply #5 on: January 20, 2012, 05:48:45 am »
Here is the code (valid as VBScript):


Sub CloneElement (ElementToClone)
        Dim i
        Dim Found
        Dim OriginalPackage
        Dim TemporalPackage

        Set OriginalPackage = Repository.GetPackageByID(ElementToClone.PackageID)
        Set TemporalPackage = OriginalPackage.Packages.AddNew("Cloned Element", "Nothing")
        TemporalPackage.Update()
        OriginalPackage.Packages.Refresh()
        OriginalPackage.Update()
        ElementToClone.PackageID = TemporalPackage.PackageID
        ElementToClone.Update()
        TemporalPackage.Elements.Refresh()
        TemporalPackage.Update()
        TemporalPackage.Clone()
        ElementToClone.PackageID = OriginalPackage.PackageID
        ElementToClone.Update()
        OriginalPackage.Elements.Refresh()
        OriginalPackage.Update()
        i = 0
        Found = False
        While i < OriginalPackage.Packages.Count And Not Found
            If OriginalPackage.Packages.GetAt(i).PackageID = TemporalPackage.PackageID Then
                OriginalPackage.Packages.DeleteAt(i, False)
                Found = True
            End If
            i = i + 1
        End While
        OriginalPackage.Packages.Refresh()
        OriginalPackage.Update()
        Repository.RefreshModelView(OriginalPackage.PackageID)
End Sub

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Copy an element
« Reply #6 on: January 20, 2012, 07:30:09 am »
Funny though that element does not have the clone method but only packages. One should think that element cloning should be more easy.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Copy an element
« Reply #7 on: January 20, 2012, 05:37:36 pm »
Quote
Here is the code (valid as VBScript):
Hmm, good one.
Need to remember that next time I need something like that.

Geert

PS. Have you noticed that a a lot of the automation work is trying to find workarounds for features that one would expect to be standard :-/

Luis J. Lobo

  • EA User
  • **
  • Posts: 252
  • Karma: +0/-0
  • IT Consultant
    • View Profile
Re: Copy an element
« Reply #8 on: January 20, 2012, 07:14:22 pm »
Yes, in this case, the deep copy-paste of an element (with all its relationships) should be a "native" function.

There are several situations like this, but it's understandable in a tool with so much functionality.

I don't know what I do without the API & scripting...!

Pawel Jasinski

  • EA User
  • **
  • Posts: 29
  • Karma: +0/-0
    • View Profile
Re: Copy an element
« Reply #9 on: January 25, 2012, 08:29:40 pm »
There is a little side effect to clone. It is only relevant if the element to be copied is used as Classifier (it is used as type of other element).
After clone the classifier ids are no longer pointing to original one, but to the cloned one. Once you decide to remove cloned element, you end up with type pointing to nowhere.

I also tried to use ExportPackageXMI, ImportPackageXMI (with stripped GUIDs), same problem :-(

Cheers,
Pawel
« Last Edit: January 26, 2012, 11:03:45 am by pawel.jasinski »