获取AutoCAD中.Net程序定义的命令——Through the Interface
2013-10-30 23:29
381 查看
原文:Getting the list of .NET-defined commands in AutoCAD
Kerry Brown提出了一个有趣的问题:
有没有一种办法来确定从托管代码加载到Acad中的命令…是一个全局列表或与一个特定的组件相关的列表…或着两者都有:-)
我设法把一些代码组合到一起来实现这个功能(虽然我需要考虑如何AutoCAD是如何做到的来实现某些细节)。我选择了实现两种类型的命令——一是获取所有的加载的程序集的命令,另一个只是对当前正在执行的程序集有效。但是第一个命令执行的相当缓慢,因为它需要花时间来查询每个加载的组件-所以我增加了一个命令,只能查询显式声明过CommandClass属性的组件。
我没有写查询特定组件定义的命令,这作为留给读者的练习。:-)
以下是C#代码
Kerry Brown提出了一个有趣的问题:
有没有一种办法来确定从托管代码加载到Acad中的命令…是一个全局列表或与一个特定的组件相关的列表…或着两者都有:-)
我设法把一些代码组合到一起来实现这个功能(虽然我需要考虑如何AutoCAD是如何做到的来实现某些细节)。我选择了实现两种类型的命令——一是获取所有的加载的程序集的命令,另一个只是对当前正在执行的程序集有效。但是第一个命令执行的相当缓慢,因为它需要花时间来查询每个加载的组件-所以我增加了一个命令,只能查询显式声明过CommandClass属性的组件。
我没有写查询特定组件定义的命令,这作为留给读者的练习。:-)
以下是C#代码
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using System; using System.Reflection; using System.Collections.Specialized; namespace GetLoadedCommands { public class Commands { [CommandMethod("TC")] static public void ListCommandsFromThisAssembly() { // Just get the commands for this assembly DocumentCollection dm = Application.DocumentManager; Editor ed = dm.MdiActiveDocument.Editor; Assembly asm = Assembly.GetExecutingAssembly(); string[] cmds = GetCommands(asm, false); foreach (string cmd in cmds) { ed.WriteMessage(cmd + "\n"); } } [CommandMethod("LCM")] static public void ListMarkedCommands() { // Get the commands for all assemblies, // but only those with explicit // CommandClass attributes (much quicker) StringCollection cmds = new StringCollection(); DocumentCollection dm = Application.DocumentManager; Editor ed = dm.MdiActiveDocument.Editor; Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly asm in asms) { cmds.AddRange(GetCommands(asm, true)); } foreach (string cmd in cmds) { ed.WriteMessage(cmd + "\n"); } } [CommandMethod("LC")] static public void ListCommands() { // Get the commands for all assemblies, // marked or otherwise (much slower) StringCollection cmds = new StringCollection(); DocumentCollection dm = Application.DocumentManager; Editor ed = dm.MdiActiveDocument.Editor; Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly asm in asms) { cmds.AddRange(GetCommands(asm, false)); } foreach (string cmd in cmds) { ed.WriteMessage(cmd + "\n"); } } private static string[] GetCommands( Assembly asm, bool markedOnly ) { StringCollection sc = new StringCollection(); object[] objs = asm.GetCustomAttributes( typeof(CommandClassAttribute), true ); Type[] tps; int numTypes = objs.Length; if (numTypes > 0) { tps = new Type[numTypes]; for(int i=0; i < numTypes; i++) { CommandClassAttribute cca = objs[i] as CommandClassAttribute; if (cca != null) { tps[i] = cca.Type; } } } else { // If we're only looking for specifically // marked CommandClasses, then use an // empty list if (markedOnly) tps = new Type[0]; else tps = asm.GetExportedTypes(); } foreach (Type tp in tps) { MethodInfo[] meths = tp.GetMethods(); foreach (MethodInfo meth in meths) { objs = meth.GetCustomAttributes( typeof(CommandMethodAttribute), true ); foreach (object obj in objs) { CommandMethodAttribute attb = (CommandMethodAttribute)obj; sc.Add(attb.GlobalName); } } } string[] ret = new string[sc.Count]; sc.CopyTo(ret,0); return ret; } } }
相关文章推荐
- 优化AutoCAD .NET 程序的加载——Through the Interface
- 替换AutoCAD的OPEN命令——Through the Interface
- 在.Net程序中调用ObjectARX函数——Through the Interface
- Through the Interface——.NET 和 AutoCAD
- 在AutoCAD中使用Python——Through the Interface
- 支持多个版本的AutoCAD——C#中的条件编译——Through the Interface
- .Net中获取程序执行时间的方法
- .net一般处理程序获取前台页面请求方法
- 关于.NET程序【程序遇到了用户定义的断点错误】的解决方案
- linux C程序中获取shell脚本输出(如获取system命令输出)
- linux C程序中获取shell脚本输出(如获取system命令输出)
- ubuntu下获取一个程序在终端的运行命令的方法
- .NET 获取执行程序所在路径
- 使用ionice命令获取或设置程序的I/O调度类别和优先级
- linux C程序中获取shell脚本输出(如获取system命令输出)
- 怎样在Web.config定义变量值,并且从程序怎么获取值?
- .net获取打包程序路径
- 在.net中悄悄执行dos命令,并获取执行的结果
- 程序内执行shell命令自动获取局域网内所有在线IP和开放端口(Linux)
- 怎样在Web.config定义变量值,并且从程序怎么获取值?