find attribute definition value in one shot

find attribute definition value in one shot

Anonymous
Not applicable
407 Views
1 Reply
Message 1 of 2

find attribute definition value in one shot

Anonymous
Not applicable

hi,

 

I have a block definition which has 15 attribute definitions in my drawing. I need to know one of the attribute definition value of a block reference. In order to do that I have to loop all the attrribute collection of my block reference and load the object by its ID . this way, I have a performance problem. is there an easy way to find a block's attdef in one shot?

 

----------------------------------

 

here is my method:

 

public String GetAttDefOfABlock(BlockReference br,String attributeName)
{
String returnValue = "";
foreach (ObjectId id in br.AttributeCollection)
{
AttributeReference attRef = (AttributeReference)HandleToBlockReference(id.Handle.ToString()); // this line loads the object to get its value
if (attRef.Tag.ToString() == attributeName)
{
returnValue = attRef.TextString;
break;
}
}
return returnValue;
}

 

attributedeffs.png

0 Likes
408 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant

Hi,

 

AFAIK, you can't get an attribute reference "in one shot". But I never notice any "performance issue" even with much more attributes than 15.

 

I don't know what doe's the HandleToBlockReference() method but it's perhaps the cause of the "performance issue".

 

Try this way (assuming the method is called from within a transaction):

 

 

        public string GetAttValue(BlockReference br, string attributeTag)
        {
            string returnValue = "";
            foreach (ObjectId id in br.AttributeCollection)
            {
                AttributeReference attRef = (AttributeReference)id.GetObject(OpenMode.ForRead);
                if (attRef.Tag == attributeTag)
                {
                    returnValue = attRef.TextString;
                    break;
                }
            }
            return returnValue;
        }

 

PS: the value you want has nothing to do with an attribute definition, it's the value of the attribute reference. An AttributeDefinition typically owns to a block definition (BlockTableRecord).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes