STAAD.Pro Help

OS. Write an OpenSTAAD Program in Python

This example will introduce you to several concepts necessary to writing OpenSTAAD applications in Python.

Tip: If you followed the previous "Hello World!" example, you may simply delete that line and start from that point.

OS. Initiate OpenSTAAD in Python

Note: You must have a model open in STAAD.Pro for this example. Otherwise your code will return an error.
  1. Type import win32com.client and then press <Return>.

    This uses the pywin32 extension for Windows to allow you to use COM objects (such as OpenSTAAD).

  2. Type os = win32com.client.GetActiveObject("StaadPro.OpenSTAAD") and then press <Return>. This initiates OpenSTAAD and connects to the current STAAD.Pro model to your program.
Your program at this point should look like:
import win32com.client
os = win32com.client.GetActiveObject("StaadPro.OpenSTAAD")

OS. Use Geometry Methods

  1. Type geometry = os.Geometry and then press <Return>.

    This defines the geometry variable as the Geometry group of methods in OpenSTAAD.

  2. Type geometry._FlagAsMethod("GetNodeCount") geometry._FlagAsMethod("GetMemberCount") and then press <Return>. This is required to correctly identify the OpenSTAAD methods as such in Python.
    Note: This approach is required for this first time you use a method in a python program using OpenSTAAD method. It is not required for each time that method is re-used within the same code.
  3. Type nodeCount = geometry.GetNodeCount and then press <Return>.

    This uses the OpenSTAAD method GetNodeCount() to return the number of nodes in the active STAAD.Pro model into the specified variable.

  4. Type beamCount = geometry.GetMemberCount and then press <Return>.

    Similarly, this uses the OpenSTAAD method GetMemberCount() to return the number of members in the active STAAD.Pro model into the specified variable.

    Tip: Reference the OpenSTAAD documentation for details on available methods and what values are accepted as input or returned from each.
Your program at this point should look like:
import win32com.client
os = win32com.client.GetActiveObject("StaadPro.OpenSTAAD")
geometry = os.Geometry
geometry._FlagAsMethod("GetNodeCount")
geometry._FlagAsMethod("GetMemberCount")
nodeCount = geometry.GetNodeCount
beamCount = geometry.GetMemberCount

OS. Generate OpenSTAAD Output

  1. Type print( str(nodeCount) + ' joints') and then press <Return>.

    Working from the inner-most operation outward, this line is first converting the numerical result stored in nodeCount to a string. This is then combined with the text string ' joints' to make the output easier to interpret. Then the entire concatenated string is being printed to the terminal.

  2. Type print(str(beamCount) + ' members').

    Similarly, this is concatenating the beamCount value, converted to a string, with some text and displaying that result to the terminal.

Your final program should look like:
import win32com.client
os = win32com.client.GetActiveObject("StaadPro.OpenSTAAD")
geometry = os.Geometry
geometry._FlagAsMethod("GetNodeCount")
geometry._FlagAsMethod("GetMemberCount")
nodeCount = geometry.GetNodeCount
beamCount = geometry.GetMemberCount
print(str(nodeCount) + ' joints')
print(str(beamCount) + ' members')

OS. Run Your Code

You should now have a working piece of python code which will provide you information about the currently active STAAD.Pro model. You’ll now run the code to test it.

  1. Save your progress by either:

    selecting File > Save

    or

    pressing <Ctrl+S>

  2. Select the Run tool to execute your code. The terminal will open and display the results.
    Tip: You may notice the Powershell instruction used to perform this action. This command can be used from any Powershell window outside of Visual Studio Code to run your program.

It is best practice to annotate your code with comments. This allows others to easily understand what your code is doing (or at least what your intention was). The full example here contains comments accordingly.

Microsoft Visual Studio Code running a Python example

OS. What if it Didn't Work?

If you did not already do so, be sure to install a linter. This provides useful feedback on your python program along with helpful hints to optimize the code.

The IDE should also provide you feedback on any detected issues with the code. Much like a spell check or grammar check in a word processor, this can alert you to errors before you ever run the code.

Verify that the full environment is set up, including the python interpreter (Python v3.8.n), the pywin32 extension, and that you have a model open in STAAD.Pro. While not all STAAD.Pro programs do require an active model in STAAD.Pro, this particular program does.

You can use the debugging tools in Visual Studio Code to help "step" through the code interpretation to identify issues. Clicking to the left of the line number in the main code window will add a break point. Then select the Run tab (or press <Ctrl+Shift+D>). Click the Run and Debug button to start the process. The terminal will provide additional information as it runs the program and will pause at each break point.

Refer to the Visual Studio Code help for detailed information on using the debugging features in this editor.