<?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: Is it possible to get the nearest text ? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7807468#M27219</link>
    <description>Thanks Gasty, I have learnt something.</description>
    <pubDate>Sun, 25 Feb 2018 22:56:41 GMT</pubDate>
    <dc:creator>TiStars</dc:creator>
    <dc:date>2018-02-25T22:56:41Z</dc:date>
    <item>
      <title>Is it possible to get the nearest text ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7799899#M27212</link>
      <description>&lt;P&gt;When I specify a start point and end point of line, how to get text on the&amp;nbsp;top side or left side of the line ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="line.png" style="width: 681px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/466280i715EFA479BFA4B9F/image-size/large?v=v2&amp;amp;px=999" role="button" title="line.png" alt="line.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 15:10:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7799899#M27212</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-22T15:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to get the nearest text ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7801166#M27213</link>
      <description>&lt;P&gt;it's not too difficult but:&lt;/P&gt;
&lt;P&gt;1. what did you do so far;&lt;/P&gt;
&lt;P&gt;2. what restrictions, i.e. angle of text== angle of line, what layer, what text specs, what is the max distance, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 20:37:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7801166#M27213</guid>
      <dc:creator>SENL1362</dc:creator>
      <dc:date>2018-02-22T20:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to get the nearest text ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7801747#M27214</link>
      <description>&lt;P&gt;Thanks for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Assume that we don't consider the layer and text specs , but angle of text== angle of line,&amp;nbsp; the max distance is 100mm, how to get the nearest text ?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 00:32:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7801747#M27214</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-23T00:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to get the nearest text ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7801874#M27215</link>
      <description>&lt;P&gt;I usually&amp;nbsp;will get entities by loop.&lt;/P&gt;&lt;P&gt;Blow is a part of my code for your reference.&amp;nbsp; I was going to get nearest line to a Text.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if (textId.ObjectClass.IsDerivedFrom(textClass))
                {
                    DBText text = (DBText)tr.GetObject(textId, OpenMode.ForRead);
                    var owner = (BlockTableRecord)tr.GetObject(text.OwnerId, OpenMode.ForRead);

                    foreach (ObjectId id in owner)
                    {
                        if (id.ObjectClass.IsDerivedFrom(lineClass))
                        {
                            Line tmpline = (Line)tr.GetObject(id, OpenMode.ForRead);
                            double d = text.Position.DistanceTo(tmpline.GetClosestPointTo(text.Position, false));
                            if (d &amp;lt; distance)
                            {
                                distance = d;
                                line = tmpline;
                            }
                        }
                        
                    }
                    
                }&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2018 02:22:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7801874#M27215</guid>
      <dc:creator>TiStars</dc:creator>
      <dc:date>2018-02-23T02:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to get the nearest text ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7801977#M27216</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I understand that the OP need to get the closest text to a given&amp;nbsp;line, and your code is getting the closest line to&amp;nbsp; given text. You also should note that a crossing line from the middle of the given line would fail if the crossing line pass trough the space between letters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A better approach would be to detect the text using a crossing window or crossing polygon: you need to obtain the end points of the line, then use them to get the angle of the line, now create four new points aiming 90°/180° (pi/2, pi) from the angle of the line (using polar coordinates), with those points create a crossing polygon with the points aiming 90°, add a filter to the proper object&amp;nbsp; and attributes of the text (layer, color, etc) and check if the selection contains the object, if not, repeat with the points aiming 180°.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Gasty.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 03:51:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7801977#M27216</guid>
      <dc:creator>hgasty1001</dc:creator>
      <dc:date>2018-02-23T03:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to get the nearest text ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7802272#M27217</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Inspired by &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/587082"&gt;@hgasty1001&lt;/a&gt; method, you could start from this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("TEST", CommandFlags.Redraw)]
        public void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var peo = new PromptEntityOptions("\nSelect line: ");
            peo.SetRejectMessage("\nSelected object is not a Line.");
            peo.AddAllowedClass(typeof(Line), true);
            var per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK)
                return;

            var wcs2ucs = ed.CurrentUserCoordinateSystem.Inverse();
            dynamic line = per.ObjectId;
            Point3d startPoint = line.StartPoint.TransformBy (wcs2ucs);
            Point3d endPoint = line.EndPoint.TransformBy(wcs2ucs);

            Vector3d vector;
            if (Math.Abs(startPoint.X - endPoint.X) &amp;lt; 1e-9)
            {
                vector = new Vector3d(-100.0, 0.0, 0.0);
            }
            else if (Math.Abs(startPoint.Y - endPoint.Y) &amp;lt; 1e-9)
            {
                vector = new Vector3d(0.0, 100.0, 0.0);
            }
            else
            {
                ed.WriteMessage("\nThe selected line is neither Horizontal nor vertical.");
                return;
            }

            var filter = new SelectionFilter(new[] { new TypedValue(0, "TEXT") });
            Point3dCollection polygon = new Point3dCollection();
            polygon.Add(startPoint);
            polygon.Add(startPoint + vector);
            polygon.Add(endPoint + vector);
            polygon.Add(endPoint);
            var psr = ed.SelectCrossingPolygon(polygon, filter);
            if (psr.Status == PromptStatus.OK)
                ed.SetImpliedSelection(psr.Value);
            else
                ed.WriteMessage("\nNone text found.");
        }&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2018 08:04:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7802272#M27217</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-02-23T08:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to get the nearest text ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7802470#M27218</link>
      <description>&lt;P&gt;A slightly more general approach, any Line Angle will do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;       [CommandMethod("TestTextNearLine", CommandFlags.Redraw)]
        public static void TestTextNearLine()
        {
            Document doc = null;
            Editor ed = null;
            Database db = null;

            double maxTxtDist = 100;
            double maxAngDiff = Math.PI / 180;

            try
            {
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                ed = doc.Editor;
                db = doc.Database;

                var peo = new PromptEntityOptions("\nSelect Line: ");
                peo.SetRejectMessage("\nLines only");
                peo.AddAllowedClass(typeof(Line), false);
                peo.AllowObjectOnLockedLayer = true;
                peo.AllowNone = true;
                var per = ed.GetEntity(peo);
                if (per.Status != PromptStatus.OK)
                {
                    return;
                }

                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var ln = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Line;
                    var angle = ln.Angle;
                    var btr = (BlockTableRecord)tr.GetObject(ln.OwnerId, OpenMode.ForRead);


                    var dxRange = -maxTxtDist * Math.Sin(angle);
                    var dyRange= +maxTxtDist * Math.Cos(angle);

                    var selRange = new Point3dCollection();
                    selRange.Add(ln.StartPoint);
                    selRange.Add(ln.EndPoint);
                    selRange.Add(new Point3d(ln.EndPoint.X + dxRange, ln.EndPoint.Y + dyRange, ln.StartPoint.Z));
                    selRange.Add(new Point3d(ln.StartPoint.X + dxRange, ln.StartPoint.Y + dyRange, ln.StartPoint.Z));

                    //Just for debugging purposes
                    {
                        var pl = new Polyline();
                        for (int i = 0; i &amp;lt; selRange.Count; i++)
                        {
                            pl.AddVertexAt(i, new Point2d(selRange[i].X, selRange[i].Y), 0, 0, 0);
                        }
                        pl.Closed = true;
                        btr.UpgradeOpen();
                        btr.AppendEntity(pl);
                        tr.AddNewlyCreatedDBObject(pl, true);
                    }

                    var filter = new SelectionFilter(new[] { new TypedValue(0, "TEXT") });
                    var psr = ed.SelectWindowPolygon(selRange, filter);
                    //var psr = ed.SelectCrossingPolygon(selRange, filter);
                    if (psr.Status == PromptStatus.OK)
                    {
                        var selTxtIds = psr.Value.GetObjectIds().ToList();

                        //Skip Text when Angle &amp;lt;&amp;gt; Line Angle within ONE degree
                        foreach (var id in selTxtIds.ToList())
                        {
                            //Find text where Angle==Line Angle (or +180 degrees within ONE degree
                            var txt = tr.GetObject(id, OpenMode.ForRead) as DBText;
                            if (txt != null &amp;amp;&amp;amp; !string.IsNullOrWhiteSpace(txt.TextString))
                            {
                                if (Math.Abs(txt.Rotation - ln.Angle) &amp;gt; maxAngDiff &amp;amp;&amp;amp; Math.Abs(txt.Rotation + Math.PI - ln.Angle) * 180.0 / Math.PI &amp;gt; maxAngDiff)
                                {
                                    selTxtIds.Remove(id);
                                }
                            }
                        }
                        ed.SetImpliedSelection(selTxtIds.ToArray());
                    }
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed?.WriteMessage($"\n{ex.Message}");
            }
        }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="textnearline_0.png" style="width: 480px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/466680i44F9F4EE33C0AC04/image-size/large?v=v2&amp;amp;px=999" role="button" title="textnearline_0.png" alt="textnearline_0.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="textnearline.png" style="width: 508px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/466681iBFCB40D67F37710B/image-size/large?v=v2&amp;amp;px=999" role="button" title="textnearline.png" alt="textnearline.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 10:04:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7802470#M27218</guid>
      <dc:creator>SENL1362</dc:creator>
      <dc:date>2018-02-23T10:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to get the nearest text ?</title>
      <link>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7807468#M27219</link>
      <description>Thanks Gasty, I have learnt something.</description>
      <pubDate>Sun, 25 Feb 2018 22:56:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/is-it-possible-to-get-the-nearest-text/m-p/7807468#M27219</guid>
      <dc:creator>TiStars</dc:creator>
      <dc:date>2018-02-25T22:56:41Z</dc:date>
    </item>
  </channel>
</rss>

