VBA: How to setup selectionset filters for a block or layer?

By Philippe Leefsma

Q:

How do I use a selection set filter to select a block with a particular name
that is on a particular layer ?

A:

The following sample code allows you to select blocks based on this criteria:

1. selectALayer() – create a selectionset with entities on "layer1"
2. selectABlock() – create a selectionset with a block insert "testBlock"
3. selectABlockOnALayer() – create a selectionset with a block insert "testBlock" on "layer1"


Sub selectALayer()

   Dim sset As AcadSelectionSet
   Set sset = ThisDrawing.SelectionSets.Add("TestSet1")
  
   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) = 8
   filterType = grpCode
   Dim grpValue(0) As Variant
   grpValue(0) = "Layer1"
   filterData = grpValue
   
   sset.Select acSelectionSetAll, p1, p2, filterType, filterData
  
   Debug.Print "Entities: " & str(sset.count)
  
   sset.Delete
  
End Sub

Sub selectABlock()

   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

Sub selectABlockOnALayer()

   Dim sset As AcadSelectionSet
   Set sset = ThisDrawing.SelectionSets.Add("TestSet3")
  
   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 To 1) As Integer
   grpCode(0) = 8
   grpCode(1) = 2
   filterType = grpCode
  
   Dim grpValue(0 To 1) As Variant
   grpValue(0) = "layer1"
   grpValue(1) = "testBlock"
   filterData = grpValue
  
   sset.Select acSelectionSetAll, p1, p2, filterType, filterData
  
   Debug.Print "Entities: " & str(sset.count)
  
   sset.Delete
  
End Sub

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”> 


Comments

5 responses to “VBA: How to setup selectionset filters for a block or layer?”

  1. Excellent! just a reminder: p1 and p2 are optional and are not required when using select all.
    sset.Select acSelectionSetAll, , ,filterType, filterData

  2. Hi,
    I have a question about the second example. What do I do if I wish to insert more that one type of block to the selection?
    I have tried
    grpValue(0) = “testBlock1” & “testBlock2” & “testBlock3”
    but to no avail.
    Any help would be deeply apreciated,
    kind regards

  3. For anyone interested, I found out. The correct line is:
    grpValue(0) = “testBlock1,testBlock2,testBlock3”

  4. Oscar Fernandez Avatar
    Oscar Fernandez

    The best way to know the value that you need to filter any entitie. is use autolisp order in command bar.
    Command:(entget (ssname (ssget) 0))
    it allow select one entitie and present you all his Codes and values from the entitie.
    in case of block reference present:
    ((-1 . ) (0 . “INSERT”) (330 . ) (5 . “6C77E”) (100 . “AcDbEntity”) (67 . 0) (410 . “Model”) (8 . “ARQ-ALUMBRA”) (100 . “AcDbBlockReference”) (2 . “FP”) (10 581408.0 4.79624e+06 0.0) (41 . 1.5) (42 . 1.5) (43 . 1.5) (50 . 5.58545) (70 . 0) (71 . 0) (44 . 0.0) (45 . 0.0) (210 0.0 0.0 1.0))
    then (0 . “INSERT”):
    for block filter :
    IntCod (n) = 0
    VarValue (n) = “INSERT”

Leave a Reply to RicardoCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading