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

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 5925|回复: 115
打印 上一主题 下一主题

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

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12404

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-8 16:13:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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方法是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

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对

相关帖子

工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言

66

主题

1001

帖子

2354

积分

BIM顾问

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

积分
2354

元老勋章特殊贡献勋章

推荐
发表于 2014-8-1 14:29:32 | 只看该作者
路过!!!
不发表意见……
官方主页:www.eabim.net

66

主题

1001

帖子

2354

积分

BIM顾问

Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8Rank: 8

积分
2354

元老勋章特殊贡献勋章

推荐
发表于 2014-8-1 14:26:45 | 只看该作者
路过!!!
不发表意见……
官方主页:www.eabim.net

13

主题

840

帖子

1580

积分

BIM经理

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

积分
1580
4F
发表于 2014-5-7 10:01:48 | 只看该作者
路过!!!
不发表意见……

7

主题

865

帖子

1435

积分

BIM经理

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

积分
1435
5F
发表于 2014-5-13 10:19:28 | 只看该作者
(*^__^*) 嘻嘻……

28

主题

763

帖子

1425

积分

BIM经理

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

积分
1425
6F
发表于 2014-5-13 10:24:48 | 只看该作者
路过!!!
不发表意见……

0

主题

843

帖子

1280

积分

BIM经理

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

积分
1280
8F
发表于 2014-5-13 10:35:28 | 只看该作者
路过!!!
不发表意见……

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12404

社区QQ达人

12F
 楼主| 发表于 2014-7-28 10:18:08 | 只看该作者

(*^__^*) 嘻嘻……
工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-23 10:38

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表