一键打断线(根据相交点打断)——CAD c# 二次开发

多条相交线根据交点一键打断,如下图:

部分代码如下:

finally

cs 复制代码
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)
        {
        }
     

    }
}

插件联系↓↓↓

相关推荐
laocooon5238578869 分钟前
C#二次开发中简单块的定义与应用
android·数据库·c#
CNRio14 分钟前
智能赋能全球化:AI Agent驱动中国科技企业出海的政技融合新范式
人工智能·科技·microsoft
YJlio25 分钟前
BgInfo 学习笔记(11.5):多种输出方式(壁纸 / 剪贴板 / 文件)与“更新其他桌面”实战
笔记·学习·c#
Zhen (Evan) Wang26 分钟前
.NET 6 API使用Serilog APM
c#·.net
总有刁民想爱朕ha38 分钟前
MySQL 8 Windows日常维护教程(安装、备份、还原、重置密码等)
windows·mysql
世转神风-1 小时前
ps1脚本-运行报错-并带有乱码
windows·脚本
2501_925317132 小时前
[鸿蒙2025领航者闯关] 把小智AI装进「第二大脑」:从开箱到MCP智能体的全链路实战
人工智能·microsoft·harmonyos·鸿蒙2025领航者闯关·小智ai智能音箱·mcp开发
青w韵2 小时前
Claude 高级工具使用解析:从上下文优化到程序化调用的工程实践
数据库·windows
wuk9982 小时前
C# 开发 FTP 客户端
开发语言·c#
淼淼7632 小时前
Qt拖动工具栏控件到图页中均匀展示
开发语言·c++·windows·qt