CAD二次开发IFoxCAD框架系列(18)-块表操作

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);
        }
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术闲聊DD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值