Book a Demo

Author Topic: Script for making connectors between elements  (Read 5963 times)

pianoman

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Script for making connectors between elements
« on: November 01, 2019, 11:11:38 am »
Hello all,

I'm looking to make a VBScript script to perform an action, which in this case is to add connectors.

I'm not savvy with this type of a script, but I've tried referring to some scripts I've found online to make my script. I've got the below script written, but can't get the results I am looking for. Appreciate any troubleshoot/script revisions anyone can offer.

dim source as EA.Element
dim target as EA.Element
dim conn as EA.Connector

set conn = source.Connectors.AddNew("Testcon", "Association")
conn.SupplierID = target.ElementID(2652)
conn.Update
source.Connectors.Refresh
target.Connectors.Refresh

I am trying to create a connector from element with Object_ID 2651 to element with Object_ID 2652.

Thank you


KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +54/-3
    • View Profile
Re: Script for making connectors between elements
« Reply #1 on: November 01, 2019, 11:55:53 am »
Well you would need to get the source and target objects:

set source = Repository.GetElementByID(2651)
set target = Repository.GetElementByID(2652)

and then to set the SupplierID correctly

conn.SupplierID = 2652

Caveat: I haven't tested this; it looks like it should work ;)

dim source as EA.Element
dim target as EA.Element
dim conn as EA.Connector

set source = Repository.GetElementByID(2651)
set target = Repository.GetElementByID(2652)

set conn = source.Connectors.AddNew("Testcon", "Association")
conn.SupplierID = 2652
conn.Update
source.Connectors.Refresh
target.Connectors.Refresh
The Sparx Team
[email protected]

rupertkiwi

  • EA User
  • **
  • Posts: 133
  • Karma: +5/-0
    • View Profile
Re: Script for making connectors between elements
« Reply #2 on: November 01, 2019, 11:59:47 am »
Hi Pianoman,

this might help:

Function addConnector(SourceElementID, TargetElementID, ConnectorType, connectorname)
Dim SourceElement As EA.element
Dim newconnector As EA.connector
    Dim repository As EA.repository
    Set eaapp = GetObject(, "EA.App")
Set repository = eaapp.repository
Set SourceElement = repository.GetElementByID(SourceElementID)
Set newconnector = SourceElement.connectors.AddNew(" ", ConnectorType)
newconnector.SupplierID = TargetElementID
newconnector.Name = connectorname
newconnector.Update
End Function

pianoman

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Script for making connectors between elements
« Reply #3 on: November 02, 2019, 04:46:18 am »
Thank you both for the help. Went ahead and tried KP's suggestion as it was a minor revision to what I had. And it worked!

What I'm trying to do is add a SQL query into the mix and see if I can add connectors in mass based on the query's specification. So I've tried the following modification utilizing EA's Repository.GetElementSet method. My queries return a list of source Object_IDs and a list of target Object_IDs, between which I want to establish a 1:1 relationship per array row.

For instance:
Query 1 would output
Object 1
Object 2
Object 3

Query 2 would output
Object 4
Object 5
Object 6

And I want to add connectors automatically between Object 1 and Object 4; Object 2 and Object 5; Object 3 and Object 6

I know the below won't execute, any suggestions?

Code: [Select]
dim source as EA.Element
dim target as EA.Element
dim conn as EA.Connector

set source = Repository.GetElementSet(SELECT t_object.Object_ID
FROM t_object INNER JOIN t_diagramobjects ON t_object.Object_ID = t_diagramobjects.Object_ID
WHERE (((t_diagramobjects.Diagram_ID)=14) AND ((t_object.Stereotype)="Risk"));,)

set target = Repository.GetElementSet(SELECT t_object.Object_ID
FROM t_object INNER JOIN t_diagramobjects ON t_object.Object_ID = t_diagramobjects.Object_ID
WHERE (((t_diagramobjects.Diagram_ID)=14) AND ((t_object.Stereotype)="ResourceArtifact"));,)

set conn = source.Connectors.AddNew("", "UAFP::Affects")
conn.SupplierID = Repository.GetElementSet(SELECT t_object.Object_ID
FROM t_object INNER JOIN t_diagramobjects ON t_object.Object_ID = t_diagramobjects.Object_ID
WHERE (((t_diagramobjects.Diagram_ID)=14) AND ((t_object.Stereotype)="ResourceArtifact"));,)

conn.Update
source.Connectors.Refresh
target.Connectors.Refresh

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Script for making connectors between elements
« Reply #4 on: November 02, 2019, 08:01:43 am »
The parameter is a STRING containing the query.

q.

pianoman

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Script for making connectors between elements
« Reply #5 on: November 02, 2019, 10:09:53 am »
The parameter is a STRING containing the query.

q.

Thanks for the reminder. I updated my code as shown below, with quotes around the SQL Query. I still get an error however that says Object doesn't support this property or method: 'Connectors'
Additionally, is there a way to type in anything other than a number for the second parameter of the Repository.GetElementSet(string, number) method? I would like this script to work with differently sized arrays.

Code: [Select]
dim source as EA.Element
dim target as EA.Element
dim conn as EA.Connector

set source = Repository.GetElementSet("SELECT t_object.Object_ID FROM t_object INNER JOIN t_diagramobjects ON t_object.Object_ID = t_diagramobjects.Object_ID WHERE (((t_diagramobjects.Diagram_ID)=14) AND ((t_object.Stereotype)='Risk'))", 2)

set target = Repository.GetElementSet("SELECT t_object.Object_ID FROM t_object INNER JOIN t_diagramobjects ON t_object.Object_ID = t_diagramobjects.Object_ID WHERE (((t_diagramobjects.Diagram_ID)=14) AND ((t_object.Stereotype)='ResourceArtifact'))", 2)

set conn = source.Connectors.AddNew("", "Association")

conn.SupplierID = Repository.GetElementSet("SELECT t_object.Object_ID FROM t_object INNER JOIN t_diagramobjects ON t_object.Object_ID = t_diagramobjects.Object_ID WHERE (((t_diagramobjects.Diagram_ID)=14) AND ((t_object.Stereotype)='ResourceArtifact'))", 2)

conn.Update
source.Connectors.Refresh
target.Connectors.Refresh

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13471
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Script for making connectors between elements
« Reply #6 on: November 02, 2019, 05:43:53 pm »
GetElementSet returns an EA.Collection, not a EA.Element

You don't have to guess these things, they are documented in the help file:
https://www.sparxsystems.com/enterprise_architect_user_guide/15.0/automation/reference.html

Geert

pianoman

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Script for making connectors between elements
« Reply #7 on: November 05, 2019, 11:37:36 am »
Thanks everyone for your guidance and assistance. I will be doing some more exploring with how to tie SQL queries and CreateObject function in VBScript to perhaps create two arrays from which to reference Object_IDs to make connectors in large quantities based on array size.