【问题标题】:Autocad .Net integration - successfully compiled DLL errors on calling a simple function from ExcelAutocad .Net 集成 - 从 Excel 调用简单函数时成功编译 DLL 错误
【发布时间】:2019-12-14 20:49:40
【问题描述】:

我正在处理一个需要在 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)”:自动化错误

【问题讨论】:

    标签: c# .net excel com autocad


    【解决方案1】:

    您是否尝试在 Excel 中运行您的代码,并尝试让 Excel 打开 AutoCAD 来操作绘图?我认为这行不通。您可以另辟蹊径,打开 AutoCAD,加载插件,然后通过 API 将 AutoCAD 中的信息输入 Excel。 AutoCAD API 需要运行 AutoCAD(或 ACCORECONSOLE,它是 AutoCAD 的命令行版本,但需要一些额外的管道)才能对图形文件执行任何操作。如果它是 AutoCAD,而不是 ACCORECONSOLE,您通常需要至少打开一个图形(..DocumentManager.MdiActiveDocument)。然后,您可以使用文档管理器打开其他文档,前提是您有权这样做。

        /// <summary>
        /// Look through the Application's Document manager for a Document object with the given name.  If found return it,
        /// else open the drawing/Document and return it.
        /// </summary>
        /// <param name="name">The name to look for in the collection</param>
        /// <returns>An AutoCAD Document object.</returns>
        public static ACADApp.Document GetDocumentByName(string name)
        {
            try
            {
                foreach (ACADApp.Document doc in ACADApp.Application.DocumentManager)
                {
                    if (doc.Database.Filename.ToUpper() == name.ToUpper() || doc.Name.ToUpper() == name.ToUpper())
                    {
                        return doc;
                    }
                }
                return ACADApp.Application.DocumentManager.Open(name);
    
            }
            catch (System.Exception ex)
            {
                TBExceptionManager.HandleException(name, ex);
                return null;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 2023-03-30
      • 2021-10-12
      • 2020-09-23
      • 1970-01-01
      相关资源
      最近更新 更多