AcadSelectionSet.Select ignoring my dynamic blocks

AcadSelectionSet.Select ignoring my dynamic blocks

Anonymous
Not applicable
1,795 Views
4 Replies
Message 1 of 5

AcadSelectionSet.Select ignoring my dynamic blocks

Anonymous
Not applicable

Hello all.

 

I've recently opened a post on the Select method of the AcadSelectionSelect object as I couldn't get a good grasp of its use. Now I understand what it is for but I can't still fix a problem with my dynamic blocks that is about to make me lose it: the VBA code is ignoring them in the final tally. See code below for two different dynamic blocks "ModeloDeTransportadorEnAluminio" and "Modelo de transportador en forma de U".  

 

The next snippet shows ZERO blocks for the count, ignoring all the dynamic block instances in the model space.

 

Sub FiltraTransportadorTipo1()

 

Dim MiFiltro As AcadSelectionSet
Dim TipodeFiltro(0) As Integer
Dim DatodeFiltro(0) As Variant
Dim i As Integer

 

Set MiFiltro = ThisDrawing.SelectionSets.Add("MisBloques")
TipodeFiltro(0) = 2
DatodeFiltro(0) = "ModeloDeTransportadorEnAluminio"
MiFiltro.Select acSelectionSetAll, , , TipodeFiltro, DatodeFiltro
MsgBox MiFiltro.Count
MiFiltro.Delete

 

End Sub

 

 

Whereas the next code is counting 3 out of 5 dynamic blocks, all of them on the same layer.

 

Sub FiltraTransportadorTipo2()

 

Dim MiFiltro As AcadSelectionSet
Dim TipodeFiltro(0) As Integer
Dim DatodeFiltro(0) As Variant
Dim i As Integer

 

Set MiFiltro = ThisDrawing.SelectionSets.Add("MisBloques")
TipodeFiltro(0) = 2
DatodeFiltro(0) = "Modelo de transportador en forma de U"
MiFiltro.Select acSelectionSetAll, , , TipodeFiltro, DatodeFiltro
MsgBox MiFiltro.Count
MiFiltro.Delete

 

End Sub

 

 

To add some information:

 

1) I've changed the SelectSimilar tool Settings to just the name, in case this was having an impact, especially in the second case given that it was considering the blocks on one of the layers (though it wouldn't explain why the count was zero for the first dynamic block that was only present on one layer). This didn't work out.

2) IMPORTANT. I used the oldschool Filter command. Well, it turns out I was facing the same issues, identical to those with the VBA code. The filter couldn't pick up the first dynamic block. And guess what...it was picking up 3 of the second type, just as the results with VBA. Why is this happening? I'm totally puzzled. 

3) The Quick Select tool works like a charm. I wish there was something that could mimic it through code.

 

In addition, these dynamic blocks have one level of nested blocks, if that could be of relevance... But then again, if that was the issue, the code wouldn't pick up any instance at all.


I hope my problem is clear. 

0 Likes
Accepted solutions (1)
1,796 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor
Accepted solution

The problem you have is with the block name filter, because the block references are dynamic block references. That is, very likely, the dynamic block reference's definition (thus, its name) is an anonymous block due to the dynamic propery/properties being different from its default value (in the dynamic block definition). If you truly understood how dynamic block works in AutoCAD, you would know that whenever a dynamic property is set to a value different from the value in the block definition, AutoCAD would define a new block definition as anonymous block based on the dynamic property value, if necessary. Then the block reference is re-pointed to this new anonymous block definition, instead of original block definition. Thus, the block reference name is not the name of original block name, but is something like "*Uxxxx".

 

Therefore, you should not use block name as filter in SelectionSet. Instead, you can use Entity filter to select all block references, and then use code to loop through the selectionset to identify the blocks you are targeting. Something like:

 

Sub FiltraTransportadorTipo2(blkName As String)

 

Dim MiFiltro As AcadSelectionSet
Dim TipodeFiltro(0) As Integer
Dim DatodeFiltro(0) As Variant
Dim i As Integer

 

Set MiFiltro = ThisDrawing.SelectionSets.Add("MisBloques")

'' filter for block references
TipodeFiltro(0) = 0
DatodeFiltro(0) = "INSERT" 
MiFiltro.Select acSelectionSetAll, , , TipodeFiltro, DatodeFiltro

 

Dim ent As AcadEntity

Dim blk As AcadBlockReference

Dim ent() as AcadEntity

Dim i As Intger

For Each ent In MiFiltro

  If TypeOf ent Is AcadBlockReference Then

    Set blk = ent

    If UCase(blk.EffectiveName)=Ucase(blkName) Then

        RreDim Preserve ents(i)

        Set ents(i)=ent

        i = i+1

    End If

  End If

End If

 

'' If target blocks found, 

If UBound(ents)>0 Then

    MiFiltro.Clear

    MiFiltro.AddItems ents

End If


MsgBox MiFiltro.Count
MiFiltro.Delete

 

End Sub

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

Anonymous
Not applicable

Once again, thanks a lot for your help. Actually, I can't thank you enough for your explanation plus the code. In fact, the other day I had a very hard time looping through block references and their properties. I couldn't understand why a U6 block name kept appearing in the block list name. I tried purging but it didn't work so I just assumed it was a "bug". I had no idea that the block references with different properties than the ones for the original block definition were internally named by Autocad. 

 

I'm very glad that I registered to post my questions, it's totally worth it. You've made my day 🙂 

0 Likes
Message 4 of 5

NGVBA
Contributor
Contributor

Hi Norman,

As always you are on spot, I am required to retrieve the dynamic block "Configuration" it is set to in the AutoCad drawing.

If we want to retrieve the actual value which is termed as "*Uxxxx" how can we do that ?

 

0 Likes
Message 5 of 5

norman.yuan
Mentor
Mentor

Well, if you read my previous reply, you might have known the answer: 

1. First select all block references with an AcadSelectionSet with filter "INSERT";

2. Loop through the selection set to find the target block with "EffectiveName", equal to "Configuration"

 

Or, you can skip the selectionset part and simply loop through the ModelSpace, or PaperSpace(s), where the target block(s) exist:

 

Dim ent As AcadEntity

Dim blk As AcadBlockReference

For Each ent in ThisDrawing.ModelSpace

  If TypeOf ent Is AcadBlockReference Then

    Set blk = ent

    If UCase(blk.EffectiveName)="CONFIGURATION" Then

      DoSomthingWithTheBlock blk

    End If

  End If

Next

 

Private Sub DoSomethingWithTheBlock(blk As AcadBlockReference)

  '' Do whatever needed with the block reference here

End Sub

 

You would use the same code inside the "For Each...Next", if you use selection set to select all "INSERT" (block references) first and the  loop through the selection set, obviously.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes