Traversing through attribute references in Mleader

By Virupaksha Aithal

Mleader entity allows users to set the block (including user defined custom block) as the content type. Many times, these blocks contain attribute definitions and hence Mleader maintains attribute references to the contained attribute definitions of the block table record.  Below code show the procedure to get the attribute reference for selected Mleader.

[CommandMethod("mLeaderBlockTest")]
public void mLeaderBlockTest()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;
 
    PromptEntityOptions options =
            new PromptEntityOptions("nSelect a Mleader with block");
    options.SetRejectMessage("nSelect only Mleader with block");
    options.AddAllowedClass(typeof(MLeader), false);
 
    PromptEntityResult acSSPrompt = ed.GetEntity(options);
 
    if (acSSPrompt.Status != PromptStatus.OK)
        return;
 
    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        //get the mleader
        MLeader mleader = Tx.GetObject(acSSPrompt.ObjectId,
                                   OpenMode.ForRead) as MLeader;
 
        ObjectId blockId = mleader.BlockContentId;
        if (blockId == ObjectId.Null)
        {
            ed.WriteMessage("Mleader does not contains blockn");
            return;
        }
 
        BlockTableRecord record = Tx.GetObject(blockId,
                               OpenMode.ForRead) as BlockTableRecord;
 
        if(!record.HasAttributeDefinitions)
        {
            ed.WriteMessage("No attributes in blockn");
            return;
        }
        foreach (ObjectId Id in record)
        {
            DBObject dbObj = Tx.GetObject(Id, OpenMode.ForRead) ;
 
            if (dbObj is AttributeDefinition)
            {
                AttributeDefinition attDef =
                                        dbObj as AttributeDefinition;
                using (AttributeReference attRef =
                                      mleader.GetBlockAttribute(Id))
                {
                    ed.WriteMessage("Reference text for tag " +
                     attDef.Tag + " is " + attRef.TextString + "n");
                }
            }
        }
 
        Tx.Commit();
    }
 
}

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading