.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Read attribute from the block

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
BestFriendCZ
410 Views, 2 Replies

Read attribute from the block

Hello,

i have problem read value from block in some DWG because they contains more same references. Do you have someone any experiences with this? Thanks for any advice

        public static string GetAttributeValue(string blockName, string atributeName)
        {
            List<string> errorList = new List<string>();
            try
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    var blkId = GetBlockId(db, blockName);
                    return ReadAttributeValue(blkId, db, tr, atributeName);
                }
            }
            catch (System.Exception ex)
            {
                errorList.Add($"Critical error at function GetAtributeValue  {ex.Message}");
                return String.Empty;
            }
            finally
            {
                if (errorList.Count > 0)
                {
                    throw new System.Exception(string.Join(Environment.NewLine, errorList));
                }
            }
        }

     public static ObjectId GetBlockId(Database db, string blockName)
        {
            ObjectId blkId = ObjectId.Null;

            if (db == null)
                return ObjectId.Null;
            if (string.IsNullOrWhiteSpace(blockName))
                return ObjectId.Null;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (bt.Has(blockName))
                    blkId = bt[blockName];
                tr.Commit();
            }
            return blkId;
        }


       public static string ReadAttributeValue(ObjectId blkId, Database db, Transaction tr, string attributeName)
        {
            try
            {
                if (blkId.IsNull == false)
                {
                    BlockTableRecord blk = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);


                   
                    var blkRefs = blk.GetBlockReferenceIds(true, true);
               
                    for (int i = 0; i < blkRefs.Count; i++)
                    {
                        BlockReference acBlkRef = (BlockReference)tr.GetObject(blkRefs[i], OpenMode.ForRead);
                        AttributeCollection attCol = acBlkRef.AttributeCollection;
                        foreach (ObjectId objID in attCol)
                        {
                            DBObject dbObj = tr.GetObject(objID, OpenMode.ForRead) as DBObject;
                            if (dbObj != null)
                            {
                                AttributeReference acAttRef = dbObj as AttributeReference;
                   
                                if (acAttRef.Tag.ToUpper() == attributeName.ToUpper())
                                {
                                    if (acAttRef.TextString != "0")
                                    {
                                        return acAttRef.TextString;
                                    }
                                }
                            }
                        }
                    }
                }
                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

 

This is how it looks in ACAD only one reference

 

inACAD.png 

 

This is how it looks in DB there are two values 60 and 90 and i don't know how to recognize this right

in code.png

...
2 REPLIES 2
Message 2 of 3

Hi,

 

>> This is how it looks in DB there are two values 60 and 90 and

>> i don't know how to recognize this right

That means you have 2 block insertions (blockreferences) in your dwg-file and those have the attribute set to different values.

I can't tell you how you find "your correct blockreference", how would you decide which one is the correct one in your drawing manually?

 

- alfred -

 

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2024
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 3 of 3

Yeah it looks like i don't have another choice. Thank you for your advice

...

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report