<?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 : Ordering blocks based on distance along a polyline in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6397606#M35663</link>
    <description>&lt;P&gt;Thanks very much for the code and explanation. Slowly getting the hang of c# and CAD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Jun 2016 10:24:20 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-06-22T10:24:20Z</dc:date>
    <item>
      <title>Ordering blocks based on distance along a polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6395849#M35659</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, let me provide some context to my problem. I have a number of blocks which are placed along&amp;nbsp;a polyline. What I want to achieve is a command whereby the user selects a number of blocks, and then they are ordered based on their distance from the start of the polyline.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;PRE&gt;TypedValue[] filterlist = new TypedValue[1];
            filterlist[0] = new TypedValue(0, "INSERT");

            SelectionFilter filter = new SelectionFilter(filterlist);
            PromptSelectionResult psr1 = ed.GetSelection(filter);

            if (psr1.Status != PromptStatus.OK)
                return;

            SelectionSet psrSet = psr1.Value;
           
            ObjectIdCollection obs = new ObjectIdCollection();
            foreach (ObjectId id1 in psrSet.GetObjectIds())
            {
                obs.Add(id1);
            }&lt;/PRE&gt;&lt;P&gt;This first section allows the user to select some blocks, puts them into a selection set and then creates an ObjectIDCollection to store them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is at this point that I am not sure how to order them based on distance from start point. I can create a&amp;nbsp;string that tells me the distance to the start point of each block but can't get that final step to actually order them based on this criteria.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Matt&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jun 2016 13:40:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6395849#M35659</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-06-21T13:40:45Z</dc:date>
    </item>
    <item>
      <title>Re : Ordering blocks based on distance along a polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6396000#M35660</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the &lt;A href="https://msdn.microsoft.com/en-us/library/bb534966(v=vs.100).aspx" target="_blank"&gt;OrderBy()&lt;/A&gt; Linq extension method.&lt;/P&gt;
&lt;P&gt;It requires a key selector function as argument.&lt;/P&gt;
&lt;P&gt;The key selector function could return:&lt;/P&gt;
&lt;PRE&gt;pline.GetDistAtPoint(pline.GetClosestPointTo(blockRef.Position, false));&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jun 2016 14:28:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6396000#M35660</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-06-21T14:28:34Z</dc:date>
    </item>
    <item>
      <title>Re : Ordering blocks based on distance along a polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6396099#M35661</link>
      <description>&lt;P&gt;Thanks! I'm just struggling to set up the linq function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code below calls all the blocks into an array (I think):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;var obj = new BlockReference[obs.Count];
                for (int i = 0; i &amp;lt; obs.Count; i++)
                {
                    obj[i] = tr.GetObject(obs[i], OpenMode.ForRead) as BlockReference;
                }&lt;/PRE&gt;&lt;P&gt;and then the sorting bit I still can't get to work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;var sorted = obj.OrderBy(c.GetDistAtPoint(c.GetClosestPointTo(obj[i].Position, false)));&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jun 2016 14:59:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6396099#M35661</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-06-21T14:59:52Z</dc:date>
    </item>
    <item>
      <title>Re : Ordering blocks based on distance along a polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6396194#M35662</link>
      <description>&lt;P&gt;OrderBy applies to a sequence (&lt;A href="https://msdn.microsoft.com/en-us/library/9eekhta0(v=vs.100).aspx" target="_blank"&gt;IEnumerable&lt;/A&gt;) of some Type instances, not to a single instance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a little snippet:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;IEnumerable&amp;lt;BlockReference&amp;gt; sortedBlocks = psr.Value
    .GetObjectIds() 
    .Select(id =&amp;gt; (BlockReference)tr.GetObject(id, OpenMode.ForRead)) // map the ObjectId array into a BlockReference sequence
    .OrderBy(br =&amp;gt; pline.GetDistAtPoint(pline.GetClosestPointTo(br.Position, false))); // order the blockReference sequence&lt;/PRE&gt;
&lt;P&gt;sortedBlocks type is : IEnumerable&amp;lt;BlockReference&amp;gt;.&lt;/P&gt;
&lt;P&gt;You can iterate it with foreach, convert it to a BlockReference[] with ToArray() method or to a List&amp;lt;BlockRefernce&amp;gt; with Tolist().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need an ObjectIdColletcion, you can map back the sequence to an ObjectId array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ObjectIdCollection ids = new ObjectIdCollection(sortedBlocks.Select(br =&amp;gt; br.ObjectId).ToArray());&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jun 2016 15:34:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6396194#M35662</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-06-21T15:34:29Z</dc:date>
    </item>
    <item>
      <title>Re : Ordering blocks based on distance along a polyline</title>
      <link>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6397606#M35663</link>
      <description>&lt;P&gt;Thanks very much for the code and explanation. Slowly getting the hang of c# and CAD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jun 2016 10:24:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/ordering-blocks-based-on-distance-along-a-polyline/m-p/6397606#M35663</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-06-22T10:24:20Z</dc:date>
    </item>
  </channel>
</rss>

