Vb and C# code
[CommandMethod("AttRefBlockName")]
public void AttRefBlockName()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelect a Attribute reference:");
PromptNestedEntityResult pner = ed.GetNestedEntity(pno);
if (pner.ObjectId.ObjectClass != RXClass.GetClass(typeof(AttributeReference)))
{
ed.WriteMessage("\nNot a Attribute Reference");
}
using (Transaction trx = db.TransactionManager.StartTransaction())
{
AttributeReference attRef = (AttributeReference)trx.GetObject(pner.ObjectId, OpenMode.ForRead);
BlockReference bref = (BlockReference)trx.GetObject(attRef.OwnerId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trx.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
ed.WriteMessage("\nBlock Name: {0} Handle: {1}", btr.Name, btr.Handle.ToString());
trx.Commit();
}
}
<CommandMethod("AttRefBlockNameVb")> _
Public Sub AttRefBlockNameVb()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim pno As New PromptNestedEntityOptions(vbLf & "Select a Attribute reference:")
Dim pner As PromptNestedEntityResult = ed.GetNestedEntity(pno)
If pner.ObjectId.ObjectClass <> RXClass.GetClass(GetType(AttributeReference)) Then
ed.WriteMessage(vbLf & "Not a Attribute Reference")
End If
Using trx As Transaction = db.TransactionManager.StartTransaction()
Dim attRef As AttributeReference = trx.GetObject(pner.ObjectId, OpenMode.ForRead)
Dim bref As BlockReference = trx.GetObject(attRef.OwnerId, OpenMode.ForRead)
Dim btr As BlockTableRecord = trx.GetObject(bref.BlockTableRecord, OpenMode.ForRead)
ed.WriteMessage("{0}Block Name: {1} Handle: {2}", Environment.NewLine, btr.Name, btr.Handle.ToString())
trx.Commit()
End Using
End Sub