using System;
using System.Collections.Generic;
using cad = Autodesk.AutoCAD.ApplicationServices.Application;
using Ap = Autodesk.AutoCAD.ApplicationServices;
using Db = Autodesk.AutoCAD.DatabaseServices;
using Ed = Autodesk.AutoCAD.EditorInput;
using Gm = Autodesk.AutoCAD.Geometry;
using Rt = Autodesk.AutoCAD.Runtime;
[assembly: Rt.CommandClass(typeof(Bushman.Samples.TestClass))]
namespace Bushman.Samples {
public sealed class TestClass {
[Rt.CommandMethod("test", Rt.CommandFlags.Session)]
public static void Test() {
Ap.Document doc = cad.DocumentManager.MdiActiveDocument;
Db.Database db = doc.Database;
Ed.Editor ed = doc.Editor;
using (doc.LockDocument()) {
using (Db.Transaction tr = db.TransactionManager.StartTransaction()) {
using (Db.BlockTable bt = tr.GetObject(db.BlockTableId, Db.OpenMode.ForRead) as Db.BlockTable) {
const String blockName = "test_block";
const String attName = "MY_ATTRIBUTE";
if (!bt.Has(blockName)) {
ed.WriteMessage("Определение блока \"{0}\" не найдено.\n", blockName);
return;
}
List<Db.ObjectId> bRefIds = new List<Db.ObjectId>();
Db.ObjectId bDefId = bt[blockName];
Db.BlockTableRecord bDef = tr.GetObject(bDefId, Db.OpenMode.ForRead) as Db.BlockTableRecord;
foreach (Db.ObjectId id in bDef.GetBlockReferenceIds(false, true)) bRefIds.Add(id);
if (bDef.IsDynamicBlock) {
foreach (Db.ObjectId anDefId in bDef.GetAnonymousBlockIds()) {
using (Db.BlockTableRecord anDef = tr.GetObject(anDefId, Db.OpenMode.ForRead) as Db.BlockTableRecord)
foreach (Db.ObjectId id in anDef.GetBlockReferenceIds(false, true)) bRefIds.Add(id);
}
}
foreach (Db.ObjectId id in bRefIds) {
using (Db.BlockReference br = tr.GetObject(id, Db.OpenMode.ForRead) as Db.BlockReference)
foreach (Db.ObjectId attId in br.AttributeCollection) {
using (Db.AttributeReference ar = tr.GetObject(attId, Db.OpenMode.ForRead) as Db.AttributeReference)
if (String.Equals(ar.Tag, attName, StringComparison.CurrentCultureIgnoreCase)) {
ar.UpgradeOpen();
ed.WriteMessage("{0}\n", new String('*', 15));
ed.WriteMessage("TextString = {0}\n", ar.TextString);
ed.WriteMessage("getTextWithFieldCodes = {0}\n", ar.getTextWithFieldCodes());
ed.WriteMessage("меняем значение атрибута...\n");
ar.TextString = "Новое значение";
ed.WriteMessage("TextString = {0}\n", ar.TextString);
ed.WriteMessage("getTextWithFieldCodes = {0}\n\n", ar.getTextWithFieldCodes());
break;
}
}
}
}
tr.Commit();
}
}
}
}
}