OpenSTAAD V8i
This example demonstrates a small macro which can be used within STAAD.Pro.
Often, when learning to program, you begin with a program which simply outlines the basic structure of an application, module, or function; typically resulting with a screen message displaying the phrase "Hello World." This example expands upon this to include the foundation from a practical application as well.
Additional examples in this section demonstrate how to poll STAAD data from external programs.
Open STAAD.Pro.
Select Edit > Create New VB Macro to create a new file. In the Select New Macro Name dialog, enter a title of CreateNewView.vbs with the following description:
Creates a new view from the selected beams.
You can save the macro file anywhere, so long as the directory has write permission for your user account.
The STAAD VBA Editor window opens with an subroutine title Main.
Just after the description, type the following just after the description comment line:
DimobjOpenSTAAD As Object
Dim SelBeamsNo As Long
Dim SelBeams() As Long
This is used to provide some declarations of the objects and variables used in this program.
Add the following lines to instantiate the OpenSTAAD object:
'Launch the OpenSTAAD Object
Set objOpenSTAAD = GetObject(,"StaadPro.OpenSTAAD")
The first line beginning with the apostrophe (') is a comment. It isn't necessary, but it is good practice to add remarks such as this to make your code clear to others (as well as to yourself when you revisit the code at a later time).
Add the following lines to set up a logical check for if any beams are selected:
'Get no. of selected beams
SelBeamsNo = objOpenSTAAD.Geometry.GetNoOfSelectedBeams
If (SelBeamsNo > 0) Then
Here, the GetNoOfSelectedBeams Geometry function in OpenSTAAD is being used to aid our test. The test is a if… then… else… statement, which continues in the following steps.
The following lines will instruct the program what to do if our statement is true (i.e., there is at least one beam selected). That is to create a new view from the active selection using the CreateNewViewForSelection View function in OpenSTAAD.
ReDim SelBeams(SelBeamsNo) As Long
'Create a new view
objOpenSTAAD.View.CreateNewViewForSelections
Since this macro might be run with no beams selected, a message can be provided to the user for some feedback in this instance with the following line:
Else
MsgBox "No beams are currently selected.", vbOkOnly
End If
You could add the message Hello World, if you prefer to stick with a more traditional introductory example of programming.
Add the following statement to close the instance of the OpenSTAAD object:
Set objOpenSTAAD = Nothing
This is all it requires to create a macro. Obviously, this particular example really only duplicates the functionality of selecting View > New View in STAAD.Pro. However, it easy to combine other OpenSTAAD functions to automate a series of commonly used features in order to create your own time saving tools.
In this example, for the sake of brevity, the only model entities checked for selection are beams (that is, a new view is only created if beam elements are selected). You could easily expand this to Nodes, Plates, Solids, etc.
The full code for this macro is as follows:
Sub Main()
'DESCRIPTION:Creates a new view from the selected beams.
Dim objOpenSTAAD As Object
Dim SelBeamsNo As Long
Dim SelBeams() As Long
'Launch OpenSTAAD Object
Set objOpenSTAAD = GetObject(,"StaadPro.OpenSTAAD")
'Get no. of selected beams
SelBeamsNo = objOpenSTAAD.Geometry.GetNoOfSelectedBeams
If (SelBeamsNo > 0) Then
ReDim SelBeams(SelBeamsNo) As Long
'Create a new view
objOpenSTAAD.View.CreateNewViewForSelections 'SelBeams
Else
MsgBox "No beams are currently selected.", vbOkOnly
End If
Set objOpenSTAAD = Nothing
End Sub