首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过.NET应用编程接口打开AutoCAD 2015

如何通过.NET应用编程接口打开AutoCAD 2015
EN

Stack Overflow用户
提问于 2014-06-24 22:59:32
回答 5查看 13K关注 0票数 11

我已经浏览了一个小时了,还没有找到对此有帮助的东西。我正在使用C#从VS2013中的.NET应用程序接口打开AutoCAD,但由于某些原因,我永远无法让AutoCAD真正启动。我使用了以下代码:

代码语言:javascript
复制
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:\Users\Administrator\Documents\All Code\main-libraries\IOAutoCADHandler\bin\Debug\IOAutoCADHandler.dll" + (char)34 + ") ");

            acDocComObj.SendCommand("DRAWCOMPONENT");
        }
    }

不幸的是,它总是停在嵌套的catch语句处,并且总是在不打开AutoCAD的情况下显示弹出框。有什么建议至少能让AutoCAD为我打开吗?

编辑:错误消息

EN

回答 5

Stack Overflow用户

发布于 2014-09-10 23:17:47

问题是您(正确地)对AutoCAD互操作接口进行了编码。我建议不要这么做(因为可能会有版本变化)。

另一个问题是,使用较新的.net应用程序接口的AutoCAD插件的文档是针对AutoCAD已经运行的插件的。

最后一个问题可能是AutCAD的程序Id是一个谜。我采用了可配置的设置,但默认设置为" AutoCAD.Application ",它将获取生产机器上当前注册的AutoCAD.Application。如果机器上安装了多个版本,而您想要具体一点,那么您可以将版本号(需要研究)附加到ProgID中,如:"AutoCAD.Application.19",或"AutoCAD.Application.20“,用于2015年。

对于第一个问题,一种技术是对autoCad对象使用动态,特别是在创建实例时。我已经使用ObjectARX应用程序接口在一个虚拟项目中创建了我的应用程序,然后在对属性和方法名称满意时切换到dynamics。

在启动AutoCAD的独立.Net应用程序中,您可以使用如下代码:

代码语言:javascript
复制
// I comment these out in production
//using Autodesk.AutoCAD.Interop;
//using Autodesk.AutoCAD.Interop.Common;
//...
//private static AcadApplication _application;
private static dynamic _application;
static string _autocadClassId = "AutoCAD.Application";

private static void GetAutoCAD()
{
    _application = Marshal.GetActiveObject(_autocadClassId);
}

private static void StartAutoCad()
{
    var t = Type.GetTypeFromProgID(_autocadClassId, true);
    // Create a new instance Autocad.
    var obj = Activator.CreateInstance(t, true);
    // No need for casting with dynamics
    _application = obj;
}

public static void EnsureAutoCadIsRunning(string classId)
{
    if (!string.IsNullOrEmpty(classId) && classId != _autocadClassId)
        _autocadClassId = classId;
    Log.Activity("Loading Autocad: {0}", _autocadClassId);
    if (_application == null)
    {
        try
        {
            GetAutoCAD();
        }
        catch (COMException ex)
        {
            try
            {
                StartAutoCad();
            }
            catch (Exception e2x)
            {
                Log.Error(e2x);
                ThrowComException(ex);
            }
        }
        catch (Exception ex)
        {
            ThrowComException(ex);
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2015-08-18 22:58:22

当一台计算机上安装了多个版本的AutoCAD时,使用ProgID "AutoCAD.Application“创建实例将运行当前用户在此计算机上启动的最新版本。如果使用的互操作程序集的版本与正在启动的版本不匹配,您将得到一个带有HRESULT0x80004002 (E_NOINTERFACE)的System.InvalidCastException。

在您的特定案例中,错误消息中的{070AA05D-DFC1-4E64-8379-432269B48B07} IID是64位R19 (AutoCAD 2013和2014)中AcadApplication接口的GUID。因此,有一个正在启动的AutoCAD 2013或2014,您不能将此COM对象强制转换为2015类型,因为2015是R20 (不兼容二进制)。

为了避免这种情况,你可以向你的ProgID添加一个特定的版本(比如AutoCAD 2015 (R20.0)到2016 (R20.1)的"AutoCAD.Application.20“),以启动与你的互操作程序集匹配的版本,或者你可以使用后期绑定(例如。删除对Autodesk.AutoCAD.Interop*的引用,并使用dynamic关键字而不是AutoCAD类型)。

在最后一种情况下,您将失去自动完成功能,但您的程序可以与所有版本的AutoCAD一起工作。

还要检查32位和64位,因为TypeLib/Interop程序集不同。

票数 2
EN

Stack Overflow用户

发布于 2014-08-08 01:13:30

我以一种非常简单的方式打开应用程序。首先,确保引用正确的类型库。我使用的是autodesk 2014类型库,位于: c:\program files\common files\ AutoCAD shared\acax19enu.tlb

要初始化应用程序:

代码语言:javascript
复制
using AutoCAD;

namespace test
{
class Program
{
    static void Main(string[] args)
    {
        AutoCAD.AcadApplication app;

        app = new AcadApplication();

        app.Visible = true;

        Console.Read();
    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24389965

复制
相关文章

相似问题

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