AutoCAD C# 两不平行直线倒圆弧算法

news/发布时间2024/5/18 22:20:15

参考的博客:https://www.cnblogs.com/JJBox/p/14300098.html

下面是计算示例

主要计算代码:

   var peo = new PromptEntityOptions("选择直线1"){AllowNone = false,AllowObjectOnLockedLayer = false};peo.SetRejectMessage("请选择直线Line");peo.AddAllowedClass(typeof(Line), false);var per1 = AcEnv.CurEd.GetEntity(peo);peo.Message = "选择直线2";var per2 = AcEnv.CurEd.GetEntity(peo);var r = AcEnv.CurEd.GetDouble("输入圆弧半径:\n").Value;if (per1.Status == PromptStatus.OK && per2.Status == PromptStatus.OK){using (var tr = AcEnv.CurDb.TransactionManager.StartTransaction()){var l1 = (Line)tr.GetObject(per1.ObjectId, OpenMode.ForRead);var l2 = (Line)tr.GetObject(per2.ObjectId, OpenMode.ForRead);var pts = new Point3dCollection();l1.IntersectWith(l2, Intersect.ExtendBoth, l1.GetPlane(), pts, IntPtr.Zero, IntPtr.Zero);if (pts.Count == 0){AcApp.Application.ShowAlertDialog("2直线无交点无法到圆角!");tr.Abort();return;}var p0 = pts[0];var p02d = p0.Convert2d(l1.GetPlane());var voa = p0.GetVectorTo(l1.EndPoint.GetMidPoint(l1.StartPoint));var vob = p0.GetVectorTo(l2.EndPoint.GetMidPoint(l2.StartPoint));var angle = voa.GetAngleTo(vob) * 0.5;var doc = r / Math.Sin(angle);var pa = p0.Add(voa.GetNormal() * (double)(r / Math.Tan(angle)));var pb = p0.Add(vob.GetNormal() * (double)(r / Math.Tan(angle)));var isCw = Tools.CrossAclockwise(p02d, pa.Convert2d(l1.GetPlane()), pb.Convert2d(l1.GetPlane()));var pc = pa.Add((isCw ? voa : voa.Negate()).GetPerpendicularVector().GetNormal() * r);var startAngle = pc.GetVectorTo(pb).Convert2d(l1.GetPlane()).GetAngle2XAxis();var endAngle = pc.GetVectorTo(pa).Convert2d(l1.GetPlane()).GetAngle2XAxis();Arc a = new Arc(pc, Vector3d.ZAxis, r, isCw ? startAngle : endAngle, isCw ? endAngle : startAngle);//AcEnv.CurDb.AppendEntities(new DBPoint(p0), new DBPoint(pa), new DBPoint(pb), new DBPoint(pc));AcEnv.CurDb.AppendEntities(a);tr.Commit();}}

用到的拓展函数

    public static Point3d GetMidPoint(this Point3d p1, Point3d p2){return new Point3d((p1.X + p2.X) * 0.5, (p1.Y + p2.Y) * 0.5, (p1.Z + p2.Z) * 0.5);}/// <summary>/// 叉积,二维叉乘计算/// </summary>/// <param name="a">传参是向量,表示原点是0,0</param>/// <param name="b">传参是向量,表示原点是0,0</param>/// <returns>其模为a与b构成的平行四边形面积</returns>public static double Cross(Vector2d a, Vector2d b){return a.X * b.Y - a.Y * b.X;}/// <summary>/// 叉积,二维叉乘计算 /// </summary>/// <param name="o">原点</param>/// <param name="a">oa向量</param>/// <param name="b">ob向量,此为判断点</param>/// <returns>返回值有正负,表示绕原点四象限的位置变换,也就是有向面积</returns>public static double Cross(Point2d o, Point2d a, Point2d b){return Cross(o.GetVectorTo(a), o.GetVectorTo(b));}/// <summary>/// 叉积,逆时针方向为真/// </summary>/// <param name="o">直线点1</param>/// <param name="a">直线点2</param>/// <param name="b">判断点</param>/// <returns>b点在oa的逆时针<see cref="true"/></returns>public static bool CrossAclockwise(Point2d o, Point2d a, Point2d b){return Cross(o, a, b) > -1e-6;//浮点数容差考虑}/// <summary>/// X轴到向量的弧度,cad的获取的弧度是1PI,所以转换为2PI(上小,下大)/// </summary>/// <param name="ve">向量</param>/// <returns>弧度</returns>public static double GetAngle2XAxis(this Vector2d ve){double alz = Vector2d.XAxis.GetAngleTo(ve);//观察方向不要设置 alz = ve.Y > 0 ? alz : Math.PI * 2 - alz; //逆时针为正,如果-负值控制正反alz = Math.Abs(Math.PI * 2 - alz) < 1e-10 ? 0 : alz;return alz;}public static ObjectIdCollection AppendEntities(this Database acdb, params Entity[] ents){ObjectIdCollection oids = [];using (var tr = acdb.TransactionManager.StartTransaction()){var btr = (BlockTableRecord)acdb.CurrentSpaceId.GetObject(OpenMode.ForWrite);foreach (var item in ents){if (!item.IsNewObject) continue;oids.Add(btr.AppendEntity(item));tr.AddNewlyCreatedDBObject(item, true);}tr.Commit();}return oids;}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ulsteruni.cn/article/47335783.html

如若内容造成侵权/违法违规/事实不符,请联系编程大学网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

2. 基础配置

1. 配置文件格式 1.1 配置文件自动提示功能消失解决方案 ​​ 1.2 SpringBoot配置文件加载顺序(了解) application.properties > application.yml > application.yaml 1.3 注意事项 SpringBoot核心配置文件名为application SpringBoot内置属性过多,且所有属性集中…

搭建自己的博客

基于github和Hexo 搭建自己的博客 【摘要】该教程基于个人的虚拟机和个人的GitHub,过程会详细注明对应的安装包的版本。 1、搭建hexo环境 环境配置 本地虚拟机:ubuntu 20.4(也可以基于对应的服务器) Hexo搭建步骤 1. 安装nodejs和npm 由于Ubuntu20通过apt安装nodejs默认只能…

06.数组

1.数组概述 数组是相同类型数据的有序集合; 数组描述的是相同类型的若干各数据,按照一定的先后次序排列组合而成; 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们。 2.数组声明创建 首先必须声明数组变量,才能在程序中使用数组: dataType[] a…

.Net 8.0 下的新RPC,IceRPC之如何创建连接connection

作者引言很高兴啊,我们来到了IceRPC之如何创建连接connection,基础引导,让自已不在迷茫,快乐的畅游世界。如何创建连接connection学习如何使用IceRPC,创建和接受连接。连接有什么用途? 连接在 IceRPC 中发挥着核心作用: 通过连接向服务端发送请求,然后通过同一连接收到响应…

数据分析的五大流程:需求、获取、处理、分析、可视化

数据分析的五大流程:需求、获取、处理、分析、可视化

Linux网络-ssh远程连接协议

SSH基础 1.ssh协议概述SSH(Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录、远程 复制等功能; SSH 协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令; SSH 为建立在应用层和传输层基础上的安全协议。对数据进行压缩,加快传输…