[CommandMethod("dewa")]
public static void DelAttribute()
{
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("IMP"))
{
ed.WriteMessage("\nBlock definition IMP does not exist");
return;
}
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt["IMP"], OpenMode.ForRead, false);
AttributeDefinition foundDef
= new AttributeDefinition
();
foreach (ObjectId id in btr)
{
DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead, false);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
if (attDef.Tag == "METRIC")
{
foundDef = attDef;
//break;
}
bt.UpgradeOpen();
btr.UpgradeOpen();
foundDef.UpgradeOpen();
foundDef.Erase();
btr.DowngradeOpen();
bt.DowngradeOpen();
}
}
tr.Commit();
ed.WriteMessage("\nPerform command \"ATTSYNC\" manually");
}
}
[CommandMethod("newa")]
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 PART does not exist");
return;
}
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt["PART"], OpenMode.ForRead, false);
// location of the AttributeDefinition in the
// block definition
Point3d ptloc
= new Point3d
(0,
2,
0); // create a AttributeDefinition
// specify the text,tag and prompt
string strvalue = "NEW VALUE ADDED";
string strtag = "MYTAG";
string strprompt = "Enter a new value";
// used current text style
AttributeDefinition attDef
= new AttributeDefinition
(ptloc, strvalue, strtag, strprompt, db
.Textstyle); attDef.Height = 0.12;
attDef.Layer = "0";
attDef.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 0);
attDef.LinetypeId = db.ContinuousLinetype;
// append the AttributeDefinition to the definition
btr.UpgradeOpen();
btr.AppendEntity(attDef);
tr.AddNewlyCreatedDBObject(attDef, true);
btr.DowngradeOpen();
tr.Commit();
}
}