I have a problem with implementing WCF in Autocad P&ID 2014. I've downloaded the trial version of P&ID 2014 to run some test with it. I want to talk to the Autcad application with an external windows aplication. With the help of this example, I've managed it to open Autocad with the external applicatoin. I also created some self made Commands by creating .dll files and NETLOAD them into Autocad. All works fine as I expected. After that, I want to use WCF to talk to the Autocad application. I watched the lesson of Paul Schleppy, which is recommanded in this discussion. I get the WCF service working in Autocad and I can send messages to it without any problems. The service is hosted inside autocad.
The only thing is: When I want to talk to the 'CurrentDocument' of Autocad, I get the following error: eNotApplicable. This error occurs for an example, when I want to send something to the editor of the CurrentDocument. But also when I want to open the database of the CurrentDocument. An expert on .NET programming with Autocad tells me this is because I want to use UI dependent functions on a background thread, which are only available on the UI thread. He said I must avoid this kind of functions. But that means I can't do anything with the CurrentDocument?? He also gave another solution. He says I have to store the UI SynchronizationContext at the startup of the host and then use the UI dependent functions with a 'Post' or 'Send'. But that isn't working either, I still get the eNotApplicable error.
He said the problem could be the trial version of Autocad P&ID 2014, because sometimes trail versions acting weird.
Now I'am curious: Do you have other solutions? Or do you have other experiences with WCF and Autocad and how do you work this out? I really want to make this working.
I have attached my Visual Studio solution. It contains three projects. One host, one service and one client (a little Winform app). I use VS2012 on a 64-bit laptop. Befor you build the soluton, make shure you change the IP adress in the 'Form1' class of the client. Once you build the solution, first NETLOAD the host in Autocad and then the service. Then use the following command: "AutoHostAutocadService". Now the service is listening. After that, you can attach youre VS to Autocad and the you can press F5 for debugging. A little winform app will start. Type something in the textbox and click the button. If you have set a breakpoint in the constructor of the 'FindTag' class, this breakpoint will hit. In the constructor, the eNotApplicable is raised by writing a message to the editor.
Please, can anyone help me?
Solved! Go to Solution.
Thanks for your reply. I already looked at that thread two weeks ago, but I didn't get it working. Maybe I'm going to look at it another time again.
Do you have it working now? You first also get the eNotApplicable error? Is there another way to use WCF with autocad or do you use the same way as I?
Thanks in advance.
eNotApplicable just means you are trying to access the dwg DB from a service response which is running in a different thread than your IEXtensionApplication.
All you have to do is create a new VB class project and copy/paste the code into it, add the usual references, (make sure you remove double space ow attribute refs will not compile) , compile the DLL and it should netload and start the WCF service. I am using this template for actual projects and it works. If you get stuck let me know where, chances are I can help you out.
Here is the list of references you'll have to add to your project:
BTW, I just downloaded and took a quick look at your code, I think now I remeber the issue, I struggled with it myself. The bottom line: Editor access has to be from the main thread. Again take a look at my logMessage function.
I tried that like this, it didn't work for me (no exceptions, but never saw the message either)
Action action = () =>
fDocument.Editor.WriteMessage("Hello, this is FindTag : IFindTag");
I have to issue a correction to my earlier post! (I am using Adam Nagy's solution for this) it's not that your call has to be in the main thread, rather it has to go through the Invoke method of a Control, at least that's what worked for me, but if you can make Fenton's idea work that certainly looks more elegant since your FindTag class won't even have to inherit from Control.
Thanks for your reply. I did a web search on 'Dispatcher.beginInvoke()'. Is it true that this used in WPF, and not in Winforms? Because in Winforms we have the 'Control.BeginInvoke' method. I can't use Dispatcher.beginInvoke() because there is no reference for Winforms called 'System.Windows.Threading'. Also adding the 'Windows.Base' reference isn't working. So how would I implement this?
Start with some of our most frequented solutions to get help installing your software.