您的位置:首页 > 其它

优化AutoCAD .NET 程序的加载——Through the Interface

2013-10-23 21:26 399 查看
原文:Optimizing the loading of AutoCAD .NET applications

以前的一个帖子中,我描述了如何使用Autodesk.AutoCAD.Runtime.IExtensionApplication接口在实现.NET模块的初始化代码。在此基础上,我们现在要看看如何通过Autodesk.AutoCAD.Runtime.IExtensionApplication接口来优化托管模块的架构能更快的加载到AutoCAD中。

首先,是Using .NET for AutoCAD documentation中的一些信息(这些可以在ObjectARX开发指南中找到):

在AutoCAD加载托管应用程序,它先在查询程序的组件中查找ExtensionApplication这个自定义属性,如果发现这个属性,AutoCAD会把属性相关的类型设置为程序的入口,如果没发现这个属性,AutoCAD就会跳过特定于应用程序的初始化步骤。



除了寻找一个IExtensionApplication的实现,AutoCAD还会在查询程序的组件中查找CommandClass属性。如果发现有此属性的实例,AutoCAD只搜索相关类型作为命令对应的方法。否则,它会搜索所有的导出类型。

我在这篇博客中所提到例子以及大部分在ObjectARX SDK中的例子,它们不会告诉你在代码中如何使用ExtensionApplication或者CommandClass属性,因为实现它们对于让你的程序工作起来并不是必须的,但是但如果你有一个庞大大的.NET模块要加载到AutoCAD,AutoCAD检查该组件中的各对象来找出哪些是ExtensionApplication哪些是各种命令类就需要花费一些时间了。

你需要实现的属性都是非常简单的:

C#:

[assembly: ExtensionApplication(typeof(InitClass))]
[assembly: CommandClass(typeof(CmdClass))]

VB .NET

[assembly: ExtensionApplication(typeof(InitClass))]
[assembly: CommandClass(typeof(CmdClass))]</


这些组件级别的属性只是告诉AutoCAD在哪里寻找各种对象,否则就需要通过搜来确定。以下是一些关于如何使用这些属性的文档:

该ExtensionApplication属性只能附加到一种类型。它所附加的类型必须实现IExtensionApplication接口。

CommandClass属性可以为任何定义了AutoCAD命令处理程序的函数声明。如果应用程序使用commandClass属性,它必须对每种包含AutoCAD命令处理程序方法的实例声明此属性。

为了优化昨天的代码减少加载时间,我也稍微改变了下结构使得它更合乎逻辑。上述属性也在命名空间中关联类,我决定把初始化代码(Initialization类)从命令的实现代码(Commands类)中分离出来,但是把他们都放在(“ManagedApplication”)命名空间中

这里是代码…:

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using System;
[assembly:
ExtensionApplication(
typeof(ManagedApplication.Initialization)
)
]
[assembly:
CommandClass(
typeof(ManagedApplication.Commands)
)
]
namespace ManagedApplication
{
public class Initialization
: Autodesk.AutoCAD.Runtime.IExtensionApplication
{
public void Initialize()
{
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("Initializing - do something useful.");
}
public void Terminate()
{
Console.WriteLine("Cleaning up...");
}
}
public class Commands
{
[CommandMethod("TST")]
public void Test()
{
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("This is the TST command.");
}
}
}


VB .NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports System
<Assembly: _
ExtensionApplication( _
GetType(ManagedApplication.Initialization))>
<Assembly: _
CommandClass( _
GetType(ManagedApplication.Commands))>
Namespace ManagedApplication
Public Class Initialization
Implements Autodesk.AutoCAD.Runtime.IExtensionApplication
Public Sub Initialize() Implements _
IExtensionApplication.Initialize
Dim ed As Editor = _
Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage("Initializing - do something useful.")
End Sub
Public Sub Terminate() Implements _
IExtensionApplication.Terminate
Console.WriteLine("Cleaning up...")
End Sub
End Class
Public Class Commands
<CommandMethod("TST")> _
Public Sub Test()
Dim ed As Editor = _
Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage("This is the TST command.")
End Sub
End Class
End Namespace
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: