EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

萧闫子 发表于 2014-1-8 16:13:54

[资料] AutoCAD .NET API基础(五) 进程外与进程内

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 theCreateObject andGetObject 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 theSendCommand method that is a member of theActiveDocument property of the AcadApplication.如果需要创建一个独立的应用程序来驾驭AutoCAD,最好是在应用程序中用CreateObject方法和GetObject方法创建AutoCAD程序的一个新的实例,或者返回正在运行的AutoCAD程序的一个实例。一旦返回对AcadApplication的引用,我们就可以使用SendCommand方法将我们的进程内.NET应用程序加载到AutoCAD。 SendCommand方法是AcadApplication的ActiveDocument属性的一个成员。
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 2011的COM应用程序要用到的ProgID值是AutoCAD.Application.18。
VB.NET
Imports SystemImports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.InteropImports Autodesk.AutoCAD.RuntimeImports 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 SubC#
using System;using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.ApplicationServices;
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

嫣雨遥 发表于 2014-8-1 14:29:32

路过!!!
不发表意见……

嫣雨遥 发表于 2014-8-1 14:26:45

路过!!!
不发表意见……

leowave 发表于 2014-5-7 10:01:48

路过!!!
不发表意见……

等你回来 发表于 2014-5-13 10:19:28

(*^__^*) 嘻嘻……

一梦千寻 发表于 2014-5-13 10:24:48

路过!!!
不发表意见……

苦田辛君 发表于 2014-5-13 10:28:16

顶!!!!!!!!!!

代号蓝牙 发表于 2014-5-13 10:35:28

路过!!!
不发表意见……

月之影 发表于 2014-5-16 13:49:37

顶!!!!!!!!!!!!!!!!!!!!!!!!!

NetBeetle 发表于 2014-5-22 15:36:00

顶!!!!!!!!!!

We晕晕 发表于 2014-6-13 14:56:48

顶!!!!!!!!!!!!!!!!!!!!!!!!!

萧闫子 发表于 2014-7-28 10:18:08


(*^__^*) 嘻嘻……

波罗友 发表于 2014-8-1 14:21:00

顶!!!!!!!!!!!!!!!!!!!!!!!!!

宇航员 发表于 2014-8-1 14:21:07

顶!!!!!!!!!!!!!!!!!!!!!!!!!

泰安oim 发表于 2014-8-1 14:21:15

顶!!!!!!!!!!

猫猫girl 发表于 2014-8-1 14:21:24

顶起来…………

audigy 发表于 2014-8-1 14:21:32

顶!!!!!!!!!!!!!!!!!!!!!!!!!
页: [1] 2 3 4 5 6 7 8
查看完整版本: [资料] AutoCAD .NET API基础(五) 进程外与进程内