Author Topic: Access Sparx Ea from Visual Basic  (Read 9740 times)

Robert Glover Jr

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Access Sparx Ea from Visual Basic
« on: April 17, 2014, 10:06:24 pm »
I am trying to run this VB code snippet from "Scripting Enterprise Architect" by Thomas Kilian. Am using MS Visual Studio Express 2012 for Windows Desktop.  I have Sparx EA 10.10 running as a standalone application.  This compile-time error message occurs:  "Type 'EA.repository' is not defined".  How do I  connect the EA API to VS Express 2012 so that this code will compile cleanly?
Code: [Select]
Module Module1

    Sub Main()
        Dim Repository As EA.repository
        EAapp = GetObject(, "EA.App")
        Repository = EAapp.repository
        Print(Repository.ConnectionString)
    End Sub

End Module

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Access Sparx Ea from Visual Basic
« Reply #1 on: April 18, 2014, 12:08:47 am »
I don't remember set settings (there's a similar post from Geert Bellekens where he names the Excel settings location). But you need to make aware which API to use. There is some References setting somewhere.

q.

Nizam Mohamed

  • EA User
  • **
  • Posts: 193
  • Karma: +1/-0
    • View Profile
Re: Access Sparx Ea from Visual Basic
« Reply #2 on: April 18, 2014, 01:46:36 pm »
Try these
  • Have you added the EA Interop reference in your VB Project?
  •            It must be EA.Repository (Case sensitive)

Robert Glover Jr

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Access Sparx Ea from Visual Basic
« Reply #3 on: April 18, 2014, 08:53:06 pm »
Quote
Try these
  • Have you added the EA Interop reference in your VB Project?
  •            It must be EA.Repository (Case sensitive)
  Here is what I have done so far MS Visual Studio Express 2012 for Windows Desktop
  • Project...... Add Reference...COM...Type Libraries
  • put checkmark next to: EA Simulation Library 1.0
  • put checkmark next to: EAViewer 1.0 Type Library
After doing the above, "Interop.EASLLib" and "Interop.EAViewer" appear in "Object Browser" of the VB Project
When I expand "Interop.EAView" it shows:
  • EAViewer
  • ...DiagramView
  • ...IDiagramView
  • ..._IDiagramViewEvents
