Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Andre_b_b on June 19, 2023, 10:07:30 pm

Title: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 19, 2023, 10:07:30 pm
Hi guys,
i have been working with Enterprise Architect for 2 months and my objective is, from a Excel file, generate a diagram within the relations between the elements(mainly "InformationFlow", "Derive","Usage"). In order to achieve this, i use the MDG Integration Office Add-in and i could do it, but i need to do the process in an automatic way: Run a script inside EA should be enough. I have developed a code in VBScript but it is not working - in fact, i can run the script but anything happens/changes in the software. Can you help me with the code or suggest any other way to achieve my objective? Unfortunately i only have 2 months to finish this project... Here i let the code that was written in the scripting Window of my Enterprise Architect.
Code: [Select]
option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Script Name: import excel into EA
' Author:  Andre
' Purpose: import an excel file in order to generate a diagram and its elements
' Date: 19/06/2023
'
Sub importexcel()   


    ' Set the path and filename of the Excel file to import 
    Dim filePath
    Dim fileName
    filePath = "MyPathtomyFile" 
    fileName = "my_file.xlsx" 
     
    ' Set the name of the package to import the data into 
   Dim packageName
    packageName = "New Package" 
     
    ' Set the name of the object profile and connector profile to use 
   Dim objProfileName
   Dim conProfileName 
    objProfileName = "my_objprofile_name" 
    conProfileName = "my_conprofile_name" 
     
    ' Import the data from the Excel file 
    Dim repository As EA.Repository 
    Set repository = GetObject("MyPathtomyFile", "EA.App").Repository 
    repository.ImportCSVEx filePath & fileName, objProfileName, conProfileName, packageName, False, False, False, False, False, False 
     
    ' Create a new diagram to represent the imported data 
    Dim diagram As EA.Diagram 
    Set diagram = repository.Diagrams.AddNew("name_of_diagram", "ActivityDiagram") 
     diagram.PackageID = repository.GetPackageByGuid(repository.Models.GetAt(0).PackageGUID).PackageID 
     diagram.Update 
     
 
 

    ' Find the objects that were imported and add them to the diagram 
    Dim objects As EA.Collection 
    Set objects = repository.GetElementsByQuery("SELECT Object_ID FROM t_object WHERE Stereotype = 'YourStereotypeName' AND Package_ID = " & repository.GetPackageByName(packageName).PackageID) 
    Dim obj As EA.Element 
    For Each obj In objects 
        Dim diagramObject As EA.DiagramObject 
        Set diagramObject = diagram.DiagramObjects.AddNew("", "") 
        diagramObject.ElementID = obj.ElementID 
        diagramObject.Update 
    Next   
     
    ' Find the connectors that were imported and add them to the diagram 
    Dim connectors As EA.Collection 
    Set connectors = repository.GetElementsByQuery("SELECT Connector_ID FROM t_connector WHERE Package_ID = " & repository.GetPackageByName(packageName).PackageID) 
    Dim con As EA.Connector 
    For Each con In connectors 
        Dim diagramLink As EA.DiagramLink 
        Set diagramLink = diagram.DiagramLinks.AddNew("", "") 
        diagramLink.ConnectorID = con.ConnectorID 
        diagramLink.Update 
    Next 
     
    ' Refresh the view to show the imported data and the new diagram 
    repository.RefreshModelView (packageName) 
     
End Sub 
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 20, 2023, 06:34:52 am
"Is not working" is not a very useful problem statement.

The trick is to start small. make a small part that works, then add another part, work on it until it works, and then continue.

One thing I see is wrong in your code is the way you try to create a diagram

Code: [Select]
Set diagram = repository.Diagrams.AddNew("name_of_diagram", "ActivityDiagram")
There is no such thing as Repository.Diagrams, so that's never going to work.
Instead you create them from the Package.Diagrams collection.
If you do, you won't need this nonsense

Code: [Select]
diagram.PackageID = repository.GetPackageByGuid(repository.Models.GetAt(0).PackageGUID).PackageID
Geert
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Eve on June 20, 2023, 02:10:37 pm
Have you used ChatGPT or similar to write that script?

It looks superficially like an EA script, but I don't think any part of it is reasonable.


My advice. Throw the whole script away and actually look at example scripts and documentation when you start writing.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 21, 2023, 05:55:42 am
I'm guessing something like ChatGPT has been involved in producing this script.

