找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1224|回复: 0

[原创] 练习 选择相连线

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-18 20:09:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
[ 本帖最后由 csharp 于 2014-5-18 20:16 编辑 ]\n\n参照这个 Lisp 算法 [url]http://bbs.xdcad.net/thread-674035-1-1.html[/url] 写的,练习 List<T>
  1. using System;
  2. using System.Collections.Generic;
  3. using Autodesk.AutoCAD.Runtime;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.DatabaseServices;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.EditorInput;
  8. using Application = Autodesk.AutoCAD.ApplicationServices.Application;
  9. using Exception = System.Exception;
  10. [assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in11.MyCommands))]
  11. namespace AutoCAD_CSharp_plug_in11
  12. {
  13.     public class MyCommands
  14.     {
  15.         [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
  16.         public void MyCommand() // This method can have any name
  17.         {
  18.             // Put your command code here
  19.             Document document = Application.DocumentManager.MdiActiveDocument;
  20.             Database db = document.Database;
  21.             Editor editor = document.Editor;
  22.             Transaction transaction = db.TransactionManager.StartTransaction();
  23.             //选择线
  24.             PromptEntityOptions peo = new PromptEntityOptions("\n选择线");
  25.             peo.SetRejectMessage("\nEntity Must be Curve!");
  26.             peo.AddAllowedClass(typeof (Curve), false );//带过滤器的 Entsel
  27.             PromptEntityResult per = editor.GetEntity(peo);
  28.             if (per.Status != PromptStatus.OK)
  29.                 {
  30.                     return;
  31.                 }
  32.             ObjectId baseCurveId = per.ObjectId;
  33.             Curve baseCurve = (Curve) baseCurveId.GetObject(OpenMode.ForRead);
  34.             Tolerance oldTolerance = Tolerance .Global ;
  35.             Tolerance .Global = new Tolerance(0.2,0.2);//误差精度
  36.             //选择所有曲线
  37.             TypedValue[] tvs = new TypedValue[]
  38.             {
  39.                 new TypedValue( (int)DxfCode .Subclass , "CURVE")
  40.             };
  41.             SelectionFilter filter = new SelectionFilter(tvs);
  42.             PromptSelectionResult allCurveResult = editor.SelectAll(filter );//(ssget x filter)
  43.             if (allCurveResult  .Status  !=  PromptStatus .OK )
  44.             {
  45.                 return;
  46.             }
  47.             using (transaction)
  48.             {
  49.                 try
  50.                 {
  51.                     SelectionSet ss = (SelectionSet)allCurveResult .Value;
  52.                     ObjectId[] ids = ss.GetObjectIds();
  53.                     int cCount = ids.Length;
  54.                     //所有曲线集
  55.                     List< List< Object> > allCurves = new List<List<Object>>();
  56.                     for (int i = 0; i < cCount; i++)
  57.                     {
  58.                         ObjectId id = ids[i];
  59.                         if (id != baseCurveId)
  60.                         {
  61.                             Curve tmpCurve = (Curve)id.GetObject(OpenMode.ForRead);
  62.                             allCurves.Add(CurveInfoToList( tmpCurve ));
  63.                         }
  64.                     }
  65.                     //输出曲线列表
  66.                     List< List< Object > > outCurves = new List<List<Object>>();
  67.                     outCurves.Add(CurveInfoToList(baseCurve));
  68.                     //
  69.                     bool flag = true;
  70.                     List<List<Object>> tmpCurveList = new List<List<Object>>();//临时集合
  71.                     while (allCurves.Count > 0 && flag )
  72.                     {
  73.                         int len = outCurves.Count;
  74.                         for (int i = 0; i < outCurves .Count ; i++)
  75.                         {
  76.                             Curve cv1 = (Curve)outCurves [i][0];
  77.                             bool isClosed1 = (bool)outCurves [i][1];
  78.                             Point3d sp1 = (Point3d)outCurves [i][2];
  79.                             Point3d ep1 = (Point3d)outCurves [i][3];
  80.                             for (int j = 0; j < allCurves.Count  ; j++)
  81.                             {
  82.                                 List<object> index1 = allCurves[j];
  83.                                 Curve cv2 = (Curve)index1[0];
  84.                                 bool isClosed2 = (bool)index1[1];
  85.                                 Point3d sp2 = (Point3d)index1[2];
  86.                                 Point3d ep2 = (Point3d)index1[3];

  87.                                 if (isClosed1  && !isClosed2)
  88.                                 {
  89.                                 Point3d tmpPoint1 = cv1.GetClosestPointTo(sp2, false );
  90.                                 Point3d tmpPoint2 = cv1.GetClosestPointTo(ep2,false );
  91.                                 if (tmpPoint1.DistanceTo(sp1) < Tolerance .Global .EqualPoint  ||
  92.                                     tmpPoint2.DistanceTo(ep2) < Tolerance .Global .EqualPoint )
  93.                                     {
  94.                                         tmpCurveList.Add(index1);
  95.                                     }
  96.                                 }
  97.                                 else if (isClosed2 && !isClosed1)
  98.                                 {
  99.                                     Point3d tmp1 = cv2.GetClosestPointTo(sp1,false);
  100.                                     Point3d tmp2 = cv2.GetClosestPointTo(ep1,false);
  101.                                     if (tmp1.DistanceTo(sp1) < Tolerance .Global .EqualPoint  ||
  102.                                         tmp2.DistanceTo(ep1) < Tolerance .Global .EqualPoint )
  103.                                     {
  104.                                         tmpCurveList.Add(index1);
  105.                                     }
  106.                                 }
  107.                                 else if (!isClosed2  && !isClosed1)
  108.                                 {
  109.                                     if (sp1.DistanceTo(sp2) < Tolerance .Global .EqualPoint  ||
  110.                                         sp1.DistanceTo(ep2) < Tolerance .Global .EqualPoint  ||
  111.                                         ep1.DistanceTo(sp2) < Tolerance .Global .EqualPoint  ||
  112.                                         ep1.DistanceTo(ep2) < Tolerance .Global .EqualPoint )
  113.                                     {
  114.                                         tmpCurveList.Add(index1);
  115.                                     }
  116.                                 }
  117.                             }
  118.                         }
  119.                         if (tmpCurveList .Count > 0)
  120.                         {
  121.                             foreach (List<object> objects in tmpCurveList)
  122.                             {
  123.                                 allCurves.Remove(objects );
  124.                                 outCurves.Add(objects);
  125.                             }
  126.                             tmpCurveList.Clear();
  127.                         }
  128.                         if (len == outCurves .Count )//输出曲线集长度不再变化时退出
  129.                         {
  130.                             flag = false;
  131.                         }
  132.                     }
  133.                     
  134.                     
  135.                     foreach (List< object >  obj in outCurves )
  136.                     {
  137.                         Entity ent = (Entity)obj[0];
  138.                         ent.Highlight();
  139.                     }
  140.                     Tolerance.Global = oldTolerance;
  141.                     transaction.Commit();
  142.                 }
  143.                 catch (Exception)
  144.                 {
  145.                     
  146.                     throw;
  147.                 }
  148.             }
  149.         }
  150.         //构造曲线特征表 参数曲线,返回 (曲线 闭合状态 起点 终点)
  151.         private static List<Object> CurveInfoToList(Curve cv)
  152.         {
  153.             List<Object> resultList = new List<object>();
  154.             resultList.Add(cv);
  155.             resultList.Add(cv.Closed);
  156.             resultList.Add(cv.StartPoint);
  157.             resultList.Add(cv.EndPoint);
  158.             return resultList;
  159.         }
  160.     }
  161. }


Lisp 的表更灵活,或许 List<T> 就是 MS 借鉴 Lisp 的表概念
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-6-15 07:44 , Processed in 0.365366 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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