赞
踩
获取动态块的块名需要使用AcadEntity类或其派生类的BlockName属性。
但是,由于动态块可以设置一个或多个匿名块定义。
因此,程序返回的是匿名块的名称(以“*U”开头),而不是动态块的真实块名。
会返回如: “*U818” 的结果。
因此需要先获取到匿名块的ObjectId,再通过该ObjectId获取匿名块的BlockTableRecord对象,最后使用BlockTableRecord对象的Name属性获取匿名块的真实块名
如果一个动态块使用了匿名块定义,则需要通过以下步骤获取它的真实块名:
说明:使用“获取普通块名的代码”只能获取到动态块的匿名。
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; namespace ClassLibrary { public class Class { [CommandMethod("GetBlockName")] 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" } } } }
说明:
可以参考以下代码获取匿名块的真实块名。
使用了进行递归查找
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; // Demo 获取块的名字 namespace ClassLibrary { public class Class { [CommandMethod("GetDynamicBlockName")] public void GetDynamicBlockName() { 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; 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(); } } // 递归查找真实块名 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 = br.DynamicBlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord; return GetRealBlockName(dynamicBtr, tr); } } } return ""; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。