AutoCAD .NET API基础(五) 进程外与进程内

来源:互联网 发布:淘宝刚开店怎么运营 编辑:程序博客网 时间:2024/04/30 22:31

Out-of-Process versus In-Process进程外与进程内

When you develop a new application, it can either run in or out-of-process. The AutoCAD .NET API is designed to run in-process only, which is different from the ActiveX Automation library which can be used in or -out-of-process.

当我们开发新的应用程序时,即可以运行在进程内,又可以运行在进程外。AutoCAD .NET API只设计用来运行于进程内,这与能运行于进程外的ActiveX Automation库是不同的。

·         In-process applications are designed to run in the same process space as the host application. In this case, a DLL assembly is loaded into AutoCAD which is the host application. 进程内应用程序运行在与宿主应用程序相同的进程空间。这种情况下,由宿主程序AutoCAD来加载DLL程序集。

·         Out-of-process applications do not run in the same space as the host application. These applications are often built as stand-alone executables. 进程外应用程序不和宿主应用程序运行在相同的进程空间,这样的应用程序通常生成为独立的可执行程序。

If you need to create a stand-alone application to drive AutoCAD, it is best to create an application that uses the CreateObject and GetObject methods to create a new instance of an AutoCAD application or return one of the instances that is currently running. Once a reference to an AcadApplication is returned, you can then load your in-process .NET application into AutoCAD by using the SendCommand method that is a member of the ActiveDocument property of the AcadApplication.

如果需要创建一个独立的应用程序来驾驭AutoCAD,最好是在应用程序中用CreateObject方法和GetObject方法创建AutoCAD程序的一个新的实例,或者返回正在运行的AutoCAD程序的一个实例。一旦返回对AcadApplication的引用,我们就可以使用SendCommand方法将我们的进程内.NET应用程序加载到AutoCAD SendCommand方法是AcadApplicationActiveDocument属性的一个成员。

As an alternative to executing your .NET application in-process, could use COM interop for your application.

作为运行进程内.NET应用程序的一个代替,我们还可以使用COM互操作。

 

Note: The ProgID for COM application access to AutoCAD 2011 is AutoCAD.Application.18.

注意:访问AutoCAD 2011COM应用程序要用到的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

 

  '' 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("AutoCAD.Application.18")

 

      '' 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.18";

 

  // Get a running instance of AutoCAD获取正在运行的AutoCAD实例;

  try

  {

      acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);

  }

  catch // An error occurs if no instance is running没有正在运行的实例时出错;

  {

      try

      {

          // Create a new instance of AutoCAD创建新的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 demand-loaded, 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 Code Reference

 

Sub ConnectToAcad()

    Dim acadApp As AcadApplication

    On Error Resume Next

 

    Set acadApp = GetObject(, "AutoCAD.Application.18")

    If Err Then

        Err.Clear

        Set acadApp = CreateObject("AutoCAD.Application.18")

        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

 

 

 

原创粉丝点击