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

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

[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

你可能感兴趣的:(AutoCAD二次开发,AutoCAD,.Net,AutoCAD)