CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

进程外与进程内 (.NET)

2023-1-1 16:30| 发布者: admin| 查看: 817| 评论: 0|来自: AutoCAD

开发新应用程序时,它可以在进程内运行,也可以在进程外运行。AutoCAD .NET API 设计为仅在进程内运行,这与可在进程内或进程外使用的 ActiveX 自动化库不同。

  • 进程内应用程序设计为在与主机应用程序相同的进程空间中运行。在这种情况下,DLL 程序集将加载到作为主机应用程序的 AutoCAD 中。
  • 进程外应用程序不会在与主机应用程序相同的空间中运行。这些应用程序通常构建为独立的可执行文件。

如果需要创建独立应用程序来驱动 AutoCAD,最好创建一个应用程序,该应用程序使用 theand方法 创建 AutoCAD 应用程序的新实例或返回当前正在运行的实例之一。返回对 anis 的引用后,可以使用作为属性成员的方法将进程内 .NET 应用程序加载到 AutoCAD 中。CreateObjectGetObjectAcadApplicationSendCommandActiveDocumentAcadApplication

作为在进程中执行 .NET 应用程序的替代方法,可以为应用程序使用 COM 互操作。

注意:用于 COM 应用程序访问 AutoCAD 2021 的 ProgID 是AutoCAD.Application.24

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.24"
 
  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.24";
 
  // 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.24")
    If Err Then
        Err.Clear
        Set acadApp = CreateObject("AutoCAD.Application.24")
        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

路过

雷人

握手

鲜花

鸡蛋

最新评论

CAD软件2007~2024远程安装服务

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-7 01:29

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部