Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Scorpion2k on August 08, 2016, 06:13:49 am

Title: VBscript: Adding multiplicity
Post by: Scorpion2k on August 08, 2016, 06:13:49 am
Hello everyone,

This is my first post on this forum. I'm using Enterprise Architect for a little while now and I'm focussing on generating a datamodel from Excel.

At first I tried using "eaexcelimporter_v4.xls" but it did not work for me. The company I work at has some strict network policies and makes importing from Excel to EA impossible. So I started a VBscript in EA and now it reads the Excel.
So far I learned how to create an element with properties, attributes with properties, tagged values and creating connections. Now I have one challenged left, multiplicities.

I have really learned a lot from the local scripts and the excel from Geert Bellekens. But I can't find anything on the internet about adding multiplicities.

It reads the information from an Excel with the following layout:
ABCDEFGH
1Source_NameSource_IDTarget_NameTarget_IDTagged_ValueSource_MultiplicityTarget_Multiplicity
2Chair12975Desk12945P110..*
3Computer12954Desk12945P21..*0..*

It creates an connection with the Source_ID and Target_ID. It also adds a TaggedValue. I want it to add the multiplicity too, but I can't figure out the proper code to do this. I tried many ways, but my work comes to a stand still. I added my code with this post. It's no clean code and has no error handling, but that comes later, after I get used again to VBscript.

I hope someone can help me to add the multiplicity in VBscript.

Code: [Select]
sub Relations

Repository.EnsureOutputVisible "Script"

dim Package as EA.Package
set Package = Repository.GetTreeSelectedPackage()

if not Package is nothing and Package.ParentID <> 0 then

dim objExcel
dim objWorkbook
dim objWorksheet
dim path
dim file

path = "D:\Modellen\Importbestanden\"
file = "Relationship_Matrix_Input.xlsx"

set objExcel = CreateObject("Excel.Application")
set objWorkbook = objExcel.Workbooks.Open(path & file)
set objWorksheet = objWorkbook.Worksheets("Relations")

dim Object_sourceID as EA.Element
dim Object_targetID as EA.Element
dim Object_source as EA.Element
dim AddRelation as EA.Connector
dim Line
dim Check

Check = ""
Line = 2

Session.Output( "Relations from Excel" )
Session.Output( "====================================" )

dim tags as EA.Collection
dim newTag as EA.AttributeTag
' ----------------------------------

Do Until objWorksheet.Cells(Line,1).Value = ""

If Check <> objWorksheet.Cells(Line, 1).Value Then
Object_sourceID = objWorksheet.Cells(Line, 3).Value
Object_targetID = objWorksheet.Cells(Line, 5).Value

set Object_source = Repository.GetElementByID(Object_sourceID)
set AddRelation = Object_source.Connectors.AddNew("", "Association")

AddRelation.SupplierID = Object_targetID
AddRelation.Update

' TAGS aan en uit zetten
' ----------------------------------------------------------------------------
set tags = AddRelation.TaggedValues
set newTag = tags.AddNew( "Tag Matrix", objWorksheet.Cells(Line, 6).Value )
newTag.Update()
tags.Refresh()

Check = objWorksheet.Cells(Line, 1).Value
Session.Output( objWorksheet.Cells(Line, 2).Value & " AddRelation met " & objWorksheet.Cells(Line, 4).Value & " with priority " & objWorksheet.Cells(Line, 6).Value & ".")

set Object_sourceID = nothing
set Object_targetID = nothing
set Object_source = nothing
set AddRelation = nothing
set tags = nothing
set newTag = nothing

End If

Line = Line + 1

Loop

Session.Output( "====================================" )
Session.Output( "AddRelations verwerkt" )

objExcel.Quit

else

MsgBox( "No package found" & vbCrLf & _
"Select a package and try again." )
end if

end sub

Relations
Title: Re: VBscript: Adding multiplicity
Post by: Dermot on August 08, 2016, 04:01:25 pm
See the Connector End Class on:
http://sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/connectorend.html
This covers the Cardinality.
Title: Re: VBscript: Adding multiplicity
Post by: Geert Bellekens on August 08, 2016, 04:35:37 pm
See AssociationEnd.cs (https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EAWrappers/AssociationEnd.cs)
and Multiplicity.cs (https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EAWrappers/Multiplicity.cs) for a C# example on handling multiplicity.

Geert
Title: Re: VBscript: Adding multiplicity
Post by: Scorpion2k on August 08, 2016, 06:56:04 pm
Thank you for the advice. I think I can learn a lot from the links. I will try this out this week. Thank you!
Title: Re: VBscript: Adding multiplicity
Post by: Scorpion2k on August 18, 2016, 01:19:14 am
The links and examples made me understand the logics of EA a lot better. I solved my problem and enabled me to write more enhanced scripts. In case someone else has the same problem, here's my solution. The solution to my problem was fairly easy. I just needed to add the following lines before the AddRelation.Update

Code: [Select]
AddRelation.ClientEnd.Cardinality = objWorksheet.Cells(Line, 7).Value
AddRelation.SupplierEnd.Cardinality = objWorksheet.Cells(Line, 8).Value

Thanks again!