<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: find nearest lines(or pline) from selected point in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12050461#M8554</link>
    <description>&lt;P&gt;This should work (returns null if none curve found on the axis):&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        public static Curve GetClosestCurve(IEnumerable&amp;lt;Curve&amp;gt; curves, Point3d point, Vector3d axis)
        {
            using (var xline = new Xline { BasePoint = point, UnitDir = axis })
            {
                var curvesOnAxis = new Dictionary&amp;lt;Curve, Point3d&amp;gt;();
                Curve closestCurve = null;
                double minDist = double.MaxValue;
                foreach (var curve in curves)
                {
                    var points = new Point3dCollection();
                    curve.IntersectWith(xline, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
                    if (0 &amp;lt; points.Count)
                    {
                        double dist = points.Cast&amp;lt;Point3d&amp;gt;().Select(p =&amp;gt; p.DistanceTo(point)).Min();
                        if (dist &amp;lt; minDist)
                        {
                            closestCurve = curve;
                            minDist = dist;
                        }
                    }
                }
                return closestCurve;
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Jun 2023 14:48:15 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2023-06-21T14:48:15Z</dc:date>
    <item>
      <title>find nearest lines(or pline) from selected point</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12026831#M8550</link>
      <description>&lt;P&gt;Hi, I tried to trace boundary from selected point (close boundary with polyline) but I need to find/get nearest lines.&lt;BR /&gt;can some one help me with this.&lt;/P&gt;&lt;P&gt;I tried:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Entity pl = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
//entity is polyline
Extents3d extents = pl.GeometricExtents;
Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1226216i7971436DD141EF06/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Need to find lines from point in all direction&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 05:52:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12026831#M8550</guid>
      <dc:creator>iPranavKulkarni</dc:creator>
      <dc:date>2023-06-12T05:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: find nearest lines(or pline) from selected point</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12027414#M8551</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Here's a way to get the closest curve to a point.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        public static Curve GetClosestCurve(IEnumerable&amp;lt;Curve&amp;gt; curves, Point3d pt)
        {
            var closest = curves.First();
            var minDist = pt.DistanceTo(closest.GetClosestPointTo(pt, false));
            foreach (var curve in curves.Skip(1))
            {
                double dist = pt.DistanceTo(curve.GetClosestPointTo(pt, false));
                if (dist &amp;lt; minDist)
                {
                    minDist = dist;
                    closest = curve;
                }
            }
            return closest;
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The same thing with F#:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;let getClosestCurve (pt: Point3d) =
    Seq.minBy (fun (c: Curve) -&amp;gt; pt.DistanceTo(c.GetClosestPointTo(pt, false)))&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 12 Jun 2023 10:59:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12027414#M8551</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-06-12T10:59:49Z</dc:date>
    </item>
    <item>
      <title>Re: find nearest lines(or pline) from selected point</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12027443#M8552</link>
      <description>&lt;P&gt;.NET 7 has a &lt;A href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.minby?view=net-7.0&amp;amp;viewFallbackFrom=netframework-4.8.1" target="_blank" rel="noopener"&gt;MinBy&lt;/A&gt; (and MaxBy) extension method but as we're limited to .NET Framework with AutoCAD, we need to build it (the following extension methods are taken from &lt;A href="https://github.com/gileCAD/Gile.AutoCAD.Extension" target="_blank" rel="noopener"&gt;Gile.AutoCAD.Extension library&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;    public static class LinqExtension
    {
        /// &amp;lt;summary&amp;gt;
        /// Gets the smallest item of the sequence using the default comparer with the 'selector' function returned values.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name="TSource"&amp;gt;Type the items.&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;typeparam name="TKey"&amp;gt;Type of the returned value of 'selector' function.&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name="source"&amp;gt;Sequence to which the method applies.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="selector"&amp;gt;Mapping function from 'TSource' to 'TKey'.&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;The smallest item in the sequence.&amp;lt;/returns&amp;gt;
        /// &amp;lt;exception cref="System.ArgumentNullException"&amp;gt;Thrown if 'source' is null.&amp;lt;/exception&amp;gt;
        /// &amp;lt;exception cref="System.ArgumentNullException"&amp;gt;Thrown if 'selector' is null.&amp;lt;/exception&amp;gt;
        public static TSource MinBy&amp;lt;TSource, TKey&amp;gt;(
            this IEnumerable&amp;lt;TSource&amp;gt; source,
            Func&amp;lt;TSource, TKey&amp;gt; selector)
        {
            return source.MinBy(selector, Comparer&amp;lt;TKey&amp;gt;.Default);
        }

        /// &amp;lt;summary&amp;gt;
        /// Gets the smallest item of the sequence using the 'comparer' with the 'selector' function returned values.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;typeparam name="TSource"&amp;gt;Type the items.&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;typeparam name="TKey"&amp;gt;Type of the returned value of 'selector' function.&amp;lt;/typeparam&amp;gt;
        /// &amp;lt;param name="source"&amp;gt;Sequence to which the method applies.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="selector"&amp;gt;Mapping function from 'TSource' to 'TKey'.&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="comparer"&amp;gt;Comparer used for the type 'TKey'.&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;The smallest item in the sequence.&amp;lt;/returns&amp;gt;
        /// &amp;lt;exception cref="System.ArgumentNullException"&amp;gt;Thrown if 'source' is null.&amp;lt;/exception&amp;gt;
        /// &amp;lt;exception cref="System.ArgumentNullException"&amp;gt;Thrown if 'selector' is null.&amp;lt;/exception&amp;gt;
        /// &amp;lt;exception cref="System.ArgumentNullException"&amp;gt;Thrown if 'comparer' is null.&amp;lt;/exception&amp;gt;
        public static TSource MinBy&amp;lt;TSource, TKey&amp;gt;(
            this IEnumerable&amp;lt;TSource&amp;gt; source,
            Func&amp;lt;TSource, TKey&amp;gt; selector,
            IComparer&amp;lt;TKey&amp;gt; comparer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));
            if (comparer == null)
                throw new ArgumentNullException(nameof(comparer));
            using (var iterator = source.GetEnumerator())
            {
                if (!iterator.MoveNext())
                    throw new InvalidOperationException("Empty sequence");

                var min = iterator.Current;
                var minKey = selector(min);
                while (iterator.MoveNext())
                {
                    var current = iterator.Current;
                    var currentKey = selector(current);
                    if (comparer.Compare(currentKey, minKey) &amp;lt; 0)
                    {
                        min = current;
                        minKey = currentKey;
                    }
                }
                return min;
            }
        }
    }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now the GetClosestCurve can be simply written:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static Curve GetClosestCurve(IEnumerable&amp;lt;Curve&amp;gt; curves, Point3d pt) =&amp;gt;
            curves.MinBy(c =&amp;gt; pt.DistanceTo(c.GetClosestPointTo(pt, false)));&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 11:41:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12027443#M8552</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-06-12T11:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: find nearest lines(or pline) from selected point</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12049728#M8553</link>
      <description>&lt;P&gt;Thanks!&lt;BR /&gt;Is this possible with the direction(axis)?&lt;/P&gt;&lt;P&gt;I need to get closest line in X-dir, Y-dir.(left right up and down)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jun 2023 11:22:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12049728#M8553</guid>
      <dc:creator>iPranavKulkarni</dc:creator>
      <dc:date>2023-06-21T11:22:54Z</dc:date>
    </item>
    <item>
      <title>Re: find nearest lines(or pline) from selected point</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12050461#M8554</link>
      <description>&lt;P&gt;This should work (returns null if none curve found on the axis):&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        public static Curve GetClosestCurve(IEnumerable&amp;lt;Curve&amp;gt; curves, Point3d point, Vector3d axis)
        {
            using (var xline = new Xline { BasePoint = point, UnitDir = axis })
            {
                var curvesOnAxis = new Dictionary&amp;lt;Curve, Point3d&amp;gt;();
                Curve closestCurve = null;
                double minDist = double.MaxValue;
                foreach (var curve in curves)
                {
                    var points = new Point3dCollection();
                    curve.IntersectWith(xline, Intersect.OnBothOperands, points, IntPtr.Zero, IntPtr.Zero);
                    if (0 &amp;lt; points.Count)
                    {
                        double dist = points.Cast&amp;lt;Point3d&amp;gt;().Select(p =&amp;gt; p.DistanceTo(point)).Min();
                        if (dist &amp;lt; minDist)
                        {
                            closestCurve = curve;
                            minDist = dist;
                        }
                    }
                }
                return closestCurve;
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jun 2023 14:48:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12050461#M8554</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-06-21T14:48:15Z</dc:date>
    </item>
    <item>
      <title>Re: find nearest lines(or pline) from selected point</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12052051#M8555</link>
      <description>&lt;P&gt;thanks for your support. It worked for me with small changes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2023 06:16:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12052051#M8555</guid>
      <dc:creator>iPranavKulkarni</dc:creator>
      <dc:date>2023-06-22T06:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: find nearest lines(or pline) from selected point</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12052064#M8556</link>
      <description>&lt;P&gt;thanks with your support I can get all the lines and closest line from that.&lt;BR /&gt;now I am finding some of lines in all direction.&lt;/P&gt;&lt;P&gt;need to get boundary (trace boundary)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="iPranavKulkarni_0-1687415132425.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1231484i329AC763A8BF4EDF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="iPranavKulkarni_0-1687415132425.png" alt="iPranavKulkarni_0-1687415132425.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2023 06:39:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-nearest-lines-or-pline-from-selected-point/m-p/12052064#M8556</guid>
      <dc:creator>iPranavKulkarni</dc:creator>
      <dc:date>2023-06-22T06:39:56Z</dc:date>
    </item>
  </channel>
</rss>

