Community
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.
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(); }
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'~
[quote]
Thanks
but, i only need to upgrate one attribute.
It is posible?
[/quote]
That is exactly what the code I posted does.