2

I'm developing an AutoCAD plugin for my application. I'm using AutoCAD 2012. Plugin opens .NET named pipe, and so I can connect to it from my desktop application very easily.

First of all, i have created an interface. Here it is

[ServiceContract]
public interface IConnector
{
    [OperationContract]
    [FaultContract(typeof(Exception))]
    void GetPdfVersion(string filePath, string exportFilePath);
}

My AutoCAD plugin derived from IExtensionApplication interface, so on Initialize method i've written this

this.host = new ServiceHost(typeof(Printer), new[] { new Uri("net.pipe://localhost") });
this.host.AddServiceEndpoint(typeof(IConnector), new NetNamedPipeBinding(), "GetPdfVersion");
this.host.Open();

In one of functions, I need to open document and process it. So, I have written folowing code

var docColl = Application.DocumentManager;
Document curDraw = null;
try
{
    if (File.Exists(@"d:\1.dwg"))
    {
        curDraw = docColl.Open(@"d:\1.dwg", true, string.Empty);
    }
}
catch (Exception e)
{
    Console.WriteLine(e);
}

But it throws a COM exception, on the curDraw = docColl.Open(@"d:\1.dwg", true, string.Empty); code, with HRESULT=-2147418113

I need Document object for processing the dwg file. Is there any possible ways to fix that error?

4

1 回答 1

2

AutoCAD 无法处理来自外螺纹的文档对象。这就是问题的根源。如果我编写方法并放置一个 CommandMethodAttribute - 它会起作用,但只能从 AutoCAD 控制台...但是,如果我需要从外部应用程序执行此操作怎么办?首先,需要在服务行为类上指定属性

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]

因此,所有操作只使用一个线程。

下一步,在Initialize()方法中获取 CurrentDispatcher 对象并将其放入静态变量中。

private static Dispatcher dispatcher;
public void Initialize()
    {
        dispatcher = Dispatcher.CurrentDispatcher;

        this.host = new ServiceHost(typeof(Printer), new[] { new Uri("net.pipe://localhost") });
        this.host.AddServiceEndpoint(typeof(IConnector), new NetNamedPipeBinding(), "GetPdfVersion");
        this.host.Open();
    }

通过这种方式,可以实现对AutoCAD执行上下文的控制。下一步是通过调度程序调用方法

public void GetPdfVersion(string filePath, string exportFilePath)
    {
        dispatcher.Invoke(new Action<string, string>(this.GetPdfVer), filePath, exportFilePath);   
    }

因此,通过使用这种方法,我可以运行包含在GetPdfVer来自外部应用程序的方法中的代码,并获得使用 WCF 而不是 COM 交互的所有好处。

于 2013-12-19T11:11:52.433 回答