VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Selection Set

8 REPLIES 8
Reply
Message 1 of 9
Anonymous
487 Views, 8 Replies

Selection Set

Using VBA, I want to select a bunch of items and then call a command. How do I select the items in code so they can be used for the following command (layiso - Isolate Layers)?
8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Anonymous

    Dim acDim As
AcadDimension
    Dim acSelection As
AcadSelectionSet
    Dim i As
Integer
    
    Set acSelection =
ThisDrawing.SelectionSets.Add("temporary")
    acSelection.SelectOnScreen

 

    For i = 0 To acSelection.Count -
1

        'your code
here...

    Next   


   
acSelection.Delete
   
    Set acSelection
= Nothing
    Set acDim = Nothing

--

Kevin

 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Using
VBA, I want to select a bunch of items and then call a command. How do I
select the items in code so they can be used for the following command (layiso
- Isolate Layers)?
Message 3 of 9
Anonymous
in reply to: Anonymous

Great. If the user already has a selection and I want to add more items to the current selection how would I do that?
Message 4 of 9
Anonymous
in reply to: Anonymous

SelectionSet.AddItems


--
Kevin

 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Great.
If the user already has a selection and I want to add more items to the
current selection how would I do that?
Message 5 of 9
Anonymous
in reply to: Anonymous

If you specifically want the user to pick objects first, and THEN run your
command, you'll have to use the PickfirstSelectionSet. You have to take
steps to keep VBA from emptying this selection set when you run your
command. Have a look at these Google searches for related posts. I picked
one out that seemed particularly relevant.

James

- - - - - - - - - - - - -
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&q=noun+verb+grou
p%3Aautodesk.autocad.customization.vba&btnG=Google+Search
- - - - - - - - - - - - -
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&q=PickfirstSelec
tionSet+group%3Aautodesk.autocad.customization.vba
- - - - - - - - - - - - -
From: Minkwitz Design (joshwest@ameritech.net)
Subject: Re: Determining selected items (sets?)

Place the following code in the ThisDrawing module of your project:

Public NounVerb As Boolean

Private Sub AcadDocument_SelectionChanged()
If ThisDrawing.PickfirstSelectionSet.Count > 0 Then
NounVerb = True
End If
If GetAsyncKeyState(&H10) Then NounVerb = False
End Sub

Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
NounVerb = False
End Sub

That will get you noun/verb selection. The sub below can be placed in a
standard module to test it and demonstrate how to use it. The methods
Joe and Joeri posted can then be used if nothing was selected noun/verb.
-Josh

Sub test()
Dim Preselection As AcadSelectionSet
Set Preselection = ThisDrawing.SelectionSets.Add("prev")
If ThisDrawing.NounVerb Then
Preselection.Select acSelectionSetPrevious
MsgBox Preselection.Count & " Items were selected noun/verb and are
now in the PreSelection Set."
ElseIf Not ThisDrawing.NounVerb Then
MsgBox "Nothing Selected noun/verb. Use Select on Screen."
End If
Preselection.Delete
End Sub
- - - - - - - - - - - - -
Message 6 of 9
Anonymous
in reply to: Anonymous

In code, can I add items to the PickFirstSelectionSet? I have tried taking the currently selected items and putting them in a new selection set, then adding a few more new items to the selection set with code (not with
SelectOnScreen), and then calling the command but it's like the command doesn't reference the right selection set, it doesn't run the command on my new items.
Message 7 of 9
Anonymous
in reply to: Anonymous

Can you post the code? I'm pretty sure there are
some examples in the help mess (I mean help file) but I haven't actually
done this myself.

 

You might have to transfer the
PickFirstSelectionSet into your own selection set, then you should be able to
additems to your own selection set. I know there are some problems in retaining
the objects within the pickfirst set, they're lost as soon as you run a macro,
but it sounds like that's not your problem.


--
Kevin

 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
In
code, can I add items to the PickFirstSelectionSet? I have tried taking the
currently selected items and putting them in a new selection set, then adding
a few more new items to the selection set with code (not with

SelectOnScreen), and then calling the command but it's like the command
doesn't reference the right selection set, it doesn't run the command on my
new items.
Message 8 of 9
Anonymous
in reply to: Anonymous

'Here's the code, the layiso (isolate layer) command doesn't work.

Public Sub Main()
Dim CurSelSet As AcadSelectionSet, CurSet As AcadSelectionSet
Dim j As Integer, i As Integer, k As Integer
Dim SelCode(0 To 1) As Integer
Dim SelValue(0 To 1) As Variant

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'TESTING INSTRUCTIONS
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'Set up a test Drawing with the following layers:
'0
'Bottom
'Bottom PROBE
'Bottom DEADSTOP
'Bottom PUSHPIN
'Test

'add one circle to each layer and turn on all layers so they are visible
'Set the Bottom layer as the current layer
'Select the circle on the Bottom layer
'Create a new VBA Project and add a module and paste this code
'Run the program
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'the PickFirstSelectionSet should have one item in it ("Bottom" layer circle)
Set CurSelSet = ThisDrawing.PickfirstSelectionSet

