Revit Addin without blocking Revit UI

Revit Addin without blocking Revit UI

mizrachi_amir2
Advocate Advocate
738 Views
5 Replies
Message 1 of 6

Revit Addin without blocking Revit UI

mizrachi_amir2
Advocate
Advocate

Hello everyone,

 

I have my Revit Add-in and I would like now to make it running without blocking the UI.

Should I use the MVVM design pattern for that? 

 

In any case, how would you recommend me to make this shift and make my addin not blocking the Revit UI?

I followed the examples of @nice3point here, but am not sure what is the best approach for me now.

 

Thank you,

Amir

 

0 Likes
739 Views
5 Replies
Replies (5)
Message 2 of 6

jeremy_tammik
Alumni
Alumni

Repetition number 849 (well, actually number print(int(1000*random.random())))...

   

The most important differentiation in this context is between modal and modeless.

   

Please be aware that the Revit API is single-threaded and only runs within a valid Revit API context, Such a context is only provided by Revit in one of the numerous event handlers defined by the API and runs in the main thread of Revit. This blocks the UI, just as you say.

   

You can avoid this by executing parts of your add-in in a separate modeless thread. The modeless part can release the main thread, allow Revit to continue doing other stuff, and both Revit and the add-in can continue interacting with the user. However, this modeless part of your add-in has no access to the Revit API.

   

The preferred (and almost only) way for the modeless part of your add-in to interact with the Revit API and gain access to a valid Revit API context enabling it to do so is to implement an external event. The external event can be raised from the modeless part of the add-in. Revit then calls the corresponding event handler and provides it with a valid Revit API context.

   

This is discussed in great depth and with many examples by The Building Coder, and in nuerous other posts here in the forum:

   

   

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

nice3point
Advocate
Advocate

The link you provided has an example "Single-project WPF Application (Modeless)", this is the preferred way of interacting a modeless WPF application and Revit, without blocking the UI

Message 4 of 6

mizrachi_amir2
Advocate
Advocate

Thank you for the detailed explanation.
What is Modal though? Is it simply the pop up window using WPF? Is it related to a MVVM design pattern?

What is the advantage of using MVVM in Revit plugins?

0 Likes
Message 5 of 6

mizrachi_amir2
Advocate
Advocate

So basically the difference between the template of Single-project WPF Application (Modeless) and Single-project WPF Application (Modal) is that the latter blocks Revit UI?

0 Likes
Message 6 of 6

nice3point
Advocate
Advocate

Yes, modeless doesn't block Revit, Modal does. As Jeremy said and as it is written in the samples Readme, in modeless variant you have to use IExternalEventHandler to modify the document

 

You may not use MVVM if you are just learning, but it is a good practice and should be considered for commercial plugin development