Try this code example, change to suit
#region "Sort points"
[CommandMethod("SortPoints", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
public void GeWindowPointSelection()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
List<Point3d> points = new List<Point3d>();
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
try
{
PromptPointOptions ppo = new PromptPointOptions("\n\tSpecify a first corner: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK) return;
PromptCornerOptions pco = new PromptCornerOptions("\n\tOther corner: ", ppr.Value);
PromptPointResult pcr = ed.GetCorner(pco);
if (pcr.Status != PromptStatus.OK) return;
Point3d pt1 = ppr.Value;
Point3d pt2 = pcr.Value;
if (pt1.X == pt2.X || pt1.Y == pt2.Y)
{
ed.WriteMessage("\nInvalid point specification");
return;
}
TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "POINT") };
SelectionFilter flt = new SelectionFilter(tv);
PromptSelectionResult res;
res = ed.SelectWindow(pt1, pt2, flt);
if (res.Status != PromptStatus.OK)
return;
SelectionSet sset = res.Value;
if (sset.Count == 0)
return;
BlockTableRecord btr= (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (SelectedObject obj in sset)
{
DBObject dbobj = obj.ObjectId.GetObject(OpenMode.ForRead);
DBPoint ptobj = dbobj as DBPoint;
if (ptobj != null)
{
Point3d pt = ptobj.Position;
points.Add(pt);
}
}
points.Sort(sortByX);
int n = 0;
foreach (Point3d dp in points)
{
n += 1;
DBText txt = new DBText();
txt.SetDatabaseDefaults();
txt.Position = dp ;
txt.TextString = n.ToString();
btr.AppendEntity(txt);
tr.AddNewlyCreatedDBObject(txt, true);
}
tr.Commit();
ed.Regen();
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
}
}
}
//http://www.theswamp.org/index.php?topic=32761.msg382406
static public int sortByX(Point3d a, Point3d b)
{
if (a.X == b.X)
return a.Y.CompareTo(b.Y);
return a.X.CompareTo(b.X);
}
#endregion
_____________________________________
C6309D9E0751D165D0934D0621DFF27919