问题描述
我已经浏览了一个小时,但还没有找到可以帮助解决这个问题的东西.我正在使用 C# 从 VS2013 中的 .NET API 打开 AutoCAD,但由于某种原因,我永远无法真正启动 AutoCAD.我正在使用以下代码:
I've been browsing for a good hour and have yet to find something that would help with this. I'm working on opening AutoCAD from the .NET API in VS2013 using C#, but for some reason, I can never get AutoCAD to actually launch. I'm using the following code:
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
namespace IOAutoCADHandler
{
public static class ACADDocumentManagement
{
[CommandMethod("ConnectToAcad")]
public static void ConnectToAcad()
{
AcadApplication acAppComObj = null;
// no version number so it will run with any version
const string strProgId = "AutoCAD.Application";
// Get a running instance of AutoCAD
try
{
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch //// STOPS HERE
{
// If an instance of AutoCAD is not created then message and exit
// NOTE: always shows this box and never opens AutoCAD
System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
" could not be created.");
return;
}
}
// Display the application and return the name and version
acAppComObj.Visible = true;
System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name +
" version " + acAppComObj.Version);
// Get the active document
AcadDocument acDocComObj;
acDocComObj = acAppComObj.ActiveDocument;
// Optionally, load your assembly and start your command or if your assembly
// is demandloaded, simply start the command of your in-process assembly.
acDocComObj.SendCommand("(command " + (char)34 + "NETLOAD" + (char)34 + " " +
(char)34 + @"C:UsersAdministratorDocumentsAll Codemain-librariesIOAutoCADHandlerinDebugIOAutoCADHandler.dll" + (char)34 + ") ");
acDocComObj.SendCommand("DRAWCOMPONENT");
}
}
不幸的是,它总是停在嵌套的 catch
语句处,并且总是在不打开 AutoCAD 的情况下显示弹出框.关于如何至少为我打开 AutoCAD 的任何建议?
Unfortunately, it always stops at the nested catch
statement and always displays the popup box without opening AutoCAD. Any suggestions on how to at least make AutoCAD open for me?
错误信息
推荐答案
当计算机上安装了多个版本的 AutoCAD 时,创建一个 ProgID 为AutoCAD.Application"的实例将运行在这台计算机上启动的最新版本当前用户.如果使用的互操作程序集的版本与启动的版本不匹配,您将收到 System.InvalidCastException 和 HRESULT 0x80004002 (E_NOINTERFACE).
When there are several versions of AutoCAD installed on a computer, creating an instance with the ProgID "AutoCAD.Application" will run the latest version started on this computer by the current user. If the version of the Interop assemblies used does not match the version that is starting, you'll get a System.InvalidCastException with an HRESULT 0x80004002 (E_NOINTERFACE).
在您的特定情况下,错误消息中的 {070AA05D-DFC1-4E64-8379-432269B48B07} IID 是 R19 64 位 (AutoCAD 2013 & 2014) 中 AcadApplication
接口的 GUID).所以有一个 AutoCAD 2013 或 2014 正在启动,您不能将此 COM 对象转换为 2015 类型,因为 2015 是 R20(不兼容二进制).
In your specific case, the {070AA05D-DFC1-4E64-8379-432269B48B07} IID in your error message is the GUID for the AcadApplication
interface in R19 64-bit (AutoCAD 2013 & 2014). So there is an AutoCAD 2013 or 2014 that is starting, and you cannot cast this COM object to a 2015 type because 2015 is R20 (not binary compatible).
为避免这种情况,您可以将特定版本添加到您的 ProgID(例如 AutoCAD 2015 (R20.0) 到 2016 (R20.1) 的AutoCAD.Application.20")以启动与您的互操作程序集匹配的版本,或者您可以使用后期绑定(例如,删除对 Autodesk.AutoCAD.Interop* 的引用并使用 dynamic
关键字而不是 AutoCAD 类型).
To avoid that, you can add a specific version to your ProgID (like "AutoCAD.Application.20" for AutoCAD 2015 (R20.0) to 2016 (R20.1)) to start the version matching your Interop assemblies or you can use late binding (eg. remove your references to Autodesk.AutoCAD.Interop* and use the dynamic
keyword instead of the AutoCAD types).
在最后一种情况下,您将失去自动完成功能,但您的程序将适用于所有版本的 AutoCAD.
In the last case, you will lost autocompletion, but your program will work with all the versions of AutoCAD.
还要检查 32 位和 64 位,因为 TypeLib/Interop 程序集不一样.
Check also 32-bit vs 64-bit because TypeLib/Interop assemblies are not the same.
这篇关于如何通过 .NET API 打开 AutoCAD 2015的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!