option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Iterates through an EAP file using recursion.
' 
' Related APIs
' =================================================================================
' Repository API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/repository3.html
'
sub RecursiveModelDumpExample()

	' Show the script output window
	Repository.EnsureOutputVisible "Script"

	Session.Output( "VBScript RECURSIVE MODEL DUMP EXAMPLE" )
	Session.Output( "=======================================" )
	
	' Iterate through all models in the project
	dim currentModel as EA.Package
	
	for each currentModel in Repository.Models
		' Recursively process this package
		DumpPackage "", currentModel
	next
	
	Session.Output( "Done!" )
	
end sub

'
' Outputs the packages name and elements, and then recursively processes any child 
' packages
'
' Parameters:
' - indent A string representing the current level of indentation
' - thePackage The package object to be processed
'
sub DumpPackage ( indent, thePackage )
	
	' Cast thePackage to EA.Package so we get intellisense
	dim currentPackage as EA.Package
	set currentPackage = thePackage
	
	' Add the current package's name to the list
	Session.Output( indent & currentPackage.Name & " (PackageID=" & currentPackage.PackageID & ")" )
	
	' Dump the elements this package contains
	DumpElements indent & "    ", currentPackage
	
	' Recursively process any child packages
	dim childPackage as EA.Package
	for each childPackage in currentPackage.Packages
		DumpPackage indent & "    ", childPackage
	next
	
end sub

'
' Outputs the elements of the provided package to the Script output window
'
' Parameters:
' - indent A string representing the current level of indentation
' - thePackage The package object to be processed
'
sub DumpElements ( indent, thePackage )
	
	' Cast thePackage to EA.Package so we get intellisense
	dim currentPackage as EA.Package
	set currentPackage = thePackage
	
	' Iterate through all elements and add them to the list
	dim currentElement as EA.Element
	for each currentElement in currentPackage.Elements
		Session.Output( indent & "::" & currentElement.Name & _
			" (" & currentElement.Type & _
			", ID=" & currentElement.ElementID & ")" )
	next
	
end sub

RecursiveModelDumpExample
