.NET

Reply
Valued Contributor
Posts: 81
Registered: ‎03-06-2012
Message 1 of 8 (684 Views)
Accepted Solution

How to get all blockreference in a drawing

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

Distinguished Contributor
santoshr0114
Posts: 133
Registered: ‎03-21-2012
Message 2 of 8 (678 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
Posts: 81
Registered: ‎03-06-2012
Message 3 of 8 (672 Views)

Re: How to get all blockreference in a drawing

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

Thanks santosh

Distinguished Contributor
santoshr0114
Posts: 133
Registered: ‎03-21-2012
Message 4 of 8 (670 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
Posts: 81
Registered: ‎03-06-2012
Message 5 of 8 (665 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
Posts: 370
Registered: ‎05-06-2012
Message 6 of 8 (646 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*
Posts: 1,975
Registered: ‎04-29-2006
Message 7 of 8 (624 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
Posts: 8
Registered: ‎10-17-2012
Message 8 of 8 (288 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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community