[CommandMethod("TestAtr", CommandFlags.Modal)]
public static void TestAtr()
{
const string file = @"c:\temp\testAtr.dwg";
using (var db = new Database(false, true))
{
db.ReadDwgFile(file, FileShare.Read, false, "");
db.CloseInput(true);
using (var t = db.TransactionManager.StartTransaction())
{
SetAtrValue(db, "TESTTAG", "TestValue");
t.Commit();
}
db.SaveAs(file, DwgVersion.Current);
}
}
private static void SetAtrValue(Database db, string tag, string value)
{
var ms = (BlockTableRecord) SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject(OpenMode.ForRead);
foreach (var id in ms)
{
var blRef = id.GetObject(OpenMode.ForRead, false, true) as BlockReference;
if (blRef == null) continue;
foreach (ObjectId atrId in blRef.AttributeCollection)
{
var atrRef = (AttributeReference) atrId.GetObject(OpenMode.ForRead, false, true);
if (string.Equals(atrRef.Tag, tag, StringComparison.OrdinalIgnoreCase))
{
atrRef.UpgradeOpen();
atrRef.TextString = value;
atrRef.AdjustAlignment(db);
}
}
}
}