我正在与一个需要在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)”:自动化错误
发布于 2019-08-08 15:04:36
您是否试图在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;
}
}https://stackoverflow.com/questions/57390622
复制相似问题