037集——JoinEntities连接多段线polyline和圆弧arc(CAD—C#二次开发入门)

如图:最终效果

polyline连接:

代码如下:

cs 复制代码
public void joinentities()
{
    Curve pLine = Z.db.SelectEntities<Curve>().First().Clone() as Curve;
    pLine.ChangeEntityColor(1);
    Curve pLine1 = Z.db.SelectEntities<Curve>().First().Clone() as Curve;
    Curve pLine2 = Z.db.SelectEntities<Curve>().First().Clone() as Curve;
    Entity[] otherEntities = new Entity[2];
    otherEntities[0] = pLine1;
    otherEntities[1] = pLine2;
    // join the other entities
    try
    {
        IntegerCollection joinedEntities = pLine.JoinEntities(otherEntities);//调用函数的主题必须polyline
        if (joinedEntities.Count > 0) Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("nSome or all entities joined!");
        Z.db.AddEntityToModeSpace(pLine);
        Z.Zoom();
    }
    catch (System.Exception)
    {
        Z.ed.WriteMessage("连接失败!\n");
    } 
}

polyline3d连接:

cs 复制代码
 public void joinentitiesPolyline3d()
 {
     Point3d[] p3d = new Point3d[2] { new Point3d(10, 10, 0), new Point3d(0, 0, 0) };
     Polyline3d p3 = new Polyline3d(Poly3dType.SimplePoly,new Point3dCollection(p3d),false);
     Curve firstPl = p3;//polyline3d可连接范围广,包括圆弧,椭圆弧,样条曲线,直线,多段线
    // Curve firstPl = Z.db.SelectEntities<Curve>().First().Clone() as Curve;//连接的线不能是数据库中已有的线
     if (firstPl is null) return;    
     firstPl.ChangeEntityColor(1);
     List<Curve> curveslist = Z.db.SelectEntities<Curve>();
     Curve[] curvesarray = new Curve[curveslist.Count];
     for (int i = 0; i < curveslist.Count; i++)
     {
         curvesarray[i] = curveslist[i].Clone()as Curve;
     }
     try
     {
         IntegerCollection resultPl = firstPl.JoinEntities(curvesarray);//调用函数的主题必须polyline
         if (resultPl.Count > 0) Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("nSome or all entities joined!");
         Z.db.AddEntityToModeSpace(firstPl);
         Z.Zoom();
     }
     catch (System.Exception)
     {
         Z.ed.WriteMessage("连接失败!\n");
     } 
 }

Polyline连接(可连接弧,不可连spline和ellipse)

cs 复制代码
 public void joinentitiesPolyline3d()
 {
     Point3d[] p3d = new Point3d[2] { new Point3d(10, 10, 0), new Point3d(0, 0, 0) };
     Polyline3d p3 = new Polyline3d(Poly3dType.SimplePoly,new Point3dCollection(p3d),false);
     Curve firstPl = p3;//polyline3d可连接范围广,包括圆弧,椭圆弧,样条曲线,直线,多段线
    // Curve firstPl = Z.db.SelectEntities<Curve>().First().Clone() as Curve;//连接的线不能是数据库中已有的线
     if (firstPl is null) return;    
     firstPl.ChangeEntityColor(1);
     List<Curve> curveslist = Z.db.SelectEntities<Curve>();
     Curve[] curvesarray = new Curve[curveslist.Count];
     for (int i = 0; i < curveslist.Count; i++)
     {
         curvesarray[i] = curveslist[i].Clone()as Curve;
     }
     try
     {
         IntegerCollection resultPl = firstPl.JoinEntities(curvesarray);//调用函数的主题必须polyline
         if (resultPl.Count > 0) Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("nSome or all entities joined!");
         Z.db.AddEntityToModeSpace(firstPl);
         Z.Zoom();
     }
     catch (System.Exception)
     {
         Z.ed.WriteMessage("连接失败!\n");
     } 
 }

封装后结果:

public void xx()

{

Polyline polyline = Z.PolyJoin();

}

cs 复制代码
 public static Polyline PolyJoin()
 {
     //Point3d[] p3d = new Point3d[2] { new Point3d(10, 10, 0), new Point3d(0, 0, 0) };
     //Polyline3d p3 = new Polyline3d(Poly3dType.SimplePoly,new Point3dCollection(p3d),false);
     //Curve firstPl = p3;//polyline3d可连接范围广,包括圆弧,椭圆弧,样条曲线,直线,多段线
     //Curve firstPl = Z.db.SelectEntities<Curve>().First().Clone() as Curve;//连接的线不能是数据库中已有的线
     //if (firstPl is null) return;    
     Polyline firstPl = new Polyline();
     List<Curve> curveslist = Z.db.SelectEntities<Curve>();
     Curve[] curvesarray = new Curve[curveslist.Count];
     for (int i = 0; i < curveslist.Count; i++)
     {
         curvesarray[i] = curveslist[i].Clone() as Curve;
     }
     try
     {
         firstPl = curvesarray.Where(x => x is Polyline).First() as Polyline;
         IntegerCollection resultPl = firstPl.JoinEntities(curvesarray);//调用函数的主题必须polyline
         if (resultPl.Count > 0) Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("nSome or all entities joined!");
         firstPl.ChangeEntityColor(1); Z.db.AddEntityToModeSpace(firstPl);
         //Z.Zoom();
     }
     catch (System.Exception)
     {
         Z.ed.WriteMessage("连接失败!\n");
     }
     return firstPl;
 }
相关推荐
stevewongbuaa38 分钟前
一些烦人的go设置 goland
开发语言·后端·golang
撸码到无法自拔1 小时前
MATLAB中处理大数据的技巧与方法
大数据·开发语言·matlab
island13141 小时前
【QT】 控件 -- 显示类
开发语言·数据库·qt
sysu631 小时前
95.不同的二叉搜索树Ⅱ python
开发语言·数据结构·python·算法·leetcode·面试·深度优先
hust_joker2 小时前
go单元测试和基准测试
开发语言·golang·单元测试
wyg_0311132 小时前
C++资料
开发语言·c++
小机学AI大模型3 小时前
关于使用PHP时WordPress排错——“这意味着您在wp-config.php文件中指定的用户名和密码信息不正确”的解决办法
开发语言·php
臻一3 小时前
关于bash内建echo输出多行文本
开发语言·bash
步、步、为营3 小时前
C# 探秘:PDFiumCore 开启PDF读取魔法之旅
开发语言·pdf·c#·.net
小黄人软件4 小时前
【MFC】C++所有控件随窗口大小全自动等比例缩放源码(控件内字体、列宽等未调整) 20250124
开发语言·c++·ui·mfc