进程外与进程内 (.NET) 
开发新应用程序时,它可以在进程内运行,也可以在进程外运行。AutoCAD .NET API 设计为仅在进程内运行,这与可在进程内或进程外使用的 ActiveX 自动化库不同。 
 如果需要创建独立应用程序来驱动 AutoCAD,最好创建一个应用程序,该应用程序使用 和 方法创建 AutoCAD 应用程序的新实例或返回当前正在运行的实例之一。返回对 的引用后,可以使用作为 的属性成员的方法将进程内的 .NET 应用程序加载到 AutoCAD 中。CreateObjectGetObjectAcadApplicationSendCommandActiveDocumentAcadApplication 作为在进程内执行 .NET 应用程序的替代方法,可以对应用程序使用 COM 互操作。 注意:用于 COM 应用程序访问 AutoCAD 2018 的 ProgID 为 AutoCAD.Application.22。 
 
	 VB.NETImports 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.22"
 
  On Error Resume Next
 
  '' Get a running instance of AutoCAD
  acAppComObj = GetObject(, strProgId)
 
  '' An error occurs if no instance is running
  If Err.Number > 0 Then
      Err.Clear()
 
      '' Create a new instance of AutoCAD
      acAppComObj = CreateObject(strProgId)
 
      '' Check to see if an instance of AutoCAD was created
      If Err.Number > 0 Then
         Err.Clear()
 
         '' 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.22";
 
  // 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 代码参考Sub ConnectToAcad()
    Dim acadApp As AcadApplication
    On Error Resume Next
 
    Set acadApp = GetObject(, "AutoCAD.Application.22")
    If Err Then
        Err.Clear
        Set acadApp = CreateObject("AutoCAD.Application.22")
        If Err Then
            MsgBox Err.Description
            Exit Sub
        End If
    End If
 
    acadApp.Visible = True
    MsgBox "Now running " + acadApp.Name + _
           " version " + acadApp.Version
 
    Dim acadDoc as AcadDocument
    Set acadDoc = acadApp.ActiveDocument
 
    '' Optionally, load your assembly and start your command or if your assembly
    '' is demandloaded, simply start the command of your in-process assembly.
    acadDoc.SendCommand("(command " & Chr(34) & "NETLOAD" & Chr(34) & " " & _
                        Chr(34) & "c:/myapps/mycommands.dll" & Chr(34) & ") ")
 
    acadDoc.SendCommand("MyCommand ")
End Sub
 
	  | 
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1   苏公网安备32011402011833)
GMT+8, 2025-11-4 20:11
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.