Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Uffe on December 21, 2011, 09:32:15 pm

Title: Script: Move element/diagram between Collections
Post by: Uffe 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
Title: Re: Script: Move element/diagram between Collectio
Post by: qwerty 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.
Title: Re: Script: Move element/diagram between Collectio
Post by: Uffe on December 21, 2011, 10:01:48 pm
No, composite diagrams retain their Package IDs.
Title: Re: Script: Move element/diagram between Collectio
Post by: Geert Bellekens 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
Title: Re: Script: Move element/diagram between Collectio
Post by: Uffe 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
Title: Re: Script: Move element/diagram between Collectio
Post by: qwerty 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.
Title: Re: Script: Move element/diagram between Collectio
Post by: Aaron B 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.