Search code examples
c#autocadautocad-plugin

Autocad .NET get attributes of selected blocks


I am new to autocad scripting and am trying to create an application which allows the user to select blocks and show the associated attributes of the block. I have found an alternative solution for now but would like to try an understand why my initial method was not working.

What I initially tried to do is create a selection set from the user input, get the objectid and try to create a blocktablerecord from this information. However every time I tried it returned Null.

here is the code I was using:

public void getattrib()
    {
        Database acCurDb;
        acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
        Document acDoc = Application.DocumentManager.MdiActiveDocument;

        using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
        {
            // Open the Block table for read
            BlockTable acBlkTbl;
            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
            
            PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
            if (acSSPrompt.Status == PromptStatus.OK)
            {
                SelectionSet acSSet = acSSPrompt.Value;
                ObjectId anothertest = acSSet[0].ObjectId;
                BlockTableRecord br = acTrans.GetObject(anothertest, OpenMode.ForRead) as BlockTableRecord;

Solution

  • If You select the block, it's BlockReference not BlockTableRecord. BlockTableRecord is a block definition. If You want BlockTableRecord you can read it's ObjectId as property BlockTableRecord of selected BlockReference