using System;
using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
namespace AutocadTestProject {
public class AutocadTest : IExtensionApplication {
[CommandMethod("Проба1")]
public void Test1() {
var block_name = "PrintRamka";
var scale = 2;
var doc = Application.DocumentManager.MdiActiveDocument;
using (doc.LockDocument()) {
using (var tr = doc.TransactionManager.StartTransaction()) {
var db = doc.Database;
using (var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable) {
using (var space = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord) {
var block_id = ObjectId.Null;
foreach (var object_id in space) {
using (var obj = tr.GetObject(object_id, OpenMode.ForRead) as BlockReference) {
if (obj == null || !obj.IsDynamicBlock) continue;
using (var block_def = tr.GetObject(obj.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord) {
if (block_def.Name != block_name) continue;
}
}
block_id = object_id;
break;
}
if (block_id == ObjectId.Null) {
doc.Editor.WriteMessage("Не найден динамический блок " + block_name);
return;
}
using (var block = tr.GetObject(block_id, OpenMode.ForWrite) as BlockReference) {
if (block == null || !block.IsDynamicBlock) {
doc.Editor.WriteMessage("Найденный объект не является динамическим блоком");
return;
}
SetBlockProperty(block, "Формат", String.Format("A4"), tr);
SetBlockProperty(block, "Выбор A4", String.Format("A4x2"), tr);
if (scale > 1) {
foreach (var prop_name in new[] {"Высота", "Ширина"}) {
SetBlockProperty(block, prop_name, scale * GetBlockPropertyDouble(block, prop_name), tr);
}
}
}
}
}
}
}
}
private double GetBlockPropertyDouble(BlockReference block, string prop_name) {
return (from DynamicBlockReferenceProperty prop in block.DynamicBlockReferencePropertyCollection where String.Compare(prop.PropertyName, prop_name, StringComparison.InvariantCultureIgnoreCase) == 0 select (double) prop.Value).FirstOrDefault();
}
public void SetBlockProperty(BlockReference block, string prop_name, object value, Transaction trn = null) {
try {
foreach (DynamicBlockReferenceProperty prop in block.DynamicBlockReferencePropertyCollection) {
if (String.Compare(prop.PropertyName, prop_name, StringComparison.InvariantCultureIgnoreCase) != 0) continue;
prop.Value = value;
break;
}
} catch {
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(String.Format("Не удалось изменить свойство \"{0}\"", prop_name));
}
}
public void Initialize() {
}
public void Terminate() {
}
}
}