Now while that can be a useful thing to speed up the development once you know what you want to write, I'm afraid it's not a substitute for getting to know the environment and the API.

Geert
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 21, 2023, 06:07:04 pm
Ok, thanks for your help; i will go step by step then :)
I do not understand and i can't find how to add an element from VBScript in EA.
Should i use this command?
Code: [Select]
Set newElement = eaElement.AddNew(name,type)
I want to read the Excel and generate from the columns new elements.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: qwerty on June 21, 2023, 09:15:11 pm
Well, that's not the way to do it. Take a lesson or read something to learn the API. You won't get the codez here.


q.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 21, 2023, 09:45:11 pm
Ok, thanks for your help; i will go step by step then :)
I do not understand and i can't find how to add an element from VBScript in EA.
Should i use this command?
Code: [Select]
Set newElement = eaElement.AddNew(name,type)
I want to read the Excel and generate from the columns new elements.
Adding something is always done on the collection of the owner.

So if you want to add a diagram to a package you do package.Diagrams.AddNew(), if you want to add an attribute to an element, you do Element.Attributes.AddNew()

The whole API is documented here: https://sparxsystems.com/enterprise_architect_user_guide/16.1/add-ins___scripting/reference.html (https://sparxsystems.com/enterprise_architect_user_guide/16.1/add-ins___scripting/reference.html)

Geert
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 21, 2023, 09:47:02 pm
Ok Qwerty, thanks for your attention.
Would you recomend a course or some materials i could use to learn?
 Unfortunately, i have been looking for materials, but i did not find a lot. Iam not expert in Programming and in Enterprise Architect, but i do want to learn and produce content and important tools for me.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 21, 2023, 09:48:00 pm
Thanks Geert. I will read and i will arrive with more questions :)
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: qwerty on June 21, 2023, 10:16:18 pm
Since you asked: see my scripting book (link below). I think it's the easiest way to get accustomed with the API.

q.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 21, 2023, 10:28:39 pm
Thanks Qwerty!
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 23, 2023, 10:37:15 pm
Hi guys,
just to say that i could make an interface with Excel, from a VBscript in Enterprise Architect, in order to generate the elements in Enterprise Architect within a package with the value of the cells of the Excel. Another script is udes to show the elements in the diagram. Next step is to discover how to add the connections between the elements.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 23, 2023, 11:22:54 pm
Next step is to discover how to add the connections between the elements.

Element.Connectors.AddNew()

Geert
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 23, 2023, 11:54:55 pm
Thanks Geert. But how do i put the Source, Target and Type of the connector? Shoud i use the name of Source and Target or their GUIDs?
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 24, 2023, 12:35:13 am
The source is the element you create it on.
The target can be set with the connector.SupplierID

Geert
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 26, 2023, 07:34:15 pm
Nice. Thanks Geert. Should i get the element by GUID? If yes, i am trying to use the code, but it says that there is a "Internal Application Error".
Code: [Select]
' Active Diagram 
Set Diagram = Repository.GetCurrentDiagram() 
 
' Get 2 elements from their GUID 
Set Element1 = Repository.GetElementByGuid("{53C6DD5C-04D6-4b6f-9102-142557A3F4C7}")   
Set Element2 = Repository.GetElementByGuid("{49602501-62AA-4426-A862-45B066F62DF0}") 
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 26, 2023, 07:40:10 pm
On what line exactly does it give you this error?
Are you sure this guid exists in t_object.ea_guid?

Geert
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 26, 2023, 09:37:29 pm
There is this "internal error" in the line "Set Element1 = Repository.GetElementByGuid("{53C6DD5C-04D6-4b6f-9102-142557A3F4C7}")". And the GUID i have copied by right-clicking in the element from the browser and copying the GUID. Here it is the code i have been working with. I am sorry, i do not understand what is the t_object.ea_guid you asked me.

Code: [Select]
option explicit 
 
!INC Local Scripts.EAConstants-VBScript 

sub main 

 

Dim Repository 
Dim Diagram 
Dim Element1
Dim Element2
Dim Connector 
 

Set Repository = CreateObject("EA.Repository") 
 

Set Diagram = Repository.GetCurrentDiagram() 
 

Set Element1 = Repository.GetElementByGuid("{53C6DD5C-04D6-4b6f-9102-142557A3F4C7}")
Set Element2 = Repository.GetElementByGuid("{49602501-62AA-4426-A862-45B066F62DF0}")

