using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace AutocadTestProject {
public class AutocadTestProxy : IExtensionApplication {
[CommandMethod("ProxyExplodeTest")]
public void ProxyExplode() {
var doc = Application.DocumentManager.MdiActiveDocument;
using (doc.LockDocument()) {
ProxyEntity proxy = null;
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.ForWrite) as BlockTableRecord) {
var proxy_id = ObjectId.Null;
foreach (var object_id in space) {
using (var obj = tr.GetObject(object_id, OpenMode.ForRead) as ProxyEntity) {
if (obj == null) continue;
proxy_id = object_id;
}
break;
}
if (proxy_id == ObjectId.Null) {
doc.Editor.WriteMessage("Не найдено ни одного прокси-объекта");
return;
}
proxy = tr.GetObject(proxy_id, OpenMode.ForWrite) as ProxyEntity;
}
}
if (proxy == null) {
doc.Editor.WriteMessage("Не удалось определить прокси-объект по id");
return;
}
var extents = GetProxyGeometricExtents(proxy, tr);
doc.Editor.WriteMessage(String.Format("Размеры прокси-объекта определились как {0}x{1}", extents.MaxPoint.X - extents.MinPoint.X, extents.MaxPoint.Y - extents.MinPoint.Y));
}
}
}
public Extents3d GetProxyGeometricExtents(ProxyEntity proxy, Transaction trn) {
var collection = new DBObjectCollection();
proxy.Explode(collection);
Extents3d? extents = null;
foreach (var obj in collection) {
var ent = obj as Entity;
if (ent == null || ent is DBText || ent is MText) continue;
try {
if (extents == null) {
extents = ent.GeometricExtents;
} else {
var mip1 = extents.Value.MinPoint;
var mip2 = ent.GeometricExtents.MinPoint;
var map1 = extents.Value.MaxPoint;
var map2 = ent.GeometricExtents.MaxPoint;
var pe1 = new Point3d(Math.Min(mip1.X, mip2.X), Math.Min(mip1.Y, mip2.Y), Math.Min(mip1.Z, mip2.Z));
var pe2 = new Point3d(Math.Max(map1.X, map2.X), Math.Max(map1.Y, map2.Y), Math.Max(map1.Z, map2.Z));
extents = new Extents3d(pe1, pe2); //extents.Value.AddExtents(ent.GeometricExtents);
}
} catch {}
}
return extents ?? new Extents3d();
}
public void Initialize() {
}
public void Terminate() {
}
}
}