本帖最后由 d1742647821 于 2024-3-12 10:12 编辑
C#来凑个热闹
powered by IFoxCAD
https://gitee.com/inspirefunction/ifoxcad
https://www.kdocs.cn/l/cc6ZXSa0vMgD
- [CommandMethod(nameof(Cs6))]
- public static void Cs6()
- {
- // 多选直线
- var r1 = Env.Editor.GetSelection(OpFilter.Build(e => e.Dxf(0) == "LINE"));
- if (r1.Status != PromptStatus.OK)
- return;
- using var tr = new DBTrans();
- var lines = r1.Value.GetEntities<Line>().ToList();
- var brfList = tr.CurrentSpace.GetEntities<BlockReference>().ToList();
- foreach (var line in lines)
- {
- var brfIntersectList = brfList.Where(e =>
- {
- var p3dc = new Point3dCollection();
- e.BoundingBoxIntersectWith(line, Intersect.OnBothOperands, new Plane(), p3dc, IntPtr.Zero, IntPtr.Zero);
- return p3dc.Count > 0;
- });
- foreach (var brf in brfIntersectList)
- {
- BreakLineByBlock(line, brf);
- }
- }
- }
- private static void BreakLineByBlock(Line line, BlockReference brf)
- {
- var btr = (BlockTableRecord)brf.BlockTableRecord.GetObject();
- var curves = btr.GetEntities<Curve>().Select(e => e.GetTransformedCopy(brf.BlockTransform)).OfType<Curve>()
- .ToList();
- var ptList = new List<Point3d>();
- ptList.AddRange(curves.SelectMany(e => e.IntersectWith(line, Intersect.OnBothOperands, new Plane())));
- if (ptList.Count == 0)
- return;
- var box = brf.GetBoundingBoxEx()!.Value;
- using (line.ForWrite())
- {
- line.StartPoint = line.StartPoint.Z20();
- line.EndPoint = line.EndPoint.Z20();
- if (box.MidCenter.Distance2dTo(line.StartPoint) < box.MidCenter.Distance2dTo(line.EndPoint))
- {
- line.StartPoint = ptList.FindByMax(line.GetParameterAtPoint);
- }
- else
- {
- line.EndPoint = ptList.FindByMin(line.GetParameterAtPoint);
- }
- }
- }
|