VBA - Return selection to user for further manipulation

VBA - Return selection to user for further manipulation

Anonymous
Not applicable
3,474 Views
2 Replies
Message 1 of 3

VBA - Return selection to user for further manipulation

Anonymous
Not applicable

Hi,

 

I've tried hard to figure this out but to no avail. When you run an AutoCAD command that works on a selection (like `move`, `erase`, `mirror`, etc), it usually prompts you to make a selection. But if you have already selected some entities, it simply starts the `move`/`erase`/`mirror` operation.

 

I want to make a macro that would loop through every entity in `ModelSpace` and select some of them that fulfill a certain criteria (like maybe all lines that are horizontal or vertical). At this point I want the macro to end with the objects selected by the macro available for use by a regular AutoCAD command that the user can then run.

 

For instance:

1. The user runs the macro

2. Macro selects all lines having same X or Y coordinates and exits

3. The user then uses the PROPERTIES window to change some of their properties, or she could enter `E` to erase these lines, or `MI` to mirror them, etc.

 

Is this even possible? Cuz I have never come across a similar issue being brought up by ppl in the acad community,

0 Likes
3,475 Views
2 Replies
Replies (2)
Message 2 of 3

Ed__Jobe
Mentor
Mentor

Below is a sample that selects text only from the current space. The only problem is that once the code finishes running, the selectionset will not be active. However, you can just use the Previous option when prompted to select entities in the MOVE command. I used the Highlight option just to show you its working.

Public Function AddSelectionSet(SetName As String) As AcadSelectionSet
' This routine does the error trapping neccessary for when you want to create a
' selectin set. It takes the proposed name and either adds it to the selectionsets
' collection or sets it.
    On Error Resume Next
    Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
    If Err.Number <> 0 Then
        Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
        AddSelectionSet.Clear
    End If
End Function

Public Function GetSS_TextFilter() As AcadSelectionSet
    'creates an ss of Text only
    Dim s1 As AcadSelectionSet      'for filtered ss
    
    
        'filter is needed if there's no pfss
        Dim intFtyp(0) As Integer                       ' setup for the filter
        Dim varFval(0) As Variant
        Dim varFilter1, varFilter2 As Variant
        intFtyp(0) = 0: varFval(0) = "TEXT"             ' get only text
        varFilter1 = intFtyp: varFilter2 = varFval
        Set s1 = AddSelectionSet("ssTextFilter")        ' create or get the set
        s1.Clear                                        ' clear the set
        s1.Select acSelectionSetAll, , , varFilter1, varFilter2      ' do it
    
    Set GetSS_TextFilter = s1

End Function

Sub test2()
    Dim ss As AcadSelectionSet
    Set ss = GetSS_TextmFilter()
    ss.Highlight True
End Sub

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 3 of 3

norman.yuan
Mentor
Mentor

In addition to @Ed__Jobe's excellent code, I add a bit more so the code would do what you exactly want: use VBA code to simulate user's manual selecting:

Option Explicit

Private Const MY_SS As String = "MySelectionSet"

Public Sub DoSelection()

    '' Select all circles
    CreateSelection "CIRCLE"
    
    '' Or select all Lines
    ''CreateSelection "LINE"
    
    '' Or select everything
    ''CreateSelection ""
    
    '' Send the selectionset to AutoCAD editor
    '' so that they look like being selected by user and highlighted
    ThisDrawing.SendCommand "SELECT" & vbCr & "P" & vbCr & vbCr
    
End Sub

Private Sub CreateSelection(entType As String)
    
    On Error Resume Next
    
    Dim ss As AcadSelectionSet
    
    Set ss = ThisDrawing.SelectionSets(MY_SS)
    If ss Is Nothing Then
        Set ss = ThisDrawing.SelectionSets.Add(MY_SS)
    Else
        ss.Clear
    End If
    
    Dim GType(0) As Integer
    GType(0) = 0
    Dim GVal(0) As Variant
    GVal(0) = entType
    
    If UCase(entType) = "CIRCLE" Or UCase(entType) = "LINE" Then
        ss.Select acSelectionSetAll, , , GType, GVal
    Else
        ss.Select acSelectionSetAll
    End If
    
    ''MsgBox "Selected entity count: " & ss.Count
    
End Sub

Once you run the VBA macro "DoSelection", either all circles (or all lines, or all entities) will be highlighed as if they are selected by user, and the properties window would update according the selection.

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature