I repalced the lambda function with a delegate (ComparePoints) so that it can be used with NET Framework 2.0 (default for A2007 -> A2009)
- using System.Collections.Generic;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
- namespace ConvexHull
- {
- public class Commands
- {
- private Point2d _p0;
- private bool Clockwise(Point2d p1, Point2d p2, Point2d p3)
- {
- return ((p2.X - p1.X) * (p3.Y - p1.Y) - (p2.Y - p1.Y) * (p3.X - p1.X)) < 1e-8;
- }
- private int ComparePoints(Point2d p1, Point2d p2)
- {
- if (p1.IsEqualTo(p2)) return 0;
- double d1 = _p0.GetDistanceTo(p1);
- double d2 = _p0.GetDistanceTo(p2);
- if (d1 == 0.0) return -1;
- if (d2 == 0.0) return 1;
- double cos = (p2.X - _p0.X) / d2 - (p1.X - _p0.X) / d1;
- if (cos < -1e-8) return -1;
- if (cos > 1e-8) return 1;
- return d1.CompareTo(d2);
- }
- private List<Point2d> ConvexHull(List<Point2d> pts)
- {
- _p0 = pts[0];
- for (int i = 1; i < pts.Count; i++)
- {
- Point2d pt = pts[i];
- if (pt.Y < _p0.Y || (pt.Y == _p0.Y && pt.X < _p0.X))
- _p0 = pt;
- }
- pts.Sort(ComparePoints);
- for (int i = 1; i < pts.Count - 1; i++)
- {
- while (i > 0 && Clockwise(pts[i - 1], pts[i], pts[i + 1]))
- {
- pts.RemoveAt(i);
- i--;
- }
- }
- return pts;
- }
- [CommandMethod("ch")]
- public void testCh()
- {
- Document doc = AcAp.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- TypedValue[] filter = new TypedValue[1] { new TypedValue(0, "POINT") };
- PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
- if (psr.Status != PromptStatus.OK) return;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- using (Polyline pline = new Polyline())
- {
- List<Point2d> pts = new List<Point2d>();
- foreach (SelectedObject so in psr.Value)
- {
- DBPoint dbPt = (DBPoint)tr.GetObject(so.ObjectId, OpenMode.ForRead);
- pts.Add(new Point2d(dbPt.Position.X, dbPt.Position.Y));
- }
- for (int i = 0; i < ConvexHull(pts).Count; i++)
- {
- pline.AddVertexAt(i, pts[i], 0.0, 0.0, 0.0);
- }
- pline.Closed = true;
- pline.SetDatabaseDefaults();
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- btr.AppendEntity(pline);
- tr.AddNewlyCreatedDBObject(pline, true);
- tr.Commit();
- }
- }
- }
- }
[url]http://www.theswamp.org/index.php?topic=31865.msg373462#msg373462[/url] |