Programmatically (C#) count sub blocks in model

Hi!

I'm developing an extension to generate BOMs that plugs into an ERP. I have a problem finding all instances of blocks that are used inside another block.

The following drawing (see picture below) contains 4 main blocks, titled "S, MA, NO, NO, LED, blue, P2". 

This main block consists of 3 sub blocks (all articles in the ERP system and coupled to it through an article id attribute in the block).

The program is based on the example "Get and Set Block Definitions, Block Instances, and BlockAttribute Instances Example":

When I retrieve all instances of a sub block only 1 instance is found:

My extension works the same as the example provided:

  1. All block definitions are retrieved using the "TestBlockDefinitions" method
    This works as expected;
    1. the main block defintion is found  
    2. the three inner/sub block definitions are found as well.

  2. For each block definition the method TestBlockInstance is called. This function should retrieve all instances of the block definition.
    1. All instances of the main block are found (4 in total)
    2. For the inner block definitions a total instance count of 1 is found. 

When retrieving properties like coordinates from these inner blocks, the coordinates of that block inside the main block are returned. So, probably the count is just the count inside that block too. 

The blockattributemanager (bam) show correct values though: 

The block 'White front fixing LED, 12-30Vacdc' is a sub block. 
Please note the difference between instances in drawing and model. 


How can I programmaticly find the total number of instances of sub blocks in the model, as shown by the Block Attribute Manger?