Try this code, just you have to change block name,
new tag, prompt and default attribute value:
//using Autodesk.AutoCAD.Interop.Common;
//using Autodesk.AutoCAD.Interop;
// Add new attribute and synchronize all blockreferences
[CommandMethod("nea")]
public static void AddNewAtt()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);
if (!bt.Has("PART"))
{
ed.WriteMessage("\nBlock definition TEST does not exist");
return;
}
List<AttributeDefinition> attColl = new List<AttributeDefinition>();
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt["PART"], OpenMode.ForRead, false);
foreach (ObjectId adefId in btr)
{
DBObject attObj = (DBObject)tr.GetObject(adefId, OpenMode.ForRead, false);
if (attObj.GetRXClass().Name == "AcDbAttributeDefinition")
{
AttributeDefinition attObjDef = attObj as AttributeDefinition;
if (attObjDef != null)
{
attColl.Add(attObjDef);
}
}
}
AttributeDefinition[] atts = new AttributeDefinition[] { };
atts = attColl.ToArray();
//sort attribute location by X then by Y
Array.Sort(atts, (AttributeDefinition x, AttributeDefinition y) =>
((x.Position.Y.CompareTo(y.Position.Y)) +
(y.Position.X.CompareTo(x.Position.X))));
// get a location of the last AttributeDefinition in the
// block definition
AttributeDefinition lastAtt = atts[0];
Point3d Oldloc = lastAtt.Position;
double attTxtHeight = lastAtt.Height;
ed.WriteMessage("\n{0:f3}", attTxtHeight);
// calculate location of the new AttributeDefinition in the
// block definition
Point3d ptNewloc = new Point3d(Oldloc.X, Oldloc.Y - attTxtHeight * 1.97, 0) + btr.Origin.GetAsVector();
//// create a AttributeDefinition
// specify the text,tag and prompt
string strValue = "[ Enter a new value here ]";
string strTag = "[ Enter a new tag here ]";
string strPrompt = "[ Enter a new prompt here ]";
// used text style of the last attribute definition
AttributeDefinition attDef = new AttributeDefinition(ptNewloc, strValue, strTag, strPrompt, lastAtt.TextStyleId);
attDef.Height = 0.12;
attDef.Layer = "0";
attDef.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0);
attDef.LinetypeId = lastAtt.LinetypeId;
attDef.Height = lastAtt.Height;
attDef.TextStyleId = lastAtt.TextStyleId;
attDef.Annotative = lastAtt.Annotative;
attDef.Preset = lastAtt.Preset;
attDef.Invisible = lastAtt.Invisible;
attDef.Constant = lastAtt.Constant;
attDef.Justify = lastAtt.Justify;
attDef.AlignmentPoint = ptNewloc;
attDef.LockPositionInBlock = lastAtt.LockPositionInBlock;
attDef.AdjustAlignment(db);
// append the AttributeDefinition to the definition
btr.UpgradeOpen();
btr.AppendEntity(attDef);
tr.AddNewlyCreatedDBObject(attDef, true);
btr.DowngradeOpen();
tr.Commit();
Autodesk.AutoCAD.Interop.AcadDocument acdoc = (Autodesk.AutoCAD.Interop.AcadDocument)doc.AcadDocument;
AcadBlock acBlk = btr.AcadObject as AcadBlock;
acdoc.SendCommand("_.ATTSYNC _N " + acBlk.Name + "\n");
}
} Tested on A2010 only
~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919