diff --git a/src/IFoxCAD.Cad.Shared/ExtensionMethod/EntityEx.cs b/src/IFoxCAD.Cad.Shared/ExtensionMethod/EntityEx.cs index d839ba8bdad8b5e714db2014d9591ef63549d673..1ef15f7b67997fb6c9fa203e3446be7c82f13055 100644 --- a/src/IFoxCAD.Cad.Shared/ExtensionMethod/EntityEx.cs +++ b/src/IFoxCAD.Cad.Shared/ExtensionMethod/EntityEx.cs @@ -1,4 +1,4 @@ -namespace IFoxCAD.Cad; +namespace IFoxCAD.Cad; /// @@ -158,6 +158,37 @@ public static void ValidateMirror(this DBText txt) txt.IsMirroredInY = false; } } + + /// + /// 获取文字最佳包围点 + /// + /// 文字 + /// 点集 + public static List GetBestBoundPoints(this DBText text) + { + var angle = text.Rotation % (Math.PI * 0.5); + if (angle == Math.PI * 0.25) + { + var textnew = text.GetTransformedCopy(Matrix3d.Rotation(1e-6, Vector3d.ZAxis, text.AlignmentPoint)) as DBText; + return textnew!.GetBestBoundPoints(); + } + var ratio = Math.Abs(Math.Tan(angle)); + var box = text.GetBoundingBoxEx(); + var w = box.MaxX - box.MinX; + var h = box.MaxY - box.MinY; + var a = (w * ratio - h) / (ratio * ratio - 1); + var b = (w - ratio * h) / (1 - ratio * ratio); + if (a < 0 || b < 0) + { + var textnew = text.GetTransformedCopy(Matrix3d.Rotation(1e-6, Vector3d.ZAxis, box.Extents3d.MidPoint())) as DBText; + return textnew!.GetBestBoundPoints(); + }; + var pt1 = new Point2d(box.MinX, box.MinY + a); + var pt2 = new Point2d(box.MinX + b, box.MaxY); + var pt3 = new Point2d(box.MaxX, box.MaxY - a); + var pt4 = new Point2d(box.MaxX - b, box.MinY); + return new() { pt1, pt2, pt3, pt4 }; + } #endregion #region 多行文字