How to get all blockreference in a drawing

How to get all blockreference in a drawing

chockalingam
Advocate Advocate
15,854 Views
7 Replies
Message 1 of 8

How to get all blockreference in a drawing

chockalingam
Advocate
Advocate

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......

0 Likes
Accepted solutions (1)
15,855 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Accepted solution

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

0 Likes
Message 3 of 8

chockalingam
Advocate
Advocate

Thanks santosh

0 Likes
Message 4 of 8

Anonymous
Not applicable

Glad i could be your help...

0 Likes
Message 5 of 8

chockalingam
Advocate
Advocate

Hi santosh,

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

0 Likes
Message 6 of 8

DiningPhilosopher
Collaborator
Collaborator

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.

 

Message 7 of 8

_gile
Consultant
Consultant

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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8

Anonymous
Not applicable
Thanks. It's good solution for me.
0 Likes