When I expand "Interop.EASLLIB" it shows too many items to list here. They are of 3 different types (That information appears in the VB Object Browser when I click on one of them. They are one of:
  • Public Class xxx Member of EASLLib
  • Public Interface Ixxxx Member of EASLLib
  • Public Enum Exxx Member of EASSLib
Although this is all encouraging, I am still getting a compile error on the first line of the code snippet that I copied from that book (the error message is: "Type EA.repository is not defined"
Code: [Select]
 Sub Main()
        Dim Repository As EA.repository
        EAapp = GetObject(, "EA.App")
        Repository = EAapp.repository
        Print(Repository.ConnectionString)
    End Sub

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13407
  • Karma: +568/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Access Sparx Ea from Visual Basic
« Reply #4 on: April 18, 2014, 09:46:06 pm »
Neither is the correct library.
You need the "Enterprise Architect Object Model 2.10"

Geert

Robert Glover Jr

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Access Sparx Ea from Visual Basic
« Reply #5 on: April 18, 2014, 10:13:07 pm »
Quote
Neither is the correct library.
You need the "Enterprise Architect Object Model 2.10"

Geert
Yes, I agree that I need "Enterprise Architect Object Model 2.10".
It says that at:
  • Help...Help Contents....
  • Automation and Scripting
  • Enterprise architect Object Model
  • Using the automation Interface
  • Connect to the Interface
  • Set References In Visual Basic
I even tried the recommended:
  • C:\Program Files (x86)\Sparx Systems\EA>ea.exe /unregister
  • C:\Program Files (x86)\Sparx Systems\EA>ea.exe /register
But it is still not appearing in MS Visual Studio Express 2012 for Windows Desktop.  Only Interop.EASSLIB and Interop.EAViewer appear

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13407
  • Karma: +568/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Access Sparx Ea from Visual Basic
« Reply #6 on: April 18, 2014, 10:35:17 pm »
Browse to you install folder and select ea.tlb
On my machine that is: C:\Program Files (x86)\Sparx Systems\EA\EA.tlb

Geert

Robert Glover Jr

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Access Sparx Ea from Visual Basic
« Reply #7 on: April 20, 2014, 12:36:12 am »
Quote
Browse to you install folder and select ea.tlb
On my machine that is: C:\Program Files (x86)\Sparx Systems\EA\EA.tlb

Geert
Thanks!  :)
Your solution worked.
The following code now runs successfully in MS Visual Studio Express 2012 for Windows Desktop:
Code: [Select]
Module Module1
    Sub Main()
        Dim vRep As EA.Repository
        Dim vApp As EA.App = GetObject(, "EA.App")
        vRep = vApp.Repository
        MsgBox(vRep.ConnectionString)
    End Sub
End Module


My code snippet above is a little different than the example in the EA Help ("Automation and Scripting" ... Enterprise architect Object Model ... Using the Automation Interface ... Examples and Tips)  because the help refers to VB 6.  According to Wikipedia:  
Quote
Mainstream Support for Microsoft Visual Basic 6.0 ended on March 31, 2005


Here is a slightly better code example based loosely on "Scripting Enterprise Architect" by Thomas Kilian :
Code: [Select]
Module Module1
    Sub Main()
        Dim vRep As EA.Repository
        Dim vApp As EA.App = GetObject(, "EA.App")

        vRep = vApp.Repository
        Console.WriteLine("Repository.ConnectionString: '" & vRep.ConnectionString & "'")

        Dim vRoot As EA.Package = vRep.Models.GetAt(0)
        Console.WriteLine("Root.Name: '" & vRoot.Name & "' Root has no ParentID. So, ParentID: " & vRoot.ParentID)

        Dim vFirstView As EA.Package = vRoot.Packages.GetAt(0)
        Console.WriteLine("1st View Name= '" & vFirstView.Name & "' 1st View Flags: '" & vFirstView.Flags & "' ParentID: " & vFirstView.ParentID)

        InputBox("While this Input box open, you can read console", "bye")
    End Sub
End Module
The above's console output is:
Code: [Select]
Repository.ConnectionString: 'C:\Users\rdgjr\AppData\Roaming\Sparx Systems\EA\EAExample.eap'
Root.Name: 'Project Models' Root has no ParentID. So, ParentID: 0
1st View Name= 'Getting Started' 1st View Flags: 'isModel=1;VICON=1;' ParentID: 1
Alternatively, here is a version of the code that instead of writing to the console writes instead to a text file:
Code: [Select]
Module Module1

    Sub Main()
        Dim outfile_text As System.IO.StreamWriter
        outfile_text = My.Computer.FileSystem.OpenTextFileWriter("C:\rdg_dev\sparx\eaps_for_EA_in_10_days\vbOutput.txt", False)

        Dim repo_Rep As EA.Repository
        Dim app_App As EA.App = GetObject(, "EA.App")

        repo_Rep = app_App.Repository
        outfile_text.WriteLine("Repository.ConnectionString: '" & repo_Rep.ConnectionString & "'")

        Dim root_package As EA.Package = repo_Rep.Models.GetAt(0)
        outfile_text.WriteLine("root_package.Name: '" & root_package.Name & "' Root has no ParentID. So, ParentID: " & root_package.ParentID)

        Dim view_View As EA.Package = root_package.Packages.GetAt(1)
        outfile_text.WriteLine("view_View.Name= '" & view_View.Name & "' 1st View Flags: '" & view_View.Flags & "' ParentID: " & view_View.ParentID)

        Dim pack_Pack As EA.Package = view_View.Packages.GetAt(0)
        outfile_text.WriteLine("pack_Pack.name='" & pack_Pack.Name & "'")

        outfile_text.WriteLine("... the end ...")
        outfile_text.Close()
    End Sub
End Module
« Last Edit: April 21, 2014, 08:22:41 pm by RobertGloverJr »