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

Edit Attribute in a BLock Reference

5 REPLIES 5
Reply
Message 1 of 6
DUCALITO
1047 Views, 5 Replies

Edit Attribute in a BLock Reference

Hi all

 

I want to know, How edit Attributes in a Block reference.

 

Not all attributes, only one

 

 

 

Please forgive me, I dont speak Ingles

 

Thanks.

 

 

5 REPLIES 5
Message 2 of 6
mohnston
in reply to: DUCALITO

I use something like this:

 

        public static void SetAttributeValue(
            ObjectId BlockRefID, 
            string AttTagName, 
            string AttValue)
        {
            AttTagName = AttTagName.ToUpper();
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            DocumentLock dl = doc.LockDocument();
            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                BlockReference bref = (BlockReference)tr.GetObject(BlockRefID, OpenMode.ForRead);
                bref.RecordGraphicsModified(true);
                foreach (ObjectId attid in bref.AttributeCollection)
                {
                    AttributeReference attref = (AttributeReference)tr.GetObject(attid, OpenMode.ForRead);
                    if (attref.Tag == AttTagName)
                    {
                        bref.UpgradeOpen();
                        attref.UpgradeOpen();
                        attref.TextString = AttValue;
                        attref.Draw();
                        break;
                    }
                    attref.Dispose();
                }
                tr.Commit();
            }
            dl.Dispose();

        }

 

 

CAD Programming Solutions
Message 3 of 6
DUCALITO
in reply to: DUCALITO

Thanks

 

but, i only need to upgrate one attribute.

 

It is posible?

 

 

Message 4 of 6
AlexanderRivilis
in reply to: DUCALITO

 


@DUCALITO wrote:

Thanks

 

but, i only need to upgrate one attribute.

 

It is posible?

 

 


Yes. It is possible but you must select this attribute (for example, with hepl of Tag property).

 

 
Message 5 of 6
Hallex
in reply to: DUCALITO

You can use GetNestedEntity method for that

 

Try this code

 

        [CommandMethod("CAV")]
        public static void UpdateSingleAttribute()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Editor ed = acad.DocumentManager.MdiActiveDocument.Editor;

            Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)

            {

            try
            {

            PromptNestedEntityOptions pno =  new PromptNestedEntityOptions("\nSelect attribute >>");

            PromptNestedEntityResult nres =   ed.GetNestedEntity(pno);

            if (nres.Status != PromptStatus.OK)  return;

            ObjectId id = nres.ObjectId;

            Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);

            ed.WriteMessage("\nObject type selected\t{0}", ent.GetRXClass().DxfName);

            Point3d pnt = nres.PickedPoint;

            AttributeReference attref = ent as AttributeReference;

            if (attref == null) return;

            PromptStringOptions pso =
                            new PromptStringOptions("\nEnter new attribute value: ");

            pso.AllowSpaces = true;

            PromptResult res;

            res = ed.GetString(pso);

            if (res.Status != PromptStatus.OK)  return;

            string strvalue = res.StringResult;

            attref.UpgradeOpen();

            attref.TextString = strvalue;

            tr.Commit();
                }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {

                ed.WriteMessage("\n" + ex.StackTrace + "\n" + ex.Message);
            }
            finally 
            {

            }
        }
        }

 

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 6 of 6
mohnston
in reply to: DUCALITO

[quote]

Thanks

 

but, i only need to upgrate one attribute.

 

It is posible?

[/quote]

 

That is exactly what the code I posted does.

CAD Programming Solutions

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