CAD二次开发-基础流程

1. 创建.NET Framework类库项目

①打开vs,点击创建新项目
在这里插入图片描述
②筛选windows-库,选择类库(.NET Framework)点击下一步
在这里插入图片描述
③填写项目名称,选择项目框架,点击创建。
在这里插入图片描述

2. 引用CAD dll 库文件

①在项目的引用处右键,点击管理NuGet程序包
在这里插入图片描述
②在浏览选项卡搜索CAD2018,选择AutoCAD2018NET库进行安装。(这里以CAD2018举例,其他版本可以引用CAD安装目录下对应的同名dll)
在这里插入图片描述

3. 编写模板代码

模板代码如下:

1
2
3
4
5
[CommandMethod("test")]
public void TestCommand()
{
//在此输入业务逻辑代码
}

以下示例业务代码为创建一条线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//在此输入业务逻辑代码
//创建两条线
Line line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0));
Line line2 = new Line(new Point3d(0, 0, 0), new Point3d(-1, 1, 0));
//打开CAD块表记录
using (IDisposable db = HostApplicationServices.WorkingDatabase, trans = (db as Database).TransactionManager.StartTransaction(), blockTable = (trans as Transaction).GetObject((db as Database).BlockTableId, OpenMode.ForRead), blockTableRecord = (trans as Transaction).GetObject((blockTable as BlockTable)[BlockTableRecord.ModelSpace], OpenMode.ForWrite))
{
//添加实体
(blockTableRecord as BlockTableRecord).AppendEntity(line);
(blockTableRecord as BlockTableRecord).AppendEntity(line2);
(trans as Transaction).AddNewlyCreatedDBObject(line, true);
(trans as Transaction).AddNewlyCreatedDBObject(line2, true);
//提交实体
(trans as Transaction).Commit();
}

4. 生成dll载入CAD进行调用

①在项目右键,点击生成。即可生成dll库文件。
在这里插入图片描述
②打开CAD,新建一张空白CAD图纸。在控制台输入NETLOAD命令按回车进行dll程序加载。
在这里插入图片描述
②在弹出的选择窗口找到我们刚才生成的dll文件,点击打开进行加载。(dll文件在项目文件夹下的bin/Debug文件夹中)
在这里插入图片描述
③在弹出的安全提示窗口点击“始终加载”。
在这里插入图片描述

④在控制台输入“test”命令按回车即可执行我们自定义的程序命令,生成两条线。
在这里插入图片描述
⑤生成效果入下:
在这里插入图片描述

5. 完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;

namespace CADDEMO
{
public class Class1
{
[CommandMethod("test")]
public void TestCommand()
{
//在此输入业务逻辑代码
//创建两条线
Line line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0));
Line line2 = new Line(new Point3d(0, 0, 0), new Point3d(-1, 1, 0));
//打开CAD块表记录
using (IDisposable db = HostApplicationServices.WorkingDatabase, trans = (db as Database).TransactionManager.StartTransaction(), blockTable = (trans as Transaction).GetObject((db as Database).BlockTableId, OpenMode.ForRead), blockTableRecord = (trans as Transaction).GetObject((blockTable as BlockTable)[BlockTableRecord.ModelSpace], OpenMode.ForWrite))
{
//添加实体
(blockTableRecord as BlockTableRecord).AppendEntity(line);
(blockTableRecord as BlockTableRecord).AppendEntity(line2);
(trans as Transaction).AddNewlyCreatedDBObject(line, true);
(trans as Transaction).AddNewlyCreatedDBObject(line2, true);
//提交实体
(trans as Transaction).Commit();
}
}
}
}