Author Topic: script to move a diagram from a source to a destination  (Read 1763 times)

yonatan.lehman

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
script to move a diagram from a source to a destination
« on: June 09, 2022, 08:49:37 pm »
Hi
I need to move a diagram and all of its diagram objects from a source to a destination
The source and the destination can be a Package or an Element (e.g. an Activity)
I manage to move all the diagram objects by changing the ParentID (to an Element) or the Package (by setting PackageID)


But I can't move the actual Diagram - setting the ParentID and/or PackageID makes no difference
It doesn't move. If I move it using the UI then the  ParentID and/or PackageID update as expected.

Do I have to create a new Diagram on the destination and copy all the fields from the source diagram or is there an easier way to do it?

I'm working in JScript EA14
Thanks

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: script to move a diagram from a source to a destination
« Reply #1 on: June 09, 2022, 08:54:30 pm »
Are you sure you are actually saving your changes by calling Update()?
Always post your script code if you have a scripting problem. Otherwise we can only guess what you are doing wrong.

In cases like this you need to reload (you can do that in a script as well) in order to let EA's GUI know you have changed something.

Geert

yonatan.lehman

  • EA User
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
Re: script to move a diagram from a source to a destination
« Reply #2 on: June 12, 2022, 06:35:31 pm »
I finally got it to work. Posting here in case it's useful to others, and because I'm curious if I understood what was wrong and fixed it correctly and if all the steps are really necessary

Highlights:
1) When moving a source diagram or element S to a destination package or element D:
  a) set S.PackageID to the D.PackageID as a 1st step
  b) After setting the PackageID
      i) if the destination is a Package set the S.ParentID to zero
     ii) If the destination is an Element set S.ParentID to D. ElementId.
2) To make sure the UI is updated
   a)  before moving anything do
Code: [Select]
          Repository.EnableUIUpdates(false)   b)    After moving everything do
Code: [Select]
         Repository.RefreshModelView(theSrc.PackageID)
Repository.RefreshModelView(theDest.PackageID)

Code: [Select]
function transferObjects(src, dest) {
var theSrc as EA.Package;
theSrc = src;
var theDest as EA.Element
theDest = dest;
var transferToPackage;
     transferToPackage =  (theDest.Type == "Package");
Repository.EnableUIUpdates(false);
for (var i=0; i<src.Elements.Count; i++) {
var e as EA.Element;
elem = src.Elements.GetAt(i);
if (transferToPackage) {
elem.PackageID = dest.PackageID;
elem.ParentID =0;
} else {
elem.PackageID = dest.PackageID;
elem.ParentID = dest.ElementId;
}
elem.Update()
}
for (var i=0; i<src.Diagrams.Count; i++) {
var dsrc as EA.Diagram;
diag = theSrc.Diagrams.GetAt(i);
if (transferToPackage) {
diag.PackageID = theDest.PackageID;
diag.ParentID = 0
} else {
diag.PackageID = dest.PackageID;
diag.ParentID = theDest.ElementId;
}
diag.Update();
//dest.Update();
}
theSrc.Update();
theDest.Update();
Repository.RefreshModelView(theSrc.PackageID)
Repository.RefreshModelView(theDest.PackageID)
 }
« Last Edit: June 12, 2022, 06:43:07 pm by yonatan.lehman »