[CommandMethod("vost_otm_ur")]
public void ustanov_mashtab()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
TypedValue[] filterlist = new TypedValue[1];
filterlist[0] = new TypedValue(0, "INSERT");
//filterlist[1] = new TypedValue(2, "отметка уровня");
SelectionFilter filter = new SelectionFilter(filterlist);
PromptSelectionResult selRes = ed.SelectAll(filter);
// если произошла ошибка - сообщаем о ней
if (selRes.Status != PromptStatus.OK)
{
ed.WriteMessage("\nОшибка!\n");
return;
}
ObjectId[] ids = selRes.Value.GetObjectIds();
using (DocumentLock docLock = doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectContextManager ocm = db.ObjectContextManager;
ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
if (ocm == null)
return;
/////////////
foreach (ObjectId id in ids)
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
if(ent.GetType() == typeof(BlockReference))
{
BlockReference brr = (BlockReference)ent;
BlockTableRecord btrr = tr.GetObject(brr.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
if (btrr.Name == "отметка уровня")
{
BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForWrite);
BlockTableRecord btr = tr.GetObject(br.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
AttributeDefinition atr_def = new AttributeDefinition();
if (!br.HasContext(occ.GetContext("1:1")))
{
br.AddContext(occ.GetContext("1:1"));
}
foreach (var idEnt in btr)
{
Entity en = tr.GetObject(idEnt, OpenMode.ForRead) as Entity;
if (en.GetType() == typeof(AttributeDefinition))
{
AttributeDefinition atr = tr.GetObject(idEnt, OpenMode.ForWrite, false, true) as AttributeDefinition;
atr_def = atr;
}
}
AnnotationScale currentScale = db.Cannoscale;
foreach (ObjectContext oc in occ)
{
if (br.HasContext(oc))
{
AnnotationScale annoScale = oc as AnnotationScale;
db.Cannoscale = annoScale;
foreach (ObjectId idAtrRef in br.AttributeCollection)
{
AttributeReference atr_ref = tr.GetObject(idAtrRef, OpenMode.ForWrite) as AttributeReference;
if (atr_ref.AlignmentPoint != null)
{
atr_ref.AlignmentPoint = atr_def.AlignmentPoint.TransformBy(br.BlockTransform);
}
if (atr_ref.IsMTextAttribute)
{
atr_ref.UpdateMTextAttribute();
}
}
}
}
db.Cannoscale = currentScale;
}
}
}
tr.Commit();
}
}
MessageBox.Show("Выполнено!");
} //public void ustanov_mashtab()