You are on page 1of 3

OS. Simple STAAD.

Pro Macro
This example demonstrates a small macro which can be used within STAAD.Pro.

O en, when learning to program, you begin with a program which simply outlines the basic structure of an applica on, module, or func on; typically resul ng with a screen message displaying the phrase "Hello World". This example expands upon this to include the
founda on from a prac cal applica on as well.

Note: Addi onal examples in this sec on demonstrate how to poll STAAD data from external programs.

1. Open STAAD.Pro.

2. On the U li es ribbon tab, select the Macro tool in the Developer group.

The Macro dialog opens.


3. Click Create New. The New Macro File Name dialog opens.

4. Type a tle of CreateNewView.vbs with the following descrip on: 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 subrou ne tle Main.
5. Just a er the descrip on, type the following just a er the descrip on comment line:

DimobjOpenSTAAD As Object
Dim SelBeamsNo As Long Copy to clipboard
Dim SelBeams() As Long

This is used to provide some declara ons of the objects and variables used in this program.

6. Type the following lines to instan ate the OpenSTAAD object:

'Launch the OpenSTAAD Object Copy to clipboard


Set objOpenSTAAD = GetObject(,"StaadPro.OpenSTAAD")

Note: The first line beginning with the apostrophe (') is a comment. It isn't necessary, but it is good prac ce 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 me).

7. 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 Copy to clipboard
If (SelBeamsNo > 0) Then

Here, the GetNoOfSelectedBeams Geometry func on in OpenSTAAD is being used to aid our test. The test is a if… then… else… statement, which con nues in the following steps.

8. 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 ac ve selec on using the CreateNewViewForSelection View func on in OpenSTAAD.
ReDim SelBeams(SelBeamsNo) As Long
'Create a new view Copy to clipboard
objOpenSTAAD.View.CreateNewViewForSelections

9. 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 Copy to clipboard
End If

Tip: You could add the message Hello World, if you prefer to s ck with a more tradi onal introductory example of programming.

10. Type the following statement to close the instance of the OpenSTAAD object:
Copy to clipboard
Set objOpenSTAAD = Nothing

This is all it requires to create a macro. Obviously, this par cular example really only duplicates the func onality of selec ng the New View tool in STAAD.Pro. However, it easy to combine other OpenSTAAD func ons to automate a series of
commonly used features in order to create your own me saving tools.

In this example, for the sake of brevity, the only model en es checked for selec on 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.

Example
The full code for this macro is as follows:

1 Sub Main()
2 'DESCRIPTION:Creates a new view from the selected beams.
3 Dim objOpenSTAAD As Object
4 Dim SelBeamsNo As Long
5 Dim SelBeams() As Long
6 'Launch OpenSTAAD Object
7 Set objOpenSTAAD = GetObject(,"StaadPro.OpenSTAAD")
8 'Get no. of selected beams
9 SelBeamsNo = objOpenSTAAD.Geometry.GetNoOfSelectedBeams
10 If (SelBeamsNo > 0) Then
11 ReDim SelBeams(SelBeamsNo) As Long
12 'Create a new view
13 objOpenSTAAD.View.CreateNewViewForSelections 'SelBeams
14 Else
15 MsgBox "No beams are currently selected.", vbOkOnly
16 End If
17 Set objOpenSTAAD = Nothing Copy to clipboard
18 End Sub

You might also like