Let me see if I understand your question correctly: you want to determine an attribute reference found in a block reference is created based on which attribute definition defined in the BlockTableRecord. If that is the question, then read on.
AttributeReference in a BlockReference has no "hard link" to an AttributeDefinition on which it is based. AttributeDefinition is only served as a template when an AttributeReference is created. After being created, it has no tie to any AttributeDefinition.
One might think AttributeReference.Tag can be used for looking up which AttributeDefinition in the BlockTableRecord is used to create the AttributeReference. Well, AttributeReference's Tag property is Read/Write-able, you can change it to anything (to be different from the AttributeDefinition it is based) with code.
You can also erase an AttributeReference from the BlockReference, so that even the block definition contains x number of attribute defined, but the block reference can y number of AttrbuteReferences in it.
Furthur more, you can add AttributeReference to a block reference without AttributeDefinition.
There relationship between AttributeDefinition and AttributeReference is different from block definition (BlockTableRecord) and block reference (BlockReference)
Therefore, there is no relaible way for you to trace back to AttributeDefinition from a give AttributeReference.
Below is some quick code to show what I described (assume you have a drawing, in which you have a block with 3 attributes defined, and you have that block inserted. See attached picture)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;
[assembly: CommandClass(typeof(AttributeInBlock.MyCommand))]
namespace AttributeInBlock
{
public class MyCommand
{
[CommandMethod("AttrTest")]
public static void AttrTest()
{
Document dwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
ObjectId blkRefID = PickBlockReference(ed);
if (blkRefID == ObjectId.Null) return;
ChangeAttributeInBlockReference(dwg,blkRefID);
ed.WriteMessage("\nMyCommand executed.");
}
#region private methods
private static ObjectId PickBlockReference(Editor ed)
{
PromptEntityOptions opt = new PromptEntityOptions("\nPick a block reference: ");
opt.SetRejectMessage("Invalid pick - not a block refernece.");
opt.AddAllowedClass(typeof(BlockReference), true);
PromptEntityResult res = ed.GetEntity(opt);
if (res.Status == PromptStatus.OK)
{
return res.ObjectId;
}
else
{
return ObjectId.Null;
}
}
private static void ChangeAttributeInBlockReference(Document dwg, ObjectId brefID)
{
using (Transaction tran = dwg.Database.TransactionManager.StartTransaction())
{
BlockReference blkRef = (BlockReference)tran.GetObject(brefID, OpenMode.ForWrite);
//Rename attribute
foreach (ObjectId att in blkRef.AttributeCollection)
{
AttributeReference attRef = (AttributeReference)tran.GetObject(att, OpenMode.ForWrite);
if (attRef.Tag.ToUpper() == "AAA") attRef.Tag = "XXX";
if (attRef.Tag.ToUpper() == "BBB") attRef.Tag = "YYY";
if (attRef.Tag.ToUpper() == "CCC") attRef.Tag = "ZZZ";
}
//Erase an attribute reference
foreach (ObjectId att in blkRef.AttributeCollection)
{
AttributeReference attRef = (AttributeReference)tran.GetObject(att, OpenMode.ForWrite);
if (attRef.Tag.ToUpper() == "ZZZ")
{
attRef.Erase();
break;
}
}
//Create a new attributereference (without referencing an attribute definition
AttributeReference attr = new AttributeReference(
new Point3d(0.0, 0.0, 0.0), "12345", "DDD", dwg.Database.Textstyle);
attr.TransformBy(Matrix3d.Displacement(new Vector3d(blkRef.Position.X,blkRef.Position.Y,blkRef.Position.Z)));
//Add the attribute to the blockreference
blkRef.AttributeCollection.AppendAttribute(attr);
tran.AddNewlyCreatedDBObject(attr, true);
tran.Commit();
}
}
#endregion
}
}