Author Topic: Script for copying diagrams  (Read 1840 times)

Wishmaster84

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Script for copying diagrams
« on: December 16, 2024, 10:19:58 pm »
Hi Everyone,

I need to create a JScript for copying a diagram from a package source to a destination source. I'm able to copy through the use of targetPackage.Diagrams.AddNew functions and setting elementId, top, bottom, right and left for all the elements to move within the diagram. The issue that I have is that the copied diagram is not well formatted and there are some elements that are overshadowing others or the positions are different. Is there a way to simplify the copy and have a full copied diagrams identical to the original through functions in a script?
Thanks
« Last Edit: December 17, 2024, 01:14:32 am by Wishmaster84 »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13287
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Script for copying diagrams
« Reply #1 on: December 16, 2024, 10:56:57 pm »
I did that once by moving it to an empty package, and then copying the package (with the diagram). After that, move the diagram back to it's original location.

Code: [Select]
function copyDiagram(diagram, targetOwner)
dim copiedDiagram as EA.Diagram
'initialize at nothing
set copiedDiagram = nothing
'get the owner package
dim ownerPackage as EA.Package
set ownerPackage = Repository.GetPackageByID(diagram.PackageID)
'check if we need to lock the package to clone it
if isRequireUserLockEnabled() then
dim ownerOfOwnerPackage as EA.Package
if ownerPackage.ParentID > 0 then
set ownerOfOwnerPackage = Repository.GetPackageByID(ownerPackage.ParentID)
if not ownerOfOwnerPackage.ApplyUserLock() then
'tell the user we couldn't do it and then exit the function
msgbox "Could not lock package " &  ownerPackage.Name & " in order to copy the diagram " & diagram.Name,vbError,"Could not lock Package"
exit function
end if
end if
end if
'then create a temporary package to clone
dim tempPackage as EA.Package
dim targetPackage as EA.Package
if targetOwner.ObjectType = otElement then
set targetPackage = Repository.GetPackageByID(targetOwner.PackageID)
else
set targetPackage = targetOwner
end if
set tempPackage = targetPackage.Packages.AddNew("temp", "")
tempPackage.Update
'move te diagram to the temp package
diagram.PackageID = tempPackage.PackageID
diagram.Update
'then actually clone the owner package
dim clonedPackage as EA.Package
set clonedPackage = tempPackage.Clone()
'reset diagram packageID
diagram.PackageID = ownerPackage.PackageID
diagram.Update
' if isRequireUserLockEnabled() then
' clonedPackage.ApplyUserLockRecursive true,true,true
' end if
'then get the diagram corresponding to the diagram to copy
set copiedDiagram = getCorrespondingDiagram(clonedPackage,diagram)
'set the owner of the copied diagram
if targetOwner.ObjectType = otElement then
copiedDiagram.ParentID = targetOwner.ElementID
else
copiedDiagram.PackageID = targetOwner.PackageID
end if
'set the styleEx as this is not always copied correctly
copiedDiagram.StyleEx = diagram.StyleEx
'save the update to the owner
copiedDiagram.Update
'delete the cloned package
deletePackage clonedPackage
'delete the temp package
deletePackage tempPackage
'return the copied diagram
set copyDiagram = copiedDiagram
end function

Geert