Set Connector = Element1.Connectors.AddNew("", "Association")
 
Connector.SupplierID = Element2.ElementID 
 
Diagram.DiagramLinks.Refresh() 
Diagram.Update() 

end sub 
 
main 
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 26, 2023, 11:40:07 pm
Loose the Set Repository = CreateObject("EA.Repository") that creates a new instance of EA (but without a model loaded), which you probably don't want.

The Repository object is a inserted as a global variable in your scripting environment, you can use it without setting it first.

You also don't need the diagram to create an association, and you should only Refresh/Update if really needed.

And you forgot to update() your new connector. Update() means: Save my changes to the database.

Geert
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 27, 2023, 12:02:21 am
Thanks for your help Geert.
Unfortunately, even with the suggestions and corrections you've made, i am still not able to run the script and so, i cannot make the connections. The error given is "this object does not manage" and it does reference to the line of "Element1 = Repository.GetElementByGUID("{53C6DD5C-04D6-4b6f-9102-142557A3F4C7}")"
Code: [Select]
option explicit 
 
!INC Local Scripts.EAConstants-VBScript 
sub main

Dim Element1
Dim Element2
Dim Connector
Element1 = Repository.GetElementByGUID("{53C6DD5C-04D6-4b6f-9102-142557A3F4C7}")
Element2 = Repository.GetElementByGUID("{49602501-62AA-4426-A862-45B066F62DF0}")

Set Connector = Element1.Connectors.AddNew("", "Association")
   Msgbox ("!")
Connector.SupplierID = Element2.ElementGUID
Connector.Update()
end sub 
 
main 

Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 27, 2023, 12:26:58 am
I have managed to create the connection, but how can i turn it visible? I would like to show the relation in a diagram.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 27, 2023, 12:29:48 am
Hi Andre,

I think you might want to follow a few tutorials to get the basics right.
When setting an object to a variable in VBScript you should use set

You got it right at first, and then you changed it for some reason

this Element1 = Repository.GetElementByGUID("{53C6DD5C-04D6-4b6f-9102-142557A3F4C7}") should be set Element1 = Repository.GetElementByGUID("{53C6DD5C-04D6-4b6f-9102-142557A3F4C7}")

And the supplierID is a long, so it will give you an error when you set a string value (GUID) to it.

I can't keep debugging your scripts for you like this.
Checkout https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library There are lots of scripts that you can use as an example there.

Geert
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: qwerty on June 27, 2023, 12:33:15 am
I think you might want to follow a few tutorials to get the basics right.
This was suggested in the very beginning. Doesn't look to have been a fruitful suggestion.

q.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 27, 2023, 12:40:23 am
Thanks guys! I have tried reading the tutorials and everything but i do not understand the documentation, and everything looks advanced to me, so in this moment i am not able to learn - apparently -, that is why i keep asking here in the forum, because i do not find answers in any other place. Sorry for the mistakes and thanks for your help.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: qwerty on June 27, 2023, 12:58:23 am
You know that people like you are called vampires? I'm pretty allergic against those since I had unpleasant experiences in the past.
q.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 27, 2023, 05:27:30 pm
Excuse me, i am not sure what you mean from this comment. I hope you are not judging me for trying to learn programming and how to use Enterprise Architect.
Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Geert Bellekens on June 27, 2023, 05:40:02 pm
Excuse me, i am not sure what you mean from this comment. I hope you are not judging me for trying to learn programming and how to use Enterprise Architect.
Don't worry about it too much. Q can get a bit grumpy sometimes ;D

What he means I think is that you might want to do a bit more research/trying yourself before asking for help.
If we look back at the history of this post, you might have called for help too soon a few times, which can be perceived as a send me teh codez request.

For context about help vampires: https://meta.stackoverflow.com/questions/258206/what-is-a-help-vampire (https://meta.stackoverflow.com/questions/258206/what-is-a-help-vampire)

Geert

Title: Re: Using VBScript to import Excel Files and generate diagrams automatically
Post by: Andre_b_b on June 27, 2023, 06:16:56 pm
ok  :D. Yes, looking back in the post, it really seems i am a "vampire", but if i am, it was not my intention. Sorry guys, i am trying to understand stuff, but sometimes my low-konwledge of programming, in general, do not allow me to evolute  :-\ :-\