<?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: How to get all blockreference in a drawing in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818191#M50268</link>
    <description>&lt;P&gt;Hi santosh,&lt;/P&gt;&lt;P&gt;Can't we use that to select blockreference from paper space&lt;/P&gt;</description>
    <pubDate>Fri, 29 Mar 2013 09:04:23 GMT</pubDate>
    <dc:creator>chockalingam</dc:creator>
    <dc:date>2013-03-29T09:04:23Z</dc:date>
    <item>
      <title>How to get all blockreference in a drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818179#M50264</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;I need to get list of all blockreference in &amp;nbsp;a drawing without iterating through all entities in the drawing. Can any one help in this scenario.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance......&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2013 08:39:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818179#M50264</guid>
      <dc:creator>chockalingam</dc:creator>
      <dc:date>2013-03-29T08:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all blockreference in a drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818181#M50265</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;SPAN&gt;&lt;A target="_self" href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1003527"&gt;chockalingam&lt;/A&gt;,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If you do not want to iterate all the entities in drawing or from the database, you can use the API to select only the blockreferences.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Which inturn selects only the blockreferences from the modelspace.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Refer the below Kean's link&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A target="_blank" href="http://through-the-interface.typepad.com/through_the_interface/2006/09/getting_autocad.html"&gt;http://through-the-interface.typepad.com/through_the_interface/2006/09/getting_autocad.html&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You can add a selection filter to select only the blocks and use selectAll&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Accept as Solution and kudos if this is solution for your Query&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2013 08:54:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818181#M50265</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-03-29T08:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all blockreference in a drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818186#M50266</link>
      <description>&lt;P&gt;Thanks santosh&lt;A target="_self" href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/1014993"&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2013 09:00:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818186#M50266</guid>
      <dc:creator>chockalingam</dc:creator>
      <dc:date>2013-03-29T09:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all blockreference in a drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818187#M50267</link>
      <description>&lt;P&gt;Glad i could be your help...&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2013 09:00:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818187#M50267</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-03-29T09:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all blockreference in a drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818191#M50268</link>
      <description>&lt;P&gt;Hi santosh,&lt;/P&gt;&lt;P&gt;Can't we use that to select blockreference from paper space&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2013 09:04:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818191#M50268</guid>
      <dc:creator>chockalingam</dc:creator>
      <dc:date>2013-03-29T09:04:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all blockreference in a drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818209#M50269</link>
      <description>&lt;P&gt;The code from Kean's blog that you were referred to by another reply is not really the best way to solve the problem, as it will not work with dynamic blocks, and only gives you blocks inserted into model or paper space (which may or may not be what you intended). &amp;nbsp;Since&amp;nbsp;&lt;SPAN&gt;you didn't mention if you wanted only those BlockReferences in the current space, model &amp;amp; paper space, or all block references, including those inserted into other blocks, I'll assume nothing, since a robust solution will give you the option.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is a much faster and more direct way to solve the problem, which gives you the option of including every BlockReference regardless of what owner block it's inserted into, AND&lt;SPAN&gt;&amp;nbsp;also gives you all dynamic BlockReferences.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;public static class AcDbLinqExtensionMethods
{
   /// &amp;lt;summary&amp;gt;
   /// Get all references to the given BlockTableRecord, including 
   /// references to anonymous dynamic BlockTableRecords.
   /// &amp;lt;/summary&amp;gt;

   public static IEnumerable&amp;lt;BlockReference&amp;gt; GetBlockReferences( 
      this BlockTableRecord btr, 
      OpenMode mode = OpenMode.ForRead, 
      bool directOnly = true )
   {
      if( btr == null )
         throw new ArgumentNullException( "btr" );
      var tr = btr.Database.TransactionManager.TopTransaction;
      if( tr == null )
         throw new InvalidOperationException( "No transaction" );
      var ids = btr.GetBlockReferenceIds( directOnly, true );
      int cnt = ids.Count;
      for( int i = 0; i &amp;lt; cnt; i++ )
      {
         yield return (BlockReference) 
            tr.GetObject( ids[i], mode, false, false );
      }
      if( btr.IsDynamicBlock )
      {
         BlockTableRecord btr2 = null;
         var blockIds = btr.GetAnonymousBlockIds();
         cnt = blockIds.Count;
         for( int i = 0; i &amp;lt; cnt; i++ )
         {
            btr2 = (BlockTableRecord) tr.GetObject( blockIds[i], 
               OpenMode.ForRead, false, false );
            ids = btr2.GetBlockReferenceIds( directOnly, true );
            int cnt2 = ids.Count;
            for( int j = 0; j &amp;lt; cnt2; j++ )
            {
               yield return (BlockReference) 
                  tr.GetObject( ids[j], mode, false, false );
            }
         }
      }
   }
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Using the above, you just open the BlockTableRecord whose references you need, and call GetBlockReferences() on the open BlockTableRecord, and use foreach() to iterate over the blocks. If you only want block references from model space, you can simply check the BlockId property of each BlockReference and compare it to the ObjectId of the model space BlockTableRecord, to filter out BlockReferences that aren't in model space. If you wanted only BlockReferences inserted into paper space, you can do the same as described above, except comparing the BlockId property of each BlockReference to the ObjectId of the paper space BlockTableRecord.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2013 10:09:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818209#M50269</guid>
      <dc:creator>DiningPhilosopher</dc:creator>
      <dc:date>2013-03-29T10:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all blockreference in a drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818321#M50271</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you want to use the Editor.selectAll() method, you can add this TypedValue to the SelectionFilter:&lt;/P&gt;&lt;P&gt;new TypedValue(410, "~Model")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, the Editor.SelectAll() does internally iterate the drawing and would not be faster than iterating the layouts BlockTableRecords as shown below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only way I know to avoid iterating BlockTableRecords is using the BlockTableRecord.GetBlockReferenceIds() method as shown by Tony in the upper message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        private IEnumerable&amp;lt;BlockReference&amp;gt; GetPaperSpaceBlockReferences(Database db)
        {
            Transaction tr = db.TransactionManager.TopTransaction;
            if (tr == null)
                throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NotTopTransaction);

            RXClass rxc = RXClass.GetClass(typeof(BlockReference));
            DBDictionary layouts = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
            foreach (var entry in layouts)
            {
                if (entry.Key != "Model")
                {
                    Layout lay = (Layout)tr.GetObject(entry.Value, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(lay.BlockTableRecordId, OpenMode.ForRead);
                    foreach (ObjectId id in btr)
                    {
                        if (id.ObjectClass == rxc)
                        {
                            yield return (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                        }
                    }
                }
            }
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2013 13:12:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/3818321#M50271</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2013-03-29T13:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all blockreference in a drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/4686123#M50272</link>
      <description>Thanks. It's good solution for me.</description>
      <pubDate>Wed, 11 Dec 2013 08:18:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-get-all-blockreference-in-a-drawing/m-p/4686123#M50272</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2013-12-11T08:18:51Z</dc:date>
    </item>
  </channel>
</rss>

