OS. Write an OpenSTAAD Program in Python
This example will introduce you to several concepts necessary to writing OpenSTAAD applications in Python.
OS. Initiate OpenSTAAD in Python
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).
- 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.
OS. Use Geometry Methods
Type geometry = os.Geometry and
then press <Return>.
This defines the geometry variable as the Geometry group of methods in OpenSTAAD.
- Type geometry._FlagAsMethod("GetNodeCount") geometry._FlagAsMethod("GetMemberCount") and then press <Return>. This is required to correctly identify the OpenSTAAD methods as such in Python.
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.
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.
OS. Generate OpenSTAAD Output
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.
Type print(str(beamCount) + '
Similarly, this is concatenating the beamCount value, converted to a string, with some text and displaying that result to the terminal.
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.
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.