#region "System Imports" using System; using System.Collections.Generic; using System.Text; #endregion #region "AutoCAD Imports" using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Colors; #endregion // import accoremgd.dll for A2013 [assembly: CommandClass(typeof(YourProjectName.AddExtraAttribute))] namespace YourProjectName { class AddExtraAttribute { [CommandMethod("nea", CommandFlags.Modal)] public static void AddNewAtt() { string blkName = "PART"; 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(blkName)) { ed.WriteMessage("\nBlock definition TEST does not exist"); return; } double dx = new double(); double dy = new double(); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[blkName], OpenMode.ForRead, false); int cnt = 0; AttributeDefinition matchAtt = new AttributeDefinition(); 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 (cnt == 0) { matchAtt = attObjDef; dx = matchAtt.Position.X; dy = matchAtt.Position.Y; cnt += 1; } if (attObjDef != null) { Point3d ip = attObjDef.Position; if ((attObjDef.Position.X >= dx) && (attObjDef.Position.Y <= dy)) { dx = attObjDef.Position.X; dy = attObjDef.Position.Y; } } } } // get a location of the last AttributeDefinition in the // block definition Point3d Oldloc = matchAtt.Position; double attTxtHeight = matchAtt.Height; // calculate location of the new AttributeDefinition in the // block definition Point3d ptNewloc = new Point3d(dx, dy - attTxtHeight * 1.97, btr.Origin.Z) + 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, matchAtt.TextStyleId); attDef.Height = matchAtt.Height; attDef.Layer = "0"; attDef.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0); attDef.LinetypeId = matchAtt.LinetypeId; attDef.Height = matchAtt.Height; attDef.TextStyleId = matchAtt.TextStyleId; attDef.Annotative = matchAtt.Annotative; attDef.Preset = matchAtt.Preset; attDef.Invisible = matchAtt.Invisible; attDef.Constant = matchAtt.Constant; attDef.Justify = matchAtt.Justify; attDef.AlignmentPoint = ptNewloc; attDef.LockPositionInBlock = matchAtt.LockPositionInBlock; attDef.AdjustAlignment(db); // append the AttributeDefinition to the definition btr.UpgradeOpen(); btr.AppendEntity(attDef); tr.AddNewlyCreatedDBObject(attDef, true); btr.DowngradeOpen(); tr.Commit(); ed.WriteMessage("nPerform \"_ATTSYNC _N " + blkName + "\" + command manually"); } } } }