diff --git a/src/IFoxCAD.Cad/ExtensionMethod/SymbolTableEx.cs b/src/IFoxCAD.Cad/ExtensionMethod/SymbolTableEx.cs index f777147202ae85d2f8c7b62732989bf307e129ad..bbeba008c744459aa28ba1ded466f13d4a66da3e 100644 --- a/src/IFoxCAD.Cad/ExtensionMethod/SymbolTableEx.cs +++ b/src/IFoxCAD.Cad/ExtensionMethod/SymbolTableEx.cs @@ -71,7 +71,7 @@ public static ObjectId Rename(this SymbolTable tab /// 成功返回 ,失败返回 public static bool Delete(this SymbolTable table, string name) { - if (name == "0" || name == "Defpoints" || !table.Has(name) ||table[name] == DBTrans.Top.Database.Clayer) + if (name == "0" || name == "Defpoints" || !table.Has(name) || table[name] == DBTrans.Top.Database.Clayer) { return false; } @@ -95,38 +95,50 @@ public static bool Delete(this SymbolTable table, /// /// 块表 /// 块名 - /// 图元 - /// 属性定义 + /// 对所添加块表的委托n + /// 添加图元的委托 + /// 添加属性定义的委托 /// 块定义id /// TODO: 需要测试匿名块等特殊的块是否能定义 - public static ObjectId Add(this SymbolTable table, string name, Action action = null, Func < IEnumerable> ents = null, Func> attdef = null) + public static ObjectId Add(this SymbolTable table, string name, Action action = null, Func> ents = null, Func> attdef = null) { return table.Add(name, btr => { action?.Invoke(btr); - btr.AddEntity(ents?.Invoke()); - //ents.Invoke().ForEach(ent => btr.AppendEntity(ent)); - btr.AddEntity(attdef?.Invoke()); - + if (ents is not null) + { + btr.AddEntity(ents?.Invoke()); + } + if (attdef is not null) + { + btr.AddEntity(attdef?.Invoke()); + } }); - //var id = table.Add(name, action); - - //table.Change(id, btr => - //{ - // if (ents is not null) - // { - // btr.AddEntity(ents?.Invoke()); - // //ents.Invoke().ForEach(ent => btr.AppendEntity(ent)); - // } - // if (attdef is not null) - // { - // btr.AddEntity(attdef?.Invoke()); - // } - //}); - //return id; + } + /// + /// 添加块定义 + /// + /// 块表 + /// 块名 + /// 图元 + /// 属性定义 + /// + public static ObjectId Add(this SymbolTable table, string name, IEnumerable ents = null, IEnumerable attdef = null) + { + return table.Add(name, null, () => { return ents; }, () => { return attdef; }); } - + /// + /// 添加块定义 + /// + /// 块表 + /// 块名 + /// 图元(包括属性) + /// + public static ObjectId Add(this SymbolTable table, string name, params Entity[] ents) + { + return table.Add(name, null, () => { return ents; }); + } /// /// 从文件中获取块定义 @@ -158,7 +170,7 @@ public static ObjectId GetBlockFrom(this SymbolTable /// 从文件中获取块定义 diff --git a/src/IFoxCAD.Cad/Runtime/SymbolTable.cs b/src/IFoxCAD.Cad/Runtime/SymbolTable.cs index e135ab7f96c88a38f33f95865e3b3405263b5ba0..aa30c2cfe5a161e3f8fb2eb3cd2533bc0e56e444 100644 --- a/src/IFoxCAD.Cad/Runtime/SymbolTable.cs +++ b/src/IFoxCAD.Cad/Runtime/SymbolTable.cs @@ -114,7 +114,6 @@ public ObjectId Add(string name, Action action = null) Name = name }; id = Add(record); - record = GetRecord(id); using (record.ForWrite()) { action?.Invoke(record); diff --git a/tests/DBTrans.test/Test.cs b/tests/DBTrans.test/Test.cs index c7e8e4774160d3d1bbde34fae0fcac2564559cce..6bb53aaaa1477ee9d6d51468a4344d01e4948122 100644 --- a/tests/DBTrans.test/Test.cs +++ b/tests/DBTrans.test/Test.cs @@ -197,7 +197,7 @@ public void BlockDef() using var tr = new DBTrans(); //var line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0)); tr.BlockTable.Add("test", - btr => + btr => { btr.Origin = new Point3d(0, 0, 0); }, @@ -237,23 +237,34 @@ public void BlockDefChange() public void InsertBlockDef() { using var tr = new DBTrans(); - //var line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0)); - //tr.InsertBlock(new Point3d(4, 4, 0), "test1"); //测试插入不存在的块定义 - //tr.InsertBlock(new Point3d(4, 4, 0), "test"); // 测试默认 - //tr.InsertBlock(new Point3d(0, 0, 0),"test", new Scale3d(2)); // 测试放大2倍 - //tr.InsertBlock(new Point3d(4, 4, 0), "test", new Scale3d(2), Math.PI / 4); // 测试放大2倍,旋转45度 - - tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test1"); //测试插入不存在的块定义 - tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test"); // 测试默认 - tr.CurrentSpace.InsertBlock(new Point3d(0, 0, 0), "test", new Scale3d(2)); // 测试放大2倍 - tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test", new Scale3d(2), Math.PI / 4); // 测试放大2倍,旋转45度 - - var def = new Dictionary + var line1 = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0)); + var line2 = new Line(new Point3d(0, 0, 0), new Point3d(-1, 1, 0)); + var att1 = new AttributeDefinition() { Position = new Point3d(10, 10, 0), Tag = "tagTest1", Height = 1, TextString = "valueTest1" }; + var att2 = new AttributeDefinition() { Position = new Point3d(10, 12, 0), Tag = "tagTest2", Height = 1, TextString = "valueTest2" }; + tr.BlockTable.Add("test1", line1, line2, att1, att2); + var line3 = new Line(new Point3d(5, 5, 0), new Point3d(6, 6, 0)); + var line4 = new Line(new Point3d(5, 5, 0), new Point3d(-6, 6, 0)); + var att3 = new AttributeDefinition() { Position = new Point3d(10, 14, 0), Tag = "tagTest3", Height = 1, TextString = "valueTest3" }; + var att4 = new AttributeDefinition() { Position = new Point3d(10, 16, 0), Tag = "tagTest4", Height = 1, TextString = "valueTest4" }; + tr.BlockTable.Add("test2", new List { line3, line4 }, new List { att3, att4 }); + //tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test1"); // 测试默认 + //tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test2"); + //tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test3"); //测试插入不存在的块定义 + //tr.CurrentSpace.InsertBlock(new Point3d(0, 0, 0), "test1", new Scale3d(2)); // 测试放大2倍 + //tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test1", new Scale3d(2), Math.PI / 4); // 测试放大2倍,旋转45度 + + var def1 = new Dictionary + { + { "tagTest1", "1" }, + { "tagTest2", "2" } + }; + tr.CurrentSpace.InsertBlock(new Point3d(0, 0, 0), "test1", atts: def1); + var def2 = new Dictionary { - { "start", "1" }, - { "end", "2" } + { "tagTest3", "1" }, + { "tagTest4", "" } }; - tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test", atts: def); + tr.CurrentSpace.InsertBlock(new Point3d(10,10, 0), "test2", atts: def2); } @@ -261,7 +272,7 @@ public void InsertBlockDef() public void TestClipBlock() { using var tr = new DBTrans(); - tr.BlockTable.Add("test1", + tr.BlockTable.Add("test1", btr => { btr.Origin = new Point3d(0, 0, 0);