Revit addin communicating with other process

Revit addin communicating with other process

Anonymous
Not applicable
4,670 Views
10 Replies
Message 1 of 11

Revit addin communicating with other process

Anonymous
Not applicable

Hello, I need to write a Revit addin(the external command) which listens messages from other process (not from revit itself).
In other word, the revit addin would be the server, another process would be a client.

 

I tried to use async pipe to communicate with other process but revit addin couldn't listen any messages. I think it's because of my server side pipe in revit closed as execute() returns value.

 

So my question is below.
Is there any way to keep the server pipe still alive even after the addin's execute() finished?
I think I should create a thread in the execute() to resolve this issue. Is this approach feasible?
If there are some example codes, I really appreciate.

0 Likes
Accepted solutions (2)
4,671 Views
10 Replies
Replies (10)
Message 2 of 11

jeremytammik
Autodesk
Autodesk
Accepted solution

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:

  

  • SDK/Samples/ModelessDialog/ModelessForm_ExternalEvent/

  

Many related discussions and solutions are listed by The Building Coder on External Events for Modeless Access and Driving Revit from Outside:

  

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

  

Also check out some more recent discussions on using IPC:

 

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 11

Anonymous
Not applicable

Mr. Tammik, I really appreciate for your prompt answer.

 

I'm curious about entry point. The only way I know in order to load my customized code(dll) is implementing external command, but you said to me forget about external command. Does revit have another interfaces to import customized code?

0 Likes
Message 4 of 11

jeremytammik
Autodesk
Autodesk

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:

 

https://www.revitapidocs.com/2020/b86712d6-83b3-e044-8016-f9881ecd3800.htm

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 11

Anonymous
Not applicable

Mr. Tammik, I have one more question.

From the link you gave me

https://thebuildingcoder.typepad.com/blog/2019/04/set-floor-level-and-use-ipc-for-disentanglement.ht...

 

I found an example code "IPC_test_revit_plugin.zip".

There were a browser project and a revit-addin project. Is this addin(external command) able to listen messages from other applications until the revit program is terminated?

0 Likes
Message 6 of 11

jeremytammik
Autodesk
Autodesk

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.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 7 of 11

Anonymous
Not applicable

Ah, what I exactly want is to develop custom DLL which can be loaded in Revit that listens messages from another process(application) during Revit is running. Is it possible to implement?

0 Likes
Message 8 of 11

jeremytammik
Autodesk
Autodesk
Accepted solution

Yes, using an external event: 

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

 

However, Revit is not designed for that purpose and you may violate the license by doing so.

 

For that purpose, a DA4R application may be a more appropriate choice:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.55

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 9 of 11

jeremytammik
Autodesk
Autodesk

In yet another vain attempt at not having to repeat myself in the future, I publish our discussion for posterity:

 

https://thebuildingcoder.typepad.com/blog/2020/02/external-communication-and-async-await-event-wrapp...

 

 


Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 10 of 11

Anonymous
Not applicable

I found a post in your blog. Can it be a solution for me?

 

https://thebuildingcoder.typepad.com/blog/2012/11/drive-revit-through-a-wcf-service.html

0 Likes
Message 11 of 11

mastjaso
Advocate
Advocate

Hey there, yes you can do this. I'm just wrapping up work on an addin that does precisely this. The code is too rough to share at the moment but I will try and post a working sample later. 

The general gist of the flow goes like this though:

* When your ICommand runs it's execute method, you use ExternalEvent.Create() to create the ExternalEvent handlers that'll you'll use to communicate back and forth to Revit asynchronously.
* Then use var pipeThread = Thread.Start(), and pass it a callback function that starts your named pipe server, and can access those ExternalEvent handlers that you created.

* Then to keep your server thread alive and open after you start it, just call pipeThread.Join() before your ICommand finishes it's execute function. 

0 Likes