.NET

Reply
Valued Contributor
chockalingam
Posts: 82
Registered: ‎03-06-2012
Message 1 of 8 (1,105 Views)
Accepted Solution

How to get all blockreference in a drawing

1105 Views, 7 Replies
03-29-2013 01:39 AM

Hi all,

   I need to get list of all blockreference in  a drawing without iterating through all entities in the drawing. Can any one help in this scenario.

 

Thanks in advance......

Hi chockalingam,

 

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.

 

Which inturn selects only the blockreferences from the modelspace.

 

Refer the below Kean's link

http://through-the-interface.typepad.com/through_the_interface/2006/09/getting_autocad.html

 

You can add a selection filter to select only the blocks and use selectAll

 

 

 

 

Accept as Solution and kudos if this is solution for your Query

Mentor
santoshr0114
Posts: 162
Registered: ‎03-21-2012
Message 2 of 8 (1,099 Views)

Re: How to get all blockreference in a drawing

03-29-2013 01:51 AM in reply to: chockalingam

Hi chockalingam,

 

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.

 

Which inturn selects only the blockreferences from the modelspace.

 

Refer the below Kean's link

http://through-the-interface.typepad.com/through_the_interface/2006/09/getting_autocad.html

 

You can add a selection filter to select only the blocks and use selectAll

 

 

 

 

Accept as Solution and kudos if this is solution for your Query

Regards
Santosh
Valued Contributor
chockalingam
Posts: 82
Registered: ‎03-06-2012
Message 3 of 8 (1,093 Views)

Re: How to get all blockreference in a drawing

03-29-2013 02:00 AM in reply to: chockalingam

Thanks santosh

Mentor
santoshr0114
Posts: 162
Registered: ‎03-21-2012
Message 4 of 8 (1,091 Views)

Re: How to get all blockreference in a drawing

03-29-2013 02:00 AM in reply to: chockalingam

Glad i could be your help...

Regards
Santosh
Valued Contributor
chockalingam
Posts: 82
Registered: ‎03-06-2012
Message 5 of 8 (1,086 Views)

Re: How to get all blockreference in a drawing

03-29-2013 02:04 AM in reply to: santoshr0114

Hi santosh,

Can't we use that to select blockreference from paper space

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 6 of 8 (1,067 Views)

Re: How to get all blockreference in a drawing

03-29-2013 03:04 AM in reply to: chockalingam

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).  Since you didn't mention if you wanted only those BlockReferences in the current space, model & 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.

 

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 also gives you all dynamic BlockReferences.

 

public static class AcDbLinqExtensionMethods
{
   /// <summary>
   /// Get all references to the given BlockTableRecord, including 
   /// references to anonymous dynamic BlockTableRecords.
   /// </summary>

   public static IEnumerable<BlockReference> 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 < 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 < 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 < cnt2; j++ )
            {
               yield return (BlockReference) 
                  tr.GetObject( ids[j], mode, false, false );
            }
         }
      }
   }
}

 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.

 

*Expert Elite*
_gile
Posts: 2,084
Registered: ‎04-29-2006
Message 7 of 8 (1,045 Views)

Re: How to get all blockreference in a drawing

03-29-2013 06:12 AM in reply to: DiningPhilosopher

Hi,

 

if you want to use the Editor.selectAll() method, you can add this TypedValue to the SelectionFilter:

new TypedValue(410, "~Model")

 

Anyway, the Editor.SelectAll() does internally iterate the drawing and would not be faster than iterating the layouts BlockTableRecords as shown below.

 

The only way I know to avoid iterating BlockTableRecords is using the BlockTableRecord.GetBlockReferenceIds() method as shown by Tony in the upper message.

 

        private IEnumerable<BlockReference> 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);
                        }
                    }
                }
            }
        }

 

Gilles Chanteau
Active Member
Barabasishe
Posts: 8
Registered: ‎10-17-2012
Message 8 of 8 (709 Views)

Re: How to get all blockreference in a drawing

12-11-2013 12:18 AM in reply to: DiningPhilosopher
Thanks. It's good solution for me.
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.