diff --git a/CADShared/CADShared.projitems b/CADShared/CADShared.projitems index ff89251c9029d427594c38f8970659c63304c5ba..77ac84ef813e142415f7d88661fb92a9217d5b31 100644 --- a/CADShared/CADShared.projitems +++ b/CADShared/CADShared.projitems @@ -23,6 +23,7 @@ + diff --git a/CADShared/ExtensionMethod/Entity/ArcEx.cs b/CADShared/ExtensionMethod/Entity/ArcEx.cs new file mode 100644 index 0000000000000000000000000000000000000000..1db87cb26fb5e106e8f271d937451dd7be011c19 --- /dev/null +++ b/CADShared/ExtensionMethod/Entity/ArcEx.cs @@ -0,0 +1,81 @@ +namespace IFoxCAD.Cad; + +/// +/// 圆弧扩展类 +/// +public static class ArcEx +{ + #region 圆弧 + + /// + /// 根据圆心、起点、终点来创建圆弧(二维) + /// + /// 起点 + /// 圆心 + /// 终点 + /// 圆弧 + public static Arc CreateArcSCE(Point3d startPoint, Point3d centerPoint, Point3d endPoint) + { + Arc arc = new(); + arc.SetDatabaseDefaults(); + arc.Center = centerPoint; + arc.Radius = centerPoint.DistanceTo(startPoint); + Vector2d startVector = new(startPoint.X - centerPoint.X, startPoint.Y - centerPoint.Y); + Vector2d endVector = new(endPoint.X - centerPoint.X, endPoint.Y - centerPoint.Y); + // 计算起始和终止角度 + arc.StartAngle = startVector.Angle; + arc.EndAngle = endVector.Angle; + return arc; + } + /// + /// 三点法创建圆弧(二维) + /// + /// 起点 + /// 圆弧上的点 + /// 终点 + /// 圆弧 + public static Arc CreateArc(Point3d startPoint, Point3d pointOnArc, Point3d endPoint) + { + // 创建一个几何类的圆弧对象 + CircularArc3d geArc = new(startPoint, pointOnArc, endPoint); + // 将几何类圆弧对象的圆心和半径赋值给圆弧 + return (Arc)Curve.CreateFromGeCurve(geArc); + } + + /// + /// 根据起点、圆心和圆弧角度创建圆弧(二维) + /// + /// 起点 + /// 圆心 + /// 圆弧角度 + /// 圆弧 + public static Arc CreateArc(Point3d startPoint, Point3d centerPoint, double angle) + { + Arc arc = new(); + arc.SetDatabaseDefaults(); + arc.Center = centerPoint; + arc.Radius = centerPoint.DistanceTo(startPoint); + Vector2d startVector = new(startPoint.X - centerPoint.X, startPoint.Y - centerPoint.Y); + arc.StartAngle = startVector.Angle; + arc.EndAngle = startVector.Angle + angle; + return arc; + } + /// + /// 圆弧转为多段线 + /// + /// 圆弧 + /// 多段线 + public static Polyline ToPolyline(this Arc arc) + { + var plane = new Plane(arc.Center, arc.Normal); + var pl = new Polyline(); + pl.Normal = arc.Normal; + pl.AddVertexAt(0, arc.StartPoint.Convert2d(plane), + Math.Tan(arc.TotalAngle * 0.25), 0, 0); + pl.AddVertexAt(1, arc.EndPoint.Convert2d(plane), 0, 0, 0); + pl.TransformBy(Matrix3d.Displacement(pl.StartPoint.GetVectorTo(arc.StartPoint))); + pl.SetPropertiesFrom(arc); + return pl; + } + #endregion +} \ No newline at end of file diff --git a/CADShared/ExtensionMethod/SymbolTableEx.cs b/CADShared/ExtensionMethod/SymbolTableEx.cs index 0a70da67315d87789b33c056c784dfe8f4502e0b..67bd7ad5717c22107475d776f71a1f5a01a3c98a 100644 --- a/CADShared/ExtensionMethod/SymbolTableEx.cs +++ b/CADShared/ExtensionMethod/SymbolTableEx.cs @@ -46,28 +46,6 @@ public static ObjectId Rename(this SymbolTable tab }); return table[newName]; } - /// - /// 删除图层 - /// - /// 层表 - /// 图层名 - /// 成功返回 ,失败返回 - public static bool Delete(this SymbolTable table, string name) - { - if (name == "0" || name == "Defpoints" || !table.Has(name) || table[name] == table.Database.Clayer) - return false; - - table.CurrentSymbolTable.GenerateUsageData(); - var ltr = table.GetRecord(name); - if (ltr is null) - return false; - - if (ltr.IsUsed) - return false; - using (ltr.ForWrite()) - ltr.Erase(); - return true; - } #endregion #region 块表 diff --git a/Test/GlobalUsing.cs b/Test/GlobalUsing.cs index a7b8c491d0200fa34f9114e2cbfa3ec2d3395cb3..54312095721ab7c3eb4a258bbaa9544588b8121a 100644 --- a/Test/GlobalUsing.cs +++ b/Test/GlobalUsing.cs @@ -4,4 +4,5 @@ global using Autodesk.AutoCAD.Geometry; global using Autodesk.AutoCAD.Runtime; global using System.ComponentModel; -global using System.Runtime.InteropServices; \ No newline at end of file +global using System.Runtime.InteropServices; +global using Autodesk.AutoCAD.Colors; \ No newline at end of file diff --git a/Test/TestAddEntity.cs b/Test/TestAddEntity.cs index 70cb05fecaa40ea4079ced50d80b908f9e5faba8..df868d1cb5a5fb8e5233b5a1ff9cb1bcd8937e42 100644 --- a/Test/TestAddEntity.cs +++ b/Test/TestAddEntity.cs @@ -1,98 +1,158 @@ namespace Test; -public partial class Test + +public class TestAddEntity { - [CommandMethod(nameof(Test_Rec))] - public void Test_Rec() +#region 直线 + [CommandMethod(nameof(Test_AddLinetoCurrentSpace))] + public void Test_AddLinetoCurrentSpace() { - Point2d p1 = new(10000.2, 100000.5); - Point2d p2 = new(15000.9, 100000.5); - Point2d p3 = new(15000.9, 105000.7); - Point2d p4 = new(10000.2, 105000.7); + using DBTrans tr = new(); // 开启事务 - var p12 = p2 - p1; - var p23 = p3 - p2; - var p34 = p4 - p3; - var p41 = p1 - p4; - var p13 = p3 - p1; - var p24 = p4 - p2; + Line line = new(new(0, 0, 0), new(1, 1, 0)); // 定义一个直线 + tr.CurrentSpace.AddEntity(line); // 将直线添加到当前空间 + } + [CommandMethod(nameof(Test_AddLinetoModelSpace))] + public void Test_AddLinetoModelSpace() + { + using DBTrans tr = new(); // 开启事务 - const double pi90 = Math.PI / 2; - Env.Print(pi90); + Line line = new(new(0, 0, 0), new(1, 1, 0)); // 定义一个直线 + tr.ModelSpace.AddEntity(line); // 将直线添加到模型空间 + } - Tools.TestTimes(1000000, "对角线", () => - { - var result = false; - if (Math.Abs(p13.Length - p24.Length) <= 1e8) - { - result = p41.IsParallelTo(p12); - } - }); + [CommandMethod(nameof(Test_AddLinetoPaperSpace))] + public void Test_AddLinetoPaperSpace() + { + using DBTrans tr = new(); // 开启事务 -#pragma warning disable CS0219 // 变量已被赋值,但从未使用过它的值 - Tools.TestTimes(1000000, "三次点乘", () => - { - bool result = Math.Abs(p12.DotProduct(p23)) < 1e8 && - Math.Abs(p23.DotProduct(p34)) < 1e8 && - Math.Abs(p34.DotProduct(p41)) < 1e8; - }); + Line line = new(new(0, 0, 0), new(1, 1, 0)); // 定义一个直线 + tr.PaperSpace.AddEntity(line); // 将直线添加到图纸空间 + } - Tools.TestTimes(1000000, "三次垂直", () => - { - bool result = p12.IsParallelTo(p23) && - p23.IsParallelTo(p34) && - p34.IsParallelTo(p41); - }); -#pragma warning restore CS0219 // 变量已被赋值,但从未使用过它的值 + [CommandMethod(nameof(Test_AddEntities))] + public void Test_AddEntities() + { + // 开启事务 + using DBTrans tr = new(); + // 定义三条直线 + Line line1 = new(new Point3d(0, 0, 0), new Point3d(1, 1, 0)); + Line line2 = new(new Point3d(0, 0, 0), new Point3d(1, 1, 0)); + Line line3 = new(new Point3d(1, 1, 0), new Point3d(3, 3, 0)); + var circle = CircleEx.CreateCircle(new(0,0,0),10); + // 一次性添加到当前空间 + tr.CurrentSpace.AddEntity(line2, line2, line3, circle); + // 或者可以传入个列表 + List lines = [line1, line2, line3]; + tr.CurrentSpace.AddEntity(lines); + // 或者可以传入个数组 + Entity[] lines1 = [line1, line2, line3]; + tr.CurrentSpace.AddEntity(lines1); + // 图元数组 + Entity[] lines2 = [line1, line2, line3, circle]; + tr.CurrentSpace.AddEntity(lines2); + // c#12 新语法,集合表达式 + tr.CurrentSpace.AddEntity([line1, line2, circle]); } +#endregion +#region 圆 + [CommandMethod(nameof(Test_AddCircle))] + public void Test_AddCircle() + { + var cir = CircleEx.CreateCircle(Point3d.Origin, new(1,0,0)); // 两点创建圆 + var cir1 = CircleEx.CreateCircle(Point3d.Origin, new(1,1,0), new(2,0,0)); //三点创建圆 + var cir2 = CircleEx.CreateCircle(Point3d.Origin, 5); // 圆心半径创建圆 - [CommandMethod(nameof(Test_EntRoration))] - public void Test_EntRoration() + using DBTrans tr = new(); + tr.CurrentSpace.AddEntity(cir, cir2); + + // 由于三点不一定能成功创建一个圆,因此返回值是可空的,需要判空 + if (cir1 is not null) + { + tr.CurrentSpace.AddEntity(cir1); + } + } +#endregion + +#region 圆弧 + [CommandMethod(nameof(Test_AddArc))] + public void Test_AddArc() { - var line = new Line(new(0, 0, 0), new(100, 0, 0)); + using DBTrans tr = new(); + var arc1 = ArcEx.CreateArcSCE(new Point3d(2, 0, 0), new Point3d(0, 0, 0), new Point3d(0, 2, 0));// 起点,圆心,终点 + var arc2 = ArcEx.CreateArc(new Point3d(4, 0, 0), new Point3d(0, 0, 0), Math.PI / 2); // 起点,圆心,弧度 + var arc3 = ArcEx.CreateArc(new Point3d(1, 0, 0), new Point3d(0, 0, 0), new Point3d(0, 1, 0)); // 起点,圆上一点,终点 + tr.CurrentSpace.AddEntity(arc1, arc2, arc3); + } +#endregion + + + + + +#region 多段线 + [CommandMethod(nameof(Test_AddPolyline1))] + public void Test_AddPolyline1() + { using DBTrans tr = new(); - tr.CurrentSpace.AddEntity(line); - var line2 = (Line)line.Clone(); - tr.CurrentSpace.AddEntity(line2); - line2.Rotation(new(100, 0, 0), Math.PI / 2); + Polyline pl = new(); + pl.SetDatabaseDefaults(); + pl.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0); + pl.AddVertexAt(1, new Point2d(10, 10), 0, 0, 0); + pl.AddVertexAt(2, new Point2d(20, 20), 0, 0, 0); + pl.AddVertexAt(3, new Point2d(30, 30), 0, 0, 0); + pl.AddVertexAt(4, new Point2d(40, 40), 0, 0, 0); + pl.Closed = true; + pl.Color = Color.FromColorIndex(ColorMethod.ByColor, 6); + tr.CurrentSpace.AddEntity(pl); } - [CommandMethod(nameof(Test_TypeSpeed))] - public void Test_TypeSpeed() + [CommandMethod(nameof(Test_AddPolyline2))] + public void Test_AddPolyline2() { - var line = new Line(); - var line1 = line as Entity; - Tools.TestTimes(100000, "is 匹配:", () => - { - var t = line1 is Line; - }); - Tools.TestTimes(100000, "name 匹配:", () => - { - // var t = line.GetType().Name; - var tt = line1.GetType().Name == nameof(Line); - }); - Tools.TestTimes(100000, "dxfname 匹配:", () => - { - // var t = line.GetType().Name; - var tt = line1.GetRXClass().DxfName == nameof(Line); - }); + // 集合表达式 + List<(Point3d, double, double, double)> pts = + [ + (new Point3d(0,0,0),0,0,0), + (new Point3d(10,0,0),0,0,0), + (new Point3d(10,10,0),0,0,0), + (new Point3d(0,10,0),0,0,0), + (new Point3d(5,5,0),0,0,0) + ]; + + using DBTrans tr = new(); + var pl = pts.CreatePolyline(); + tr.CurrentSpace.AddEntity(pl); } - // 测试延迟刷新 - [CommandMethod(nameof(Test_sleeptrans))] - public static void Test_sleeptrans() + + [CommandMethod(nameof(Test_AddPolyline3))] + public void Test_AddPolyline3() { using var tr = new DBTrans(); - for (int i = 0; i < 100; i++) - { - var cir = CircleEx.CreateCircle(new Point3d(i, i, 0), 0.5); - cir.ColorIndex = i; - tr.CurrentSpace.AddEntity(cir); - tr.Editor?.Redraw(cir); - Thread.Sleep(10); - } + List pts = + [ + new(0, 0, 0), + new(0, 1, 0), + new(1, 1, 0), + new(1, 0, 0) + ]; + var pline = pts.CreatePolyline(); + tr.CurrentSpace.AddEntity(pline); + + // 可以通过委托,一次性的创建多段线并设置属性 + var pline1 = pts.CreatePolyline(p => + { + p.Closed = true; + p.ConstantWidth = 0.2; + p.ColorIndex = 1; + }); + tr.CurrentSpace.AddEntity(pline1); } -} \ No newline at end of file + +#endregion + +} diff --git a/Test/TestLayer.cs b/Test/TestLayer.cs new file mode 100644 index 0000000000000000000000000000000000000000..c976fd82ea67322cbb4874e2a2ba430fce1cdcf6 --- /dev/null +++ b/Test/TestLayer.cs @@ -0,0 +1,67 @@ +namespace Test; + + +public class TestLayer +{ + [CommandMethod(nameof(Test_LayerAdd0))] + public void Test_LayerAdd0() + { + using DBTrans tr = new(); + tr.LayerTable.Add("1"); + tr.LayerTable.Add("2", lt => { + lt.Color = Color.FromColorIndex(ColorMethod.ByColor, 1); + lt.LineWeight = LineWeight.LineWeight030; + }); + tr.LayerTable.Remove("3"); + tr.LayerTable.Remove("0"); + tr.LayerTable.Change("4", lt => { + lt.Color = Color.FromColorIndex(ColorMethod.ByColor, 2); + }); + } + + + // 添加图层 + [CommandMethod(nameof(Test_LayerAdd1))] + public void Test_LayerAdd1() + { + using DBTrans tr = new(); + tr.LayerTable.Add("test1", Color.FromColorIndex(ColorMethod.ByColor, 1)); + } + + // 添加图层 + [CommandMethod(nameof(Test_LayerAdd2))] + public void Test_LayerAdd2() + { + using DBTrans tr = new(); + tr.LayerTable.Add("test2", 2); + // tr.LayerTable["3"] = new LayerTableRecord(); + } + // 删除图层 + [CommandMethod(nameof(Test_LayerDel))] + public void Test_LayerDel() + { + using DBTrans tr = new(); + tr.LayerTable.Remove("0"); // 删除图层 0 + tr.LayerTable.Remove("Defpoints");// 删除图层 Defpoints + tr.LayerTable.Remove("1"); // 删除不存在的图层 1 + tr.LayerTable.Remove("2"); // 删除有图元的图层 2 + tr.LayerTable.Remove("3"); // 删除图层 3 + + tr.LayerTable.Remove("2"); // 测试是否能强制删除 + } + + [CommandMethod(nameof(Test_PrintLayerName))] + public void Test_PrintLayerName() + { + using DBTrans tr = new(); + foreach (var layerRecord in tr.LayerTable.GetRecords()) + { + Env.Printl(layerRecord.Name); + } + foreach (var layerRecord in tr.LayerTable.GetRecords()) + { + Env.Printl(layerRecord.Name); + break; + } + } +} \ No newline at end of file