Autocad .Net 集成 - 从 Excel 调用简单函数时成功编译 DLL 错误

我正在处理一个需要在 Excel 中集成 Autocad 的项目。我编译了一个 DLL,并在 Excel 中成功引用,但调用一个简单函数失败。                                         


COM接口没有问题;该项目已勾选这些,我可以成功地从 Excel 调用一个简单的“hello world”测试函数。我还拥有 C# 项目中所有正确的引用。多余的参考资料是后续工作所需要的。


该函数在这一行失败:


var acDocMgr = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;

如果无论 Autocad 应用程序是否打开都失败。


请帮忙。


using System.Collections.Generic;

using System.Runtime.InteropServices;

using System.Linq;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Runtime;

using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

using Autodesk.AutoCAD.ApplicationServices;


[assembly: CommandClass(typeof(AutocadHandler.MyCommands))]


namespace AutocadHandler

{

    [ClassInterface(ClassInterfaceType.AutoDual)]



    public class MyCommands

    {


        public static void TestFunction()

        {                       

            string strFileName = "C:\\Users\\CORE I7\\Documents\\Drawing2XLS.dwg";

            var acDocMgr = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;

            acDocMgr.Open(strFileName, false);

            acDocMgr.MdiActiveDocument.Editor.WriteMessage("Hello Excel");

        }


    }


}

Excel 返回的错误是:


运行时错误'-2146233036 (80131534)':自动化错误


慕森卡
浏览 124回答 1
1回答

慕姐4208626

您是否尝试从 Excel 中运行代码,并尝试让 Excel 打开 AutoCAD 来操作绘图?我认为这行不通。您可以采用另一种方式,打开 AutoCAD,加载插件,然后通过 API 将 AutoCAD 中的信息提供给 Excel。AutoCAD API 需要运行 AutoCAD(或 ACCORECONSOLE,这是 AutoCAD 的命令行版本,但这需要一些额外的管道)才能对图形文件执行任何操作。如果是 AutoCAD,而不是 ACCORECONSOLE,则通常需要至少打开一张图形(..DocumentManager.MdiActiveDocument)。然后,您可以使用文档管理器打开其他文档(假设您有权限这样做)。&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Look through the Application's Document manager for a Document object with the given name.&nbsp; If found return it,&nbsp; &nbsp; /// else open the drawing/Document and return it.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="name">The name to look for in the collection</param>&nbsp; &nbsp; /// <returns>An AutoCAD Document object.</returns>&nbsp; &nbsp; public static ACADApp.Document GetDocumentByName(string name)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (ACADApp.Document doc in ACADApp.Application.DocumentManager)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (doc.Database.Filename.ToUpper() == name.ToUpper() || doc.Name.ToUpper() == name.ToUpper())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return doc;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ACADApp.Application.DocumentManager.Open(name);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (System.Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TBExceptionManager.HandleException(name, ex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP