Author Topic: Script: Move element/diagram between Collections  (Read 6909 times)

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Script: Move element/diagram between Collections
« on: December 21, 2011, 09:32:15 pm »
Hi all,


I'm trying to write a script which, given a diagram, creates a new element, moves the diagram into the new element, and moves the elements shown in the diagram into the same new element.

Problem is, the EA.Collection class has no Add() method - just AddNew().
I've tried just setting the ParentID of the diagram to the new element's ID, and deleting it from the package's diagrams collection, but it doesn't seem to work.

It seems to me this is a fairly common need, so does anyone have a script snippet to get it done right?

Cheers,


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

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Script: Move element/diagram between Collectio
« Reply #1 on: December 21, 2011, 09:45:46 pm »
Just a guess: I think you need to set the ParentID as you do AND to set the PackageID to zero.

q.
« Last Edit: December 21, 2011, 09:46:17 pm by qwerty »

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Script: Move element/diagram between Collectio
« Reply #2 on: December 21, 2011, 10:01:48 pm »
No, composite diagrams retain their Package IDs.
My theories are always correct, just apply them to the right reality.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Script: Move element/diagram between Collectio
« Reply #3 on: December 21, 2011, 11:09:49 pm »
I don't think there's a "clean" solution.
I had a similar need to move operations to another element, and I ended up writing a dirty "update" sql stament:
Code: [Select]
       /// <summary>
        /// add a given operation from this element
        /// </summary>
        /// <param name="operation">the operation to add</param>
        public override void addOperation(UMLOperation operation)
        {
            int originalParentID = operation.getParentElement().getID();
            //pretty dirty way to actually move the existing operation to this parent.
            ((EAModel)this.model).getWrappedModel().Execute(@"update t_operation
                                             set object_id = " + this.getID().ToString() +
                                             "where OperationID = " + operation.getID());
            //make sure EA knows both elements have changed.
            ((EAModel)this.model).getWrappedModel().AdviseElementChange(this.getID());
            ((EAModel)this.model).getWrappedModel().AdviseElementChange(originalParentID);
        }

Geert

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Script: Move element/diagram between Collectio
« Reply #4 on: December 21, 2011, 11:54:41 pm »
OK, thanks Geert.

I'm in a production environment so I think I'll bite the bullet this time and do it manually.

Cheers,


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

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Script: Move element/diagram between Collectio
« Reply #5 on: December 22, 2011, 12:40:32 am »
Well, I just moved one element into another by the following code:
Code: [Select]
$child = $rep->GetElementByGUID ('{6F6AB339-7D4A-41ef-B7EA-06C230810971}');
$newParent = $rep->GetElementByGUID ('{782D2EBB-49AA-4a98-A7C8-8834E2773EDC}');
$child->{ParentID} = $newParent->ElementID;
$child->Update();
So you just need to grab the single Elements inside the diagram and set the parentID.

q.

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Script: Move element/diagram between Collectio
« Reply #6 on: December 22, 2011, 08:59:30 am »
After updating the ParentID, call .Refresh() on your collections and they should each now show the appropriate contents.