jun353835273 发表于 2020-2-13 23:21:17

C#如何遍历块获取块,内部图元坐标点集的最大包围框

本帖最后由 jun353835273 于 2020-2-13 23:25 编辑

C#如何遍历块获取块,内部图元坐标点集的最大包围框

https://forums.autodesk.com/t5/net/bounding-boxes-around-blocks/td-p/3822317


public void testBlockBound()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

Database db = doc.Database;

Editor ed = doc.Editor;

//Matrix3d ucs = ed.CurrentUserCoordinateSystem;
BlockReference clon = null;

doc.TransactionManager.EnableGraphicsFlush(true);
try
{
PromptEntityOptions peo = new PromptEntityOptions("\nSelect block: ");

peo.SetRejectMessage("Only a block instance !");

peo.AddAllowedClass(typeof(BlockReference), false);

PromptEntityResult per = ed.GetEntity(peo);

if (per.Status != PromptStatus.OK) return;

ObjectId id = per.ObjectId;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);

if (ent == null) return;

BlockReference bref = ent as BlockReference;

if (bref == null) return;

clon = bref.Clone() as BlockReference;

clon.Rotation = 0;

clon.TransformBy(ed.CurrentUserCoordinateSystem);

tr.TransactionManager.QueueForGraphicsFlush();

Extents3d ext = clon.GeometryExtentsBestFit(ed.CurrentUserCoordinateSystem);

ext.TransformBy(ed.CurrentUserCoordinateSystem);

Polyline pl = new Polyline(4);

Point3d p1 = ext.MinPoint.TransformBy(ed.CurrentUserCoordinateSystem);

Point3d p3 = ext.MaxPoint.TransformBy(ed.CurrentUserCoordinateSystem);

Point3d p2 = new Point3d(p3.X, p1.Y, p1.Z).TransformBy(ed.CurrentUserCoordinateSystem);

Point3d p4 = new Point3d(p1.X, p3.Y, p1.Z).TransformBy(ed.CurrentUserCoordinateSystem);

pl.AddVertexAt(0, new Point2d(p1.X, p1.Y), 0.0, 0.0, 0.0);

pl.AddVertexAt(1, new Point2d(p2.X, p2.Y), 0.0, 0.0, 0.0);

pl.AddVertexAt(2, new Point2d(p3.X, p3.Y), 0.0, 0.0, 0.0);

pl.AddVertexAt(3, new Point2d(p4.X, p4.Y), 0.0, 0.0, 0.0);

pl.Closed = true;

pl.ColorIndex = 121;

pl.TransformBy(ed.CurrentUserCoordinateSystem);

Matrix3d rot = Matrix3d.Rotation(bref.Rotation, bref.Normal.GetNormal(), bref.Position);

pl.TransformBy(rot);

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(pl);

tr.AddNewlyCreatedDBObject(pl, true);

tr.TransactionManager.QueueForGraphicsFlush();

doc.TransactionManager.FlushGraphics();

tr.Commit();

}
}
catch (System.Exception e)
{
ed.WriteMessage("\nError: {0}\n{1}", e.Message, e.StackTrace);
}
finally
{
if (!clon.IsDisposed) clon.Dispose();//optional
}
}

zzyong00 发表于 2020-2-14 22:29:23

帮顶!。。。。。。

jun353835273 发表于 2020-2-18 09:58:08

自己顶一下,看了下书自己解决了
http://bbs.mjtd.com/thread-180919-1-1.html
页: [1]
查看完整版本: C#如何遍历块获取块,内部图元坐标点集的最大包围框