Execute Dynamo Script from Revit API

Hi,

I have a Revit Add-In with many commands. As part of he Add-In I am trying to allow users the ability to specify their own Dynamo scripts to be run at different points within a “long running” command. I have successfully got this setup but have run into a problem. With the way I have it implemented, the user specified Dynamo Script(s) continue to execute after my “long running” command has ended each time a change is made to the Revit project.

Below is snapshot of code I am using to execute the user specified scripts. I’ve tried many combinations of the Journal Key settings but unable to accomplish my needs.

image

Below I will try to summarize how/where my command executes the Dynamo Script(s)…

-User executes my “long running” command.
-“long running” command goes about it’s business and makes changes to project.
-during certain sections of the “long running” command, the command executes user specified Dynamo Script(s) using code in image above.
-“long running” command continues to go about it’s business and eventually finishes up

The problem I have is that any changes made to project via the command AFTER Dynamo Scripts have been executed cause the Scripts to execute again and again and again…

From my research and trial/error, the user Scripts must be saved in “Automatic” mode for the journal approach to work. From what I gather, the journal approach to executing the scripts acts very much as if user opens the Dynamo Editor and loads their script which then executes automatically but then never closes the editor.

If I switch “ForceManualRunKey” to TRUE, the scripts do not execute until my “long running” command is 100% done which doesn’t work for me.

Any help? I can provide more information if needed.

Thanks,

Jon

1 Like

Hi @jalbert300

This looks really interesting! I am wondering how you are trying to execute this, are you running the scripts through a macro?

Hi Greg,

I am not executing this from a Macro, rather it is from a .NET Revit Add-In. Within one of my custom commands I have a section of code that loops through custom setting I provide in the Add-In where user specifies dynamo scripts they would like to run. Basically I am giving the user a way to extend/augment my custom command.

1 Like

These two posts helped me:

1 Like

Thanks for the response John,

Unfortunately that’s exactly what I’m doing already, and the Dynamo Scripts are executing. My only problem is that the executed scripts continue to execute each time project is changed. If I set ForceManual = True then the scripts do not execute when I need them to. Instead they execute after the rest of my custom Revit API command is complete which is to late.

1 Like

I just went through the same thing! :grin:

What I ended up doing is running a Dynamo model without a graph, the force running a graph from a specific file.

This code is here:
(FYI the code is provided “as-is” and “for reference only” as it was a bit of an experiment from our company to see how it could be done.)

2 Likes

Very nice John, you are the man! I just tried this out and seems to work exactly as I need. I’m even able to save/store my Dynamo Scripts in “Manual” mode which is preferred for what we are doing. Before we had to save the scripts as “Automatic” which was a pain.

Here is my updated VB.NET code which is working perfectly now! I’d give you a hug but will stick to an elbow bump :smile: GREAT SUCCESS!!!

 Public Function ExecuteDynamo(uiapp As UIApplication, path As String) As Result

    Try

        If File.Exists(path) = False Then Return Result.Failed

        Dim jd As IDictionary(Of String, String) = New Dictionary(Of String, String) From {
        {Dynamo.Applications.JournalKeys.ShowUiKey, False.ToString()},
        {Dynamo.Applications.JournalKeys.AutomationModeKey, True.ToString()},
        {Dynamo.Applications.JournalKeys.DynPathKey, ""},
        {Dynamo.Applications.JournalKeys.DynPathExecuteKey, True.ToString()},
        {Dynamo.Applications.JournalKeys.ForceManualRunKey, False.ToString()},
        {Dynamo.Applications.JournalKeys.ModelShutDownKey, True.ToString()},
        {Dynamo.Applications.JournalKeys.ModelNodesInfo, False.ToString()}}

        Dim drcd As DynamoRevitCommandData = New DynamoRevitCommandData()
        drcd.Application = uiapp
        drcd.JournalData = jd

        Dim dr As DynamoRevit = New DynamoRevit()
        dr.ExecuteCommand(drcd)

        DynamoRevit.RevitDynamoModel.OpenFileFromPath(path, True)
        DynamoRevit.RevitDynamoModel.ForceRun()

        Return Result.Succeeded

    Catch ex As Exception

        Return Result.Failed

    End Try

End Function
3 Likes

Glad to hear it and happy to help out!

Also, thanks for posting the vb.net code. It’s really cool to see. :slightly_smiling_face:

1 Like