Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: J.D. Baker on February 12, 2013, 06:43:05 am
-
I'm writing a script that includes the code below. I sucessfully create the new package and the new use case but the set newElement = currentElement assignments fail with errors like
Object required: 'currentElement.Author.Author'
The content of the currentElement fields looks ok.
Session.Output( "found use case" )
'get the Packages collection for the current package
dim packages as EA.Collection
set packages = thePackage.Packages
'create a new package
dim newPackage as EA.Package
set newPackage = packages.AddNew("PJ" & thePackage.Name , "Package")
newPackage.Update()
Session.Output ( newPackage.Name )
' create use case instance
dim newElement as EA.Element
set newElement = newPackage.Elements.AddNew( thePackage.Name, "UseCase")
newElement.Update()
packages.Refresh()
Session.Output("author is " & currentElement.Author)
Session.Output("newElement " & newElement.Type)
Session.Output("currentElement " & currentElement.Type)
set newElement.Author = currentElement.Author
set newElement.Notes = currentElement.Notes
set newElement.Scenarios = currentElement.Scenarios
set newElement.Diagrams = currentElement.Diagrams
-
Unfortunately you don't tell us where currentElement comes from.
q.
-
here's the entire script
option explicit
!INC Local Scripts.EAConstants-VBScript
'
' This script is intended to be executed against a single element in the enterprise repository.
' It will create an instance of the classifier, add instances of any related elements and
' create relationships between those instances to reflect the relationships between the classifiers
' e.g. when the selected element is a use case, the script creates a use case instance, copies
' the scenarios and any related rule sets.
'
' Script Name: UseElement
' Author: J.D. Baker
' Purpose: Use enterprise element in a project
' Date: 10 February 2013
'
'
' Project Browser Script main function
'
sub OnProjectBrowserScript()
' Show the script output window
Repository.EnsureOutputVisible "Script"
' Get the type of element selected in the Project Browser
dim treeSelectedType
treeSelectedType = Repository.GetTreeSelectedItemType()
' Handling Code: Uncomment any types you wish this script to support
' NOTE: You can toggle comments on multiple lines that are currently
' selected with [CTRL]+[SHIFT]+[C].
select case treeSelectedType
' case otElement
' ' Code for when an element is selected
' dim theElement as EA.Element
' set theElement = Repository.GetTreeSelectedObject()
'
case otPackage
' Code for when a package is selected
dim thePackage as EA.Package
set thePackage = Repository.GetTreeSelectedObject()
' determine whether this is a use case or application package
dim theElements as EA.Collection
dim currentElement as EA.Element
dim i
for i = 0 to thePackage.Elements.Count - 1
set currentElement = thePackage.Elements.GetAt(i)
Session.Output ( "i = " & i)
If currentElement.Type = "UseCase" then
' a. Create an instance of the use case and copy all property data, including structured scenarios
' d. Place instance elements in the appropriate package of the project model.
' These include an instance of the use case, and any actors or rule sets associated with the use case.
Session.Output( "found use case" )
'get the Packages collection for the current package
dim packages as EA.Collection
set packages = thePackage.Packages
'create a new package
dim newPackage as EA.Package
set newPackage = packages.AddNew("PJ" & thePackage.Name , "Package")
newPackage.Update()
Session.Output ( newPackage.Name )
' create use case instance
dim newElement as EA.Element
set newElement = newPackage.Elements.AddNew( thePackage.Name, "UseCase")
newElement.Update()
packages.Refresh()
'set newElement.ClassifierID = currentElement.ElementID
Session.Output("author is " & currentElement.Author)
Session.Output("newElement " & newElement.Type)
Session.Output("currentElement " & currentElement.Type)
set newElement.Author = currentElement.Author
set newElement.Notes = currentElement.Notes
set newElement.Scenarios = currentElement.Scenarios
'set newElement.Connectors = currentElement.Connectors
set newElement.Diagrams = currentElement.Diagrams
set newElement.EmbeddedElements = currentElement.EmbeddedElements
Else
if currentElement.Type = "Component" then
Session.Output( "found Application" )
end if
end if
next
'
' b. Determine if links other than the generalization link to the Enterprise Use case exist.
' c. For each link, create an instance of the linked element and create a link between the instances to mirror the classifier links.
' d. Place instance elements in the appropriate package of the project model. These include an instance of the use case, and any actors or rule sets associated with the use case.
'
' case otDiagram
' ' Code for when a diagram is selected
' dim theDiagram as EA.Diagram
' set theDiagram = Repository.GetTreeSelectedObject()
'
' case otAttribute
' ' Code for when an attribute is selected
' dim theAttribute as EA.Attribute
' set theAttribute = Repository.GetTreeSelectedObject()
'
' case otMethod
' ' Code for when a method is selected
' dim theMethod as EA.Method
' set theMethod = Repository.GetTreeSelectedObject()
case else
' Error message
Session.Prompt "This script does not support items of this type. Select a package to use in a project.", promptOK
end select
end sub
OnProjectBrowserScript
-
Because Author is a simple field not an object, you don't need the 'set' command. Just say
newElement.Author = currentElement.Author
newElement.Notes = currentElement.Notes
Your next problem will then be the Scenarios, Diagrams and EmbeddedElements collections. You can't (AFAIK) copy a Collection in one go; you will need to loop through them item by item.