From b791eaaa8381c5190d1665a58e86a2c8520a7660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E5=82=B2=E5=B4=96?= <702099480@qq.com> Date: Wed, 2 Aug 2023 14:42:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=A4=E7=82=B9?= =?UTF-8?q?=E9=97=B4=E8=B7=9D=E7=A6=BB=E4=BB=A5=E5=8F=8A=E4=B8=A4=E7=82=B9?= =?UTF-8?q?=E9=97=B4=E8=B7=9D=E7=A6=BB=E5=AE=9A=E6=95=B0=E7=AD=89=E5=88=86?= =?UTF-8?q?/=E5=AE=9A=E8=B7=9D=E7=AD=89=E5=88=86=E7=AD=89=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExtensionMethod/PointEx.cs | 177 +++++++++++++++++- 1 file changed, 171 insertions(+), 6 deletions(-) diff --git a/src/CAD/IFox.CAD.Shared/ExtensionMethod/PointEx.cs b/src/CAD/IFox.CAD.Shared/ExtensionMethod/PointEx.cs index 76220f9..a1a8ac9 100644 --- a/src/CAD/IFox.CAD.Shared/ExtensionMethod/PointEx.cs +++ b/src/CAD/IFox.CAD.Shared/ExtensionMethod/PointEx.cs @@ -181,15 +181,75 @@ public static double GetArcBulge(this Point2d arc1, Point2d arc2, Point2d arc3, bulge = -1; break; default: - { - if (Math.Abs(bulge) < tol) - bulge = 0; - break; - } + { + if (Math.Abs(bulge) < tol) + bulge = 0; + break; + } } return bulge; } + #region 两点间距离(定数等分/定距等分) + /// + /// 获取两点之间的距离 + /// + /// 第一点 + /// 第二点 + /// 两点之间的距离 + public static double GetDistance(this Point3d point1, Point3d point2) + { + return (Math.Sqrt(Math.Pow((point1.X - point2.X), 2) + Math.Pow((point1.Y - point2.Y), 2) + Math.Pow((point1.Z - point2.Z), 2))); + } + + /// + /// 两点距离定数等分 + /// + /// 第一个点 + /// 第二个点 + /// 等分数量;大于1 + /// 等分距离和所有等分点的集合 + public static DisPts? GetNumDivide(this Point3d point1, Point3d point2, int divnum) + { + if (point1 == point2 || divnum < 2) return null; + var dispts = new DisPts(DivideType.Number, point1.GetDistance(point2) / divnum); + dispts.Pts.Add(point1); + dispts.Radian = point1.GetAngle(point2); + for (int i = 1; i < divnum; i++) + { + dispts.Pts.Add(point1.Polar(dispts.Radian, dispts.Dis * i)); + } + dispts.Pts.Add(point2); + return dispts; + } + + /// + /// 两点距离定距等分 + /// + /// 第一个点 + /// 第二个点 + /// 等分距离;大于0,且小于两点之间的距离 + /// 等分距离和所有等分点的集合 + public static DisPts? GetDisDivide(this Point3d point1, Point3d point2, double divdis) + { + var length = point1.GetDistance(point2); + if (point1 == point2 || divdis <= 0 || divdis >= length) return null; + var dispts = new DisPts(DivideType.Distance, divdis); + dispts.Pts.Add(point1); + dispts.Radian = point1.GetAngle(point2); + var tempDis = divdis; + var tempPoint = point1.Polar(dispts.Radian, tempDis); + while (point1.GetDistance(tempPoint) < length) + { + dispts.Pts.Add(tempPoint); + tempDis += divdis; + tempPoint = point1.Polar(dispts.Radian, tempDis); + } + dispts.Pts.Add(point2); + dispts.LastDis = length - tempDis; + return dispts; + } + #endregion 两点间距离(定数等分/定距等分) #region 首尾相连 /// @@ -233,4 +293,109 @@ public static void End2End(this Point3dCollection ptcol) ptcol.Add(lst[i]); } #endregion -} \ No newline at end of file +} + +#region 两点间距离等分所用对象和枚举 +/// +/// 两点间距离等分信息类 +/// +public class DisPts +{ + #region 私有字段 + /// + /// 弧度 + /// + private double radian = double.NaN; + /// + /// 角度 + /// + private double angle = double.NaN; + #endregion 私有字段 + + #region 构造函数 + /// + /// 一参构造函数 + /// + /// 等分长度 + public DisPts(double dis) => LastDis = Dis = dis; + /// + /// 两参构造函数 + /// + /// 等分类型 + /// 等分长度 + public DisPts(DivideType type, double dis) + { + Type = type; + LastDis = Dis = dis; + } + #endregion 构造函数 + + #region 公共属性 + /// + /// 等分类型,默认:无 + /// + public DivideType Type { get; set; } = DivideType.None; + + /// + /// 等分长度(定距等分不包含最后一段长度) + /// + public double Dis { get; set; } = 0; + + /// + /// 角度 + /// + public double Angle + { + get => angle; + set + { + angle = value; + radian = MathEx.ConvertDegToRad(value); + } + } + + /// + /// 弧度 + /// + public double Radian + { + get => radian; + set + { + radian = value; + angle = MathEx.ConvertRadToDeg(value); + } + } + + /// + /// 等分点点表(包含两个端点) + /// + public List Pts { get; set; } = new(); + + /// + /// 定距等分最后一段长度(定数等分最后一段长度与Dis一致) + ///
+ /// 默认:与Dis一致 + ///
+ public double LastDis { get; set; } = double.NaN; + #endregion 公共属性 +} +/// +/// 两点距离等分类型 +/// +public enum DivideType +{ + /// + /// 默认值:无 + /// + None, + /// + /// 定数等分 + /// + Number, + /// + /// 定距等分 + /// + Distance +} +#endregion 两点间距离等分所用对象和枚举 \ No newline at end of file -- Gitee From e3f3f8216b6a04a74db0dd172fed75ab1f0569fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E5=82=B2=E5=B4=96?= <702099480@qq.com> Date: Fri, 4 Aug 2023 14:36:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=8F=90=E7=A4=BA=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=A4=E7=82=B9=E5=AE=9A=E8=B7=9D=E7=AD=89=E5=88=86=E3=80=81?= =?UTF-8?q?=E7=82=B9=E9=9B=86=E8=BD=AC=E7=9B=B4=E7=BA=BF=E7=AB=AF=E7=82=B9?= =?UTF-8?q?=E9=9B=86=E5=90=88=E7=AD=89=E6=96=B9=E6=B3=95=E5=92=8C=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E7=B1=BB=E5=9E=8B=E4=BB=A5=E5=8F=8A=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IFox.CAD.Shared/ExtensionMethod/Enums.cs | 23 +- .../ExtensionMethod/ErrorInfoEx.cs | 19 +- .../ExtensionMethod/PointEx.cs | 236 ++++++++---------- .../ExtensionMethod/ReferenceTypes.cs | 142 +++++++++++ .../IFox.CAD.Shared/IFox.CAD.Shared.projitems | 1 + 5 files changed, 281 insertions(+), 140 deletions(-) create mode 100644 src/CAD/IFox.CAD.Shared/ExtensionMethod/ReferenceTypes.cs diff --git a/src/CAD/IFox.CAD.Shared/ExtensionMethod/Enums.cs b/src/CAD/IFox.CAD.Shared/ExtensionMethod/Enums.cs index 5043cad..3670dcb 100644 --- a/src/CAD/IFox.CAD.Shared/ExtensionMethod/Enums.cs +++ b/src/CAD/IFox.CAD.Shared/ExtensionMethod/Enums.cs @@ -172,4 +172,25 @@ public enum FontTTF Arial, [Description("Romans")] Romans -} \ No newline at end of file +} + +#region 两点间距离等分所用枚举 +/// +/// 两点距离等分类型 +/// +public enum DivideType +{ + /// + /// 默认值:无 + /// + None, + /// + /// 定数等分 + /// + Number, + /// + /// 定距等分 + /// + Distance +} +#endregion 两点间距离等分所用枚举 \ No newline at end of file diff --git a/src/CAD/IFox.CAD.Shared/ExtensionMethod/ErrorInfoEx.cs b/src/CAD/IFox.CAD.Shared/ExtensionMethod/ErrorInfoEx.cs index 00da86e..b57399a 100644 --- a/src/CAD/IFox.CAD.Shared/ExtensionMethod/ErrorInfoEx.cs +++ b/src/CAD/IFox.CAD.Shared/ExtensionMethod/ErrorInfoEx.cs @@ -7,13 +7,18 @@ public static class ErrorInfoEx { /// /// 打印CAD错误信息到命令行 - /// 使用方法: - /// try - /// { - /// //你的代码 - /// } - /// catch (AcException acex) { acex.AcErrorInfo(); } - /// catch (Exception) { throw; } + /// + /// 如引发AcException引用错误,请设置如下与CAD对应的引用和别名 + /// global using AcException = Autodesk.AutoCAD.Runtime.Exception; + /// global using AcException = GrxCAD.Runtime.Exception; + /// global using AcException = ZwSoft.ZwCAD.Runtime.Exception; + /// 使用方法: + /// try + /// { + /// //你的代码 + /// } + /// catch (AcException acex) { acex.AcErrorInfo(); } + /// catch (Exception) { throw; } /// /// /// AcException diff --git a/src/CAD/IFox.CAD.Shared/ExtensionMethod/PointEx.cs b/src/CAD/IFox.CAD.Shared/ExtensionMethod/PointEx.cs index a1a8ac9..842db45 100644 --- a/src/CAD/IFox.CAD.Shared/ExtensionMethod/PointEx.cs +++ b/src/CAD/IFox.CAD.Shared/ExtensionMethod/PointEx.cs @@ -1,5 +1,8 @@ namespace IFoxCAD.Cad; +/// +/// Point 扩展方法类 +/// public static class PointEx { /// @@ -112,8 +115,6 @@ public static Point3d Point3d(this Point2d pt, double z = 0) return new(pt.X, pt.Y, z); } - - /// /// 根据世界坐标计算用户坐标 /// @@ -207,20 +208,20 @@ public static double GetDistance(this Point3d point1, Point3d point2) /// /// 第一个点 /// 第二个点 - /// 等分数量;大于1 + /// 等分数量;大于1 /// 等分距离和所有等分点的集合 - public static DisPts? GetNumDivide(this Point3d point1, Point3d point2, int divnum) + public static DivideInfo? GetNumberDivideTo(this Point3d point1, Point3d point2, int number) { - if (point1 == point2 || divnum < 2) return null; - var dispts = new DisPts(DivideType.Number, point1.GetDistance(point2) / divnum); - dispts.Pts.Add(point1); - dispts.Radian = point1.GetAngle(point2); - for (int i = 1; i < divnum; i++) + if (point1 == point2 || number < 2) return null; + var divideInfo = new DivideInfo(DivideType.Number, point1.GetDistance(point2) / number); + divideInfo.Points.Add(point1); + divideInfo.Radian = point1.GetAngle(point2); + for (int i = 1; i < number; i++) { - dispts.Pts.Add(point1.Polar(dispts.Radian, dispts.Dis * i)); + divideInfo.Points.Add(point1.Polar(divideInfo.Radian, divideInfo.Distance * i)); } - dispts.Pts.Add(point2); - return dispts; + divideInfo.Points.Add(point2); + return divideInfo; } /// @@ -228,29 +229,105 @@ public static double GetDistance(this Point3d point1, Point3d point2) /// /// 第一个点 /// 第二个点 - /// 等分距离;大于0,且小于两点之间的距离 + /// 等分距离;大于0,且小于两点之间的距离 /// 等分距离和所有等分点的集合 - public static DisPts? GetDisDivide(this Point3d point1, Point3d point2, double divdis) + public static DivideInfo? GetDistaceDivideTo(this Point3d point1, Point3d point2, double distance) { var length = point1.GetDistance(point2); - if (point1 == point2 || divdis <= 0 || divdis >= length) return null; - var dispts = new DisPts(DivideType.Distance, divdis); - dispts.Pts.Add(point1); - dispts.Radian = point1.GetAngle(point2); - var tempDis = divdis; - var tempPoint = point1.Polar(dispts.Radian, tempDis); + if (point1 == point2 || distance <= 0 || distance >= length) return null; + var divideInfo = new DivideInfo(DivideType.Distance, distance); + divideInfo.Points.Add(point1); + divideInfo.Radian = point1.GetAngle(point2); + var tempDistace = distance; + var tempPoint = point1.Polar(divideInfo.Radian, tempDistace); while (point1.GetDistance(tempPoint) < length) { - dispts.Pts.Add(tempPoint); - tempDis += divdis; - tempPoint = point1.Polar(dispts.Radian, tempDis); + divideInfo.Points.Add(tempPoint); + tempDistace += distance; + tempPoint = point1.Polar(divideInfo.Radian, tempDistace); } - dispts.Pts.Add(point2); - dispts.LastDis = length - tempDis; - return dispts; + divideInfo.Points.Add(point2); + divideInfo.LastDistance = length - tempDistace; + return divideInfo; } #endregion 两点间距离(定数等分/定距等分) + #region 点集合转直线两端点的集合 + /// + /// 点集合转直线两端点的集合 + /// + /// 点的集合 + /// 是否是首尾相连,默认为:首尾不相连 + /// 首尾相连时,是否闭合,默认为:不闭合 + /// 直线两端点集合 + public static IEnumerable GetLine2PointList(this IEnumerable points, + bool isEndToEnd = false, bool isClose = false) + { + if (points.Count() < 2) yield break; + Point3d endPoint1, endPoint2, firstEndPoint; + var itor = points.GetEnumerator(); + if (isEndToEnd) + { + if (!itor.MoveNext()) yield break; + firstEndPoint = endPoint1 = itor.Current; + while (itor.MoveNext()) + { + endPoint2 = itor.Current; + yield return new Line2Point3d(endPoint1, endPoint2); + endPoint1 = endPoint2; + } + if (endPoint1 != firstEndPoint && isClose) yield return new Line2Point3d(endPoint1, firstEndPoint); + } + else + { + while (itor.MoveNext()) + { + endPoint1 = itor.Current; + if (!itor.MoveNext()) yield break; + endPoint2 = itor.Current; + yield return new Line2Point3d(endPoint1, endPoint2); + } + } + } + + /// + /// 点集合转直线两端点的集合 + /// + /// 点集合 + /// 是否是首尾相连,默认为:首尾不相连 + /// 首尾相连时,是否闭合,默认为:不闭合 + /// 直线两端点集合 + public static IEnumerable GetLine2PointList(this IEnumerable points, + bool isEndToEnd = false, bool isClose = false) + { + if (points.Count() < 2) yield break; + Point2d endPoint1, endPoint2, firstEndPoint; + var itor = points.GetEnumerator(); + if (isEndToEnd) + { + if (!itor.MoveNext()) yield break; + firstEndPoint = endPoint1 = itor.Current; + while (itor.MoveNext()) + { + endPoint2 = itor.Current; + yield return new Line2Point2d(endPoint1, endPoint2); + endPoint1 = endPoint2; + } + if (endPoint1 != firstEndPoint && isClose) yield return new Line2Point2d(endPoint1, firstEndPoint); + } + else + { + while (itor.MoveNext()) + { + endPoint1 = itor.Current; + if (!itor.MoveNext()) yield break; + endPoint2 = itor.Current; + yield return new Line2Point2d(endPoint1, endPoint2); + } + } + } + #endregion 点集合转直线两端点的集合 + #region 首尾相连 /// /// 首尾相连 @@ -293,109 +370,4 @@ public static void End2End(this Point3dCollection ptcol) ptcol.Add(lst[i]); } #endregion -} - -#region 两点间距离等分所用对象和枚举 -/// -/// 两点间距离等分信息类 -/// -public class DisPts -{ - #region 私有字段 - /// - /// 弧度 - /// - private double radian = double.NaN; - /// - /// 角度 - /// - private double angle = double.NaN; - #endregion 私有字段 - - #region 构造函数 - /// - /// 一参构造函数 - /// - /// 等分长度 - public DisPts(double dis) => LastDis = Dis = dis; - /// - /// 两参构造函数 - /// - /// 等分类型 - /// 等分长度 - public DisPts(DivideType type, double dis) - { - Type = type; - LastDis = Dis = dis; - } - #endregion 构造函数 - - #region 公共属性 - /// - /// 等分类型,默认:无 - /// - public DivideType Type { get; set; } = DivideType.None; - - /// - /// 等分长度(定距等分不包含最后一段长度) - /// - public double Dis { get; set; } = 0; - - /// - /// 角度 - /// - public double Angle - { - get => angle; - set - { - angle = value; - radian = MathEx.ConvertDegToRad(value); - } - } - - /// - /// 弧度 - /// - public double Radian - { - get => radian; - set - { - radian = value; - angle = MathEx.ConvertRadToDeg(value); - } - } - - /// - /// 等分点点表(包含两个端点) - /// - public List Pts { get; set; } = new(); - - /// - /// 定距等分最后一段长度(定数等分最后一段长度与Dis一致) - ///
- /// 默认:与Dis一致 - ///
- public double LastDis { get; set; } = double.NaN; - #endregion 公共属性 -} -/// -/// 两点距离等分类型 -/// -public enum DivideType -{ - /// - /// 默认值:无 - /// - None, - /// - /// 定数等分 - /// - Number, - /// - /// 定距等分 - /// - Distance -} -#endregion 两点间距离等分所用对象和枚举 \ No newline at end of file +} \ No newline at end of file diff --git a/src/CAD/IFox.CAD.Shared/ExtensionMethod/ReferenceTypes.cs b/src/CAD/IFox.CAD.Shared/ExtensionMethod/ReferenceTypes.cs new file mode 100644 index 0000000..706d7b1 --- /dev/null +++ b/src/CAD/IFox.CAD.Shared/ExtensionMethod/ReferenceTypes.cs @@ -0,0 +1,142 @@ +namespace IFoxCAD.Cad; + +#region 两点间距离等分所用引用类型 +/// +/// 定数等分(定距等分)信息 +/// +public class DivideInfo +{ + #region 私有字段 + /// + /// 弧度 + /// + private double radian = double.NaN; + /// + /// 角度 + /// + private double angle = double.NaN; + #endregion 私有字段 + + #region 构造函数 + /// + /// 一参构造函数 + /// + /// 等分长度 + public DivideInfo(double distance) => LastDistance = Distance = distance; + /// + /// 两参构造函数 + /// + /// 等分类型 + /// 等分长度 + public DivideInfo(DivideType type, double distance) + { + Type = type; + LastDistance = Distance = distance; + } + #endregion 构造函数 + + #region 公共属性 + /// + /// 等分类型,默认:无 + /// + public DivideType Type { get; set; } = DivideType.None; + + /// + /// 等分长度(定距等分不包含最后一段长度) + /// + public double Distance { get; set; } = 0; + + /// + /// 等分点集合(含两端点) + /// + public List Points { get; set; } = new(); + + /// + /// 定距等分最后一段长度(定数等分最后一段长度与Dis一致) + ///
+ /// 默认:与Dis一致 + ///
+ public double LastDistance { get; set; } = double.NaN; + + /// + /// 角度 + /// + public double Angle + { + get => angle; + set + { + angle = value; + radian = MathEx.ConvertDegToRad(value); + } + } + + /// + /// 弧度 + /// + public double Radian + { + get => radian; + set + { + radian = value; + angle = MathEx.ConvertRadToDeg(value); + } + } + #endregion 公共属性 +} +#endregion 两点间距离等分所用引用类型 + +/// +/// 点表整理引用类(PointBasals Using) +/// +public class Line2Point2d +{ + /// + /// 直线的起始点 + /// + public Point2d StatrPoint { get; set; } = Point2d.Origin; + + /// + /// 直线的结束点 + /// + public Point2d EndPoint { get; set; } = Point2d.Origin; + + /// + /// 两参构造函数 + /// + /// 直线的起始点 + /// 直线的结束点 + public Line2Point2d(Point2d statrPoint, Point2d endPoint) + { + StatrPoint = statrPoint; + EndPoint = endPoint; + } +} + +/// +/// 点表整理引用类(PointBasals Using) +/// +public class Line2Point3d +{ + /// + /// 直线的起始点 + /// + public Point3d StatrPoint { get; set; } = Point3d.Origin; + + /// + /// 直线的结束点 + /// + public Point3d EndPoint { get; set; } = Point3d.Origin; + + /// + /// 两参构造函数 + /// + /// 直线的起始点 + /// 直线的结束点 + public Line2Point3d(Point3d statrPoint, Point3d endPoint) + { + StatrPoint = statrPoint; + EndPoint = endPoint; + } +} diff --git a/src/CAD/IFox.CAD.Shared/IFox.CAD.Shared.projitems b/src/CAD/IFox.CAD.Shared/IFox.CAD.Shared.projitems index bec68a4..ddc05d9 100644 --- a/src/CAD/IFox.CAD.Shared/IFox.CAD.Shared.projitems +++ b/src/CAD/IFox.CAD.Shared/IFox.CAD.Shared.projitems @@ -20,6 +20,7 @@ + -- Gitee