Quickstart Guide for Dynamo in RSA 2023

Hi there, I wrote a guide for my colleagues to help them setup dynamo in Robot 2023 and navigate the documentation; I thought it was worth sharing it with the community!

Dynamo Setup

The latest versions of Dynamo (2.13) ships with CPython 3 as the default interpreter;
unfortunately, this version doesn’t work well with the RSA API, which is a COM API (as opposed to the .NET APIs of the other Autodesk apps).

Until this is solved (it may take a very long time), you need to install the DynamoIronPython2.7 package and switch your python scripts to IronPython2 interpreter:

Python scripts template

After creating a new python script node, replace the contents with the following code:

import sys
import clr
clr.AddReferenceToFileAndPath(
    r"C:\Program Files\Autodesk\Robot Structural Analysis Professional 2023\Exe\Interop.RobotOM.dll"
)
from RobotOM import RobotApplicationClass

app = RobotApplicationClass()

NOTE: the path at the third line may change depending on your installation location and RSA version.

you can then use the app object to access the objects of the application:

project = app.Project
structure = project.Structure
labels = structure.Labels
loads = structure.Cases

TIP: You don’t need to copy these lines if you don’t need them.
For example, if you only need the load cases, you can use the one-liner loads = app.Project.Structure.Cases.

SDK Documentation

The SDK homepage found at C:\Program Files\Autodesk\Robot Structural Analysis Professional 2023\SDK\ROBOTSDK.html is a good place to start learning the Robot API.

  • Getting Started PDF: pages 6 to 19 (8 to 21 of the PDF reader);
  • Tutorial of Robot API: very useful, you can start from this and ignore the Getting Started PDF.
    Ignore the “The first program” section, since we already set up the RobotOM library with the code in the previous section; see the section below to translate the code;
  • User guide: once you get how robot API works, this is the reference to find all the objects, methods and properties available.
    Instead of using the PDF, check out the same content in windows help format from C:\Program Files\Autodesk\Robot Structural Analysis Professional 2023\SDK\Robot API.chm, that is easier to navigate around.

Translating the sample code

The sample code in the SDK documentation is written in Visual Basic or other languages (C++, C#), so it needs to be translated to python.

These are some things to know to convert VB code to Python:

  • You don’t need to declare variables, so you can skip the rows that starts with Dim;

  • You don’t need to type Set before variable assignment;

  • There’s no concept of constants, they are all variables;
    Python best practices tell us to use uppercase names with undescores between words, so Const constname = "Hello" becomes CONST_NAME = "Hello"

  • To create a new instance of a class, instead of using variable = New ClassName you use variable = ClassName() (this is what we did with app = RobotApplicationClass() above);

  • You don’t need to free the object variables, so you can skip the Set variable = Nothing lines;

  • Methods must always be called with parenthesis, even if they don’t have parameters.
    Sometimes you’ll encounter Visual Basic code like this:

    app.Project.Open filepath
    app.Project.CalcEngine.Calculate
    app.Project.SaveAs newfilename
    app.Project.Close
    

    Python equivalent:

    # we store the project variable to save a few CPU cycles and keystrokes
    prj = app.Project
    prj.Open(file_path)
    prj.CalcEngine.Calculate()
    prj.SaveAs(new_file_name)
    prj.Close()
    
  • Loops: instead of

    Dim i as Integer
    For i = 1 to bar_col.Count
        Set bar = bar_col.Get(i)
        ' Do something with bar...
    Next
    

    You use the range function to have the sequence of indices:

    for i in range(bar_col.Count):
        bar = bar_col.Get(i+1)
        # Do something with bar...
    

    Notice the colon at the end of the for line and the indentation after it - everything that has to be executed in the loop must be indented by the same amount (4) of spaces.

    NOTE: If the collection of object allows it (= it doesn’t need the Get(index) method to get an element), you can directly get the objects with

    for bar in bar_col:
        # Do something with bar
    
  • If statements: If <condition> Then... End If becomes if <condition>: and the code to execute if the condition is verified mus be indented by 4 spaces

    If c.Count > 0 Then
        'Do things here...
    End If
    
    if c.Count > 0:
        # Do things here...
    
  • You can’t use the With object ... End With construct, so you must specify object in every line of the With block:

    With str.Nodes
        .Create 1, 0, 0, 0
        .Create 2, 3, 0, 0
        '...
    End With
    

    becomes (notice the parethesis added around the methods parameters)

    nodes = str.Nodes
    nodes.Create(1, 0, 0, 0)
    nodes.Create(2, 3, 0, 0)
    # ...
    
  • The _ charachter in VB is a line continuation, so that you can split a long line of code into multiple lines;
    In python there’s you can use \, but you don’t need it if you are inside parenthesis (see the code below);

  • Enums must be imported and used with their full name (see the code below);
    Alternatively, you can look at the User guide for the corresponding integer value and use them instead;

  • Select Case: there’s no equivalent in IronPython 2. You need to turn it into a series of if... elif... elif... to test the various cases

Take a look here for more info about converting VBA code to Python.

Dynamo Forum

There are some (few) examples of python scripts in the RSA section of Dynamo BIM forum, but they try to load the required RobotOM library from an old, non-existent path.

You need to replace the beginning of the code with the one illustrated in the Python scripts template section.

Library imports

Many python examples in the Dynamo Forum use the from LibraryName import * instruction to be able to access all the object of that Library.
This is strongly discouraged by the Python Community in general, since it can result in strange behaviors.

If you need objects from the RobotOM library other than the already imported RobotApplicationClass, just import them explicitly with another from RobotOM import <ObjectType> line.

import sys
import clr
clr.AddReferenceToFileAndPath(
    r"C:\Program Files\Autodesk\Robot Structural Analysis Professional 2023\Exe\Interop.RobotOM.dll"
)
from RobotOM import RobotApplicationClass
from RobotOM import IRobotCombinationType
from RobotOM import IRobotCaseNature
from RobotOM import IRobotCaseAnalizeType

loads = app.Project.Structure.Cases
combination = loads.CreateCombination(
    1,
    "C1",
    IRobotCombinationType.I_CBT_ULS,
    IRobotCaseNature.I_CN_PERMANENT,
    IRobotCaseAnalizeType.I_CAT_COMB
)
5 Likes