AutoCAD .Net 通过块参照获取块名

来源:互联网 发布:网络骗局如何报警 编辑:程序博客网 时间:2024/06/04 23:37

通过块参照获取块名,需要区分是不是动态块。
示例代码如下:

[CommandMethod("GetBlockName")]public void GetBlockName(){    Document doc = Application.DocumentManager.MdiActiveDocument;    Database db = doc.Database;    PromptEntityOptions options = new PromptEntityOptions("\nSelect block reference");    options.SetRejectMessage("\nSelect only block reference");    options.AddAllowedClass(typeof(BlockReference), false);    PromptEntityResult ret = doc.Editor.GetEntity(options);    if (ret.Status != PromptStatus.OK)        return;    using (Transaction tr = db.TransactionManager.StartTransaction())    {        BlockReference blkRef = tr.GetObject(ret.ObjectId,            OpenMode.ForRead) as BlockReference;        ObjectId blkRecdId = ObjectId.Null;        if (blkRef.IsDynamicBlock)        {            blkRecdId = blkRef.DynamicBlockTableRecord;        }        else        {            blkRecdId = blkRef.BlockTableRecord;        }        BlockTableRecord block = tr.GetObject(blkRecdId,            OpenMode.ForRead) as BlockTableRecord;        doc.Editor.WriteMessage(block.Name + "\n");    }}

参考文章:
http://adndevblog.typepad.com/autocad/2012/05/identifying-block-name-from-the-block-reference.html

原创粉丝点击