hamid.gh
hamid.gh

Reputation: 31

How to Link a VBA code with opened ETABS OAPI?

A project on ETABS 2015 is open on my PC. I want to do some change and process on it by a VBA code automatically, but all the sample code that I found was like this:

Public Sub Example()
       Dim SapModel As cSapModel
       Dim EtabsObject As cOAPI
       Dim FileName as String 
       Dim ret As Integer = -1 

   'create ETABS object
       EtabsObject = CreateObject("CSI.ETABS.API.ETABSObject")

   'start ETABS application
       ret = EtabsObject.ApplicationStart()

   'create SapModel object
       SapModel = EtabsObject.SapModel

   'initialize model
       ret = SapModel.InitializeNewModel()

   'open an existing file - If no file exists, run the Save example first.
       FileName = "c:\CSI_API_temp\example.edb"
       ret = SapModel.File.OpenFile(FileName)

This code just open a new ETABS in my PC. but my ETABS project is already running and I want to connect to it but I don't know how! Please help me.

Upvotes: 3

Views: 6336

Answers (2)

user3493725
user3493725

Reputation: 50

The code below does as you asked. It uses VBA to connect to a currently running instance of ETABS. Please note if you're running multiple instances of ETABS it may not attach to the one you want.

You'll need to add a reference to 'ETABSv1.tlb' to your VBA project. If you're using VBA Editor for Excel, on the Tools menu select References, then click Browse then add C:\Program Files\Computers and Structures\ETABS 19\ETABSv1.tlb

Sub Main()
  'create API helper object
  Dim myHelper As ETABSv1.cHelper
  Set myHelper = New ETABSv1.Helper

  'dimension the ETABS Object as cOAPI type
  Dim myETABSObject As ETABSv1.cOAPI
  Set myETABSObject = Nothing

  'attach to a running instance of ETABS
  'get the active ETABS object
  Set myETABSObject = myHelper.GetObject("CSI.ETABS.API.ETABSObject")

  'get a reference to cSapModel to access all API classes and functions
  Dim mySapModel As ETABSv1.cSapModel
  Set mySapModel = myETABSObject.SapModel

  ' DO YOUR WORK BELOW.

  MsgBox "Do your work here."

  ' DO YOUR WORK ABOVE.

  'clean up variables
  Set mySapModel = Nothing
  Set myETABSObject = Nothing

  Exit Sub

ErrHandler:
    MsgBox "Cannot run API script: " & Err.Description

End Sub

ETABS comes with API documentation that you can find in the installation folder. On my machine it is here: C:\Program Files\Computers and Structures\ETABS 19\CSA API ETABS v1.chm

Upvotes: 0

Serag Hassouna
Serag Hassouna

Reputation: 73

Your sample code is basically and typically the one needed to open ETABS ITSELF and to initialize a new model, ETABSObject = CreateObject("CSI.ETABS.API.ETABSObject") followed by the invocation of ApplicationStart method are the direct means for that.

If you refer to the ETABS API documentation file in the installation destination (e.g "C:\Program Files\Computers and Structures\ETABS 2015\CSi API ETABS 2015.chm" ) Under the section "Release Notes>Attaching to a manually started instance of ETABS", or if you see the same section from the link http://docs.csiamerica.com/help-files/etabs-api-2015/html/3ceb8889-9028-4de3-9c87-69a12055ade7.htm you'll find code (in VB.Net) close to what you aim to, but it will not work with vba, so, what you will simply do is this

    'The ret variable, just for method invocation convenience
    Dim ret As Integer

    'Create an instance of the currently opened Program
    Dim myETABSObject As ETABS2015.cOAPI
    Set myETABSObject = GetObject(, "CSI.ETABS.API.ETABSObject")

    'Create an instance to the model
    Dim myETABSModel As ETABS2015.cSapModel
    Set myETABSModel = myETABSObject.SapModel

    'Now, after the "reference instances" are obtained

    'Initialize the model (using the model's instance of course)
    ret = myETABSModel.InitializeNewModel() 'default units in kip_in_F

    'Set the model templete, which is "Blank"
    ret = myETABSModel.File.NewBlank()

And You can normally proceed in your coding.


Note: 1- GetObject(CSI.ETABS.API.ETABSObject) method will raise an error if ETABS is not opened.

2- you still can use GetObject even if ETABS is not opened and if you want to open it from windows shell (like using command prompt to open programs instead of double clicking on their respective icons), but this of course needs some Error Handling and the use of "Windows Script Host Object Model".

3- The examples in the documentation (under the documented methods and properties) may not be complete in terms of initiating input data, in cases like this they are just to create an impression of how to use their respective methods and properties.

Upvotes: 1

Related Questions