Selecting objects on the model from selectionset created in VBA

Selecting objects on the model from selectionset created in VBA

jadeid91
Explorer Explorer
11,779 Views
2 Replies
Message 1 of 3

Selecting objects on the model from selectionset created in VBA

jadeid91
Explorer
Explorer

I created a selection set which selects all objects in a certain layer. I looped over the selected objects successfully and highlighted them. Highlight property only highlights the objects. However, I cannot see any object selected on the scene... The dropdown list in the properties panel shows "No selection".

 

How do I select objects on the model from selectionset created in VBA?

 

Autocad Version: 2015

 

Code:

 

Function selectLayer(inLayer As String)

Dim polylines As AcadSelectionSet
Dim FilterType(0) As Integer
Dim FilterData(0) As Variant
Dim pl As AcadLWPolyline

 

On Error Resume Next


Set polylines = ThisDrawing.SelectionSets.Add("polylinesSS")

 

If Err.Number <> 0 Then
ThisDrawing.SelectionSets.Item("polylinesSS").Delete
Set polylines = ThisDrawing.SelectionSets.Add("polylinesSS")
End If

 

FilterType(0) = 8
FilterData(0) = inLayer

 

polylines.Select acSelectionSetAll, , , FilterType, FilterData

 

'For Each entity In polylines
'
' If TypeOf entity Is AcadLWPolyline Then
'
' Set pl = entity
' pl.Highlight (True)
'
'
' End If
'
'Next

 

End Function

 

0 Likes
Accepted solutions (1)
11,780 Views
2 Replies
Replies (2)
Message 2 of 3

Ed__Jobe
Mentor
Mentor

When your code execution ends, the entities do not remain selected. There is a special selectionset called the PickfirstSelectionset. You access it via ThisDrawing.PickfirstSelectionset. When you click on an entity with your mouse, it is added to the pfss. Unfortunately, it is read-only. The vba methods are not intended to add anything to the pfss, it is read-only. The vba api is written so that you can manipulate the ss with code.

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
Accepted solution

As you code shown (without commenting out the highlight portion), the code has already done the selecting correctly. You need to know that selecting programmatically (by code) and selecting manually are different thing. 

 

In general, selecting by code, it is expected the selected entities would be subject to further code processing. Thus, it is up to the programmer to decide what to do with the entities selected by code. If you want user know visually that some entities are selected, you can highlight them, just as your code does.

 

However, if you want to leave some entities visibly selected (just as user manually select them) when your code is done, then you can do this:

 

Public Sub Test()

    Dim ss As AcadSelectionSet
    
    Set ss = ThisDrawing.SelectionSets.Add("TestSS")
    
    ss.Select acSelectionSetAll
    ss.Delete

    ThisDrawing.SendCommand "Select p " & vbCrLf

End Sub

As you can see, the code is to simply create a SelectionSet, do selection, and then delete the selection set. It purpose is to have AutoCAD cache a "Previous" selection, so the command "SELECT" with "P" option can select it. the result of command "SELECT" will also update Properties window.

 

HTH.

Norman Yuan

Drive CAD With Code

EESignature