Edit attribute reference value of a block reference

Edit attribute reference value of a block reference

jptallano
Enthusiast Enthusiast
357 Views
3 Replies
Message 1 of 4

Edit attribute reference value of a block reference

jptallano
Enthusiast
Enthusiast

Hi,

Using code I found in this forum, I wrote a small method for setting the value of a given attribute reference in a block reference:

 

 

 

public static void SetBlockAttributeValue(ObjectId objectId, string attributeTag, string attributeValue)
{
    using (Transaction acTrans = application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
    {
        try
        {
            BlockTable bt = (BlockTable)acTrans.GetObject(application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
            BlockTableRecord btr = (BlockTableRecord)acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

            BlockReference block = (BlockReference)acTrans.GetObject(objectId, OpenMode.ForWrite);

            foreach (ObjectId attId in block.AttributeCollection)
            {
                AttributeReference attRef = (AttributeReference)acTrans.GetObject(attId, OpenMode.ForWrite);
                if (attRef.Tag.ToUpper() == attributeTag.ToUpper())
                {
                    attRef.TextString = attributeValue;
                    break;
                }
            }

            acTrans.Commit();
        }
        catch (System.Exception ex)
        {
            application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nException reading '" + attributeTag + "' on entity " + objectId + ": " + ex.Message);
        }
    }
}

 

 

 

Through debugging, I can see that the attribute reference's TextString value is set correctly. Problem is the value is not recorded and the attribute reference value stays the same, as if the Commit() didn't happen. What am I doing wrong?

0 Likes
Accepted solutions (1)
358 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

You should show the code which calls the SetBlockAttributeValue method.

If the calling code launch this method from within a Transaction which is not commited,  the nested transaction will be rolled back.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

jptallano
Enthusiast
Enthusiast

Forget it guys, the method works, the problem came from a mess of transactions outside of it. Sorry!

0 Likes
Message 4 of 4

jptallano
Enthusiast
Enthusiast
Yes, that was exactly the problem. 😄
0 Likes