To execute this script import it into a model, open a diagram, right-click a class element and select Scripts | AddConnectorDemo. In the example model, add_con_demo, the script is located in the APG Diagram Scripts group. To install the script from the XML file:
Installation
Open the model
Import the script to set the «RequirementsRelated» association
- Project | Model Import/Export | Import Reference Data
- Select the add_con_demo.xml script file
- Select Automation Scripts in the import dialog box
- Select Import
- If it's not already opened, open the Scripting window
-- View | Scripting
- press the Refresh the Script Tree button in the Scripting window
- Verify that the APG Diagram Scripts group contains the AddConnectorDemo script
option explicit
!INC Local Scripts.EAConstants-VBScript
'
' Script Name: AddConnectorDemo
' Author:J.D. Baker
' Purpose:Assign a relationship for elements in a class diagram. This technique
' should work for any diagram type
' Date:10 July 2012
'
'
' Diagram Script main function
'
sub OnDiagramScript()
' dim objects for setting connectors
Dim i
Dim target As EA.Element
Dim source As EA.Element
Dim association As EA.Connector
' Get a reference to the current diagram
Dim currentDiagram As EA.Diagram
currentDiagram = Repository.GetCurrentDiagram()
If Not currentDiagram Is Nothing Then
' Get a reference to any selected connector or objects
Dim selectedConnector As EA.Connector
Dim selectedObjects As EA.Collection
selectedConnector = currentDiagram.SelectedConnector
selectedObjects = currentDiagram.SelectedObjects
If Not selectedConnector Is Nothing Then
' A connector is selected
Session.Prompt("This script requires that only an Class element be selected", promptOK)
ElseIf selectedObjects.Count > 0 Then
Dim currentElement As EA.DiagramObject
currentElement = selectedObjects.GetAt(0)
' One or more diagram objects are selected
'currentElement is a diagramObject. We need to get the corresponding element in the project browser
source = Repository.GetElementByID(currentElement.ElementID)
' test to see that the object selected is a Class
If (source.Type = "Class") Then
'now we need to identify all of the non-selected objects
Dim allObjects As EA.Collection
allObjects = currentDiagram.DiagramObjects
Dim thisObject As EA.DiagramObject
'now loop through the collection of objects setting a realization with
'a RequirementRelated stereotype to all objects
For i = 0 To allObjects.Count - 1
'get the next DiagramObject
thisObject = allObjects.GetAt(i)
'make sure we don't create a self reference to the selected object
If thisObject.ElementID <> currentElement.ElementID Then
target = Repository.GetElementByID(thisObject.ElementID)
'before creating a new relationship, check to see if one already exists
'If there is no existing relationship and the target is the correct type,
'add one to the collection of connectors for this source
If (target.Connectors.Count = 0 And target.type = "Interface") Then
association = source.Connectors.AddNew("", "Realization")
'now connect it to the target
association.SupplierID = target.ElementID
association.Stereotype = "RequirementRelated"
'the space surrounding the -> operator is essential if you are
'creating a directed association. Note the discontinuity between
'the use of the identifiers target and destination
'association.Direction = "Source -> Destination"
association.Update()
End If
End If
Next
Repository.ReloadDiagram(currentDiagram.DiagramID)
Else
Session.Prompt("You must run the script with a Class element selected", promptOK)
End If
Else
' Nothing is selected
Session.Prompt("Nothing selected. This script requires a Class to be selected", promptOK)
End If
Else
Session.Prompt("This script requires a diagram to be visible", promptOK)
End If
End Sub
OnDiagramScript