获取AutoCAD中.Net程序定义的命令——Through the Interface

原文: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;
    }
  }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值