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

AttributeReference TextString Not Updated

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Millerni456
1856 Views, 6 Replies

AttributeReference TextString Not Updated

Hey all,

 

I'm back so soon with more problems! 😛

 

I change a block's attributes programmatically using this helper function I created:

private void UpdateAttributes(Database database, Detail det)
        {
            using (Transaction tr = database.TransactionManager.StartTransaction())
            {   //Grab the BlockReference the detail refers to.
                BlockReference block = (BlockReference)tr.GetObject(det.ObjectId, OpenMode.ForRead);

                //Iterate over the Attribute ids.
                foreach (ObjectId attributeId in block.AttributeCollection)
                {   //Get the current AttributeReference.
                    AttributeReference acAttribute = (AttributeReference)tr.GetObject(attributeId, OpenMode.ForWrite);
                    //Find the matching attribute.
                    foreach (DetailAttribute tmpAttr in det.Attributes)
                    {
                        if (tmpAttr.TagString.Equals(acAttribute.Tag, StringComparison.OrdinalIgnoreCase))
                        {   //The attributes match, copy the value of the detail.
                            Editor ed = AcAp::Application.DocumentManager.MdiActiveDocument.Editor;
                            acAttribute.TextString = tmpAttr.TextString;
                        }
                    }
                }
                tr.Commit(); //Faster than commiting, no changes were made.
            }
        }

You can ignore the Detail and DetailAttribute objects as they are also user-defined.  Also note that this function expects the database to be currently open for working on.

 

Everything in this function works fine, but there is a strange quirk that becomes noticeable only when I use this function to edit an attribute that has its "Invisible" property set to "No".

Below, is the original block that is edited via this function:

Original Block

As you can see, the block has a number with 4 digits (for our purposes, call this the Block Number).

Next, is the block after it is the function executes, and the attribute is changes:

Edited Block

In this picture, the Block Number was changed so that it did not include the leading zero.

However, notice that the the Block Number attribute remained in the same exact position!

The position is measured from the bottom-left corner of the text, and the attribute is justified to "Middle"

 

In addition to the attribute's text being justified to middle, the "Lock Position" property is set to "No"

 

The text will auto-correct its position, however, if I move the block to a new location.  Or if I manually type in the Block Number (changing it to something else, then changing to "210").

 

Also, this is not a REGEN issue, as this command will not fix the location.

So, in short,  I do not believe this is an error with the block's settings.  It appears something internal is happening or perhaps I need to do more inside my code.

 

Also, I did attempt to use one of AttributeReference's 2 methods:

AttributeReference.UpdateMTextAttribute

 However, this resulted in an exception (my assumption is because my drawing is being modified in-memory and not in the AutoCAD editor.

 

 

If you've got any advice, I'm up for it!

 

Thanks much,

-Nicholas

6 REPLIES 6
Message 2 of 7
jaboone
in reply to: Millerni456

You should use middle center.  Middle is still left.

Learning as I go
Message 3 of 7
Millerni456
in reply to: jaboone

Thanks for the reply jabone. But unfortunately Middle Center is still left as well. I went ahead and changed the block definition and the problem still persists.

 

The alignment does NOT seem to be the issue, it is only an effect of the real problem. The problem is that the text is not re-applying it's preset alignment.

Message 4 of 7
jaboone
in reply to: Millerni456

Did you remeber to use ATTSYNC to sync the attribute?

Learning as I go
Message 5 of 7
Millerni456
in reply to: jaboone

ATTSYNC seems to do the trick.  Although I'm not too sure what it does.

 

It appeared to me that when I use ATTSYNC on the block,  all attribute properties are restored to that in the original block definition and the TextStrings (or attribute values) are NOT removed.

 

I did find the documentation on this command:

ATTSYNC Documentation

 

Must my drawing be open in the AutoCAD editor to use this?

 

Message 6 of 7
jaboone
in reply to: Millerni456

Yes there are drawbacks to useing ATTSYNC but it is the ONLY way to get attributes to update after a block has been inserted in a drawing.  I guess I should have mentioned that.  I don't like it either but we do what we have to do.

Learning as I go
Message 7 of 7
AubelecBE
in reply to: jaboone

Hi, for me the best way to do this it to insert a new bloc with all attributes and delete the previous block.

 

if you change a def of Block, you need to check all block already inserted.

 

 

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost