Execute Dynamo Script from Revit API

Execute Dynamo Script from Revit API

Anonymous
Not applicable
4,161 Views
3 Replies
Message 1 of 4

Execute Dynamo Script from Revit API

Anonymous
Not applicable

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.

 

jalbert7BW3D_1-1613484280378.png

 

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.

 

I've also posted this topic at https://forum.dynamobim.com/t/execute-dynamo-script-from-revit-api/60529

 

Thanks,

Jon

0 Likes
Accepted solutions (1)
4,162 Views
3 Replies
Replies (3)
Message 2 of 4

RPTHOMAS108
Mentor
Mentor

I'm completely unfamiliar with this approach to invoking Dynamo. Can understand why Automatic mode would continue.

 

If it is the case that the Dynamo parts are run after your command context has ended (for Manual mode). Would it be possible to preordain a sequence of ExternalEvents in the order of your long running task parts intermingled with the Dynamo parts?

 

Access to Dynamo via the API should be an Idea and every time these kinds of threads arrive that Idea should be added to or voted on.

0 Likes
Message 3 of 4

Anonymous
Not applicable

Thanks for the reply Thomas, I think I understand the idea you are proposing and perhaps it could work. However at this point I think it would take some serious re-work of my command. At this point I think I will wait to see if any others chime in familiar with invoking Dynamo in this manner.

 

I do agree with you, I wish there was a built-in method from the actual Revit API to execute Dynamo, but I do understand it's a separate add-in all together from Revit so doesn't surprise me that it's not supported.

 

Again, thanks for the response and suggested approach. If I don't get any other help I will see about giving the approach a try.

 

Jon

0 Likes
Message 4 of 4

Anonymous
Not applicable
Accepted solution

Great news, I got a response on the DynamoBIM forum with a solution to my problem and seems to work exactly as needed. Here is my updated code that is now working, along with a link again to the post on DynamoBIM forum showing the discussion. As far as I can tell this is a pure solution and I take back my comment above that Revit API needs a built-in method to execute Dynamo Scripts. Perhaps Dynamo API could use some documentation but maybe we are spoiled by the excellent Revit API documentation 🙂

 

Oh, the key to solution is to provide a BLANK path for the DynPathKey in JournalData, then execute these 2 lines of code. This required me to add an extra reference to DynamoCore.DLL which I didn't have before.

 

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

 

 

Also one more tid bit, doing it this way allows me to save any Dynamo Script I'm expecting to execute this way as "Manual" mode which is great. Before I was having to save them as "Automatic" just to get them to execute which was a pain as they would then execute immediately when opening to edit.

 

Below is my full code along with link to post on DynamoBIM forum. Hopefully this can help someone in future. Kudos to John Pierson for the solution.

 

Execute Dynamo Script from Revit API - Revit - Dynamo (dynamobim.com)

 

 

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

 

 

 

 

0 Likes