OS. Simple STAAD.Pro Macro
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.
- Open STAAD.Pro.
Utilities ribbon tab, select the
Macro tool in the
The Macro dialog opens.
- Click Create New. The New Macro File Name dialog opens.
Type a title of
CreateNewView.vbs with the following
Creates a new view from the selected beams.
Note: You can save the macro file anywhere, so long as the directory has write permission for your user account.The STAAD.Pro Script Editor window opens with an subroutine title Main.
Just after the description, type the following just after the
description comment line:
Dim objOpenSTAAD 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.
Type the following lines to instantiate the
'Launch the OpenSTAAD Object Set objOpenSTAAD = GetObject(,"StaadPro.OpenSTAAD")
Type 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.
Type following lines to 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
Type the following lines.
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
Type the following statement to close the instance of the
Set objOpenSTAAD = Nothing
This is all it requires to create a macro. Obviously, this particular example really only duplicates the functionality of selecting the New View tool 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