The first step in using the SAP2000 API from an external application is to reference SAP2000 from your application. If using Excel VBA, reference Sap2000 by opening the VBA editor, clicking the Tools menu > References command and checking the box next to SAP2000.
Next an instance of the SAP2000 object must be created (also known as instantiating the object) within your application. In VBA this could be accomplished as:
Dim SapObjectAs SAP2000.SapObject
Set SapObject = New SAP2000.SapObject
The first line creates the object variable and the second line creates the instance of the SAP2000 object. Now that an instance of the SAP2000 object has been created in your application, start SAP2000 using the following command:
SapObject.ApplicationStart
At this point you can open an existing model, or create a new one and perform whatever actions are required. In general, the API commands are accessed through SapObject.SapModel.
It may be helpful to define a SapModel object so that the API commands are accessed through SapModel instead of SapObject.SapModel. In VBA this could be accomplished as:
Dim SapModelAs cSapModel
Set SapModel= SapObject.SapModel
When finished with a model, you may want to close the SAP2000 application. This can be accomplished using the following VBA command:
SapObject.ApplicationExit True
As a last step, the SapModel and SapObjectobjects should always be set to Nothing. In VBA this is accomplished as:
Set SapModel=
Nothing
Set SapObject= Nothing
Setting the objects to Nothing is a very important step. It breaks the connection between your application and SAP2000 and frees up system resources. If the objects are not set to Nothing, the SAP2000 application will not completely close (you will still see it running in your Windows Task Manager).
Putting all the steps previously described into a single example, a VBA program might consist of the following:
Sub MyProgram
'dimension variables
Dim SapObjectAs SAP2000.SapObject
Dim SapModelAs cSapModel
Dim ret As Long
'create an instance of the Sap2000 object
Set SapObject= New SAP2000.SapObject
'start the Sap2000 application
SapObject.ApplicationStart
'create the SapModel object
Set SapModel= SapObject.SapModel
'initialize model
ret = SapModel.InitializeNewModel
'call Sap2000 API functions here to perform desired tasks
'in this example a new 2D frame is created from template
ret = SapModel.File.New2DFrame(PortalFrame,
3, 124, 3, 200)
'close the Sap2000 application, if desired
SapObject.ApplicationExit False
'set the objects to Nothing
'at the end of your program ALWAYS terminate the objects
in this manner
Set SapModel= Nothing
Set SapObject= Nothing
End Sub
See Also
Function Documentation Conventions
Visual Basic Concepts Used In The SAP2000 API