1. 块表的查询
1.1 查找名为“自定义块”的块表中的图块记录
using var tr = new DBTrans();
if (tr.BlockTable.Has("自定义块"))
{
//要执行的操作
}
遍历块表并打印所有的块表的图块名称
public void Test_DBTrans_BlockCount()
{
using var tr = new DBTrans();
var i = tr.CurrentSpace
.GetEntities<BlockReference>()
.Where(brf => brf.GetBlockName() == "自定义块")
.Count();
Env.Print(i);
}
注意:这里的所有的图块名称包含ModelSpace和PaperSpace
2. 块定义
// 块定义
[CommandMethod(nameof(Test_BlockDef))]
public void Test_BlockDef()
{
using DBTrans tr = new();
// var line = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0));
tr.BlockTable.Add("test",
btr =>
{
btr.Origin = new Point3d(0, 0, 0);
},
() => // 图元
{
return new List<Entity> { new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0)) };
},
() => // 属性定义
{
var id1 = new AttributeDefinition() { Position = new Point3d(0, 0, 0), Tag = "start", Height = 0.2 };
var id2 = new AttributeDefinition() { Position = new Point3d(1, 1, 0), Tag = "end", Height = 0.2 };
return new List<AttributeDefinition> { id1, id2 };
}
);
}
块定义
[CommandMethod(nameof(Test_InsertBlockDef))]
public void Test_InsertBlockDef()
{
using DBTrans tr = new();
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 ents = new List<Entity>();
var line5 = new Line(new Point3d(0, 0, 0), new Point3d(1, 1, 0));
var line6 = new Line(new Point3d(0, 0, 0), new Point3d(-1, 1,
0));
ents.Add(line5);
ents.Add(line6);
tr.BlockTable.Add("test44", ents);
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<Entity> { line3, line4 }, new
List<AttributeDefinition> { 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<string, string>
{
{ "tagTest1", "1" },
{ "tagTest2", "2" }
};
tr.CurrentSpace.InsertBlock(new Point3d(0, 0, 0), "test1", atts: def1);
var def2 = new Dictionary<string, string>
{
{ "tagTest3", "1" },
{ "tagTest4", "" }
};
tr.CurrentSpace.InsertBlock(new Point3d(10, 10, 0), "test2", atts: def2);
tr.CurrentSpace.InsertBlock(new Point3d(-10, 0, 0), "test44");
}
块定义+块参照插入
[CommandMethod(nameof(Test_InsertBlockDef))]
public void Test_InsertBlockDef()
{
using DBTrans tr = new();
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);
//开始做块参照插入
tr.CurrentSpace.InsertBlock(new Point3d(4, 4, 0), "test1"); //测试默认
tr.CurrentSpace.InsertBlock(new Point3d(8, 8, 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<string, string>
{
{ "tagTest1", "1" },
{ "tagTest2", "2" }
};
tr.CurrentSpace.InsertBlock(new Point3d(15, 15, 0), "test1", atts: def1);
}
3. 修改块定义名称和块参照
// 修改块定义
[CommandMethod(nameof(Test_BlockDefChange))]
public static void Test_BlockDefChange()
{
using DBTrans tr = new();
tr.BlockTable.Change("test", btr =>
{
//要注意 这个是修改块定义名称
//修改块名,改成当前块名(可以获取到动态块名)
btr.Name = btr.Name.ToUpper();
foreach (var id in btr)
{
var ent = tr.GetObject<Entity>(id);
using (ent!.ForWrite())
{
//是否标注类型
if (ent is Dimension dBText)
{
dBText.DimensionText = "234";
dBText.RecomputeDimensionBlock(true);
}
//是否填充图案类型
if (ent is Hatch hatch)
{
hatch.ColorIndex = 0;
}
if (ent is Line line)
{
line.ColorIndex = 1;
line.StartPoint = new Point3d(line.StartPoint.X+5, line.StartPoint.Y+5, 0);
line.EndPoint = new Point3d(line.EndPoint.X+5, line.EndPoint.Y+5, 0);
}
}
}
});
tr.Editor?.Regen();
}
4. 删除块定义
[CommandMethod(nameof(Test_BlockDelete))]
public static void Test_BlockDelete()
{
using var tr = new DBTrans();
tr.BlockTable.Remove("test");
}
5. 获取文件里的块定义:
public void Test_BlockFile()
{
using DBTrans tr = new();
var id = tr.BlockTable.GetBlockFrom(@"C:\Users\vic\Desktop\test.dwg", false);
//getblockfrom函数的第一个参数是文件路径,第二个参数是是否强制覆盖本图的块定义
tr.CurrentSpace.InsertBlock(Point3d.Origin, id);
}