For i = 0 To CurSelSet.Count - 1
'temporary selection set
For j = 0 To ThisDrawing.SelectionSets.Count - 1
If ThisDrawing.SelectionSets.Item(j).Name = "SelSet" Then Exit For
Next j
If j = ThisDrawing.SelectionSets.Count Then
Set CurSet = ThisDrawing.SelectionSets.Add("SelSet")
Else
Set CurSet = ThisDrawing.SelectionSets.Item(j)
End If
CurSet.Clear

'go through all of the layers and turn on all layers that begin with Bottom
For j = 0 To ThisDrawing.Layers.Count - 1
'compare layer name against each item in the PickFirstSelectionSet
For k = 0 To CurSelSet.Count - 1
If (UCase(ThisDrawing.Layers(j).Name) = UCase(CurSelSet.Item(k).Layer) Or InStr(UCase(ThisDrawing.Layers(j).Name), UCase(CurSelSet.Item(k).Layer) & " ") = 1) Then
Debug.Print ThisDrawing.Layers(j).Name
SelCode(0) = 8
SelValue(0) = ThisDrawing.Layers(j).Name
SelCode(1) = 0
SelValue(1) = "CIRCLE"
CurSet.Select acSelectionSetAll, , , SelCode, SelValue
SelValue(1) = "ARC"
CurSet.Select acSelectionSetAll, , , SelCode, SelValue
SelValue(1) = "LINE"
CurSet.Select acSelectionSetAll, , , SelCode, SelValue
SelValue(1) = "LWPOLYLINE"
CurSet.Select acSelectionSetAll, , , SelCode, SelValue
End If
Next k
Next j
Next i

If CurSelSet.Count - 1 >= 0 Then
'isolate Bottom, Bottom PROBE, Bottom DEADSTOP, Bottom PUSHPIN layers only
ThisDrawing.SendCommand "layiso" & vbCr
CurSet.Clear
CurSet.Delete
Set CurSet = Nothing
End If

End

End Sub
Message 9 of 9
Anonymous
in reply to: Anonymous

Okay, getting back to your original question...

>>How do I select the items in code so they can be used for the
>>following command (layiso - Isolate Layers)?

You don't need layiso. What is it doing that you can't?

Here, copy/paste this code into a module and run it. Then compare it to
yours. You are declaring variables and not using them; you're using
counter loops that are not required. Collections know that they contain
elements, so you use "For Each element In this_collection" instead of
counters [you must know LISP :-)]. Also, remember that the sendcommand
is to be reserved as a last resort. If you use it, you could lose
control of your program if the user interrupts the send command.

HTH,
Mike

ps. watch out for word wrapping when you copy/paste the code.
===============================
Mike Tuersley
PhD @ CADalyst's AutoCAD Clinic
http://www.cadonline.com

'=========== BEGIN CODE BLK ================
Public Sub Main()
Dim cadTmpSet As AcadSelectionSet
Dim cadCurSet As AcadSelectionSet
Dim cadLayer As AcadLayer
Dim cadEnt As AcadEntity
Dim intCntr As Integer
Dim intSelCode(0 To 1) As Integer
Dim varSelValue(0 To 1) As Variant

'the PickFirstSelectionSet should have one item in it ("Bottom" layer
circle)
Set cadCurSet = ThisDrawing.PickfirstSelectionSet

For intCntr = 0 To cadCurSet.Count - 1
'Iterate thru the collection set collection
'looking for "SelSet"
For Each cadTmpSet In ThisDrawing.SelectionSets
If cadTmpSet.Name = "SelSet" Then
'Found it so set current set to it
'and leave loop
Set cadCurSet = cadTmpSet
Exit For
End If
Next

'Check to make sure we left loop with
'a selection set, otherwise create it
If cadCurSet Is Nothing Then Set cadCurSet =
ThisDrawing.SelectionSets.Add("SelSet")

'Iterate the layers collection and turn on all layers
'that begin with Bottom i.e. performing the LAYISO command!!!
'ALSO - learn how to iterate the collection WITHOUT a counter->
For Each cadLayer In ThisDrawing.Layers
'compare layer name against each item
'in the PickFirstSelectionSet
'ditto on the iteration w/o counter->
For Each cadEnt In cadCurSet
If (UCase(cadLayer.Name) = UCase(cadEnt.Layer) _
Or InStr(UCase(cadLayer.Name), _
UCase(cadEnt.Layer) & " ") = 1) Then

'Turn it ON
cadLayer.LayerOn = True

Debug.Print cadLayer.Name

intSelCode(0) = 8
varSelValue(0) = cadLayer.Name
intSelCode(1) = 0
'Learn to use filters...you only needed one->
varSelValue(1) = "ARC,CIRCLE,LINE,LWPOLYLINE"

cadCurSet.Select acSelectionSetAll, , , intSelCode,
varSelValue

Else
'Turn it OFF
cadLayer.LayerOn = False
End If
Next
Next
Next

cadCurSet.Delete
Set cadCurSet = Nothing

End If

End Sub

'=========== END CODE BLOCK ============

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost