Auto CAD二次开发——复制和旋转图形对象
学如逆水行舟,不进则退。 愿自己不忘初心,坚持积累。感谢大家的支持。本篇博客主要是学习复制和旋转图形对象来实现对图形对象的属性操作。
目录
1、基础知识储备
2、核心代码以及封装函数调用
2.1 复制图形对象函数封装
2.2 复制图形对象函数调用
2.3 复制图形对象运行结果
2.4 旋转图形对象函数封装
2.5 旋转图形对象函数调用
2.6 旋转图形对象运行结果
1、基础知识储备
1.1 回顾一下上一篇博客内容,主要是移动图形对象,这里我对其进行总结一下:
指定参考原点,指定参考目标点(Point3d sourcePoint, Point3d targetPoint);
获取参考点原点到参考目标点的向量(Vector3d vector = sourcePoint.GetVectorTo(targetPoint););
计算移动矩阵(Matrix3d mt = Matrix3d.Displacement(vector););
调用图形的方法移动(ent.TransformBy(mt);)。
1.2复制图形对象
指定参考原点,指定参考目标点(Point3d sourcePoint, Point3d targetPoint);
获取参考点原点到参考目标点的向量(Vector3d vector = sourcePoint.GetVectorTo(targetPoint););
计算移动矩阵(Matrix3d mt = Matrix3d.Displacement(vector););
调用图形的方法复制(entR = ent.GetTransformedCopy(mt);)。注意与移动对象和旋转对象不同,GetTransformedCopy会返回一个(Entity entR;)的图形对象,同时与移动对象和旋转对象不同的是无论是否为新的实体对象都需要重新添加事务对象(AddEntityToModelSpace(entR)自定义创建实体对象函数)
1.3旋转对象
指定旋转中心(Point3d center)和旋转角度(double degree);
计算旋转矩阵(Matrix3d mt = Matrix3d.Rotation(degree.DegreeToAngle(), Vector3d.ZAxis, center);)
调用图形的方法旋转(ent.TransformBy(mt);)这里与移动图形对象相同,与复制图形对象不同。
2、核心代码以及封装函数调用
2.1 复制图形对象函数封装
/// <summary>/// 复制图形/// </summary>/// <param name="entId">图形的半ObjectId</param>/// <param name="sourcePoint">参考起点</param>/// <param name="targetPoint">参考终点</param>/// <returns>图形对象,没有加入图形数据库</returns>public static Entity CopyEntity(this ObjectId entId, Point3d sourcePoint, Point3d targetPoint){//声明一个图形对象Entity entR;//当前图形数据库Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){//打开块表BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//打开块表记录BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);//打开图形//Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);//计算变换矩阵Vector3d vector = sourcePoint.GetVectorTo(targetPoint);Matrix3d mt = Matrix3d.Displacement(vector);entR = ent.GetTransformedCopy(mt);//提交事务处理trans.Commit();}return entR;}/// <summary>/// 复制图形/// </summary>/// <param name="entId">图形对象ram>/// <param name="sourcePoint">参考起点</param>/// <param name="targetPoint">参考终点</param>/// <returns>图形对象,没有加入图形数据库</returns>public static Entity CopyEntity(this Entity ent, Point3d sourcePoint, Point3d targetPoint){//声明一个图形对象Entity entR;//判断图形对象的IsNewlyObejct属性if (ent.IsNewObject){//计算变换矩阵Vector3d vector = sourcePoint.GetVectorTo(targetPoint);Matrix3d mt = Matrix3d.Displacement(vector);entR = ent.GetTransformedCopy(mt);}else{entR = ent.ObjectId.CopyEntity(sourcePoint, targetPoint);}return entR;}
2.2 复制图形对象函数调用
//复制图形[CommandMethod("CopyEntity")]public void CopyDemo(){//当前图形数据库Database db = HostApplicationServices.WorkingDatabase;Circle c1 = new Circle(new Point3d(100, 100, 0), Vector3d.ZAxis, 100);Circle c2 = (Circle)c1.CopyEntity(new Point3d(100, 100, 0), new Point3d(100, 200, 0));db.AddEntityToModelSpace(c1, c2);Circle c3 = (Circle)c1.CopyEntity(new Point3d(0, 0, 0), new Point3d(-100, 0, 0));db.AddEntityToModelSpace(c3);}
2.3 复制图形对象运行结果

2.4 旋转图形对象函数封装
/// <summary>/// 旋转图形/// </summary>/// <param name="ent">图形对象的ObjectId</param>/// <param name="center">旋转中点</param>/// <param name="degree">旋转的角度</param>public static void RotateEntity(this ObjectId entId, Point3d center, double degree){//当前图形数据库Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){//打开块表BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//打开块表记录BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);//打开图形//Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);//计算变换矩阵Matrix3d mt = Matrix3d.Rotation(degree.DegreeToAngle(), Vector3d.ZAxis, center);ent.TransformBy(mt);//提交事务处理trans.Commit();}}/// <summary>/// 旋转图形/// </summary>/// <param name="ent">图形对象</param>/// <param name="center">旋转中点</param>/// <param name="degree">旋转的角度</param>public static void RotateEntity(this Entity ent, Point3d center, double degree){//判断图形对象的IsNewlyObejct属性if (ent.IsNewObject){//计算变换矩阵Matrix3d mt = Matrix3d.Rotation(degree.DegreeToAngle(), Vector3d.ZAxis, center);ent.TransformBy(mt);}else{ent.ObjectId.RotateEntity(center, degree);}}
2.5 旋转图形对象函数调用
//旋转图形[CommandMethod("RotateEntity")]public void RotateDemo(){//当前图形数据库Database db = HostApplicationServices.WorkingDatabase;Line l1 = new Line(new Point3d(100, 100, 0), new Point3d(300, 100, 0));Line l2 = new Line(new Point3d(100, 100, 0), new Point3d(300, 100, 0));Line l3 = new Line(new Point3d(100, 100, 0), new Point3d(300, 100, 0));l1.RotateEntity(new Point3d(100, 100, 0), 30);db.AddEntityToModelSpace(l1, l2, l3);l2.RotateEntity(new Point3d(0, 0, 0), 60);l3.RotateEntity(new Point3d(200, 500, 0), 90);}
2.6 旋转图形对象运行结果

测量结果:

