批量删除匿名块——CADc#二次开发

 public class MyPlugin : IExtensionApplication
 {
     public void Initialize()
     {
         string str = "输入命令\"scnmk\"执行删除匿名块\n输入命令\"km\"查询块名\n";
         Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n插件已加载!(山水qq443440204)\n{str}\n");
     }
     public void Terminate()
     {
         //Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n结束");
     }
 }

 

public class 获取动态块名
{
    [CommandMethod("scnmk")]
    public void 批量删除匿名块()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;
        // 这里假设块名,你可以修改为从用户输入获取
        PromptStringOptions pso = new PromptStringOptions("\n请输入需要删除的块名:\n");
        var pstr = ed.GetString(pso);
        if (pstr.Status != PromptStatus.OK) return;
        var blockName = pstr.StringResult;
        //string blockName = "TK-TQ-AUTO(NEW3)008c0078008f001e0009005400e10002";
        List<ObjectId> blockReferenceIds = GetBlockReferencesInAllSpaces(db, blockName);
        if (blockReferenceIds.Count > 0)
        {
            ed.WriteMessage("\n找到以下名为 {0} 的块参照 ObjectId:", blockName);
            foreach (ObjectId id in blockReferenceIds)
            {
                ed.WriteMessage("\n{0}", id);
                //var blockRef = (Entity)id.GetObject(OpenMode.ForWrite);
                using (var tr = new DBTrans())
                {
                    id.Erase();
                };
               
                
            }
        }
        else
        {
            ed.WriteMessage("\n未找到名为 {0} 的块参照。", blockName);
        }
    }

    public List<ObjectId> GetBlockReferencesInAllSpaces(Database db, string blockName)
    {
        List<ObjectId> result = new List<ObjectId>();

        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

            // 遍历模型空间和图纸空间
            foreach (ObjectId btrId in bt)
            {
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                if (btr.IsLayout)
                {
                    foreach (ObjectId objId in btr)
                    {
                        Entity ent = tr.GetObject(objId, OpenMode.ForRead) as Entity;
                        if (ent is BlockReference blockRef)
                        {
                            BlockTableRecord blockDef = (BlockTableRecord)tr.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead);
                            string actualBlockName = GetRealBlockName(blockDef, tr);
                            //string actualBlockName1 = blockDef.Name;这个获取不到匿名块的真是名字
                            if (actualBlockName == blockName)
                            {
                                result.Add(objId);
                            }
                        }
                    }
                }
            }

            tr.Commit();
        }

        return result;
    }

    // 递归查找真实块名
    public string GetRealBlockName(BlockTableRecord btr, Transaction tr)
    {
        if (btr.IsFromExternalReference)
        {
            // 如果匿名块在外部引用中,则需要打开外部参照数据库
            return "匿名块在外部引用中,则需要打开外部参照数据库";
        }
        else if (!btr.IsAnonymous)
        {
            // 如果匿名块不是匿名或是具名块,则返回块名称
            return btr.Name;
        }
        else
        {
            // 如果匿名块是匿名块,则继续查找其所属的块表记录
            foreach (ObjectId ownerId in btr.GetBlockReferenceIds(true, false))
            {
                BlockReference br = tr.GetObject(ownerId, OpenMode.ForRead) as BlockReference;
                if (br != null && br.IsDynamicBlock)
                {
                    BlockTableRecord dynamicBtr = (BlockTableRecord)br.DynamicBlockTableRecord.GetObject(OpenMode.ForRead);
                    return GetRealBlockName(dynamicBtr, tr);
                }
            }
        }

        return "";
    }
    [CommandMethod("km")]
    public void GetDynamicBlockName()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;

        // 选择动态块实体
        PromptEntityOptions promptEntityOptions = new PromptEntityOptions("\n请选择一个动态块:");
        var per = ed.GetEntity(promptEntityOptions);
        // PromptSelectionResult selectionResult = ed.GetSelection();
        if (per.Status != PromptStatus.OK) return;
        // 获取动态块的ObjectId
        ObjectId dynamicBlockId = per.ObjectId;

        // 获取动态块的AcDbBlockReference对象
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockReference dynamicBlock = tr.GetObject(dynamicBlockId, OpenMode.ForRead) as BlockReference;
            if (dynamicBlock == null)
                return;

            // 获取匿名块的BlockTableRecord对象及名称
            BlockTableRecord anonymousBlock = null;
            string anonymousBlockName = "";
            if (dynamicBlock.IsDynamicBlock)
            {
                anonymousBlock = dynamicBlock.DynamicBlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
                anonymousBlockName = anonymousBlock.Name;
            }
            else
            {
                anonymousBlock = dynamicBlock.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
                anonymousBlockName = anonymousBlock.Name;
            }

            // 递归查找真实块名
            string realBlockName = GetRealBlockName(anonymousBlock, tr);
            ed.WriteMessage("\n动态库的块名为:" + realBlockName);

            tr.Commit();
        }
    }
    [CommandMethod("bn1")]
    public void GetBlockName()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;

        // 选择动态块实体
        PromptSelectionResult selectionResult = ed.GetSelection();
        if (selectionResult.Status != PromptStatus.OK)
            return;

        // 获取动态块的ObjectId
        ObjectId dynamicBlockId = selectionResult.Value[0].ObjectId;

        // 获取动态块的AcDbBlockReference对象
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockReference dynamicBlock = tr.GetObject(dynamicBlockId, OpenMode.ForRead) as BlockReference;
            ed.WriteMessage("\n动态块的块名为:" + dynamicBlock.Name);
            // 例如返回:"*U818"
        }
    }
   
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

山水CAD插件定制

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

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

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

打赏作者

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

抵扣说明:

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

余额充值