The question of communicating from outside with Revit is popping up with increasing frequency.
Since Revit is designed as an interactive end user tool, misusing it as a server may fail and may also violate the license agreement.
Therefore, in many cases, the cleanest (or only clean) solution will involve use of the Forge Design Automation API for Revit or DA4R.
However, it is also possible to make use of Revit as a server in a limited way via an external event.
Many aspects of this have been discussed numerous times in the past in the topic group on external events for modeless access and driving Revit from outside.
However, people sometimes prefer not to read and research, not to fish, but rather ask repeated questions and be fed, so we return to this topic again (and again).
Furthermore, our new hero Igor shared a cool wrapper for external events that makes the process easier than ever before:
A Revit add-in can accept requests from an external source that executes outside the valid Revit API context by implementing an external event and providing a method for the external client to raise it.
This functionality can obviously also be wrapped in an async
and await
structure.
Igor Serdyukov, also known as Игорь Сердюков or WhiteSharq has implemented such a wrapper, saying:
I thought you might be interested in my small humble invention for Revit coders. I call it
RevitTask
. You can find details in the RevitTask project GitHub page. It is also available as a nuget package. It includes two samples, a minimal test and a little more interesting example project using Revit as a server and handling requests from the browser.
The issue of communicating with another process was also discussed back and forth a couple of times in the past few days in the Revit API discussion forum thread on Revit add-in communicating with other process:
Question: I need to write a Revit add-in (the external command) which listens to messages from other process (not from Revit itself). In other words, the Revit add-in would be the server, another process would be a client.
I tried to use async pipe to communicate with other process but the Revit add-in doesn't listen to any messages.
I think it's because of my server-side pipe in Revit is closed as soon as the Execute
method returns and terminates.
Is there any way to keep the server pipe still alive, even after the add-in's Execute
finishes?
I think I should create a thread in Execute to resolve this issue. Is this approach feasible?
I would really appreciate any example code.
Answer: Yes. Thank you for the relevant and interesting question.
The recommended approach is to implement and use an external event for this.
The external event SDK sample illustrates:
Many related discussions and solutions are listed by The Building Coder on External Events for Modeless Access and Driving Revit from Outside.
Also check out some more recent discussions on using IPC:
Response: I really appreciate your prompt answer.
I'm curious about the entry point. The only way I know in order to load my customized code is by implementing an external command, You said to forget about external command. Does Revit have any other interfaces to import customised code?
Answer: Yes.
The Revit API is completely event driven.
Many (or most? or all?) of the Revit events provide a valid Revit API context.
Look at the Autodesk.Revit.DB.Events namespace.
Response: I have one more question.
From the first link you gave me on using IPC for disentanglement, I found an example code IPC_test_revit_plugin.zip.
It includes a browser project and a Revit add-in project. Is this add-in (external command) able to listen for messages from other applications until Revit is terminated?
Answer: An external command listens to one message only, and nothing else.
The only message an external command is ever interested in is the Execute
message that it implements a handler for.
The only instance that can send that message is Revit.exe.
The only time the message is sent is when Revit.exe wishes the external command to be executed.
Response: I want to develop an add-in that can be loaded in Revit that listens to messages from another process (application) as long as Revit is running. Is it possible to implement?
Answewr: Yes, using an external event.
However, Revit is not designed for that purpose and you may violate the license agreement by doing so.
For that purpose, a DA4R application may be a more appropriate choice:
Autodesk is offering a number of exciting jobs in engineering positions in various parts of the world.
Two open positions in Europe right now are for software engineers in Cambridge, UK, with the following job ids:
Here are the details on those two:
Good luck applying for these or other opportunities that you can find in the Autodesk career site!
You can ask me for a personal referral link if you find something that you are interested in.