Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't attach running SAP2000 to OAPI (using VBA) 1

Status
Not open for further replies.

asrhael

Structural
May 5, 2015
1
Good morning,

I'm using SAP2000 V17.1.1 with MS Excel 2010 X86, and I want to make a Excel Sheet that can retrieve Section Cuts forces of a running model. The problem is, I know that is possible to create an instance of SAP2000 using VBA, launching SAP2000 and then opening the proper SDB file. But in this case, I want to "connect" my VBA code to the running SAP2000 (started manually). I would REALLY appreciate any help:

I know SAP2000 V17.1.1 have differences of the OAPI usage respect to other versions. The "CSI OAPI Documentation" says:

Code:
In version 17 of SAP2000, the API has been separated from the main SAP2000 executable into a dynamic link library (DLL). 
This will change how API client applications connect to CSI software. You will no longer directly reference the SAP2000 executable assembly. 
Instead, you will reference the API DLL. In addition, you will no longer be able to declare a variable of type SapObject. 
Instead, create a variable of type SAP2000v17 cOAPI, which is an interface type. 
Then instantiate an object that implements the cOAPI interface. 
This process is detailed below.

So, the first step that I did was add SAP2000v17.tlb file as a Excel VBA reference

The second step is open a SDB file manually...I use a SDB created with the Template form built-in in SAP2000

Next step is write in a new Excel module the following code, then, execute it:

Code:
Sub GetSap()

Dim ret As Long
Dim helper As SAP2000v17.cHelper
Dim mySapObject As SAP2000v17.cOAPI
Dim mySapModel As cSapModel

Set helper = New SAP2000v17.helper
Set mySapObject = helper.GetObject("CSI.SAP2000.API.SapObject")

Set mySapModel = mySapObject.SapModel

'More instruction here

End Sub

Sadly, Excel show me an error when he try to execute the line [highlight #FCE94F]Set mySapObject = helper.GetObject("CSI.SAP2000.API.SapObject")[/highlight]. Here is a screenshot:

2015-05-05_11-49-41_wpdcy6.png

A code for create a model from a Template works fine in VBA, I tried this:

Code:
Sub CreateSap()

Dim ret As Long
Dim helper As SAP2000v17.cHelper
Dim mySapObject As SAP2000v17.cOAPI
Dim mySapModel As cSapModel

Set helper = New SAP2000v17.helper
Set mySapObject = helper.CreateObject("C:\Program Files (x86)\Computers and Structures\SAP2000 17\SAP2000.exe")

ret = mySapObject.ApplicationStart()

Set mySapModel = mySapObject.SapModel

ret = mySapModel.InitializeNewModel(kip_ft_F)
ret = mySapModel.File.New2DFrame(PortalFrame, 3, 12, 3, 28)

mySapObject.ApplicationExit False
Set mySapModel = Nothing
Set mySapObject = Nothing

End Sub

That code use the cHelper class, and works fine on my X86 machine (the CSI OAPI help say's that the use of cHelper is for "x64 clients"...but in this case work fine in x86 too). A "traditional" code without the cHelper class work fine too in my tests:

Code:
Sub CreateSap2()

Dim ret As Long
Dim mySapObject As SAP2000v17.cOAPI
Dim mySapModel As cSapModel

Set mySapObject = CreateObject("CSI.SAP2000.API.SapObject")

ret = mySapObject.ApplicationStart()

Set mySapModel = mySapObject.SapModel

ret = mySapModel.InitializeNewModel(kip_ft_F)
ret = mySapModel.File.New2DFrame(PortalFrame, 3, 12, 3, 28)

mySapObject.ApplicationExit False
Set mySapModel = Nothing
Set mySapObject = Nothing

End Sub

The problem is when I try to manipulate an existent running SAP2000 instance, for example....started with a double click of a SDB file. The documentacion of the OAPI says that I need to use GetObject() instead of CreateObject():

2015-05-05_11-57-30_lmayiy.png

I tried many ways to pass this error, but is impossible to get that VBA sub working fine. The creation of a New Sap2000 instance works fine...but I need to work in a running instance, started manually.

Please...Hope any of you can help me.

Thanks!

Best regards,
Francisco Chávez
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor