对比进程外与进程内
 
 

当用户开发一个新的应用程序时,它可以运行在进程内与进程外。AutoCAD .NET API 被设计成仅可以运行在进程内,它与 ActiveX Automation 库即可以运行在进程内又可以运行在进程外不同。

如果用户需要创建一个独立的应用程序来驱动 AutoCAD,最好是使用 CreateObject 和 GetObject 方法创建一个新的 AutoCAD 应用程序的实例或返回当前正在运行的一个实例。一旦返回返回一个 AcadApplication 引用,就可以加载你的进程内 .NET 应用程序到 AutoCAD 中,它是通过使用 AcadApplication 的 ActiveDocument 属性的 SendCommand 方法来实现的。

执行进程内 .NET 应用程序的另外一种方法是使用 COM Interop。

注意AutoCAD 2010 的 COM 应用程序访问 ProgID 为“AutoCAD.Application.18”。

VB.NET

Imports System
Imports System.Runtime.InteropServices
 
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
 
<CommandMethod("ConnectToAcad")> _
Public Sub ConnectToAcad()
  Dim acAppComObj As AcadApplication
  Dim strProgId As String = "AutoCAD.Application.18"
 
  On Error Resume Next
 
  '' 获得一个正在运行的 AutoCAD 的实例  Get a running instance of AutoCAD
  acAppComObj = GetObject(, strProgId)
 
  '' 如果没有正在运行的实例将出现一个错误 An error occurs if no instance is running
  If Err.Number > 0 Then
      Err.Clear()
 
      '' 创建一个新的AutoCAD的实例  Create a new instance of AutoCAD
      acAppComObj = CreateObject("AutoCAD.Application.18")
 
      '' 检查 AutoCAD 的实例是否被创建  Check to see if an instance of AutoCAD was created
      If Err.Number > 0 Then
         Err.Clear()
 
         ''如果 AutoCAD 的实例没有创建,那么发出一个信息并退出  If an instance of AutoCAD is not created then message and exit
         MsgBox("Instance of 'AutoCAD.Application' could not be created.")
 
         Exit Sub
      End If
  End If
 
  ''显示应用程序并返回名字和版本号  Display the application and return the name and version
  acAppComObj.Visible = True
  MsgBox("Now running " & acAppComObj.Name & " version " & acAppComObj.Version)
 
  ''获得活动的文档   Get the active document
  Dim acDocComObj As AcadDocument
  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 " & Chr(34) & "NETLOAD" & Chr(34) & " " & _
                          Chr(34) & "c:/myapps/mycommands.dll" & Chr(34) & ") ")
 
  acDocComObj.SendCommand("MyCommand ")
End Sub

C#

using System;
using System.Runtime.InteropServices;
 
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
 
[CommandMethod("ConnectToAcad")]
public static void ConnectToAcad()
{
 
  AcadApplication acAppComObj = null;
  const string strProgId = "AutoCAD.Application.18";
 
  // 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
      {
          // If an instance of AutoCAD is not created then message and exit
          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:/myapps/mycommands.dll" + (char)34 + ") ");
 
  acDocComObj.SendCommand("MyCommand ");
}
VBA/ActiveX 代码参考