11
« on: December 06, 2017, 10:46:51 pm »
Very often when writing scripts I have to do something with a selection of elements in the selected package or or nested packages.
Because iterating all elements of all packages recursively is very slow, I usually try to get the elements I need using an SQL query.
Now when using SQL searches you can use the macro #Branch# to get all package ID's of the currently selected package and all nested packages. Because that macro can't be used in scripting I'm making the package ID string myself. This works ok, but it is not super fast. In the model I was using it on this function alone took about 4 minutes (of the 11 minutes of the whole script)
Does anyone have an idea for a more efficient implementation?
I guess I could bypass the API completely and rely only on database queries to get the ID's of all the packages.
'get the package id string of the given package tree
function getPackageTreeIDString(package)
'initialize at "0"
getPackageTreeIDString = "0"
dim packageTree
dim currentPackage as EA.Package
if not package is nothing then
'get the whole tree of the selected package
set packageTree = getPackageTree(package)
' get the id string of the tree
getPackageTreeIDString = makePackageIDString(packageTree)
end if
end function
'returns an ArrayList of the given package and all its subpackages recursively
function getPackageTree(package)
dim packageList
set packageList = CreateObject("System.Collections.ArrayList")
addPackagesToList package, packageList
set getPackageTree = packageList
end function
'add the given package and all subPackges to the list (recursively
function addPackagesToList(package, packageList)
dim subPackage as EA.Package
'add the package itself
packageList.Add package
'add subpackages
for each subPackage in package.Packages
addPackagesToList subPackage, packageList
next
end function
'make an id string out of the package ID of the given packages
function makePackageIDString(packages)
dim package as EA.Package
dim idString
idString = ""
dim addComma
addComma = false
for each package in packages
if addComma then
idString = idString & ","
else
addComma = true
end if
idString = idString & package.PackageID
next
'if there are no packages then we return "0"
if idString = "" then
idString = "0"
end if
'return idString
makePackageIDString = idString
end function
Geert