首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Autocad .Net集成-从Excel调用简单函数时成功编译DLL错误

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

Stack Overflow用户
提问于 2019-08-07 08:59:59
回答 1查看 434关注 0票数 0

我正在与一个需要在Excel中集成Autocad的项目进行斗争。我编译了一个DLL,并在Excel中成功引用了它,但是调用一个简单函数失败了。

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

函数在这一行上失败:

代码语言:javascript
运行
复制
var acDocMgr = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;

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

请帮帮忙。

代码语言:javascript
运行
复制
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)”:自动化错误

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-08 15:04:36

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

代码语言:javascript
运行
复制
    /// <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;
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57390622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档