多条相交线根据交点一键打断,如下图:
部分代码如下:
finally
namespace IFoxDemo;
public class Class1
{[CommandMethod("ddx")]public static void Demo(){//"ifox可以了".Print();Database db = HostApplicationServices.WorkingDatabase;Editor ed = Env.Editor;try{if (!db.GetEntities(out List<Curve> curve, "请选择需要打断的线:")) return;// List<Curve> lines = new List<Curve>();List<Curve> lines1 = new List<Curve>();foreach (var item in curve)//多段线炸开成线段{if (item is Polyline pl){List<Curve> cus = ed.ExplodePolyLine(pl);foreach (var cu in cus){if (cu is Curve) lines1.Add(cu as Curve);}}else if (item is Curve){if (item is Ellipse) return;lines1.Add(item as Curve);}}//lines1.ForEach(line => line.ColorIndex = 1);//foreach (var item in lines1)//{// db.AddEntityToModeSpace(item);//}return;Dictionary<Curve, List<Point3d>> pointsOnLine = new Dictionary<Curve, List<Point3d>>();foreach (var item in lines1)//创建线和对应点组成的字典{pointsOnLine.Add(item, new List<Point3d>());}for (int i = 0; i < lines1.Count -1; i++){for (int j = i + 1; j < lines1.Count; j++){Point3dCollection pos = new Point3dCollection();//创建点集合lines1[i].IntersectWith(lines1[j], Intersect.OnBothOperands, pos, IntPtr.Zero, IntPtr.Zero);if (pos.Count > 0){foreach (Point3d item in pos){pointsOnLine[lines1[i]].Add(item);pointsOnLine[lines1[j]].Add(item);//获取线段交点的坐标存入字典}}}}lines1.Clear();foreach (var item in pointsOnLine)//清理线{Curve line = item.Key;List<Point3d> points = item.Value;if (points.Count == 0){lines1.Add(line);}else{if (points.Count > 0){points = points.OrderBy(x => line.GetParameterAtPoint(x)).ToList();//点排序Point3dCollection pos = new Point3dCollection();points.ForEach(x => pos.Add(x));//点集合加点DBObjectCollection dbs = line.GetSplitCurves(pos);//线上有多个点,按顺序打断线foreach (var dbobject in dbs){if (dbobject is Curve) lines1.Add(dbobject as Curve);//一个line被他上面的点打成多个line后加入列表}}}}db.AddEntityToModeSpace(lines1.ToArray());db.Erase(curve);}catch (Exception ex){}}
}
插件联系↓↓↓