Script for collecting elements in a target package

Often in an ArchiMate project repository you want to separate the elements from the diagrams and move them to an objects package in EA. Rene Frieswijk one of our active community members have developed a script for this to move all the elements to an objects package. Please see the script below

 

option explicit

 

!INC Local Scripts.EAConstants-VBScript

 

Repository.EnsureOutputVisible "Script"

 

sub Collect2Objecten()

 

dim targetPackage

dim thePackage as EA.Package

dim targetPackageId

 

set thePackage = Repository.GetTreeSelectedPackage()

'Configure below value if the name of the target package needs to be different. 

targetPackage = "Objecten"

Session.Output ("Targetpackage: " & targetPackage)

 

targetPackageId = GetTargetPackageIdByName (targetPackage, thePackage) 

Session.Output ("TargetpackageId: " & targetPackageId)

 

if targetPackageId <> "" Then

Session.Output ("OK")

MoveElementsFromSubpackages thePackage, targetPackageId 

MoveElementsFromMainPackage thePackage, targetPackageId

Repository.RefreshModelView thePackage.PackageID

else

Session.Output ("Creating target package...")

dim package as EA.Package

Set package = thePackage.Packages.AddNew(targetPackage,"")

package.Update()

package.Packages.Refresh()

targetPackageId = package.PackageID

Session.Output (targetPackageid)

Session.Output (package.Name)

Session.Output ("TargetpackageId: " & targetPackageId)

MoveElementsFromSubpackages thePackage, targetPackageId 

MoveElementsFromMainPackage thePackage, targetPackageId

Repository.RefreshModelView thePackage.PackageID

end if

Session.Output ("Done!")

end sub

 

Collect2Objecten

 

Function MoveElementsFromMainPackage (thePackage, targetPackageId)

dim q

q=0

dim currentPackage as EA.Package

set currentPackage = thePackage

Session.Output ("Main package: " & currentPackage.Name)

for q = 0 to currentPackage.Elements.Count  - 1 

dim currentElement as EA.Element

set currentElement = currentPackage.Elements.GetAt( q )

Session.Output ("Moving element " & currentElement.Name)

currentElement.PackageID = targetPackageId

currentElement.Update()

next

End function

 

Function GetTargetPackageIdByName(targetPackage, thePackage )

dim j

j=0

for j = 0 to thePackage.Packages.Count -1

dim currentPackage as EA.Package

set currentPackage = thePackage.Packages.GetAt( j )

Session.Output( "Package: " & currentPackage.Name &  " PackageID: " & currentPackage.PackageID )

if currentPackage.Name = targetPackage Then

Session.Output( "PackageID:" & currentPackage.PackageID)

dim targetPackageId 

targetPackageId = currentPackage.PackageID

GetTargetPackageIdByName = targetPackageId

end if

next

end Function

 

Function MoveElementsFromSubPackages(thePackage, targetPackageId)

dim l,m

 

'   moving elements from subpackages to targetpackage

for l = 0 to thePackage.Packages.Count -1

dim currentPackage as EA.Package

set currentPackage = thePackage.Packages.GetAt( l )

Session.Output( "==>Package: " & currentPackage.Name &  " PackageID: " & currentPackage.PackageID )

 

for m = 0 to currentPackage.Elements.Count  - 1 

dim currentElement as EA.Element

set currentElement = currentPackage.Elements.GetAt( m )

Session.Output ("Moving element: " & currentElement.Name & " to targetpackage (" & currentElement.PackageID & ")")

currentElement.PackageID = targetPackageId

    currentElement.Update()

next

 

MoveElementsFromSubPackages currentPackage, targetPackageId

next

end Function

 

With this script doing this by hand is automatedso a nice feature. Please note that this is also implemented in the IDEA version where you can define a targetpackage for every stereotype in ArchiMate. Thanks to Rene Frieswijk for sharing this script!