Author Topic: find the nearest object from a point  (Read 447 times)

0 Members and 1 Guest are viewing this topic.

flenoir

  • Mosquito
  • Posts: 1
find the nearest object from a point
« on: March 30, 2018, 10:14:44 AM »
I am looking for a solution to find the nearest object (polyline in most cases) from a point.

Any ideas?

gile

  • Water Moccasin
  • Posts: 2166
  • Marseille, France
Re: find the nearest object from a point
« Reply #1 on: March 30, 2018, 10:58:28 AM »
Hi,

Here's one, probably not the most efficient, but you could get inspiration from it.

Code - C#: [Select]
  1.        [CommandMethod("FINDNEARESTCURVE", CommandFlags.Redraw)]
  2.        public void FindNearestCurve()
  3.        {
  4.            var doc = Application.DocumentManager.MdiActiveDocument;
  5.            var db = doc.Database;
  6.            var ed = doc.Editor;
  7.  
  8.            var ppr = ed.GetPoint("\nPick a point: ");
  9.            if (ppr.Status != PromptStatus.OK)
  10.                return;
  11.            var pt = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
  12.            var curveClass = RXObject.GetClass(typeof(Curve));
  13.            ObjectId closestId = ObjectId.Null;
  14.            using (var tr = new OpenCloseTransaction())
  15.            {
  16.                var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
  17.                double dist = double.MaxValue;
  18.                foreach (ObjectId id in space)
  19.                {
  20.                    if (id.ObjectClass.IsDerivedFrom(curveClass))
  21.                    {
  22.                        var curve = (Curve)tr.GetObject(id, OpenMode.ForRead);
  23.                        double d = curve.GetClosestPointTo(pt, false).DistanceTo(pt);
  24.                        if (d < dist)
  25.                        {
  26.                            closestId = id;
  27.                            dist = d;
  28.                        }
  29.                    }
  30.                }
  31.            }
  32.            if (!closestId.IsNull)
  33.            {
  34.                ed.SetImpliedSelection(new[] { closestId });
  35.            }
  36.        }
Speaking English as a French Frog

jmaeding

  • Bull Frog
  • Posts: 300
  • How many of you have never raised your hand?
Re: find the nearest object from a point
« Reply #2 on: April 06, 2018, 05:31:00 PM »
note that a distance test is very expensive.
So expensive that when you are dealing with thousands of things in loops, you must weed out items that are not candidates.
You can do that with bounding boxes if closest means in any direction.
If you mean closest "perpendicular offset" like civils do, you must do the expensive test on every one, no short cuts.
James Maeding

BIGAL

  • Newt
  • Posts: 200
  • 30 + years of using Autocad
Re: find the nearest object from a point
« Reply #3 on: April 06, 2018, 09:06:20 PM »
1st I dont write in .Net Maybe using a crossing window by the user rather than a random search could draw a circle using grread to limit objects being searched. Stepping the circle is the other way again limit the outside. If its a pline vertice type request can use offsets to keep searching for objects but again its a task that can be a bit slow.
A man who never made a mistake never made anything

dgorsman

  • Water Moccasin
  • Posts: 2337
Re: find the nearest object from a point
« Reply #4 on: April 09, 2018, 09:52:13 AM »
Better define both "object" and "nearest" e.g. if it's a polyline, how are you measuring - closest vertex?  Any point along it's length?  What about block references - is this the insert point?  A point on it's child geometry?
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}