using System;
using System.Runtime.InteropServices;
using System.Reflection;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcEd = Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(Rivilis.AttributeUpdater))]
namespace Rivilis
{
public class AttributeUpdater
{
[CommandMethod("UpdAttrs")]
public void UpdAttrs()
{
double aHeight = 2.5;
string aPrjName = "Мой Проект";
string aDwgName = "Мой Чертеж";
AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
AcEd.Editor ed = doc.Editor;
AcDb.Database db = doc.Database;
using (AcDb.Transaction tr = doc.TransactionManager.StartTransaction())
{
//////////////////////////////////////////////////////////////////////////
// Проходим по всем листам чертежа. Нам попадется и модель и мы можем
// обработать ее отдельно
//////////////////////////////////////////////////////////////////////////
AcDb.DBDictionary dict =
tr.GetObject(db.LayoutDictionaryId, AcDb.OpenMode.ForRead) as AcDb.DBDictionary;
foreach (AcDb.DBDictionaryEntry eDict in dict)
{
AcDb.Layout layout = tr.GetObject(eDict.Value, AcDb.OpenMode.ForRead) as AcDb.Layout;
// Если Пространство Модели нас не интересует - раскоментарь следующую строку
// if (layout.LayoutName == AcDb.BlockTableRecord.ModelSpace) continue;
AcDb.BlockTableRecord btrSpace =
tr.GetObject(layout.BlockTableRecordId, AcDb.OpenMode.ForRead) as AcDb.BlockTableRecord;
foreach (AcDb.ObjectId id in btrSpace)
{
if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AcDb.BlockReference))))
{
AcDb.BlockReference bRef =
tr.GetObject(id, AcDb.OpenMode.ForRead) as AcDb.BlockReference;
AcDb.ObjectId idBtr;
if (bRef.IsDynamicBlock) idBtr = bRef.DynamicBlockTableRecord;
else idBtr = bRef.BlockTableRecord;
AcDb.BlockTableRecord bBtr =
tr.GetObject(idBtr, AcDb.OpenMode.ForRead) as AcDb.BlockTableRecord;
// Если это не наш блок, то нам и делать нечего...
if (bBtr.Name.ToUpper() != "SHTAMP") continue;
AcDb.AttributeCollection attrs = bRef.AttributeCollection;
foreach (AcDb.ObjectId idAttr in attrs)
{
AcDb.AttributeReference attr =
tr.GetObject(idAttr, AcDb.OpenMode.ForRead) as AcDb.AttributeReference;
switch (attr.Tag.ToUpper())
{
case "PRJNAME":
attr.UpgradeOpen();
attr.Height = aHeight;
attr.TextString = aPrjName;
break;
case "DWGNAME":
attr.UpgradeOpen(); // Этой строки не хватало в первом варианте
attr.Height = aHeight;
attr.TextString = aDwgName;
break;
}
}
}
}
}
tr.Commit();
}
}
}
}