明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4911|回复: 12

如何实现pe命令,连接多段线

  [复制链接]
发表于 2011-7-13 11:21 | 显示全部楼层 |阅读模式
如何实现cad自带的pe命令(连接多段线),字节写的代码如下
ids是小短线,可是老是连接不起来,是怎么回事??
//link the little lines
        public DBObjectCollection polylink(DBObjectCollection ids)
        {
            DBObjectCollection polyIds = new DBObjectCollection();
            bool IsEmpty = false;
            while (!IsEmpty)
            {               
                if (ids.Count < 3)
                {
                    IsEmpty = true;                  
                }
                Polyline pl0 = ids[0] as Polyline;
                Polyline pl = new Polyline();
                Point2d plst = pl0.GetPoint2dAt(0);
                Point2d plet = pl0.GetPoint2dAt(1);
                pl.AddVertexAt(pl.NumberOfVertices, plst, 0, 0, 0);
                pl.AddVertexAt(pl.NumberOfVertices, plet, 0, 0, 0);
                ids.RemoveAt(0);
                foreach (DBObject polyid in ids)
                {
                    Polyline poly = polyid as Polyline;
                    if (poly.Length==0)
                    {
                        ids.Remove(polyid);
                    }
                    if (pl.EndPoint == poly.StartPoint)
                    {
                        pl.AddVertexAt(pl.NumberOfVertices, new Point2d(poly.EndPoint.X, poly.EndPoint.Y), 0, 0, 0);
                        ids.Remove(polyid);
                    }
                    else if (pl.EndPoint == poly.EndPoint)
                    {
                        pl.AddVertexAt(pl.NumberOfVertices, new Point2d(poly.StartPoint.X, poly.StartPoint.Y), 0, 0, 0);
                        ids.Remove(polyid);
                    }
                }
                if (pl.EndParam >= 2)
                {
                    pl.Closed = true;
                    polyIds.Add(pl);
                }
            }            
            return polyIds;
        }

发表于 2018-12-18 10:38 | 显示全部楼层
看了很多都是用command实现的,net高版本Curve对象提供了 JionLine
方法,低版本没有办法唉
发表于 2011-7-13 12:21 | 显示全部楼层
    1,        foreach (DBObject polyid in ids)
                {
                    Polyline poly = polyid as Polyline;
                    if (poly.Length==0)
                    {
                        ids.Remove(polyid);
                    }

      这里 在 foreach (DBObject polyid in ids) 里面 ids.Remove(polyid); 这是大忌
发表于 2011-7-13 12:22 | 显示全部楼层
2,    if (pl.EndPoint == poly.StartPoint)
                    {
这样的判断很不确定
发表于 2011-7-13 13:38 | 显示全部楼层
可惜是VB编的,谁有选择第一条线段而连接起多段线的lisp程序吗?
 楼主| 发表于 2011-7-13 16:23 | 显示全部楼层
    public DBObjectCollection polylink(DBObjectCollection ids)
        {
            DBObjectCollection polyIds = new DBObjectCollection();
            bool IsEmpty = false;
            if (ids.Count < 1)
            {
                IsEmpty = true;
                return polyIds;
            }         
             while(!IsEmpty)   
             {
                Polyline pl0 = ids[0] as Polyline;
                Polyline pl = new Polyline();
                Point2d plst = pl0.GetPoint2dAt(0);
                Point2d plet = pl0.GetPoint2dAt(1);
                pl.AddVertexAt(pl.NumberOfVertices, plst, 0, 0, 0);
                pl.AddVertexAt(pl.NumberOfVertices, plet, 0, 0, 0);
                ids.RemoveAt(0);
                 bool close=false;
                while (!close)
                {
                    foreach (DBObject polyid in ids)
                    {
                        Polyline poly = polyid as Polyline;
                        if (poly.Length == 0)
                        {
                            ids.Remove(polyid);
                        }
                        if (pl.EndPoint == poly.StartPoint)
                        {
                            pl.AddVertexAt(pl.NumberOfVertices, new Point2d(poly.EndPoint.X, poly.EndPoint.Y), 0, 0, 0);
                            ids.Remove(polyid);
                        }
                        else if (pl.EndPoint == poly.EndPoint)
                        {
                            pl.AddVertexAt(pl.NumberOfVertices, new Point2d(poly.StartPoint.X, poly.StartPoint.Y), 0, 0, 0);
                            ids.Remove(polyid);
                        }
                        if (pl.EndPoint == pl.StartPoint)
                        {
                            close=true;
                            pl.Closed=true;
                        }
                        
                    }
                    if (ids.Count<1)
                    {
                        close = true;
                    }
                }
                if (pl.EndParam >= 2)
                {
                    pl.ColorIndex = 2;
                    polyIds.Add(pl);
                }
                 if (ids.Count<1)
                 {
                     IsEmpty = true;
                 }
            }
            
            return polyIds;
        }
  [CommandMethod("linkTst")]
        public void linkTst()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            TypedValue[] fillist = new TypedValue[] { new TypedValue((int)DxfCode.Start, "Lwpolyline") };
            SelectionFilter filter = new SelectionFilter(fillist);
            PromptSelectionResult polyRes = ed.GetSelection(filter);
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                if (polyRes.Status == PromptStatus.Cancel)
                {
                    return;
                }
                else if (polyRes.Status==PromptStatus.OK||polyRes.Status==PromptStatus.None)
                {
                    SelectionSet polyset = polyRes.Value;
                    DBObjectCollection ids = new DBObjectCollection();
                    for (int i = 0; i < polyset.Count;i++ )
                    {
                        Polyline pl = trans.GetObject(polyset[i].ObjectId, OpenMode.ForWrite) as Polyline;
                        ids.Add(pl);
                    }
                  DBObjectCollection polyids=  polylink(ids);
                  jinhe.ObjectARX.Tools.AddEntities(polyids);
                }
                trans.Commit();
            }
        }
这样测试就是正确滴喽,可能有错误没有测试出来吧!!!
 楼主| 发表于 2011-7-13 16:24 | 显示全部楼层
回复 李毛毛 的帖子

为什么,我都是这么用滴???
 楼主| 发表于 2011-7-13 16:24 | 显示全部楼层
回复 sieben 的帖子

因为我看到了,它们的起始点确实是一摸一样,所以没有用误差了。
 楼主| 发表于 2011-7-13 16:25 | 显示全部楼层
回复 sieben 的帖子

呃,this is C#,not VB
发表于 2011-7-13 16:28 | 显示全部楼层
这样测试就是正确滴喽
---------------------
是 测试结果正确 还是 测试方法正确?
呵呵,好吧! 你说正确就正确吧!
发表于 2011-7-13 16:31 | 显示全部楼层
回复 teykmcqh 的帖子

高手!估计是你看穿了C#的外表!看到了VB的本质!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-19 10:14 , Processed in 0.292428 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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