How to do PostableCommand execution without closing Windows Form? pyrevit

How to do PostableCommand execution without closing Windows Form? pyrevit

romasantalov
Explorer Explorer
361 Views
2 Replies
Message 1 of 3

How to do PostableCommand execution without closing Windows Form? pyrevit

romasantalov
Explorer
Explorer

I'm currently working on a custom Add-in for Revit with pyRevit. The Add-in runs a form created with Windows.Forms library with a button in it. When the button is pressed its supposed to take id of an object from project and appliy Selection Box command to it. I did everything right, but the problem is that the PostableCommand only executes if you close the form. Else if you press the button twice or more it just gives back an error saying 'Revit does not support more than command are posted'.
If I get it right, you cannot run a command and form together at one time. Anyways, I'm sure there is some way to avoid it.
On the internet I found solutions like:
-subscribing to Idling to see if Revit is busy with some command at the moment
-some 'Invoking' methods. (Didnot get how it works)
-closing the current form and creating a new one (the closest one)
maybe the last one is what I need. But how do I make Revit do the command in the span of time from closing original
form and running a new one? (run the first form -> button pressed -> close the form -> execute command -> run the copy of the original form)

Big thanks to all who will answer!!

0 Likes
Accepted solutions (1)
362 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

Yes. Revit can only execute one command at a time. You add-in implements an external command. PostCommand cannot execute until your external command is terminated, librating the Revit main thread for the next task.

  

There is a solution, but it requires another small layer of complexity: your add-in can launch a modeless form instead of a modal one. The modeless form can remain open even after your external command terminates. Then your form remains visible and PostCommand can be executed. 

  

However, the moment your external command terminates, you have lost access to the valid Revit API context required to make calls to the Revit API. Therefore, you cannot call PostCommand or any other Revit API directly.

  

This can be solved by adding an external event. The external event handler provides a valid Revit API context and thus you again have access to the Revit API in it.

  

In your modeless form, you can raise the event, and Revit will call the event handler as soon as possible, depending on other running tasks, e.g., other Revit commands.

  

This is demonstrated by the Revit SDK samples ModelessDialog, ModelessForm_ExternalEvent and ModelessForm_IdlingEvent.

  

Many others are discussed by The Building Coder in the topic group on Idling and external events for modeless access and driving Revit from outside:

   

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

romasantalov
Explorer
Explorer

😮Thank you so much for describing all the details. Reading your reply I found some other answers on my questions that I thought were much particular. Your help is much appreciated!

0 Likes