明经CAD社区

 找回密码
 注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 109|回复: 2

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

[复制链接]
发表于 2020-2-13 23:21 | 显示全部楼层 |阅读模式
本帖最后由 jun353835273 于 2020-2-13 23:25 编辑

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

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

  1. [CommandMethod("BBO")]
  2. public void testBlockBound()
  3. {
  4. Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

  5. Database db = doc.Database;

  6. Editor ed = doc.Editor;

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

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

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

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

  15. PromptEntityResult per = ed.GetEntity(peo);

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

  17. ObjectId id = per.ObjectId;

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

  21. if (ent == null) return;

  22. BlockReference bref = ent as BlockReference;

  23. if (bref == null) return;

  24. clon = bref.Clone() as BlockReference;

  25. clon.Rotation = 0;

  26. clon.TransformBy(ed.CurrentUserCoordinateSystem);

  27. tr.TransactionManager.QueueForGraphicsFlush();

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

  29. ext.TransformBy(ed.CurrentUserCoordinateSystem);

  30. Polyline pl = new Polyline(4);

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

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

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

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

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

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

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

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

  39. pl.Closed = true;

  40. pl.ColorIndex = 121;

  41. pl.TransformBy(ed.CurrentUserCoordinateSystem);

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

  43. pl.TransformBy(rot);

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

  45. btr.AppendEntity(pl);

  46. tr.AddNewlyCreatedDBObject(pl, true);

  47. tr.TransactionManager.QueueForGraphicsFlush();

  48. doc.TransactionManager.FlushGraphics();

  49. tr.Commit();

  50. }
  51. }
  52. catch (System.Exception e)
  53. {
  54. ed.WriteMessage("\nError: {0}\n{1}", e.Message, e.StackTrace);
  55. }
  56. finally
  57. {
  58. if (!clon.IsDisposed) clon.Dispose();//optional
  59. }
  60. }

 楼主| 发表于 2020-2-18 09:58 | 显示全部楼层
自己顶一下,看了下书自己解决了
http://bbs.mjtd.com/thread-180919-1-1.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号  
©2000-2017 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2020-2-20 14:14 , Processed in 0.174567 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表