0

I have an External Command to create about 40 (or even more) Generic model in Revit. I want to make Revit run the command asynchronously to speed up the process according to this flow:

load command -> run command -> load families -> generate family instances asynchronously -> end command.

I 've read some ideas a bout make a modeless dialog to work around but it is not what I need. How can I do this?

Thế Long
  • 516
  • 7
  • 19

3 Answers3

4

Meiki is completely correct. The Revit API can only be used within a valid Revit API context, and such a context is provided exclusively by Revit events. You can however implement an external event and trigger that from outside to obtain access to a valid Revit API context. This is discussed in detail with many solutions provided by The Building Coder in the topic group on Idling and External Events for Modeless Access and Driving Revit from Outside.

Another approach might be to make use of the DocumentOpened Event. You could use that to trigger the execution flow you desire.

A third but unsupported approach might be to make use of a journal file, as in the IFC Import and Conversion Journal Script.

I would start out reading the numerous solutions listed in the topic group, and probably end up making use of an external event.

Good luck and have fun!

Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17
  • 1
    Another vaguely related question just came up, on batch processing with Revit: https://forums.autodesk.com/t5/revit-api-forum/memory-consumption-issue/m-p/8243779 – Jeremy Tammik Sep 05 '18 at 06:05
2

You can't run external command asynchronously for creating (or modifying) something because of Transactions and remember that Revit doesn't support Async methods or approaches. can you describe what you gonna do exactly maybe there is another approach.

Mehdi Nourollah
  • 166
  • 1
  • 11
  • What I want to do is exactly like the flow I have written. Let say I have an external command. When I execute the command, it will generate about at least 40 instances of type generic model with all possible modification (setting location, rotate, move...). All of this is inside a transaction. Normally, Revit would generate all of them one by one (synchronously), but I want to make it generates all 40 instances at the same time (in 40 threads, perhaps), wait for the generation to complete then close the transaction. – Thế Long Sep 05 '18 at 02:09
  • I heard people saying about Revit can run asynchronously but does not support or allow external commands and application to do so. One way to work around the problem is to use Idling event and modeless dialog. Not that I have full knowledge about this solution, but I don't think it will solve my problem. The solution proposes to make the command itself asynchronous and continue to be able to work after the "Execute" method has return. I only want to make a part of my command run asynchronously/ multi-thread to speed up the process before the "Execute" method returns. – Thế Long Sep 05 '18 at 02:16
0

According to the docs:

The Autodesk Revit API supports single threaded access only. This means that your API application must perform all Autodesk Revit API calls in the main thread (which is called by the Autodesk Revit process at various API entry points), and your API application cannot maintain operations in other threads and expect them to be able to make calls to Autodesk Revit at any time.

However, I believe you could create an external API and consume it from your command.

Carlos_E.
  • 671
  • 8
  • 14