明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 318|回复: 2

[选集] 交互类的封装

[复制链接]
发表于 7 天前 | 显示全部楼层 |阅读模式
本帖最后由 wang2006zhi 于 2025-2-14 12:30 编辑
  1. using HTJ.Arx.Service.Imp;

  2. namespace HTJ.Arx;

  3. public static class EditEx
  4. {
  5.     /// <summary>
  6.     ///     以单点(中心点)构造选择点集
  7.     /// </summary>
  8.     /// <param name="cenpt">中心单点3D</param>
  9.     /// <param name="ex">偏移量</param>
  10.     /// <returns>点集合</returns>
  11.     public static Point3dCollection GetPointCollection(this Point3d cenpt, double ex = 1e-3)
  12.     {
  13.         return new Point3dCollection
  14.         {
  15.             new Point3d(cenpt.X - 0.5 * ex, cenpt.Y - 0.5 * ex, 0),
  16.             new Point3d(cenpt.X + 0.5 * ex, cenpt.Y - 0.5 * ex, 0),
  17.             new Point3d(cenpt.X + 0.5 * ex, cenpt.Y + 0.5 * ex, 0),
  18.             new Point3d(cenpt.X - 0.5 * ex, cenpt.Y + 0.5 * ex, 0)
  19.         };
  20.     }

  21.     /// <summary>
  22.     ///     以单点(中心点)构造选择点集
  23.     /// </summary>
  24.     /// <param name="cenpt">中心单点2D</param>
  25.     /// <param name="ex">偏移量</param>
  26.     /// <returns>点集合</returns>
  27.     public static Point3dCollection GetPointCollection(this Point2d cenpt, double ex = 1e-3)
  28.     {
  29.         return GetPointCollection(cenpt.Point3d(), ex);
  30.     }


  31.     /// <summary>
  32.     ///     单点(左下角)构造选择点集
  33.     /// </summary>
  34.     /// <param name="minpt">二维点</param>
  35.     /// <param name="width">宽度</param>
  36.     /// <param name="height">高度</param>
  37.     /// <returns>四个角点组成的点集</returns>
  38.     public static Point3dCollection GetPointCollection(this Point2d minpt, double width, double height)
  39.     {
  40.         return new Point3dCollection
  41.         {
  42.             new Point3d(minpt.X, minpt.Y, 0),
  43.             new Point3d(minpt.X + width, minpt.Y, 0),
  44.             new Point3d(minpt.X + width, minpt.Y + height, 0),
  45.             new Point3d(minpt.X, minpt.Y + height, 0)
  46.         };
  47.     }

  48.     /// <summary>
  49.     ///     单点(左下角)构造选择点集
  50.     /// </summary>
  51.     /// <param name="minPt">三维点</param>
  52.     /// <param name="width">宽度</param>
  53.     /// <param name="height">高度</param>
  54.     /// <returns>四个角点组成的点集</returns>
  55.     public static Point3dCollection GetPointCollection(this Point3d minPt, double width, double height)
  56.     {
  57.         var pt2d = new Point2d(minPt.X, minPt.Y);
  58.         return GetPointCollection(pt2d, width, height);
  59.     }

  60.     /// <summary>
  61.     ///     两点构造选择点集
  62.     /// </summary>
  63.     /// <param name="ptA">三维点</param>
  64.     /// <param name="ptB">三维点</param>
  65.     /// <param name="ex">外扩距离</param>
  66.     /// <returns>四个角点组成的点集</returns>
  67.     public static Point3dCollection GetPointCollection(this Point3d ptA, Point3d ptB, double ex = 1e-3)
  68.     {
  69.         var minX = Math.Min(ptA.X, ptB.X) - ex;
  70.         var minY = Math.Min(ptA.Y, ptB.Y) - ex;
  71.         var maxX = Math.Max(ptA.X, ptB.X) + ex;
  72.         var maxY = Math.Max(ptA.Y, ptB.Y) + ex;

  73.         return new Point3dCollection
  74.         {
  75.             new Point3d(minX, minY, 0),
  76.             new Point3d(maxX, minY, 0),
  77.             new Point3d(maxX, maxY, 0),
  78.             new Point3d(minX, maxY, 0)
  79.         };
  80.     }

  81.     /// <summary>
  82.     ///     两点构造选择点集
  83.     /// </summary>
  84.     /// <param name="ptA">二维点</param>
  85.     /// <param name="ptB">二维点</param>
  86.     /// <param name="ex">外扩距离</param>
  87.     /// <returns>四个角点组成的点集</returns>
  88.     public static Point3dCollection GetPointCollection(this Point2d ptA, Point2d ptB, double ex = 1e-3)
  89.     {
  90.         return ptA.Point3d().GetPointCollection(ptB.Point3d(), ex);
  91.     }

  92.     /// <summary>
  93.     ///     外包构造选择点集
  94.     /// </summary>
  95.     /// <param name="box"></param>
  96.     /// <param name="ex"></param>
  97.     /// <returns></returns>
  98.     public static Point3dCollection GetPointCollection(this BoundingInfo box, double ex = 1e-3)
  99.     {
  100.         var ext3d = box.Extents3d;
  101.         var pt1 = ext3d.MinPoint;
  102.         var pt2 = ext3d.MaxPoint;
  103.         return pt1.GetPointCollection(pt2, ex);
  104.     }

  105.     /// <summary>
  106.     ///     外包构造选择点集
  107.     /// </summary>
  108.     /// <param name="ext3d"></param>
  109.     /// <param name="ex"></param>
  110.     /// <returns></returns>
  111.     public static Point3dCollection GetPointCollection(this Extents3d ext3d, double ex = 1e-3)
  112.     {
  113.         var pt1 = ext3d.MinPoint;
  114.         var pt2 = ext3d.MaxPoint;
  115.         return pt1.GetPointCollection(pt2, ex);
  116.     }

  117.     /// <summary>
  118.     ///     Rect造选择点集
  119.     /// </summary>
  120.     /// <param name="rect"></param>
  121.     /// <param name="ex"></param>
  122.     /// <returns></returns>
  123.     public static Point3dCollection GetPointCollection(this Rect rect, double ex = 1e-3)
  124.     {
  125.         return rect.MinPoint.GetPointCollection(rect.MaxPoint, ex);
  126.     }

  127.     public static Point3dCollection GetPointCollection(this RectAngEx.RectAng rectAng, double ex = 1e-3)
  128.     {
  129.         var newRectAng = rectAng.Expand(ex);
  130.         var pts = newRectAng.ToPoints();
  131.         var nptc = new Point3dCollection();
  132.         for (var i = pts.Length - 1; i >= 0; i--)
  133.         {
  134.             var pt = pts[i].Point3d();
  135.             nptc.Add(pt);
  136.         }

  137.         return nptc;
  138.     }

  139.     /// <summary>
  140.     ///     获取矩阵变换(平移,缩放,旋转)后的点集
  141.     /// </summary>
  142.     /// <param name="ptColl">原始点集</param>
  143.     /// <param name="mat">矩阵变量</param>
  144.     /// <returns>返回变换后的点集</returns>
  145.     public static Point3dCollection TransformBy(this Point3dCollection ptColl, Matrix3d mat)
  146.     {
  147.         var nptc = new Point3dCollection();
  148.         for (var i = ptColl.Count - 1; i >= 0; i--)
  149.         {
  150.             var pt = ptColl[i].TransformBy(mat);
  151.             nptc.Add(pt);
  152.         }

  153.         return nptc;
  154.     }

  155.     /// <summary>
  156.     ///     获取一个字符串
  157.     /// </summary>
  158.     /// <param name="ed"></param>
  159.     /// <param name="str"></param>
  160.     /// <param name="msg"></param>
  161.     /// <returns></returns>
  162.     public static bool GetString(this Editor ed, out string str, string msg = "输入一个字符串:<空格退出>")
  163.     {
  164.         str = string.Empty;
  165.         var pso = new PromptStringOptions(Environment.NewLine + msg)
  166.         {
  167.             AllowSpaces = true //允许空格
  168.         };
  169.         var pdr = ed.GetString(pso);
  170.         if (pdr.Status != PromptStatus.OK)
  171.             return false;
  172.         str = pdr.StringResult;
  173.         return true;
  174.     }

  175.     /// <summary>
  176.     ///     获取一个实数
  177.     /// </summary>
  178.     /// <param name="ed"></param>
  179.     /// <param name="value"></param>
  180.     /// <param name="msg"></param>
  181.     /// <returns></returns>
  182.     public static bool GetDouble(this Editor ed, out double value, string msg = "输入一个实数:<空格退出>")
  183.     {
  184.         value = 0.0;
  185.         var pdo = new PromptDoubleOptions(Environment.NewLine + msg)
  186.         {
  187.             AllowArbitraryInput = true, //任意输入
  188.             AllowNone = true //允许回车
  189.         };
  190.         var pdr = ed.GetDouble(pdo);
  191.         if (pdr.Status != PromptStatus.OK)
  192.             return false;
  193.         value = pdr.Value;
  194.         return true;
  195.     }

  196.     /// <summary>
  197.     ///     获取下一个点
  198.     /// </summary>
  199.     /// <param name="ed">命令行</param>
  200.     /// <param name="pt">获取的点</param>
  201.     /// <param name="msg">提示</param>
  202.     /// <returns></returns>
  203.     public static bool GetPoint(this Editor ed, out Point3d pt, string msg = "指定一点:<空格退出>")
  204.     {
  205.         pt = new Point3d();
  206.         var ppo = new PromptPointOptions(Environment.NewLine + msg)
  207.         {
  208.             AllowArbitraryInput = true, //任意输入
  209.             AllowNone = true //允许回车
  210.         };
  211.         var ppr = ed.GetPoint(ppo);
  212.         if (ppr.Status != PromptStatus.OK)
  213.             return false;
  214.         pt = ppr.Value;
  215.         return true;
  216.     }

  217.     /// <summary>
  218.     ///     获取下一个点
  219.     /// </summary>
  220.     /// <param name="ed">命令行</param>
  221.     /// <param name="pt">获取的点</param>
  222.     /// <param name="ptk">上一个点</param>
  223.     /// <param name="msg">提示</param>
  224.     /// <returns></returns>
  225.     public static bool GetPoint(this Editor ed, out Point3d pt, Point3d ptk, string msg = "指定一点:<空格退出>")
  226.     {
  227.         pt = new Point3d();
  228.         var ppr = ed.GetPoint(msg, ptk);
  229.         if (ppr.Status != PromptStatus.OK)
  230.             return false;
  231.         pt = ppr.Value;
  232.         return true;
  233.     }

  234.     /// <summary>
  235.     ///     屏幕拾取实体列表
  236.     /// </summary>
  237.     /// <typeparam name="T">实体类型</typeparam>
  238.     /// <param name="ed">命令行</param>
  239.     /// <param name="ents">指定类型的List</param>
  240.     /// <param name="msg">提示</param>
  241.     /// <param name="fil">过滤条件,为空时候无条件</param>
  242.     /// <returns>bool</returns>
  243.     public static bool GetEnts<T>(this Editor ed,
  244.         out List<T> ents,
  245.         SelectionFilter? fil = null,
  246.         string msg = "请选择对象:") where T : Entity
  247.     {
  248.         ents = new List<T>();
  249.         var pso = new PromptSelectionOptions
  250.         {
  251.             MessageForAdding = Environment.NewLine + msg
  252.         };
  253.         var psr = ed.GetSelection(pso, fil);
  254.         if (psr.Status != PromptStatus.OK)
  255.             return false;
  256.         ents = psr.Value.GetEntities<T>().ToList();
  257.         return ents.Any();
  258.     }

  259.     /// <summary>
  260.     ///     窗选模式获取取实体列表
  261.     /// </summary>
  262.     /// <typeparam name="T">实体类型</typeparam>
  263.     /// <param name="ed">命令行</param>
  264.     /// <param name="ents">指定类型的List</param>
  265.     /// <param name="ptColl">点集合,为空时候全选模式</param>
  266.     /// <param name="fil">过滤条件,为空时候无条件</param>
  267.     /// <returns>bool</returns>
  268.     public static bool SelEnts<T>(this Editor ed,
  269.         out List<T> ents,
  270.         Point3dCollection? ptColl = null,
  271.         SelectionFilter? fil = null) where T : Entity
  272.     {
  273.         var psr = ptColl != null ? ed.SelectCrossingPolygon(ptColl, fil) : ed.SelectAll(fil);
  274.         ents = new List<T>();
  275.         if (psr.Status != PromptStatus.OK)
  276.             return false;
  277.         ents = psr.Value.GetEntities<T>().ToList();
  278.         return ents.Count>0;
  279.     }

  280.     /// <summary>
  281.     ///     点选获取单个实体
  282.     /// </summary>
  283.     /// <typeparam name="T">实体类型</typeparam>
  284.     /// <param name="ed">命令行</param>
  285.     /// <param name="t">实体</param>
  286.     /// <param name="pt">返回点</param>
  287.     /// <param name="isNested">块内</param>
  288.     /// <param name="msg">提示</param>
  289.     /// <returns>bool</returns>
  290.     public static bool SelEnt<T>(this Editor ed,
  291.         out T t,
  292.         out Point3d pt,
  293.         bool isNested = false,
  294.         string msg = "请选择对象:") where T : Entity
  295.     {
  296.         t = null!;
  297.         ObjectId id;
  298.         if (isNested)
  299.         {
  300.             var pner = ed.GetNestedEntity(Environment.NewLine + msg);
  301.             pt = pner.PickedPoint;
  302.             if (pner.Status != PromptStatus.OK)
  303.                 return false;
  304.             id = pner.ObjectId;
  305.         }
  306.         else
  307.         {
  308.             var per = ed.GetEntity(Environment.NewLine + msg);
  309.             pt = per.PickedPoint;
  310.             if (per.Status != PromptStatus.OK)
  311.                 return false;
  312.             id = per.ObjectId;
  313.         }

  314.         if (id.GetObject<Entity>() is not T result)
  315.             return false;
  316.         t = result;
  317.         return true;

  318.     }

  319.     /// <summary>
  320.     ///     点选获取单个实体
  321.     /// </summary>
  322.     /// <typeparam name="T">实体类型</typeparam>
  323.     /// <param name="ed">命令行</param>
  324.     /// <param name="t">实体</param>
  325.     /// <param name="isNested">块内</param>
  326.     /// <param name="msg">提示</param>
  327.     /// <returns>bool</returns>
  328.     public static bool SelEnt<T>(this Editor ed,
  329.         out T t,
  330.         bool isNested = false,
  331.         string msg = "请选择对象:")
  332.         where T : Entity
  333.     {
  334.         t = null!;
  335.         ObjectId id;
  336.         if (isNested)
  337.         {
  338.             var pner = ed.GetNestedEntity(Environment.NewLine + msg);
  339.             if (pner.Status != PromptStatus.OK)
  340.                 return false;
  341.             id = pner.ObjectId;
  342.         }
  343.         else
  344.         {
  345.             var per = ed.GetEntity(Environment.NewLine + msg);
  346.             if (per.Status != PromptStatus.OK)
  347.                 return false;
  348.             id = per.ObjectId;
  349.         }

  350.         if (id.GetObject<T>() is not { } result)
  351.             return false;
  352.         t = result;
  353.         return true;
  354.     }

  355.    

  356.     /// <summary>
  357.     ///     点选获取单个实体Id
  358.     /// </summary>
  359.     /// <param name="ed"></param>
  360.     /// <param name="id"></param>
  361.     /// <param name="pt">返回点</param>
  362.     /// <param name="tClass">RXClass类型</param>
  363.     /// <param name="isNested">块内</param>
  364.     /// <param name="msg"></param>
  365.     /// <returns></returns>
  366.     public static bool SelId(this Editor ed,
  367.         out ObjectId id,
  368.         out Point3d pt,
  369.         RXClass? tClass = null,
  370.         bool isNested = false,
  371.         string msg = "请选择对象:")
  372.     {
  373.         id = ObjectId.Null;
  374.         pt = new Point3d();
  375.         if (isNested)
  376.         {
  377.             var pner = ed.GetNestedEntity(Environment.NewLine + msg);
  378.             if (pner.Status != PromptStatus.OK)
  379.                 return false;
  380.             id = pner.ObjectId;
  381.             pt = pner.PickedPoint;
  382.         }
  383.         else
  384.         {
  385.             var per = ed.GetEntity(Environment.NewLine + msg);
  386.             if (per.Status != PromptStatus.OK) return false;
  387.             id = per.ObjectId;
  388.             pt = per.PickedPoint;
  389.         }

  390.         return tClass == null || id.IsDerivedFrom(tClass);
  391.     }

  392.     /// <summary>
  393.     ///     点选获取单个实体Id
  394.     /// </summary>
  395.     /// <param name="ed"></param>
  396.     /// <param name="id"></param>
  397.     /// <param name="tClass">RXClass类型</param>
  398.     /// <param name="isNested">块内</param>
  399.     /// <param name="msg"></param>
  400.     /// <returns></returns>
  401.     public static bool SelId(this Editor ed,
  402.         out ObjectId id,
  403.         RXClass? tClass = null,
  404.         bool isNested = false,
  405.         string msg = "请选择对象:")
  406.     {
  407.         id = ObjectId.Null;
  408.         if (isNested)
  409.         {
  410.             var pner = ed.GetNestedEntity(Environment.NewLine + msg);
  411.             if (pner.Status != PromptStatus.OK) return false;
  412.             id = pner.ObjectId;
  413.         }
  414.         else
  415.         {
  416.             var per = ed.GetEntity(Environment.NewLine + msg);
  417.             if (per.Status != PromptStatus.OK) return false;
  418.             id = per.ObjectId;
  419.         }

  420.         return tClass == null || id.IsDerivedFrom(tClass);
  421.     }


  422.     /// <summary>
  423.     ///     窗选模式获取取实体列表
  424.     /// </summary>
  425.     /// <param name="ed">命令行</param>
  426.     /// <param name="ids">指定类型的List</param>
  427.     /// <param name="tClass">RXClass类型</param>
  428.     /// <param name="ptc">点集合,为空时候全选模式</param>
  429.     /// <param name="fil">过滤条件,为空时候无条件</param>
  430.     /// <returns>bool</returns>
  431.     public static bool SelIds(this Editor ed,
  432.         out List<ObjectId> ids,
  433.         RXClass? tClass = null,
  434.         Point3dCollection? ptc = null,
  435.         SelectionFilter? fil = null)
  436.     {
  437.         var psr = ptc != null ? ed.SelectCrossingPolygon(ptc, fil) : ed.SelectAll(fil);
  438.         ids = new List<ObjectId>();
  439.         if (psr.Status != PromptStatus.OK)
  440.             return false;
  441.         ids = psr.Value.GetObjectIds().ToList();
  442.         if (tClass != null)
  443.             ids = ids.Where(id => id.IsDerivedFrom(tClass)).ToList();
  444.         return ids.Count>0;
  445.     }
  446. }


评分

参与人数 1明经币 +1 收起 理由
Bao_lai + 1 神马都是浮云

查看全部评分

回复

使用道具 举报

发表于 7 天前 | 显示全部楼层
要得,感谢分享~
回复 支持 反对

使用道具 举报

发表于 4 天前 | 显示全部楼层
本帖最后由 箭头_Row 于 2025-2-17 22:38 编辑

儘量不要用PointColliction的容器,這個容器得手動去釋放或加using,無法做到像List<Point>容器一樣自動GC回收,如果要傳入桌子函數形參時可以再轉換為PointColliction的容器!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-2-21 03:17 , Processed in 0.290635 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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