获取 AUTOCAD执行的上一个命令的名称
时间: 2024-03-08 17:47:31 浏览: 38
在AutoCAD中,可以使用`CommandEnded`事件来获取执行的上一个命令的名称。具体实现步骤如下:
1. 创建一个继承自`Autodesk.AutoCAD.Runtime.IExtensionApplication`的类,并实现`Initialize`方法和`Terminate`方法。在`Initialize`方法中,订阅`CommandEnded`事件;在`Terminate`方法中,取消订阅事件。
```csharp
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.EditorInput;
public class MyCommands : IExtensionApplication
{
public void Initialize()
{
// 订阅CommandEnded事件
Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.DocumentActivated += DocumentManager_DocumentActivated;
}
public void Terminate()
{
// 取消订阅CommandEnded事件
Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.DocumentActivated -= DocumentManager_DocumentActivated;
}
private void DocumentManager_DocumentActivated(object sender, DocumentCollectionEventArgs e)
{
e.Document.CommandEnded += Document_CommandEnded;
}
private void Document_CommandEnded(object sender, CommandEventArgs e)
{
// 获取执行的上一个命令的名称
string commandName = e.GlobalCommandName;
// 在控制台输出执行的命令名称
Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n上一个命令名称: {commandName}");
}
}
```
2. 在`MyCommands`类中注册命令,将其加入AutoCAD命令列表。
```csharp
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(MyCommands))]
public class MyCommands
{
[CommandMethod("MyCommand")]
public void MyCommand()
{
// 执行自定义命令
}
// ...
}
```
通过以上步骤,就可以在控制台输出执行的上一个命令的名称了。
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)