option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Examples of how to access and use element extras - such as scenarios, constraints 
' and requirements.
' 
' NOTE: Requires an element to be selected in the Project Browser
' 
' Related APIs
' =================================================================================
' Element API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/element2.html
'
sub ElementExtrasExample()

	' Show the script output window
	Repository.EnsureOutputVisible "Script"
	
	' Get the currently selected element in the tree to work on
	dim theElement as EA.Element
	set theElement = Repository.GetTreeSelectedObject()
	
	if not theElement is nothing and theElement.ObjectType = otElement then
	
		dim i
	
		Session.Output( "VBScript ELEMENT EXTRAS EXAMPLE" )
		Session.Output( "=======================================" )
		Session.Output( "Working on element '" & theElement.Name & "' (Type=" & theElement.Type & _
			", ID=" & theElement.ElementID & ")" )
			
		' ==================================================
		' MANAGE ELEMENT CONSTRAINTS
		' ==================================================
		Session.Output( "Constraints" )
		
		' Add a new constraint
		dim constraints as EA.Collection
		set constraints = theElement.Constraints
		
		dim newConstraint as EA.Constraint
		set newConstraint = constraints.AddNew( "MyConstraint", "Pre-condition" )
		newConstraint.Update()
		constraints.Refresh()
		
		Session.Output( "    Added new Constraint: " & newConstraint.Name )
		
		set newConstraint = nothing
		
		' List all element constraints
		for i = 0 to constraints.Count - 1
			dim currentConstraint as EA.Constraint
			set currentConstraint = constraints.GetAt( i )
			
			Session.Output( "    Constraint: " & currentConstraint.Name )
			
			' Delete the constraint that we just added
			if currentConstraint.Name = "MyConstraint" then
				constraints.DeleteAt i, false
				Session.Output( "    Deleted Constraint: " & currentConstraint.Name )
			end if
		next
		
		set constraints = nothing

		' ==================================================
		' MANAGE ELEMENT EFFORTS
		' ==================================================
		Session.Output( "Efforts" )
		
		' Add a new effort
		dim efforts as EA.Collection
		set efforts = theElement.Efforts
		
		dim newEffort as EA.Effort
		set newEffort = efforts.AddNew( "MyEffort", "Coding" )
		newEffort.Update()
		efforts.Refresh()
		
		Session.Output( "    Added new Effort: " & newEffort.Name )
		
		set newEffort = nothing

		' List all efforts
		for i = 0 to efforts.Count - 1
			dim currentEffort as EA.Effort
			set currentEffort = efforts.GetAt( i )
			
			Session.Output( "    Effort: " & currentEffort.Name )
			
			' Delete the effort we just added
			if currentEffort.Name = "MyEffort" then
				efforts.DeleteAt i, false
				Session.Output( "    Deleted Effort: " & currentEffort.Name )
			end if			
		next
		
		set efforts = nothing

		' ==================================================
		' MANAGE ELEMENT RISKS
		' ==================================================
		Session.Output( "Risks" )
		
		' Add a new risk
		dim risks as EA.Collection
		set risks = theElement.Risks
		
		dim newRisk as EA.Risk
		set newRisk = risks.AddNew( "MyRisk", "Time" )
		newRisk.Update()
		risks.Refresh()
		
		Session.Output( "    Added new Risk: " & newRisk.Name )
		
		set newRisk = nothing
		
		' List all element risks
		for i = 0 to risks.Count - 1
			dim currentRisk as EA.Risk
			set currentRisk = risks.GetAt( i )
			
			Session.Output( "    Risk: " & currentRisk.Name )
			
			' Delete the risk that we just added
			if currentRisk.Name = "MyRisk" then
				risks.DeleteAt i, false
				Session.Output( "    Deleted Risk: " & currentRisk.Name )
			end if
		next

		set risks = nothing

		' ==================================================
		' MANAGE ELEMENT METRICS
		' ==================================================
		Session.Output( "Metrics" )
		
		' Add a new metric
		dim metrics as EA.Collection
		set metrics = theElement.Metrics
		
		dim newMetric as EA.Metric
		set newMetric = metrics.AddNew( "MyMetric", "Cost" )
		newMetric.Update()
		metrics.Refresh()
		
		Session.Output( "    Added new Metric: " & newMetric.Name )
		
		set newMetric = nothing
		
		' List all metrics
		for i = 0 to metrics.Count - 1
			dim currentMetric as EA.Metric
			set currentMetric = metrics.GetAt( i )
			
			Session.Output( "    Metric: " & currentMetric.Name )
			
			' Delete the metric we just added
			if currentMetric.Name = "MyMetric" then
				metrics.DeleteAt i, false
				Session.Output( "    Deleted Metric: " & currentMetric.Name )
			end if
		next

		set metrics = nothing
		
		' ==================================================
		' MANAGE ELEMENT TAGGED VALUES
		' ==================================================
		Session.Output( "Tagged Values" )
		
		' Add a new tag
		dim tags as EA.Collection
		set tags = theElement.TaggedValues
		
		dim newTag as EA.TaggedValue
		set newTag = tags.AddNew( "MyTag", "Number" )
		newTag.Update()
		tags.Refresh()
		
		Session.Output( "    Added new Tag: " & newTag.Name )
		
		set newTag = nothing
		
		' List all element tags
		for i = 0 to tags.Count - 1
			dim currentTag as EA.TaggedValue
			set currentTag = tags.GetAt( i )
			
			Session.Output( "    Tagged Value: " & currentTag.Name )
			
			' Delete the tag we just added
			if currentTag.Name = "MyTag" then
				tags.DeleteAt i, false
				Session.Output( "    Deleted Tag: " & currentTag.Name )
			end if
		next

		set tags = nothing
		
		' ==================================================
		' MANAGE ELEMENT SCENARIOS
		' ==================================================		
		Session.Output( "Scenarios" )
		
		' Add a new scenario
		dim scenarios as EA.Collection
		set scenarios = theElement.Scenarios
		
		dim newScenario as EA.Scenario
		set newScenario = scenarios.AddNew( "MyScenario", "Alternate" )
		newScenario.Update()
		scenarios.Refresh()
		
		Session.Output( "    Added new Scenario: " & newScenario.Name )
		
		set newScenario = nothing
		
		' List all element scenarios
		for i = 0 to scenarios.Count - 1
			dim currentScenario as EA.Scenario
			set currentScenario = scenarios.GetAt( i )
			
			Session.Output( "    Scenario: " & currentScenario.Name )
			
			' Delete the Scenario that we just added
			if currentScenario.Name = "MyScenario" then
				scenarios.DeleteAt i, false
				Session.Output( "    Deleted Scenario: " & currentScenario.Name )
			end if
		next
		
		set scenarios = nothing
		
		' ==================================================
		' MANAGE ELEMENT FILES
		' ==================================================		
		Session.Output( "Files" )
		
		' Add a new issue
		dim files as EA.Collection
		set files = theElement.Files
		
		dim newFile as EA.File
		set newFile = files.AddNew( "MyFile", "doc" )
		newFile.Update()
		files.Refresh()
		
		Session.Output( "    Added new File: " & newFile.Name )
		
		set newFile = nothing
		
		' List all element files
		for i = 0 to files.Count - 1
			dim currentFile as EA.File
			set currentFile = files.GetAt( i )
			
			Session.Output( "    File: " & currentFile.Name )
			
			' Delete the file that we just added
			if currentFile.Name = "MyFile" then
				files.DeleteAt i, false
				Session.Output( "    Deleted File: " & currentFile.Name )
			end if
		next
		
		set files = nothing
		
		' ==================================================
		' MANAGE ELEMENT TESTS
		' ==================================================
		Session.Output( "Tests" )
		
		' Add a new test
		dim tests as EA.Collection
		set tests = theElement.Tests
		
		dim newTest as EA.Test
		set newTest = tests.AddNew( "MyTest", "Unit" )
		newTest.Update()
		tests.Refresh()
		
		Session.Output( "    Added new Test: " & newTest.Name )
		
		set newTest = nothing
		
		' List all element Tests
		for i = 0 to tests.Count - 1
			dim currentTest as EA.Test
			set currentTest = tests.GetAt( i )
			
			Session.Output( "    Test: " & currentTest.Name )
			
			' Delete the Test that we just added
			if currentTest.Name = "MyTest" then
				tests.DeleteAt i, false
				Session.Output( "    Deleted Test: " & currentTest.Name )
			end if
		next
		
		set tests = nothing
		
		' ==================================================
		' MANAGE ELEMENT ISSUES
		' ==================================================
		Session.Output( "Issues" )
		
		' Add a new issue
		dim issues as EA.Collection
		set issues = theElement.Issues
		
		dim newIssue as EA.Issue
		set newIssue = issues.AddNew( "MyIssue", "Defect" )
		newIssue.Update()
		issues.Refresh()
		
		Session.Output( "    Added new Issue: " & newIssue.Name )
		
		set newIssue = nothing
		
		' List all element issues
		for i = 0 to issues.Count - 1
			dim currentIssue as EA.Issue
			set currentIssue = issues.GetAt( i )
			
			Session.Output( "    Issue: " & currentIssue.Name )
			
			' Delete the issue that we just added
			if currentIssue.Name = "MyIssue" then
				issues.DeleteAt i, false
				Session.Output( "    Deleted Issue: " & currentIssue.Name )
			end if
		next
		
		set issues = nothing
		
		Session.Output( "Done!" )
		
	else
		' No item selected in the tree, or the item selected was not an element
		MsgBox( "This script requires an element be selected in the Project Browser." & vbCrLf & _
			"Please select an element in the Project Browser and try again." )
	end if


end sub

ElementExtrasExample
