您的位置:首页 > 其它

获取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#代码

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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: