AutoCAD – Selection Sets are useless for command line from VBA but groups… that’s new …

First, did you know you can type VBA from the command line? Use the vbstmt command. Here is how to clear all groups. But getting back to the topic at hand. So named Selection Sets – you can’t use them. For example – using the CHSPACE command initiated by VBA (SendCommand) asks for a selection – well you cannot use the selection set name – you CAN group that selection set and name it with an UPPER CASE (did I say UPPERCASE?) name and when you are prompted for a selection – then give it the GROUP name. Then delete your group and you have what you need. You can even see the grips! Here is how I figured it out – these guys helped a lot.

There was a little bickering on that blog – but making a group and selecting it, then passing back that group as the functions’ result allows a lot of flexibility.

Here is the calling code …

[vb]

Dim mSS As ACadSelectionSet
Dim chSpaceCommand As String
‘ fill mSS with something …

Dim tGroup As AcadGroup

Set tGroup = vpo.aco.GroupItemsInSS(mSS, True, False) ‘ should select/highlight the items

‘ Do something here, for example CHSPACE
chSpaceCommand = "CHSPACE Group " & tGroup.name & "  "
        Debug.Print chSpaceCommand
        thisDrawing.SendCommand (chSpaceCommand) ‘ & VBA.Chr(13))

tGroup.Delete

[/vb]

Here is the GroupItemInSS

[vb]

Function GroupItemsInSS(SS As AcadSelectionSet, selectOnScreen As Boolean, ungroup As Boolean) As AcadGroup

Dim ans As Long

‘ Basically we make a group
Dim i As Long

Dim bob() As AcadEntity
ReDim bob(SS.count – 1)
ReDim bob(SS.count – 1) As AcadEntity
For i = 0 To SS.count – 1
Set bob(i) = SS.item(i)
Next

Dim grpTemp As AcadGroup

Dim vvar As Variant
Dim oEnt As AcadEntity

Dim grpName As String
grpName = VBA.UCase("grp" & SS.name)

For Each vvar In thisDrawing.Groups
Set grpTemp = vvar
Debug.Print grpName
If grpTemp.name = grpName Then

grpTemp.Delete
‘Exit For
End If
Next
If Not ungroup Then
Set grpTemp = thisDrawing.Groups.Add(grpName)
grpTemp.AppendItems bob
ans = grpTemp.count

grpTemp.highlight True
grpTemp.Update

SS.Application.ActiveDocument.Regen acAllViewports
If (selectOnScreen) Then
SS.Application.ActiveDocument.SendCommand ("SELECT G " & grpName & " " & VBA.Chr(13))
End If

End If
Set GroupItemsInSS = grpTemp
End Function

[/vb]