我怎样才能通过.NET API开放的AutoCAD 2015 [英] How can I open AutoCAD 2015 through the .NET API

查看:1068
本文介绍了我怎样才能通过.NET API开放的AutoCAD 2015的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了一个钟头,还没有找到的东西,将有助于这一点。我正在使用C#在VS2013的.NET API打开AutoCAD,但由于某些原因,我永远不能AutoCAD的真正启动。我用下面的code:

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

            acDocComObj.SendCommand("DRAWCOMPONENT");
        }
    }

不幸的是,它总是停在嵌套的语句,并始终显示弹出框,无需打开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?

编辑:错误消息

Error message

推荐答案

这个问题是你的编码(正确),以AutoCAD的互操作界面。我建议不要说(由于潜在的版本变化)。

The issue is you're coding (correctly) to the AutoCAD interop interface. I recommend against that (due to potential version changes).

另一个问题是,使用较新的.NET API的AutoCAD插件文件是插件时,AutoCAD已经在运行。

The other issue is that the documentation for AutoCAD plugins using the newer .net api is for plugins when AutoCAD is already running.

最后一个问题可能是AutCAD的程序ID是一个谜。我已经使出使一个可配置的设置,但默认为AutoCAD.Application,这将生产机上当前注册的AutoCAD.Application。如果有安装在计算机上有多个版本,并要具体,那么你可以追加版本号(你需要研究)的进程id,如:​​AutoCAD.Application.19,或AutoCAD.Application 0.202015年。

Final issue could be that the program Id of AutCAD is a mystery. I have resorted to making that a configurable setting, but default to "AutoCAD.Application", which will take the currently registered AutoCAD.Application on the production machine. If there are multiple versions installed on the machine and you want to be specific, then you could append the version number (which you'll need to research) to the ProgID like: "AutoCAD.Application.19", or "AutoCAD.Application.20" for 2015.

对于第一个问题,一种方法是使用动态的AutoCAD对象,特别是在创建实例。我已经使用了ObjectARX的API创建我的应用程序在一个虚拟的项目,然后切换到动态时,我很高兴的属性和方法名。

For the first issue, one technique is to use dynamics for the autoCad objects, particularly for creating instances. I have used the ObjectARX api for creating my application in a dummy project, and then switching to dynamics when I'm happy with the properties and method names.

在启动AutoCAD中,你可以使用像一个独立的.NET应用程序:

In a standalone .Net application that starts AutoCAD you could use something like:

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

这篇关于我怎样才能通过.NET API开放的AutoCAD 2015的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