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

2024-06-10 11:32

本文主要是介绍获取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;
}
}
}

这篇关于获取AutoCAD中.Net程序定义的命令——Through the Interface的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048037

相关文章

Windows中,.net framework 3.5安装

安装.net framework,目前已知2种方法,如下: 一、在MSDN下载对应的安装包,安装,这种可能无法安装成功,概率很大,不成功使用第二种方法,基本上没问题。 二、win8/8.1/10 下安装 .net framework 3.5.1: 1. 打开 win8/8.1/10 安装盘(这里指系统安装镜像文件),提取 sources\sxs 文件夹到 X:\sources\sxs (X代

Linux 下的Vim命令宝贝

vim 命令详解(转自:https://www.cnblogs.com/usergaojie/p/4583796.html) vi: Visual Interface 可视化接口 vim: VI iMproved VI增强版 全屏编辑器,模式化编辑器 vim模式: 编辑模式(命令模式)输入模式末行模式 模式转换: 编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式

【青龙面板辅助】JD商品自动给好评获取京豆脚本

1.打开链接 开下面的链接进入待评价商品页面 https://club.jd.com/myJdcomments/myJdcomments.action?sort=0 2.登陆后执行脚本 登陆后,按F12键,选择console,复制粘贴以下代码,先运行脚本1,再运行脚本2 脚本1代码 可以自行修改评价内容。 var content = '材质很好,质量也不错,到货也很快物流满分,包装快递满

Linux中拷贝 cp命令中拷贝所有的写法详解

This text from: http://www.jb51.net/article/101641.htm 一、预备  cp就是拷贝,最简单的使用方式就是: cp oldfile newfile 但这样只能拷贝文件,不能拷贝目录,所以通常用: cp -r old/ new/ 那就会把old目录整个拷贝到new目录下。注意,不是把old目录里面的文件拷贝到new目录,

ASP.Net.WebAPI和工具PostMan

1.WebAPI概述 1.1 WebAPI WebAPI 是一种传统的方式,用于构建和暴露 RESTUI风格的Web服务。它提供了丰富的功能和灵活性,可以处理各种HTTP请求,并支持各种数据格式,如JSON、XML等。 WebAPI使用控制器(Controllers)和动作方法(ActionMethods)的概念、通过路由配置将请求映射到相应的方法上。 开发人员可以使用各种属性和过滤器来处

Java的简易编译命令

生成jar包 编译生成.class 文件 编译.class文件的命令,其中的参数是输出,原文件路径 javac -sourcepath class.class MyClass.java 如果有包名的需要创建对应包的文件夹,建文件移动对应的包名下在开始包下创建meta-inf文件夹在meta-inf文件下创建manifest.mf文件 生成可运行的jar包 解压 生成了jar包后,进

Spring 内部类获取不到@Value配置值问题排查(附Spring代理方式)

目录 一、实例问题 1、现象 2、原因 3、解决 二、Spring的代理模式 1、静态代理(Static Proxy) 1)原理 2)优缺点 3)代码实现 2、JDK动态代理(JDK Dynamic Proxy) 1)原理 2)优缺点 3)代码实现 3、cglib 代理(Code Generation Library Proxy) 1)原理 2)优缺点 3)代码实

【ASP.NET】 No 'Access-Control-Allow-Origin' header is present on the requested resource.

前端JS用XMLHttpRequest,请求后端数据。出现了No ‘Access-Control-Allow-Origin’ header is present on the requested resource. 我是使用的ASP.NET框架。 解决办法: 在Web.config文件相应地方添加: <?xml version="1.0" encoding="utf-8"?><confi

获取Windows系统版本号(转)

https://blog.csdn.net/sunflover454/article/details/51525179

ApplicationContext 获取的三种方法

spring为ApplicationContext提供的3种实现分别 为:ClassPathXmlApplicationContext,FileSystemXmlApplicationContext和 XmlWebApplicationContext,其中XmlWebApplicationContext是专为Web工程定制的。使用举例如下:    1. FileSystemXmlApplicati