c# - 如何通过 .NET API 打开 AutoCAD 2015

标签 c# .net visual-studio-2013 autocad

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

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 有什么建议吗?

编辑:错误信息
][1

最佳答案

问题是您正在(正确地)针对 AutoCAD 互操作界面进行编码。我建议不要那样做(由于潜在的版本更改)。

另一个问题是,使用更新的 .net api 的 AutoCAD 插件的文档适用于 AutoCAD 已经运行时的插件。

最后一个问题可能是 AutCAD 的程序 ID 是个谜。我已将其设为可配置设置,但默认为“AutoCAD.Application”,这将在生产机器上采用当前注册的 AutoCAD.Application。如果机器上安装了多个版本并且您想具体说明,则可以将版本号(您需要研究)附加到 ProgID,例如:“AutoCAD.Application.19”或“AutoCAD.Application .20"2015 年。

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

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

// 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);
        }
    }
}

关于c# - 如何通过 .NET API 打开 AutoCAD 2015,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24389965/

相关文章:

c# - 两个 C# 扩展泛型方法之间的调用不明确,其中 T :class and other where T:struct

javascript - AJAX PageMethods 与路由冲突?

c++ - 如何在 Visual C++ 2013 中执行嵌套的 initializer_lists

sql-server-2008-r2 - SQL71501 : Trigger: [dbo]. [TriggerName] 具有对对象 [dbo].[TableName] 的未解析引用

vb.net - Web.config 正在被 msbuild 重命名

c# - 请求 channel 在等待回复时超时

c# - 在 Windows Universal App (Windows 10) 中使用 C# 获取设备信息

c# - 在字符串中查找每个 RegEx 匹配项

c# - 寻找半小时向上/向下的时间选择器控制

c# - 如何设置 ASP.Net 3.0 Core Web API 项目来使用 AutoFac 和 NLog?