【问题标题】:DocumentCollection.Open() function not working AutoCAD APIDocumentCollection.Open() 函数不工作 AutoCAD API
【发布时间】:2013-12-19 19:11:52
【问题描述】:

我正在为我的应用程序开发一个 AutoCAD 插件。我正在使用 AutoCAD 2012。插件打开 .NET 命名管道,因此我可以非常轻松地从我的桌面应用程序连接到它。

首先,我创建了一个界面。在这里

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

我的 AutoCAD 插件是从 IExtensionApplication 接口派生的,所以我写的 Initialize 方法

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

在其中一个功能中,我需要打开文档并对其进行处理。 所以,我写了以下代码

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);
}

但它会在 curDraw = docColl.Open(@"d:\1.dwg", true, string.Empty); 代码上引发 COM 异常,HRESULT=-2147418113

我需要 Document 对象来处理 dwg 文件。有什么方法可以解决这个错误吗?

【问题讨论】:

  • 除了docColl.Open之外,你有没有尝试过做其他事情?例如,关闭所有打开的文档。如果是这样,您遇到了什么错误?
  • 是的。当我尝试过时,发生了同样的错误。我刚刚找到了解决方案。我会尽快写的

标签: c# wcf named-pipes autocad autocad-plugin


【解决方案1】:

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执行上下文的控制。下一步是通过dispatcher调用方法

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2014-10-17
    • 2018-12-09
    • 1970-01-01
    相关资源
    最近更新 更多