1. 准备工作
创建一个类库项目,如下:
2. 分析Line对象
Line类的初始化方法和参数
using Autodesk.AutoCAD.DatabaseServices;
Line line = new Line();
Line 继承Curve 继承Entity 继承DBObject 继承Drawable 继承RXObject
初始化方法有两个:
3. 启动项目
我们创建一个line对象,然后打上断点,生成.dll文件,加载到CAD中,然后执行命令。
我们就会发现,自动跳到了VS界面,代码走到了断点处,代码继续往下走,我们就会看到:
透过参数,我们就会发现默认Line创建了一个起点为(0,0),终点为(0,0)的直线。
4. 如何将线条写到CAD中
4.1 数据交互原理
首先我们要明白一个事情,就是我们在代码中创建的线条目前是写到内存当中,而CAD的图纸数据是以自己的某种方式,比如文件数据库等等,存储在磁盘中,所以我们就得需要把内存中的数据想办法按照CAD的API调用写到CAD的文件数据库中。
如上图的右边,就是CAD的文件数据存储的过程:Database(数据库)-》BlockTable(块表)-》BlockTableRecord(块表记录)-》Entity(实体数据)。
4.2 代码实现
所以代码如下:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LineTest01
{
public class LineTest
{
[CommandMethod("LineDemo")]
public void LineDemo() {
//创建线条对象
Line line = new Line();
//创建坐标对象
Point3d start = new Point3d(200, 200, 200);
Point3d end = new Point3d(430, 400, 400);
//设置属性
line.StartPoint = start;
line.EndPoint = end;
//声明图形数据库对象
Document doc = Application.DocumentManager.MdiActiveDocument;
Database database = doc.Database;
//开启事务处理
using (Transaction trans = database.TransactionManager.StartTransaction()) {
//打开块表
BlockTable blockTable = (BlockTable)trans.GetObject(database.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord blockTableRecord = (BlockTableRecord)trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//加直线到块表记录
blockTableRecord.AppendEntity(line);
//更新数据
trans.AddNewlyCreatedDBObject(line, true);
//事务提交
trans.Commit();
}
}
}
}
然后我们启动服务,加载CAD程序。
4.3 CAD加载.dll程序
输入:NETLOAD
选择.dll
文件。
输入:LineDemo
就可以看到我们已经成功地将线条对象写入到CAD中了。
5. 封装工具类
从上面可以看到,其实我们可以将一些常用的方法,封装成公共方法,提高代码的复用性,这里我为大家封装了两个类,分别如下:
BaseTool.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LineTest01
{
internal static class BaseTool
{
/// <summary>
/// 角度转成弧度
/// </summary>
/// <param name="degree">角度参数</param>
/// <returns>弧度</returns>
public static Double DegreeToAngle(this Double degree)
{
return degree * Math.PI / 180;
}
/// <summary>
/// 弧度转成角度
/// </summary>
/// <param name="degree">弧度参数</param>
/// <returns>角度</returns>
public static Double AngleToDegree(this Double angle)
{
return angle * 180 / Math.PI;
}
}
}
AddEntityTool.cs
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LineTest01
{
//internal 表示只有在同一程序集的文件中才能访问
internal static class AddEntityTool
{
/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="entity">图形对象</param>
/// <returns></returns>
public static ObjectId AddEntityToModelSpace(this Database db, Entity entity) {
//声明ObjectId,用于返回
ObjectId objectId = ObjectId.Null;
//开启事务
using (Transaction trans = db.TransactionManager.StartTransaction()) {
//打开块表
BlockTable blockTable = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//打开块表记录
BlockTableRecord blockTableRecord= (BlockTableRecord)trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//添加图形到块表记录
objectId = blockTableRecord.AppendEntity(entity);
//更新数据信息
trans.AddNewlyCreatedDBObject(entity, true);
//提交事务
trans.Commit();
}
return objectId;
}
/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="entity">图形对象 可变参数</param>
/// <returns>ObjectId</returns>
public static ObjectId[] AddEntityToModelSpace(this Database db, params Entity[] entities)
{
//声明ObjectId,用于返回
ObjectId[] objectId = new ObjectId[entities.Length];
//开启事务
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//打开块表
BlockTable blockTable = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//打开块表记录
BlockTableRecord blockTableRecord = (BlockTableRecord)trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
for (int i = 0; i < entities.Length; i++)
{
//添加图形到块表记录
objectId[i] = blockTableRecord.AppendEntity(entities[i]);
//更新数据信息
trans.AddNewlyCreatedDBObject(entities[i], true);
}
//提交事务
trans.Commit();
}
return objectId;
}
/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="startPoint">起点坐标</param>
/// <param name="endPoint">终点坐标</param>
/// <returns>ObjectId</returns>
public static ObjectId AddEntityToModelSpace(this Database db, Point3d startPoint,Point3d endPoint)
{
return db.AddEntityToModelSpace(new Line(startPoint, endPoint));
}
/// <summary>
/// 将图形对象添加到图形文件中
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="startPoint">起点坐标</param>
/// <param name="length">直线长度</param>
/// <param name="degree">与X轴正方向的夹角</param>
/// <returns>ObjectId</returns>
public static ObjectId AddEntityToModelSpace(this Database db, Point3d startPoint, Double length,Double degree)
{
//计算终点坐标
double X = startPoint.X + length * Math.Cos(degree.DegreeToAngle());
double Y = startPoint.X + length * Math.Sin(degree.DegreeToAngle());
Point3d endPoint = new Point3d(X, Y, 0);
return db.AddEntityToModelSpace(new Line(startPoint, endPoint));
}
}
}
封装类的使用
[CommandMethod("LineAdd")]
public void LineAdd()
{
Database workingDatabase = HostApplicationServices.WorkingDatabase;
Line line = new Line(new Point3d(100, 100, 0), new Point3d(200, 100, 0));
workingDatabase.AddEntityToModelSpace(line);
Line line1 = new Line(new Point3d(200, 100, 0), new Point3d(200, 200, 0));
Line line2 = new Line(new Point3d(200, 100, 0), new Point3d(100, 100, 0));
workingDatabase.AddEntityToModelSpace(line1,line2);
}