明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2802|回复: 5

[图元] 自己写的一个首尾相连线段或多段线并成一条多段线的程序

[复制链接]
发表于 2011-6-15 11:31 | 显示全部楼层 |阅读模式
  1.   经测试没有大问题,只是画完多段线后原有线段不能完全删除,希望高手给予修改意见,如有BUG请回复!


  2. public class mycommand
  3.     {
  4.         //本代码由<家在湾里>书写,仅供学习用,引用请注明出处.............
  5.         //联系本人请Q:584457142
  6.         [CommandMethod("my")]//将首尾相连的线段或多段线连成多段线
  7.         public void my()
  8.         {
  9.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  10.             Database acCurDb = acDoc.Database;
  11.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  12.             try
  13.             {
  14.                 using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  15.                 {
  16.                     BlockTable acBlkTbl;
  17.                     acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
  18.                     BlockTableRecord acBlkTblRec;
  19.                     acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
  20.                                                     OpenMode.ForWrite) as BlockTableRecord;
  21.                    PromptSelectionOptions pso=new PromptSelectionOptions();
  22.                    PromptSelectionResult psr = ed.GetSelection(pso);
  23.                     Polyline pl = new Polyline();
  24.                     if (psr.Status == PromptStatus.OK)
  25.                     {
  26.                         SelectionSet ss = psr.Value;
  27.                         int n = ss.Count;
  28.                         ObjectIdCollection oc = new ObjectIdCollection() ;
  29.                         for (int i = 0; i < n; i++)
  30.                         {
  31.                             oc.Add(ss[i].ObjectId);
  32.                         }
  33.                         Entity ent2 = acTrans.GetObject(oc[0], OpenMode.ForWrite) as Entity;
  34.                         for (int j = 0; j <n; j++)
  35.                         {
  36.                           
  37.                             for (int i = 1; i < oc.Count; i++)
  38.                             {
  39.                               
  40.                                 Entity ent3 = acTrans.GetObject(oc[i], OpenMode.ForWrite) as Entity;
  41.                                 pl = mycommand.EntityUnion(ent2, ent3);
  42.                                 if (pl == null || pl.NumberOfVertices == 0) continue;
  43.                                 else
  44.                                 {                                    
  45.                                     oc.RemoveAt(i);
  46.                                     ent2 = pl as Entity;
  47.                                     ent3.Erase();
  48.                                    break;
  49.                                    
  50.                                 }
  51.                             }
  52.                         }
  53.                         //ent2.Erase();
  54.                     }
  55.                     
  56.                    acBlkTblRec.AppendEntity(pl);
  57.                    acTrans.AddNewlyCreatedDBObject(pl,true);
  58.                     acTrans.Commit();
  59.                 }
  60.             }
  61.             catch (System.Exception)
  62.             {
  63.                 Application.ShowAlertDialog("error");
  64.             }
  65.         }
  66.         public static Polyline EntityUnion(Entity ent1, Entity ent2)
  67.         {
  68.             Polyline pl1 = new Polyline();
  69.             Polyline pl2 = new Polyline();
  70.             Polyline pl = new Polyline();
  71.             Point3d equalpoint = new Point3d();
  72.             Point3dCollection pc = new Point3dCollection();
  73.             Point3dCollection final = new Point3dCollection();
  74.             #region
  75.             if (ent1 is Polyline && ent2 is Polyline)
  76.             {
  77.                 pl1 = ent1 as Polyline;
  78.                 pl2 = ent2 as Polyline;
  79.                 pc.Add(pl1.StartPoint);
  80.                 pc.Add(pl1.EndPoint);
  81.                 pc.Add(pl2.StartPoint);
  82.                 pc.Add(pl2.EndPoint);
  83.                 for (int i = 0; i < 3; i++)
  84.                 {
  85.                     for (int j = i + 1; j < 4; j++)
  86.                     {
  87.                         if (pc[i] == pc[j])
  88.                         {
  89.                             equalpoint = pc[i];
  90.                             break;
  91.                         }
  92.                     }
  93.                     if (equalpoint != new Point3d(0, 0, 0)) break;
  94.                 }
  95.                 if (equalpoint == new Point3d(0, 0, 0)) return null;
  96.                 if (pl1.EndPoint.Equals(equalpoint))
  97.                 {
  98.                     for (int i = 0; i < pl1.NumberOfVertices - 1; i++)
  99.                     {
  100.                         final.Add(pl1.GetPoint3dAt(i));
  101.                     }
  102.                     if (pl2.StartPoint.Equals(equalpoint))
  103.                     {
  104.                         for (int i = 0; i < pl2.NumberOfVertices; i++)
  105.                         {
  106.                             final.Add(pl2.GetPoint3dAt(i));
  107.                         }
  108.                     }
  109.                     else
  110.                     {
  111.                         for (int i = pl2.NumberOfVertices - 1; i > -1; i--)
  112.                         {
  113.                             final.Add(pl2.GetPoint3dAt(i));
  114.                         }
  115.                         
  116.                     }
  117.                 }
  118.                 else if (pl1.StartPoint.Equals(equalpoint))
  119.                 {
  120.                     if (pl2.EndPoint.Equals(equalpoint))
  121.                     {
  122.                         for (int i = 0; i < pl2.NumberOfVertices; i++)
  123.                         {
  124.                             final.Add(pl2.GetPoint3dAt(i));
  125.                         }
  126.                         for (int i = 1; i < pl1.NumberOfVertices; i++)
  127.                         {
  128.                             final.Add(pl1.GetPoint3dAt(i));
  129.                         }
  130.                     }
  131.                     else
  132.                     {
  133.                         for (int i = pl2.NumberOfVertices - 1; i >= 0; i--)
  134.                         {
  135.                             final.Add(pl2.GetPoint3dAt(i));
  136.                         }
  137.                         for (int i = 1; i < pl1.NumberOfVertices; i++)
  138.                         {
  139.                             final.Add(pl1.GetPoint3dAt(i));
  140.                         }
  141.                     }
  142.                   
  143.                 }
  144.                 for (int i = 0; i < final.Count; i++)
  145.                     {
  146.                         pl.AddVertexAt(i, new Point2d(final[i].X, final[i].Y), 0, 0, 0);
  147.                     }
  148.                     return pl;
  149.                 }
  150.                
  151.             #endregion
  152.                 #region
  153.                 if (ent1 is Line && ent2 is Line)
  154.                 {
  155.                     Line l1 = ent1 as Line;
  156.                     Line l2 = ent2 as Line;
  157.                     pc.Add(l1.StartPoint);
  158.                     pc.Add(l1.EndPoint);
  159.                     pc.Add(l2.StartPoint);
  160.                     pc.Add(l2.EndPoint);
  161.                     for (int i = 0; i < 3; i++)
  162.                     {
  163.                         for (int j = i + 1; j < 4; j++)
  164.                         {
  165.                             if (pc[i] == pc[j])
  166.                             {
  167.                                 equalpoint = pc[i];
  168.                                 pc.Remove(pc[i]);
  169.                                 pc.Remove(pc[j-1]);
  170.                                 break;
  171.                             }

  172.                         }
  173.                         if (equalpoint != new Point3d(0, 0, 0)) break;
  174.                      
  175.                     }
  176.                     if (equalpoint == new Point3d(0, 0, 0)) return null;
  177.                     final.Add(pc[0]);
  178.                     Point3d p1 = pc[0];
  179.                     Point3d p2 = pc[1];
  180.                     final.Add(equalpoint);
  181.                     final.Add(pc[1]);
  182.                     for (int i = 0; i < final.Count; i++)
  183.                     {
  184.                         pl.AddVertexAt(i, new Point2d(final[i].X, final[i].Y), 0, 0, 0);
  185.                     }
  186.                     return pl;
  187.                 }
  188.                 #endregion
  189.                 #region
  190.                 if (ent1 is Line && ent2 is Polyline)
  191.                 {
  192.                     Line l = ent1 as Line;
  193.                     pl1 = ent2 as Polyline;
  194.                     pc.Add(l.StartPoint);
  195.                     pc.Add(l.EndPoint);
  196.                     pc.Add(pl1.StartPoint);
  197.                     pc.Add(pl1.EndPoint);
  198.                     for (int i = 0; i < 3; i++)
  199.                     {
  200.                         for (int j = i + 1; j < 4; j++)
  201.                         {
  202.                             if (pc[i] == pc[j])
  203.                             {
  204.                                 equalpoint = pc[i];
  205.                                 break;
  206.                             }
  207.                         }
  208.                         if (equalpoint != new Point3d(0, 0, 0)) break;
  209.                        
  210.                     }
  211.                     if (equalpoint == new Point3d(0, 0, 0)) return null;
  212.                     if (equalpoint.Equals(pl1.EndPoint))
  213.                     {
  214.                         for (int i = 0; i < pl1.NumberOfVertices; i++)
  215.                         {
  216.                             final.Add(pl1.GetPoint3dAt(i));
  217.                         }
  218.                         if (equalpoint.Equals(l.StartPoint))
  219.                             final.Add(l.EndPoint);
  220.                         else final.Add(l.StartPoint);
  221.                     }
  222.                     else if (equalpoint.Equals(pl1.StartPoint))
  223.                     {
  224.                         for (int i = pl1.NumberOfVertices - 1; i >= 0; i--)
  225.                         {
  226.                             final.Add(pl1.GetPoint3dAt(i));
  227.                         }
  228.                         if (equalpoint.Equals(l.StartPoint))
  229.                             final.Add(l.EndPoint);
  230.                         else final.Add(l.StartPoint);
  231.                     }
  232.                     for (int i = 0; i < final.Count; i++)
  233.                     {
  234.                         pl.AddVertexAt(i, new Point2d(final[i].X, final[i].Y), 0, 0, 0);
  235.                     }
  236.                     return pl;
  237.                 }
  238.                 #endregion
  239.                 #region
  240.                 if (ent1 is Polyline && ent2 is Line)
  241.                 {
  242.                     Line l = ent2 as Line;
  243.                     pl1 = ent1 as Polyline;
  244.                     pc.Add(l.StartPoint);
  245.                     pc.Add(l.EndPoint);
  246.                     pc.Add(pl1.StartPoint);
  247.                     pc.Add(pl1.EndPoint);
  248.                     for (int i = 0; i < 3; i++)
  249.                     {
  250.                         for (int j = i + 1; j < 4; j++)
  251.                         {
  252.                             if (pc[i] == pc[j])
  253.                             {
  254.                                 equalpoint = pc[i];
  255.                                 break;
  256.                             }
  257.                         }
  258.                         if (equalpoint != new Point3d(0, 0, 0)) break;
  259.                      
  260.                     }  
  261.                     if (equalpoint == new Point3d(0, 0, 0)) return null;
  262.                     if (equalpoint.Equals(pl1.EndPoint))
  263.                     {
  264.                         for (int i = 0; i < pl1.NumberOfVertices; i++)
  265.                         {
  266.                             final.Add(pl1.GetPoint3dAt(i));
  267.                         }
  268.                         if (equalpoint.Equals(l.StartPoint))
  269.                             final.Add(l.EndPoint);
  270.                         else final.Add(l.StartPoint);
  271.                     }
  272.                     else if (equalpoint.Equals(pl1.StartPoint))
  273.                     {
  274.                         for (int i = pl1.NumberOfVertices - 1; i >= 0; i--)
  275.                         {
  276.                             final.Add(pl1.GetPoint3dAt(i));
  277.                         }
  278.                         if (equalpoint.Equals(l.StartPoint))
  279.                             final.Add(l.EndPoint);
  280.                         else final.Add(l.StartPoint);
  281.                     }
  282.                     for (int i = 0; i < final.Count; i++)
  283.                     {
  284.                         pl.AddVertexAt(i, new Point2d(final[i].X, final[i].Y), 0, 0, 0);
  285.                     }
  286.                     return pl;
  287.                 }
  288.                 #endregion
  289.                 else return null;
  290.         }

发表于 2011-6-15 11:33 | 显示全部楼层
应该有这种方法了吧。
 楼主| 发表于 2011-6-15 12:37 | 显示全部楼层
回复 yxr_MJTD 的帖子

别人肯定是写过了,你是API里面有这个方法吗?不妨写出方法名来一看
发表于 2011-6-29 14:06 | 显示全部楼层
多谢分享,来看看
发表于 2012-3-8 09:50 | 显示全部楼层
谢谢楼主分享,很不错的程度,
发表于 2014-3-10 15:46 | 显示全部楼层
多谢楼主,正愁着如何实现呢。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-19 10:33 , Processed in 0.258721 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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