马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
[ 本帖最后由 csharp 于 2014-5-18 20:16 编辑 ]\n\n参照这个 Lisp 算法 [url]http://bbs.xdcad.net/thread-674035-1-1.html[/url] 写的,练习 List<T>
- using System;
- using System.Collections.Generic;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- using Application = Autodesk.AutoCAD.ApplicationServices.Application;
- using Exception = System.Exception;
- [assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in11.MyCommands))]
- namespace AutoCAD_CSharp_plug_in11
- {
- public class MyCommands
- {
- [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
- public void MyCommand() // This method can have any name
- {
- // Put your command code here
- Document document = Application.DocumentManager.MdiActiveDocument;
- Database db = document.Database;
- Editor editor = document.Editor;
- Transaction transaction = db.TransactionManager.StartTransaction();
- //选择线
- PromptEntityOptions peo = new PromptEntityOptions("\n选择线");
- peo.SetRejectMessage("\nEntity Must be Curve!");
- peo.AddAllowedClass(typeof (Curve), false );//带过滤器的 Entsel
- PromptEntityResult per = editor.GetEntity(peo);
- if (per.Status != PromptStatus.OK)
- {
- return;
- }
- ObjectId baseCurveId = per.ObjectId;
- Curve baseCurve = (Curve) baseCurveId.GetObject(OpenMode.ForRead);
- Tolerance oldTolerance = Tolerance .Global ;
- Tolerance .Global = new Tolerance(0.2,0.2);//误差精度
- //选择所有曲线
- TypedValue[] tvs = new TypedValue[]
- {
- new TypedValue( (int)DxfCode .Subclass , "CURVE")
- };
- SelectionFilter filter = new SelectionFilter(tvs);
- PromptSelectionResult allCurveResult = editor.SelectAll(filter );//(ssget x filter)
- if (allCurveResult .Status != PromptStatus .OK )
- {
- return;
- }
- using (transaction)
- {
- try
- {
- SelectionSet ss = (SelectionSet)allCurveResult .Value;
- ObjectId[] ids = ss.GetObjectIds();
- int cCount = ids.Length;
- //所有曲线集
- List< List< Object> > allCurves = new List<List<Object>>();
- for (int i = 0; i < cCount; i++)
- {
- ObjectId id = ids[i];
- if (id != baseCurveId)
- {
- Curve tmpCurve = (Curve)id.GetObject(OpenMode.ForRead);
- allCurves.Add(CurveInfoToList( tmpCurve ));
- }
- }
- //输出曲线列表
- List< List< Object > > outCurves = new List<List<Object>>();
- outCurves.Add(CurveInfoToList(baseCurve));
- //
- bool flag = true;
- List<List<Object>> tmpCurveList = new List<List<Object>>();//临时集合
- while (allCurves.Count > 0 && flag )
- {
- int len = outCurves.Count;
- for (int i = 0; i < outCurves .Count ; i++)
- {
- Curve cv1 = (Curve)outCurves [i][0];
- bool isClosed1 = (bool)outCurves [i][1];
- Point3d sp1 = (Point3d)outCurves [i][2];
- Point3d ep1 = (Point3d)outCurves [i][3];
- for (int j = 0; j < allCurves.Count ; j++)
- {
- List<object> index1 = allCurves[j];
- Curve cv2 = (Curve)index1[0];
- bool isClosed2 = (bool)index1[1];
- Point3d sp2 = (Point3d)index1[2];
- Point3d ep2 = (Point3d)index1[3];
- if (isClosed1 && !isClosed2)
- {
- Point3d tmpPoint1 = cv1.GetClosestPointTo(sp2, false );
- Point3d tmpPoint2 = cv1.GetClosestPointTo(ep2,false );
- if (tmpPoint1.DistanceTo(sp1) < Tolerance .Global .EqualPoint ||
- tmpPoint2.DistanceTo(ep2) < Tolerance .Global .EqualPoint )
- {
- tmpCurveList.Add(index1);
- }
- }
- else if (isClosed2 && !isClosed1)
- {
- Point3d tmp1 = cv2.GetClosestPointTo(sp1,false);
- Point3d tmp2 = cv2.GetClosestPointTo(ep1,false);
- if (tmp1.DistanceTo(sp1) < Tolerance .Global .EqualPoint ||
- tmp2.DistanceTo(ep1) < Tolerance .Global .EqualPoint )
- {
- tmpCurveList.Add(index1);
- }
- }
- else if (!isClosed2 && !isClosed1)
- {
- if (sp1.DistanceTo(sp2) < Tolerance .Global .EqualPoint ||
- sp1.DistanceTo(ep2) < Tolerance .Global .EqualPoint ||
- ep1.DistanceTo(sp2) < Tolerance .Global .EqualPoint ||
- ep1.DistanceTo(ep2) < Tolerance .Global .EqualPoint )
- {
- tmpCurveList.Add(index1);
- }
- }
- }
- }
- if (tmpCurveList .Count > 0)
- {
- foreach (List<object> objects in tmpCurveList)
- {
- allCurves.Remove(objects );
- outCurves.Add(objects);
- }
- tmpCurveList.Clear();
- }
- if (len == outCurves .Count )//输出曲线集长度不再变化时退出
- {
- flag = false;
- }
- }
-
-
- foreach (List< object > obj in outCurves )
- {
- Entity ent = (Entity)obj[0];
- ent.Highlight();
- }
- Tolerance.Global = oldTolerance;
- transaction.Commit();
- }
- catch (Exception)
- {
-
- throw;
- }
- }
- }
- //构造曲线特征表 参数曲线,返回 (曲线 闭合状态 起点 终点)
- private static List<Object> CurveInfoToList(Curve cv)
- {
- List<Object> resultList = new List<object>();
- resultList.Add(cv);
- resultList.Add(cv.Closed);
- resultList.Add(cv.StartPoint);
- resultList.Add(cv.EndPoint);
- return resultList;
- }
- }
- }
Lisp 的表更灵活,或许 List<T> 就是 MS 借鉴 Lisp 的表概念
|