将这一句中加入openerased的选项,这样对于已经删除的块就不会去遍历了。
AttributeReference attRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForRead,true);
/// <summary> /// 获取指定名称的块属性值 /// </summary> /// <param name="blockReferenceId">块参照的Id</param> /// <param name="attributeName">属性名</param> /// <returns>返回指定名称的块属性值</returns> public static string GetAttributeInBlockReference(this ObjectId blockReferenceId, string attributeName) { string attributeValue = string.Empty; // 属性值 Database db = blockReferenceId.Database; using (Transaction trans = db.TransactionManager.StartTransaction()) { // 获取块参照 BlockReference bref = (BlockReference)trans.GetObject(blockReferenceId, OpenMode.ForRead); // 遍历块参照的属性 foreach (ObjectId attId in bref.AttributeCollection) { // 获取块参照属性对象 AttributeReference attRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForRead,true); //判断属性名是否为指定的属性名 if (attRef.Tag.ToUpper() == attributeName.ToUpper()) { attributeValue = attRef.TextString;//获取属性值 break; } } trans.Commit(); } return attributeValue; //返回块属性值 }