当前位置: 首页 > news >正文

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 旋转图形对象运行结果

测量结果:

http://www.dtcms.com/a/568667.html

相关文章:

  • 全屏响应式网站模板网站seo综合公司
  • php做简单网站教程视频教程企业门户网站模板 下载
  • Rust开发实战之WebSocket通信实现(tokio-tungstenite)
  • 编译缓存利器 ccahce、sccahce
  • Rust开发实战之使用 Reqwest 实现 HTTP 客户端请求
  • 各大公司开源网站广州出台21条措施扶持餐饮住宿
  • gmt_create为啥叫gmt
  • 从 NGINX 到 Kubernetes Ingress:现代微服务流量管理实战
  • 【C++】继承(2):继承与友元,静态成员,多继承黑/白盒复用
  • css实战:常用伪元素选择器介绍
  • 4.4 路由算法与路由协议【2013统考真题】
  • 营销型网站建设需要备案吗上饶网站建设企业
  • 福建网站建设科技有限公司品牌建设还需持续力
  • 工业CMOS相机的原理及基础知识
  • 无人机电气隔离与抗干扰技术概述
  • Elasticsearch的学习
  • GitHub 热榜项目 - 日榜(2025-11-04)
  • SAP 概述
  • 深圳家具网站建设做网站需要会写代码6
  • 常见的网站文件后缀名
  • 18、docker-macvlan-2-示例
  • ICCV2025 | GLEAM:通过全局-局部变换增强的面向视觉-语言预训练模型的可迁移对抗性攻击
  • Visual Studio 编程工程设置
  • 自我系统更新
  • 【数据结构】双向链表的实现
  • 《Linux系统编程之开发工具》【版本控制器 + 调试器】
  • C++ :C宏函数的升级:内联函数inline
  • 青海网站建设费用织梦后台怎么建设网站
  • [特殊字符] Gudu SQL Omni 在数据治理体系中的落地实践指南
  • arm寄存器虚拟化分析