Here is one function I ported from ARX to C# from my library
See if helps.
Steps:
1. Open the attached drawing (or yours).
2. For this test, make a selection set of the path object.
3. Zoom Out as far as possible.
4. Call the command GETORDER and use the 'P' previous selection.
4. Zoom Extents.
The function, will select only the objects that cross the path, for the test these objects are change in color, and returns a list of dist-points & dist-entity[id].
[code]
static public void getEntitiesInOrder(string sObjCurve)
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Point3dCollection ints = new Point3dCollection();
Point3dCollection points = new Point3dCollection();
SortedList distpoint = new SortedList();
SortedList distid = new SortedList();
TypedValue[] filterValue; SelectionFilter filter; PromptSelectionResult res;
filterValue = new TypedValue[1] { new TypedValue((int)DxfCode.Start, sObjCurve) };
filter = new SelectionFilter(filterValue);
res = ed.GetSelection(filter);
if (res.Status == PromptStatus.OK && res.Value.Count > 0)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in res.Value.GetObjectIds())
{
Curve pPoly = tr.GetObject(id, OpenMode.ForRead, false) as Curve;
if (pPoly is Curve)
{
Point3d p0, p1;
p0 = pPoly.StartPoint;
p1 = pPoly.EndPoint;
Extents3d ebox = pPoly.GeometricExtents;
Point3d ll, ur;
ll = ebox.MinPoint;
ur = ebox.MaxPoint;
// select the crossing objects
filterValue = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "LINE,ARC,LWPOLYLINE,POLYLINE,CIRCLE") };
filter = new SelectionFilter(filterValue);
res = ed.SelectCrossingWindow(ll, ur, filter);
if (res.Status == PromptStatus.OK && res.Value.Count > 0)
{
foreach (ObjectId ident in res.Value.GetObjectIds())
{
if (ident.Equals(id)) continue; //ignore the path object
Entity pEnt = tr.GetObject(ident, OpenMode.ForRead, false) as Entity;
if (pEnt is Entity)
{
pPoly.IntersectWith(pEnt, Intersect.OnBothOperands, ints, 0, 0);
if (ints.Count > 0)
{
for (int i = 0; i < ints.Count; i++)
{
if (!points.Contains(ints))
{
points.Add(ints);
// save distance and objectid of object that cross the path object
distid.Add(pPoly.GetDistAtPoint(ints), pEnt.ObjectId);
}
}
}
}
}
}
// we have some points to play with...
if (points.Count > 0)
{
points.Add(p0); // add the start and end points
points.Add(p1); // the sort is based using the starting point inside the sortedlist!
double dist;
for (int i = 0; i < points.Count; i++)
{
Point3d pt = points;
dist = pPoly.GetDistAtPoint(pt);
distpoint.Add(dist, pt); // do the sorting
}
ed.WriteMessage("\nDistance and Points: ");
foreach (KeyValuePair kvp in distpoint)
{
ed.WriteMessage("\nDistance = {0}, Point = {1}", kvp.Key, kvp.Value);
}
ed.WriteMessage("\nDistance and Entities: ");
int c = 1; //color test
foreach (KeyValuePair kvp in distid)
{
ed.WriteMessage("\nDistance = {0}, Entity = {1}", kvp.Key, kvp.Value.ToString());
Entity ent = tr.GetObject(kvp.Value, OpenMode.ForWrite, false) as Entity;
ent.ColorIndex = c; //color test
c++;
}
}//if points
}//if pPoly
}//foreach id
tr.Commit();
}
}
//cleaning...
ints.Clear();
points.Clear();
distpoint.Clear();
}
[CommandMethod("GETORDER")]
static public void testentsinorder()
{
getEntitiesInOrder("LINE"); //test with lines
}
[/code]
Output:
Distance and Points:
Distance = 0, Point = (21.5574468973974,16.2295411999676,0)
Distance = 14.1908675942083, Point = (34.1415805878428,22.7885275802971,0)
Distance = 38.7289809015161, Point = (55.9014128405577,34.1299871431571,0)
Distance = 50.2813723728079, Point = (66.1458068322563,39.4694759247564,0)
Distance and Entities:
Distance = 14.1908675942083, Entity = (2130264224)
Distance = 38.7289809015161, Entity = (2130264216)