Monday, 16 July 2012 18:57

Adding Connectors to Diagrams

Written by
Rate this item
(1 Vote)

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:


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"



              End If

            End If





          Session.Prompt("You must run the script with a Class element selected", promptOK)

        End If


        ' Nothing is selected

        Session.Prompt("Nothing selected. This script requires a Class to be selected", promptOK)

      End If


      Session.Prompt("This script requires a diagram to be visible", promptOK)

    End If

   End Sub


Read 12902 times Last modified on Tuesday, 09 April 2013 04:29

J.D. Baker

Armstrong Process Group (Consultant)
J.D. Baker, Principal Systems Engineer with Armstrong Process Group, Inc., is a recognized expert in software and system architecture, systems engineering, software development, iterative/agile development, object-oriented analysis and design, the Unified Modeling Language (UML), the UML Profile for Systems Engineering (SysML), use case driven requirements, and process improvement. His activities have included research and development of services for automatic test, leading the development of a model-based methodology for the design and construction of complex systems, and providing modeling support for teams creating OMG standards.

1 comment

  • Comment Link Vera Neupert Tuesday, 28 June 2022 12:48 posted by Vera Neupert

    How can I show/hide the connectors on a diagram?
    How can I create relations between requirements and classes via script

Login to post comments