Search code examples
excelvbaautocad

VBA - filter more than one AutoCAD block


I have recently started coding VBA and I have question about an example I found online. the following example is for filtering an AutoCAD block by name.

What do I do if I wish to insert more that one type of block to the selection?

Any help would be deeply apreciated

I have tried

grpValue(0) = "testBlock1" & "testBlock2" & "testBlock3"

but to no avail.

The example:


   Dim sset As AcadSelectionSet 
   Set sset = ThisDrawing.SelectionSets.Add("TestSet2") 

   Dim filterType As Variant 
   Dim filterData As Variant 
   Dim p1(0 To 2) As Double 
   Dim p2(0 To 2) As Double 

   Dim grpCode(0) As Integer 
   grpCode(0) = 2 
   filterType = grpCode 
   Dim grpValue(0) As Variant 
   grpValue(0) = "testBlock" 
   filterData = grpValue 

   sset.Select acSelectionSetAll, p1, p2, filterType, filterData 

   Debug.Print "Entities: " & str(sset.count) 

   sset.Delete 

End Sub

Solution

  • You can approach this in one of two ways:

    1. Wildcard Operators

    You can exploit the fact that the filter list argument for the Select method will accept wildcard operators when comparing string values, and therefore, you can use the comma (,) to separate each "pattern", e.g.:

    grpValue(0) = "testBlock1,testBlock2,testBlock3"
    

    Or, if your block names genuinely adhere to the above naming convention, you could use the following equivalent wildcard pattern:

    grpValue(0) = "testBlock[123]"
    

    2. Logical Operators

    Alternatively, you can use the logical operators <OR and OR> to implement OR logic between multiple DXF group 2 entries in the filter list, e.g.:

    grpCode(0) = -4
    grpValue(0) = "<OR"
    Dim i As Integer: i = 1
    Dim b
    For Each b In Split("testBlock1 testBlock2 testBlock3")
        grpCode(i) = 2
        grpValue(i) = b
        i = i + 1
    Next b
    grpCode(i) = -4
    grpValue(i) = "OR>"