(原创)AutoCAD初始化C#程序中的属性相关

标签:
杂谈 |
分类: C#-AutoCAD |
1.关于CAD初始化托管程序,先看一段帮助中文件:
当AutoCAD装载一个托管程序时,它查询程序的装配件(assembly)是否有ExtensionApplication自定义特性性。如果它找到这个特性,AutoCAD把这个特性所联系的类型作为程序的入口点。如果没有找到这个特性,AutoCAD查找所有实现IExtensionApplication接口的输出类。如果没有找到相关的接口实现,AutoCAD就会跳过程序的初始化步骤。
ExtensionApplication 特性只能被附加到一个类型。这个被附加的类型必须实现IExtensionApplication接口。
除了查找IExtensionApplication 接口的实现,AutoCAD 查询程序的装配件是否有一个或多个CommandClass特性。如果找到带有这个特性的实例,AutoCAD 只查找它们所联系的命令方法类型。否则,它会查找所有输出的类型。
CommandClass 特性可以被声明为定义AutoCAD命令处理函数的任何类型。如果一个程序使用CommandClass 属性,它必须为包含有AutoCAD命令处理函数的所有类型声明一个带有此特性的实例。
下面描述了怎样使用这些特性:
2.优化代码来快速载入大型程序
1.定义一个实现Autodesk.AutoCAD.Runtime.IExtensionApplication接口的类型。如果你不需要执行初始化或终止工作,那么可以为接口方法提供空实现。
2.在装配件环境中,声明一个ExtensionApplication特性。
3.把实现了IExtensionApplication接口的类型传递给ExtensionApplication特性。
4.在装配件环境中,为每个定义AutoCAD命令方法的类声明一个CommandClass特性。
5.把命令方法类的类型传递给CommandClass特性。
3.代码分析
using System;
using System.Diagnostics;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
[assembly: ExtensionApplication(typeof(ManagedApp.Init))]
[assembly: CommandClass(typeof(ManagedApp.Commands))]
namespace ManagedApp
{
}
1. Assembly:assembly标识符显式地告诉了编译器,当前这个属性用于整个程序集,所以它的位置不能在arxClass类中,可以放在namespace ClassLibrary中或者外面。上面是放在了命名空间外面。
2.[assembly: ExtensionApplication(typeof(ManagedApp.Init))]
上面表示ExtensionApplication属性,CAD首先查找它标志的类来初始化,如果没有找到,就找如上面public class Init : IExtensionApplication 实现了IExtensionApplication的类,如果还是没有找到,就跳过初始化。所以上面[assembly: ExtensionApplication(typeof(ManagedApp.Init))]和public class Init : IExtensionApplication后面的IExtensionApplication写上一处就可以了。
3.[assembly: CommandClass(typeof(ManagedApp.Commands))]
表示ManagedApp.Commands是定义CommandClass的,就是说能够执行Commands中定义的命令(CommandMethod)如上面的[CommandMethod("kaka")]和[CommandMethod("GetVersion")],但是不能执行[CommandMethod("Test")],因为[CommandMethod("Test")]所在的类Initialize()没有定义CommandClass
4. 在属性类名与后缀Attribute间存在一个自动的编译转换。因此当我们用一个属性去修饰一个程序实体时,不需要给出Attribute这个后缀。编译器首先会在System.Attribute的所有派生类中进行匹配,如果没有找到匹配属性,它就将属性名加上Attribute后缀名后再进行匹配。
所以上面使用[CommandMethod("kaka")]和[]CommandMethodAttribute("kaka")效果是一样的。
http://s6/middle/69e8fdf0h91c548862a85&690
5.从对象浏览器中可以看到有两个定义属性的,CommandMethod和CommandClass从字面上看一个是定义类的,一个是定义方法的。
[CommandMethod("kaka",CommandFlags.Modal)],定义了CAD中能运行的命令
上面有两个参数:" kaka "和CommandFlags.Modal,"Hello"表示全局名,就是输入Hello标示public void HelloWorld(),而CommandFlags.Modal表示的意思是命令是透明的。还可以选多个表示方法,打开CommandFlags看。CommandMethod的其他属性相关含义见才鸟.net教程272页
http://s10/middle/69e8fdf0h91c54916c589&690