diff --git a/src/CAD/IFox.CAD.Shared/ExtensionMethod/EditorEx.cs b/src/CAD/IFox.CAD.Shared/ExtensionMethod/EditorEx.cs index 4d7915ac56d698af5b3e1d7c7e17e63c76686979..55b7dc727f1edeede223b1a57080e4a9e7f68475 100644 --- a/src/CAD/IFox.CAD.Shared/ExtensionMethod/EditorEx.cs +++ b/src/CAD/IFox.CAD.Shared/ExtensionMethod/EditorEx.cs @@ -1,3 +1,5 @@ +using System; + namespace IFoxCAD.Cad; /// @@ -98,7 +100,8 @@ public static PromptSelectionResult SSGet(this Editor editor, if (pso.MessageForRemoval is null) pso.MessageForAdding = "选择对象"; pso.MessageForAdding += $"[{string.Join(" / ", keywords.Keys.ToArray())}]"; - pso.KeywordInput += (s, e) => { + pso.KeywordInput += (s, e) => + { if (keywords.TryGetValue(e.Input, out Action value)) value.Invoke(); }; @@ -226,7 +229,8 @@ public static void SsgetAddKeys(this PromptSelectionOptions pso, // 从选择集命令中显示关键字 pso.MessageForAdding = keyWords.GetDisplayString(true); // 关键字回调事件 ssget关键字 - pso.KeywordInput += (sender, e) => { + pso.KeywordInput += (sender, e) => + { dicActions[e.Input].Invoke(); }; } @@ -915,7 +919,7 @@ public static void ZoomObject(this Editor ed, Entity ent, double offsetDist = 0. ed.ZoomWindow(ext.MinPoint, ext.MaxPoint, offsetDist); } -#endregion + #endregion #region Get交互类 @@ -1160,4 +1164,263 @@ public static void PrepareForJig(this Editor ed, IEnumerable ents) } #endregion + + + #region EditorDraw + /// + /// 像素长转CAD长 + /// + /// + /// + /// + public static double PixelToLength(this Editor editor, double value) + { + var point = new System.Windows.Point(1.0, 1.0); + Point3d point3d = editor.PointToWorld(point); + point = new(2.0, 1.0); + Point3d point3d2 = editor.PointToWorld(point); + return value * point3d.DistanceTo(point3d2); + } + + /// + /// CAD长度转像素长度 + /// + /// + /// + /// + public static double LengthToPixel(this Editor editor, double value) + { + var point = new System.Windows.Point(1.0, 1.0); + Point3d point3d = editor.PointToWorld(point); + point = new System.Windows.Point(2.0, 1.0); + Point3d point3d2 = editor.PointToWorld(point); + return value / point3d.DistanceTo(point3d2); + } + + /// + /// 画直线 + /// + /// + /// + /// + /// + /// + public static void DrawLine(this Editor ed, Point3d ptwcs1, Point3d ptwcs2, int color, bool HighLight) + { + Point3d point3d = ed.WcsToUcs(ptwcs1); + Point3d point3d2 = ed.WcsToUcs(ptwcs2); + ed.DrawVector(point3d, point3d2, color, HighLight); + } + + /// + /// 画矩形 + /// + /// + /// + /// + /// + /// + public static void DrawRectangle(this Editor editor, Point3d ptwcs1, Point3d ptwcs2, int color, bool HighLight) + { + Point3d point3d = new Point3d(ptwcs2.X, ptwcs1.Y, ptwcs1.Z); + Point3d point3d2 = new Point3d(ptwcs1.X, ptwcs2.Y, ptwcs1.Z); + Point3d point3d3 = editor.WcsToUcs(ptwcs1); + Point3d point3d4 = editor.WcsToUcs(ptwcs2); + point3d = editor.WcsToUcs(point3d); + point3d2 = editor.WcsToUcs(point3d2); + editor.DrawVector(point3d3, point3d, color, HighLight); + editor.DrawVector(point3d, point3d4, color, HighLight); + editor.DrawVector(point3d4, point3d2, color, HighLight); + editor.DrawVector(point3d2, point3d3, color, HighLight); + } + /// + /// 画十字叉 + /// 使用方法 Editor.DrawCross(pt, 10.0, 1, false); + /// + /// + /// + /// + /// + /// + public static void DrawCross(this Editor editor, Point3d ptwcs, double pixelsize, int color, bool HighLight) + { + double num = editor.PixelToLength(pixelsize); + double num2 = num / 2.0 / Math.Sqrt(2.0); + Point3d point3d = new Point3d(ptwcs.X - num2, ptwcs.Y - num2, 0.0); + Point3d point3d2 = new Point3d(ptwcs.X + num2, ptwcs.Y + num2, 0.0); + Point3d point3d3 = new Point3d(ptwcs.X - num2, ptwcs.Y + num2, 0.0); + Point3d point3d4 = new Point3d(ptwcs.X + num2, ptwcs.Y - num2, 0.0); + point3d = editor.WcsToUcs(point3d); + point3d2 = editor.WcsToUcs(point3d2); + point3d3 = editor.WcsToUcs(point3d3); + point3d4 = editor.WcsToUcs(point3d4); + editor.DrawVector(point3d, point3d2, color, HighLight); + editor.DrawVector(point3d3, point3d4, color, HighLight); + } + /// + /// 画三角形 + /// + /// + /// + /// + /// + /// + public static void DrawTriangle(this Editor editor, Point3d ptwcs, double pixelsize, int color, bool HighLight) + { + double num = editor.PixelToLength(pixelsize); + double num2 = num / 2.0 / Math.Sin(1.0471975511965976); + double num3 = num / 2.0 / Math.Tan(1.0471975511965976); + Point3d point3d = new Point3d(ptwcs.X, ptwcs.Y + num2, 0.0); + Point3d point3d2 = new Point3d(ptwcs.X - num / 2.0, ptwcs.Y - num3, 0.0); + Point3d point3d3 = new Point3d(ptwcs.X + num / 2.0, ptwcs.Y - num3, 0.0); + point3d = editor.WcsToUcs(point3d); + point3d2 = editor.WcsToUcs(point3d2); + point3d3 = editor.WcsToUcs(point3d3); + editor.DrawVector(point3d, point3d2, color, HighLight); + editor.DrawVector(point3d2, point3d3, color, HighLight); + editor.DrawVector(point3d3, point3d, color, HighLight); + } + + /// + /// 画圆 + /// + /// + /// + /// + /// + /// + public static void DrawCircle(this Editor editor, Point3d ptwcs, double pixelsize, int color, bool HighLight) + { + double num = editor.PixelToLength(pixelsize) / 2.0; + int num2 = 60; + double num3 = Math.PI * 2 / num2; + Vector3d vector3d = new Vector3d(num, 0.0, 0.0); + int num4 = 0; + int num5 = num2 - 1; + for (int i = num4; i <= num5; i++) + { + unchecked + { + Vector3d vector3d2 = vector3d.RotateBy((double)i * num3, Vector3d.ZAxis); + Vector3d vector3d3 = vector3d.RotateBy((double)(checked(i + 1)) * num3, Vector3d.ZAxis); + Point3d point3d = editor.WcsToUcs(ptwcs + vector3d2); + Point3d point3d2 = editor.WcsToUcs(ptwcs + vector3d3); + editor.DrawVector(point3d, point3d2, color, HighLight); + } + } + } + /// + /// 画箭头 + /// 使用方法 editor.DrawArrow(pt, gradientDirection, 80.0, 7, false); + /// + /// + /// + /// + /// + /// + /// + public static void DrawArrow(this Editor editor, Point3d ptwcs, double angwcs, double pixelsize, int color, bool HighLight) + { + double num = editor.PixelToLength(pixelsize); + double num2 = 0.5 * num; + double num3 = 0.15 * num; + double num4 = 0.3 * num; + Vector3d vector3d = Vector3d.XAxis.RotateBy(angwcs, Vector3d.ZAxis); + Point3d point3d = ptwcs - num / 2.0 * vector3d; + Point3d point3d2 = point3d + num2 * vector3d; + Point3d point3d3 = point3d + num3 / 2.0 * vector3d.GetPerpendicularVector(); + Point3d point3d4 = point3d - num3 / 2.0 * vector3d.GetPerpendicularVector(); + Point3d ptwcs2 = point3d3 + num2 * vector3d; + Point3d ptwcs3 = point3d4 + num2 * vector3d; + Point3d ptwcs4 = point3d2 + num4 / 2.0 * vector3d.GetPerpendicularVector(); + Point3d ptwcs5 = point3d2 - num4 / 2.0 * vector3d.GetPerpendicularVector(); + Point3d ptwcs6 = point3d + num * vector3d; + editor.DrawVector(editor.WcsToUcs(point3d3), editor.WcsToUcs(point3d4), color, HighLight); + editor.DrawVector(editor.WcsToUcs(point3d3), editor.WcsToUcs(point3d4), color, HighLight); + editor.DrawVector(editor.WcsToUcs(point3d3), editor.WcsToUcs(ptwcs2), color, HighLight); + editor.DrawVector(editor.WcsToUcs(point3d4), editor.WcsToUcs(ptwcs3), color, HighLight); + editor.DrawVector(editor.WcsToUcs(ptwcs2), editor.WcsToUcs(ptwcs4), color, HighLight); + editor.DrawVector(editor.WcsToUcs(ptwcs3), editor.WcsToUcs(ptwcs5), color, HighLight); + editor.DrawVector(editor.WcsToUcs(ptwcs4), editor.WcsToUcs(ptwcs6), color, HighLight); + editor.DrawVector(editor.WcsToUcs(ptwcs5), editor.WcsToUcs(ptwcs6), color, HighLight); + } + + #endregion + + #region 坐标转换 + /// + /// 用户坐标系转世界坐标系 + /// + /// + /// + public static Point3d UcsToWcs(this Editor editor, Point3d ptucs) + { + Matrix3d currentUserCoordinateSystem = editor.CurrentUserCoordinateSystem; + return ptucs.TransformBy(currentUserCoordinateSystem); + } + + /// + /// 按给定角度将用户坐标系转换为世界坐标系 + /// + /// + /// + /// + public static double UcsToWcs(this Editor editor, double angucs) + { + CoordinateSystem3d coordinateSystem3d = editor.CurrentUserCoordinateSystem.CoordinateSystem3d; + return coordinateSystem3d.Xaxis.RotateBy(angucs, coordinateSystem3d.Zaxis).AngleOnPlane(new Plane()); + } + + /// + /// 世界坐标系转用户坐标系 + /// + /// + /// + /// + public static Point3d WcsToUcs(this Editor editor, Point3d ptwcs) + { + return ptwcs.TransformBy(editor.CurrentUserCoordinateSystem.Inverse()); + } + /// + /// 按给定弧度,世界坐标系转用户坐标系 + /// + /// + /// + /// + public static double WcsToUcs(this Editor editor, double angwcs) + { + Matrix3d currentUserCoordinateSystem = editor.CurrentUserCoordinateSystem; + return Vector3d.XAxis.RotateBy(angwcs, Vector3d.ZAxis).TransformBy(currentUserCoordinateSystem).AngleOnPlane(new Plane()); + } + + /// + /// 世界坐标系转显示坐标系 + /// + /// + /// + /// + public static Point3d WcsToDcs(this Editor editor, Point3d ptwcs) + { + ViewTableRecord currentView = editor.GetCurrentView(); + Matrix3d matrix3d = Matrix3d.PlaneToWorld(currentView.ViewDirection); + matrix3d = Matrix3d.Displacement(currentView.Target - Point3d.Origin) * matrix3d; + matrix3d = (Matrix3d.Rotation(-currentView.ViewTwist, currentView.ViewDirection, currentView.Target) * matrix3d).Inverse(); + currentView.Dispose(); + return ptwcs.TransformBy(matrix3d); + } + + /// + /// 世界坐标系转对象坐标系 + /// + /// + /// + /// + /// + public static Point3d WcsToOcs(this Editor editor, Point3d ptwcs, Entity ent) + { + return ptwcs.TransformBy(ent.Ecs.Inverse()); + } + #endregion + + } \ No newline at end of file