Sparx Systems Forum
Enterprise Architect => General Board => Topic started by: pianoman 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
-
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
-
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
-
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?
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
-
The parameter is a STRING containing the query.
q.
-
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.
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
-
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 (https://www.sparxsystems.com/enterprise_architect_user_guide/15.0/automation/reference.html)
Geert
-
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.