Code Red > .NET

Read a blocks attributes with C#

(1/3) > >>

sonny3g:
I need to be able to select a single block and read it's attributes.  I am using a structure to then pass those attribute values to other functions.  So far I have not been successful in reading the attributes and returning a string value.  Below is my code.  Any help is appreciated.

            using (Transaction rbTrans = db.TransactionManager.StartTransaction())
            {
                BlockReference bref = (BlockReference)rbTrans.GetObject(bed, OpenMode.ForRead);
                BlockTableRecord bdef = (BlockTableRecord)rbTrans.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
                if (bdef.HasAttributeDefinitions != true) return null;
                foreach (ObjectId id in bdef)
                    {
                    DBObject obj = (DBObject)rbTrans.GetObject(id, OpenMode.ForRead, true);
                    AttributeDefinition attdef = obj as AttributeDefinition;
                   
                    if ((attdef != null) && (!attdef.Constant))
                    {
                        if (attdef.Tag == "pos_Origin_Z")
                        {
                            structure.insPtZ = attdef.TextString;
                        }
                        else if ((attdef.Tag == "pos_Endpoint_Z"))
                        {
                            structure.endPtZ = attdef.TextString;
                        }
                        else if ((attdef.Tag == "prd_UL"))
                        {
                            structure.uLabel = attdef.TextString;
                        }
                        else if ((attdef.Tag == "prd_LP"))
                        {
                            structure.layPos = attdef.TextString;
                        }
                    }
                }
                structure.insPt = bref.Position;
                structure.blkName = ((BlockTableRecord)bref.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name;
                structure.lyrName = bref.Layer;
                structure.rotAngle = bref.Rotation;
               
                rbTrans.Dispose();

                return structure;
            }

gile:
You're collecting the the TextString values of the attribute definitions of some block definition, not the the TextString values of the attribute references of the selected block reference.
The first ones are the default values, the second ones the values entered while inserting a block reference or edited later. I guess you want to get the second ones.
If so, you have to iterate through the block reference's AttributeCollection which contains the AttributeReferences you're looking for.

sonny3g:
Thank you Giles for responding and pointing out the problem.  Your help is always appreciated.

That is correct, I need the text string values in the attributes.  But I also need to know the attribute tag name so I make sure I get the correct values for each of the other functions.

Would I replace AttributeDefinition  with AttributeReference?  And if so, would I still be able to get the attribute Tag?

Sonny

Atook:
Looks like  you can pull the Tag and TextString from the AttributeReference.

sonny3g:
Thanks Atook and Giles.  As usual, you both are very helpful!  Between the two of you and Norman Yuan, I was able to get this portion to work now. This is what I have and it is much more concise than my first try.  Structure is a separate function that will be used by other functions later.

 public BlkStrct ReadBlock(ObjectId bed)
        {
            using (Transaction rbTrans = db.TransactionManager.StartTransaction())
            {
                BlockReference bref = (BlockReference)rbTrans.GetObject(bed, OpenMode.ForRead);
                BlockTableRecord bdef = (BlockTableRecord)rbTrans.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead);
                if (bdef.HasAttributeDefinitions != true) return null;
                foreach (ObjectId id in bref.AttributeCollection)
                    {
                    AttributeReference attdef = (AttributeReference)rbTrans.GetObject(id, OpenMode.ForRead);
                    switch (attdef.Tag)
                    {
                        case "pos_Origin_Z":
                            structure.insPtZ = attdef.TextString;
                            break;
                        case "pos_Endpoint_Z":
                            structure.endPtZ = attdef.TextString;
                            break;
                        case "prd_UL":
                            structure.uLabel = attdef.TextString;
                            break;
                        case "prd_LP":
                            structure.layPos = attdef.TextString;
                            break;
                    }
                }
                structure.insPt = bref.Position;
                structure.blkName = ((BlockTableRecord)bref.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name;
                structure.lyrName = bref.Layer;
                structure.rotAngle = bref.Rotation;
               
                rbTrans.Dispose();

                return structure;
            }

        }

Navigation

[0] Message Index

[#] Next page

Go to full version