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
This is how it looks in DB there are two values 60 and 90 and i don't know how to recognize this right
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
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 -
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.