Book a Demo

Author Topic: Scripting: copy diagram?  (Read 6899 times)

semo

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Scripting: copy diagram?
« on: October 14, 2015, 09:07:37 pm »
Hi,

I'm looking for a way to automate a (shallow) copy of a diagram.
Is there any way to do this?

Context: we have one 'master' diagram, and several 'variants'. The variants are only different from the master in that some of the elements are coloured (depending on links with other elements). The colouring is done through scripting. Obviously each variant has different elements that are coloured.
so far so good.
But what if another element is added to the master diagram? Our current workaround is to manually emty the variant diagram, and then copy-paste the elements from the master diagram (and then run the colouring script).

In other words: we don't need the 'old' variant diagram anymore, but need a copy of the master diagram: all elements and their layout.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Scripting: copy diagram?
« Reply #1 on: October 14, 2015, 10:00:16 pm »
You could try moving it in a separate package and then use EA.Package.Clone() to create a copy of the package.

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Scripting: copy diagram?
« Reply #2 on: October 14, 2015, 10:32:32 pm »
The clone would probably not work as synch. You need to make a list of all diagramObjects, sort by objectId and add the missing ones.

q.

semo

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Scripting: copy diagram?
« Reply #3 on: October 14, 2015, 11:17:52 pm »
Thanks for the replies!

Geert; I tried using the package clone:
+diagram layout is preserved
-additional scripting needed to remove old diagrams and rename new diagrams and move them to correct position

Qwerty; would there be an easy way to add the missing diagramObject with the same size and in the same position as in the 'master' diagram?
The only way I can think of is using AddNew with the coordinates (left, bottom, top, right) of the diagramObject in the master diagram?

BTW: I'm just getting started with scripting, I got this really helpful book to get me started - not sure if I'm allowed to link to it here  ;)

semo

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Scripting: copy diagram?
« Reply #4 on: October 15, 2015, 12:21:27 am »
Update - I can create a copy of the diagram (including layout) using something like this:
Code: [Select]
     for(var i = 0; i < sourceDiagramObjects.Count; i++)
      {      
            var sourceElement as EA.DiagramObject;
            sourceElement = sourceDiagramObjects.GetAt(i);
            
            var left, right, top, bottom;
            left = sourceElement.left;
            right = sourceElement.right;
            top = sourceElement.top;
            bottom = sourceElement.bottom;
            
            var position = "l="+left+";r="+right+";t="+top+";b="+bottom+";"
            
            diagramObject = diagramObjects.AddNew(position, "" );
            diagramObject.ElementID( sourceElement.ElementID );
            diagramObject.Update();
            
      }

However, the 'z-order' of the elements isn't always right. Is there any way I can get this info from the source diagram, or, alternatively, change this on the target diagram through scripting? (in this case, elements with a certain stereotype need to be at the bottom)
« Last Edit: October 15, 2015, 12:22:02 am by semo »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Scripting: copy diagram?
« Reply #5 on: October 15, 2015, 12:25:55 am »
Use EA.DiagramObject.Sequence to control the z-order.

Geert

semo

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Scripting: copy diagram?
« Reply #6 on: October 15, 2015, 12:39:02 am »
Hi Geert, that works! (I should have been able to find that, but like I said...still new)

Problem solved... thanks again for your help!