AutoCAD二次开发 - 基于.Net

问题描述:

      在AutoCAD二次开发中, 使用CADCommand只能定义命令. 无法接受参数. 采用Lisp编程能接受额外的参数.

 

开发环境:

IDE: Visual Studio 2019

CAD: AutoCAD 2019版本

 

1. 打开VS, 新建C#项目,

类型选择,类库, 基于.NetFramework. (非Core / 非Standard)

 

 2. 添加程序集.

点击引用, 选择浏览, 打开AutoCAD安装目录,  选择accoremgd, acdbmgd, acmgd三个AutoCAD程序集, 这样就可以使用CAD的类了.

 

 

3. 编写代码.

假设我们的命令使用如下,注意Lisp命令需要括号.

cad命令提示行 :  (summark "type" ""layer1, layer2")

如下:

 1 using Autodesk.AutoCAD.DatabaseServices;
 2 using Autodesk.AutoCAD.Runtime;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace StubTool
10 {
11     /// <summary>
12     ///  启动AutoCAD, 调用命令
13     /// </summary>
14     public class Tool
15     {   // 这里需要添加LispFunction的Attribute.
16         [LispFunction("summark")]
17         public void doSummarkArgument(ResultBuffer args)
18         {
19             if (args == null)
20             {
21                 // 打印帮助手册.
22                 return;
23             }
24 
25             string szType = "";         // 参数1. 类型名称
26             string szLayerName = "";    // 参数2. 图层名称列表
27 
28             // 解析参数.
29             int nCnt = 0;
30             foreach (TypedValue a in args)
31             {
32                 if (a.TypeCode == (int)LispDataType.Text)
33                 {
34                     string szText = a.Value.ToString();
35                     switch (nCnt)
36                     {
37                         case 0:
38                             szType = szText;
39                         break;
40                         case 1:
41                             szLayerName = szText;
42                             break;
43                         default:
44                             break;
45                     }
46 
47                     nCnt++;
48                 }
49             }
50 
51             // TODO.
52         }
53     }
54 }

 

4. 编译与调试

由于我采用的AutoCAD是x64类型, 所以需要设置CPU类型为x64. 编译完成后, 设置调试命令, 指向acad.exe.

 

 

点击调试命令, 则会启动AutoCAD, 在命令提示符中, 输入netload, 加载我们的程序集(.dll)

 

 最终, 在命令提示符中输入创建的新的命令.

 

 

 

 

                ---------------------------- 勿在浮沙筑高台

posted @ 2022-01-14 18:22  勿在浮沙筑高台  阅读(687)  评论(1编辑  收藏  举报