如何通过.NET API打开AutoCAD 2015

我已经浏览了一个小时,还没有找到有助于此的东西。 我正在使用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互操作界面。 我建议不要这样做(由于潜在的版本更改)。

另一个问题是,当AutoCAD已经运行时,使用较新的.net api的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); } } } 

如果计算机上安装了多个版本的AutoCAD,则使用ProgID“AutoCAD.Application”创建实例将运行当前用户在此计算机上启动的最新版本。 如果使用的Interop程序集的版本与正在启动的版本不匹配,您将获得带有HRESULT 0x80004002(E_NOINTERFACE)的System.InvalidCastException。

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

为避免这种情况,您可以将特定版本添加到ProgID(例如AutoCAD 2015(R20.0)到2016(R20.1)的“AutoCAD.Application.20”)以启动与Interop组件匹配的版本,或者您可以使用后期绑定(例如,删除对Autodesk.AutoCAD.Interop *的引用,并使用dynamic关键字而不是AutoCAD类型)。

在最后一种情况下,您将丢失自动完成function,但您的程序将适用于所有版本的AutoCAD。

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

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

要初始化应用程序:

 using AutoCAD; namespace test { class Program { static void Main(string[] args) { AutoCAD.AcadApplication app; app = new AcadApplication(); app.Visible = true; Console.Read(); } } } 

试试这个:

“sourcefile”是原始文件
“newfile”是新文件

 [CommandMethod("ModifyAndSaveas", CommandFlags.Redraw | CommandFlags.Session)] public void ModifyAndSaveAs() { Document acDoc = Application.DocumentManager.Open(sourcefile); Database acDB = acDoc.Database; Transaction AcTran = acDoc.Database.TransactionManager.StartTransaction(); using (DocumentLock acLckDoc = acDoc.LockDocument()) { using (AcTran) { BlockTable acBLT = (BlockTable)AcTran.GetObject(acDB.BlockTableId, OpenMode.ForRead); BlockTableRecord acBLTR = (BlockTableRecord)AcTran.GetObject(acBLT[BlockTableRecord.ModelSpace], OpenMode.ForRead); var editor = acDoc.Editor; var SelectionSet = editor.SelectAll().Value; foreach (ObjectId id in SelectionSet.GetObjectIds()) { Entity ent = AcTran.GetObject(id, OpenMode.ForRead) as Entity; //modify entities } AcTran.Commit(); } } acDB.SaveAs(newfile, DwgVersion.AC1021); } 
 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; namespace Tekkit { class Program { static void Main(string[] args) { //make sure to add last 2 using statements ProcessStartInfo start = new ProcessStartInfo("calc.exe"); Process.Start(start);//starts the process } } }