Author Topic: Finding a nested block programmatically in ObjectARX for .Net  (Read 3108 times)

0 Members and 1 Guest are viewing this topic.

cannorth

  • Guest
Finding a nested block programmatically in ObjectARX for .Net
« on: November 22, 2012, 02:42:10 PM »
Hello,

   Is there a way to find a block that is nested within another block programmatically in ObjectARX for .Net without iterating through all the blocks?  I don't want the user to select the block themselves either.  I want a method to get a particular block within a block.  The other issue is that there can be multiple parent blocks with the same nested block of the same name in the drawing, so iterating through the block table would find each of the same block.  If you have the parent block of the nested block, is there any way to single out this nested block without selecting others of the same name?

Thanks,

cannorth

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Finding a nested block programmatically in ObjectARX for .Net
« Reply #1 on: November 22, 2012, 04:07:59 PM »
Quote
If you have the parent block of the nested block, is there any way to single out this nested block without selecting others of the same name?

Iterate the parent BlockTableRecord looking for a child BlockReference.
Speaking English as a French Frog

cannorth

  • Guest
Re: Finding a nested block programmatically in ObjectARX for .Net
« Reply #2 on: November 22, 2012, 04:11:12 PM »
This is a solution:

  titleblock_id_obj is the object ID of the parent block.

Code: [Select]
                            Dim br1 As BlockReference = trans.GetObject(titleblock_id_obj, AutoCAD.DatabaseServices.OpenMode.ForRead)
                            Dim btr1 As BlockTableRecord = trans.GetObject(br1.BlockTableRecord, AutoCAD.DatabaseServices.OpenMode.ForRead)

                            For Each id2 As ObjectId In btr1
                                Dim ent As Entity = trans.GetObject(id2, AutoCAD.DatabaseServices.OpenMode.ForRead)

                                If TypeOf ent Is BlockReference Then
                                    Dim br As BlockReference = CType(ent, BlockReference)

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Finding a nested block programmatically in ObjectARX for .Net
« Reply #3 on: November 26, 2012, 10:58:05 AM »
I guess you could check ownerId and not sure why I did this way but
Code - C#: [Select]
  1.  
  2.     public static IEnumerable<ObjectId> WhereOwnedBy(this ObjectIdCollection source, ObjectId ownerId)
  3.         {
  4.             if (source == null)
  5.             {
  6.                 throw new ArgumentNullException("source");
  7.             }
  8.             if (ownerId.IsNull)
  9.             {
  10.                 throw new ArgumentNullException("ownerId");
  11.             }
  12.             return WhereOwnedByImpl(source, ownerId);
  13.         }
  14.         private static IEnumerable<ObjectId> WhereOwnedByImpl(this ObjectIdCollection source, ObjectId ownerId)
  15.         {
  16.             TransactionManager tm = source[0].Database.TransactionManager;
  17.             foreach (ObjectId item in source)
  18.             {
  19.                 DBObject dbo = tm.GetObject(item, OpenMode.ForRead, false, false);
  20.                 if (dbo.OwnerId == ownerId)
  21.                 {
  22.                     yield return item;
  23.                 }
  24.             }
  25.         }
  26.         public static IEnumerable<ObjectId> WhereOwnedBy(this IEnumerable<ObjectId> source, ObjectId ownerId)
  27.         {
  28.             if (source == null)
  29.             {
  30.                 throw new ArgumentNullException("source");
  31.             }
  32.             if (ownerId.IsNull)
  33.             {
  34.                 throw new ArgumentNullException("ownerId");
  35.             }
  36.             return WhereOwnedByImpl(source, ownerId);
  37.         }
  38.  
  39.         private static IEnumerable<ObjectId> WhereOwnedByImpl(this IEnumerable<ObjectId> source, ObjectId ownerId)
  40.         {
  41.             TransactionManager tm = source.First().Database.TransactionManager;
  42.             foreach (ObjectId item in source)
  43.             {
  44.                 DBObject dbo = tm.GetObject(item, OpenMode.ForRead, false, false);
  45.                 if (dbo.OwnerId == ownerId)
  46.                 {
  47.                     yield return item;
  48.                 }
  49.             }
  50.         }
  51.