Iterating through a dynamic block reference.

Iterating through a dynamic block reference.

ShricharanaB
Advocate Advocate
1,451 Views
5 Replies
Message 1 of 6

Iterating through a dynamic block reference.

ShricharanaB
Advocate
Advocate

Hi,

 

I have two dynamic blocks DB1 and DB2. DB2 is nested inside DB1. An array driven by a distance parameter has DB2 block as reference.

I'm trying to iterate through DB1 to find all the references of DB2 in it when the distance is changed. I'm using below code to get the dynamic block table record. However, it only returns one reference of DB2 even if the array has added other instances of DB2 as well.

What am I missing here?

How can I get reference of all the instances of DB2 in DB1? 

 

BlockTableRecord btr = blockRef.IsDynamicBlock ?
    (BlockTableRecord)trans.GetObject(blockRef.DynamicBlockTableRecord, OpenMode.ForRead):                        
    (BlockTableRecord)trans.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead);

 

ShricharanaB_1-1708435605221.png

 

Thanks in advance for any help.

 

 

 

0 Likes
Accepted solutions (3)
1,452 Views
5 Replies
Replies (5)
Message 2 of 6

ActivistInvestor
Mentor
Mentor
Accepted solution

Dynamic block references can be represented by anonymous blocks, when changes to the dynamic block reference requires a different/distinct graphical representation.

 

You have to access the definition of the anonymous block reference (which is what gets inserted), not the dynamic block's definition (which is what the code you posted does). You use the BlockTableRecord property to get the anonymous block's definition.

 

 

 

 

Message 3 of 6

ShricharanaB
Advocate
Advocate
Accepted solution

Hi, 

 

Thank you so much for pointing it out.

I see where I did wrong. I've corrected the code as below and its working properly.

BlockTableRecord btr = blockRef.IsDynamicBlock ?
    (BlockTableRecord)trans.GetObject(blockRef.AnonymousBlockTableRecord, OpenMode.ForRead):                       
    (BlockTableRecord)trans.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead);

 

Thank you again!

0 Likes
Message 4 of 6

_gile
Consultant
Consultant
Accepted solution

@ShricharanaB wrote:

Hi, 

 

Thank you so much for pointing it out.

I see where I did wrong. I've corrected the code as below and its working properly.

BlockTableRecord btr = blockRef.IsDynamicBlock ?
    (BlockTableRecord)trans.GetObject(blockRef.AnonymousBlockTableRecord, OpenMode.ForRead):                       
    (BlockTableRecord)trans.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead);

 

Thank you again!


In case of a dynamic block reference, the BlockReference.AnonymousBlockTableRecord and BlockReference.BlockTableRecord properties have the same value: the dynamicly modified BlockTableRecord. The BlockReference.DynamicBlockTableRecord property value is the original block definition.

 

_gile_0-1708499784676.png

Inspector plugin available on Github.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 6

ShricharanaB
Advocate
Advocate

Oh! 

So I don't need to use condition here to access the proper block table record at all! I can just get the blockRef.BlockTableRecord, doesn't matter if the block is dynamic, anonymous or static, it'll return the correct btr.

 

Thank you @_gile . Much appreciated.

0 Likes
Message 6 of 6

_gile
Consultant
Consultant

@ShricharanaB wrote:

Oh! 

So I don't need to use condition here to access the proper block table record at all! I can just get the blockRef.BlockTableRecord, doesn't matter if the block is dynamic, anonymous or static, it'll return the correct btr.


Yes.

When you modify some dynamic property of a dynamic block reference, AutoCAD automatically creates a new block definition which reflect the modification (AnonymousBlockTableRecord or BlockTableRecord properties) and keeps a link to the original dynamic block definition (DynamicBlockTableRecord property).

So, you should not use the AnonymousBlockTableRecord as you did, even with a dynamic block, because it may be null if none dynamic property of the block reference have been modified.

 

In case of a static block:

  • AnonymousBlockTableRecord == null
  • DynamicBlockTableRecord == BlockTableRecord

In case of a non modified dynamic block:

  • AnonymousBlockTableRecord == null
  • DynamicBlockTableRecord == BlockTableRecord (original dynamic block definition)

In case of a dynamicly modified dynamic block:

  • AnonymousBlockTableRecord == BlockTableRecord (modified dynamic block definition)
  • DynamicBlockTableRecord (original block definition)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub