基于.NET的CAD二次开发
环境搭建
安装AUTOCAD2021,在VS2019里新建类库。
项目创建好后,在依赖项右键选择添加引用,引用类库文件的位置在cad的软件目录下,分别是以下文件
acdbmgd.dll
acmgd.dll
accoremgd.dll
创建工程 .net framework 4.7.2 C#类库工程
入门程序
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XionganyanBim
{
public class Class1
{
[CommandMethod("TestDemo")]
public void TestDemo()
{
// 声明命令行对象
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
// 向命令行输出一段文字
ed.WriteMessage("我是来学习autocad二次开发的");
System.Windows.Forms.MessageBox.Show("欢迎使用雄安创新研究院BIM插件");
}
}
}
项目右键--属性--启动外部程序--选择acad.exe
,生成后就可以开始调试了,开始调试后会启动CAD
进入CAD后,输入
NETLOAD
没有报错说明记载成功
之后可以输入
testdemo
就可以看到结果了
创建图形
public class Class1
{
[CommandMethod("LineDemo")]
public void LineDemo()
{
// 声明一个直线对象
Line line1 = new Line();
Point3d startPoint = new Point3d(100, 100, 0);
Point3d endPoint = new Point3d(200, 200, 0);
// 设置属性
line1.StartPoint = startPoint;
line1.EndPoint = endPoint;
// 声明图形数据库对象
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//加直线到块表记录中
btr.AppendEntity(line1);
//更新数据
trans.AddNewlyCreatedDBObject(line1, true);
//事务提交
trans.Commit();
}
}
}
创建图形工具类
public static class AddEntityTool
{
/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="ent">图形对象</param>
/// <returns>图形的ObjectId</returns>
public static ObjectId addEntityToModelSpace(this Database db, Entity ent)
{
ObjectId entId = ObjectId.Null;
//开启事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//open block table
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//open block table record
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="ent">图形对象, 可变参数</param>
/// <returns>图形的ObjectId,数组返回</returns>
public static ObjectId[] addEntityToModelSpace(this Database db, params Entity[] ent)
{
ObjectId[] entId = new ObjectId[ent.Length];
//开启事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//open block table
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//open block table record
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
for (int i = 0; i < ent.Length; i++)
{
entId[i] = btr.AppendEntity(ent[i]);
trans.AddNewlyCreatedDBObject(ent[i], true);
}
trans.Commit();
}
return entId;
}
/// <summary>
/// 绘制直线
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="p1">起点坐标</param>
/// <param name="p2">终点坐标</param>
/// <returns></returns>
public static ObjectId addLineToModelSpace(this Database db, Point3d p1, Point3d p2)
{
return db.addEntityToModelSpace(new Line(p1, p2));
}
/// <summary>
/// 绘制直线
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="startPoint">起点坐标</param>
/// <param name="length">直线长度</param>
/// <param name="degree">与轴正方向的弧度</param>
/// <returns>ObjectId</returns>
public static ObjectId addLineToModelSpace(this Database db, Point3d startPoint, double length, double degree)
{
double x = startPoint.X + length * Math.Cos(degree.degreeToAngle());
double y = startPoint.Y + length * Math.Sin(degree.angleToDegree());
Point3d endPoint = new Point3d(x, y, 0);
return db.addEntityToModelSpace(new Line(startPoint, endPoint));
}
}
调用方法
public class Class1
{
[CommandMethod("Demo")]
public void Demo()
{
Database db = HostApplicationServices.WorkingDatabase;
Line line1 = new Line(new Point3d(100, 100, 0), new Point3d(200, 100, 0));
db.addEntityToModelSpace(line1);
Line line2 = new Line(new Point3d(200, 100, 0), new Point3d(200, 200, 0));
Line line3 = new Line(new Point3d(200, 200, 0), new Point3d(100, 100, 0));
Line line4 = new Line(new Point3d(100, 100, 0