.NET

Reply
Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 1 of 7 (268 Views)
Accepted Solution

AttributeReference TextString Not Updated

268 Views, 6 Replies
09-04-2013 07:22 AM

Hey all,

 

I'm back so soon with more problems! :smileytongue:

 

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

Valued Contributor
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 2 of 7 (254 Views)

Re: AttributeReference TextString Not Updated

09-04-2013 08:15 AM in reply to: Millerni456

You should use middle center.  Middle is still left.

Learning as I go
Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 3 of 7 (244 Views)

Re: AttributeReference TextString Not Updated

09-04-2013 08:52 AM 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.

Valued Contributor
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 4 of 7 (229 Views)

Re: AttributeReference TextString Not Updated

09-04-2013 11:14 AM in reply to: Millerni456

Did you remeber to use ATTSYNC to sync the attribute?

Learning as I go
Valued Contributor
Millerni456
Posts: 55
Registered: ‎01-23-2013
Message 5 of 7 (207 Views)

Re: AttributeReference TextString Not Updated

09-04-2013 01:53 PM 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?

 

Valued Contributor
jaboone
Posts: 91
Registered: ‎12-05-2011
Message 6 of 7 (202 Views)

Re: AttributeReference TextString Not Updated

09-04-2013 02:35 PM 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
Valued Mentor
AubelecBE
Posts: 361
Registered: ‎01-27-2010
Message 7 of 7 (183 Views)

Re: AttributeReference TextString Not Updated

09-05-2013 03:47 AM 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.

 

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community