Get entity and selection sets

Get entity and selection sets

a.kouchakzadeh
Advocate Advocate
3,035 Views
6 Replies
Message 1 of 7

Get entity and selection sets

a.kouchakzadeh
Advocate
Advocate

Hello world and Mr. Yuan

 

Im trying to use Getentity method to create a selection set.

I have put the Getentity method in some sort of loop which will take entities till the user clicks on a command button in the userform (vbmodeless) or presses escape which will go to another userform.

my problem is, I can see the picked item in getentity but my code wont assign it to the selection set

 

dim keepsset as acadselectionset

dim entity as acadentity

i = 0

GetEntityObject:

On Error Resume Next
ThisDrawing.Utility.GetEntity Entity, bp, vbNewLine & "Select Items to keep in Plan"

Do While Entity Is Nothing
If CheckKey(VK_ESCAPE) = True Then

me.hide

initialform.show
Else
MsgBox "Emtpy area selected" & vbNewLine & "Select an Object or Press ESC to go to previous Form"
GoTo GetEntityObject
End If
Loop

Set KeepSSet(i) = Entity
Entity.Highlight True

i = i + 1
GoTo GetEntityObject

 

the reason why Im using getentity instead of KeepSSet.SelectOnScreen is so I can use the prompt in getentity.

any ideas if i can do the same if i use .SelectOnScreen?

@norman.yuan 

0 Likes
Accepted solutions (1)
3,036 Views
6 Replies
Replies (6)
Message 2 of 7

seabrahenrique
Advocate
Advocate

When i use selection set, i always do in this way:

 

 

Sub SSTest()

Dim SS As AcadSelectionSet: Set SS = ThisDrawing.SelectionSets.ADD("SS00"): SS.SelectOnScreen

Dim AcadObj As AcadEntity

For Each AcadObj In SS

    Debug.Print AcadObj.ObjectName

Next AcadObj

SS.Delete

End Sub

 

 

Can this code help u?

0 Likes
Message 3 of 7

norman.yuan
Mentor
Mentor

I am not sure and you did not say why you need a SelectionSet now that you can pick entities.  With that said, you can add entities, which is identified by your code, such as asking user to pick with GetEntity() method, to SelectionSet by calling AcadSelectionSet.AddItems() method (you cannot assign entity to SelectionSet's element, as you already know).

 

Since your process is likely expect user to pick multiple entities, you probably let user to keep picking until it is done and then add all the picked entities (saved in an entity array during picking) into the SelectionSet at the end at once.

 

BTW, the code you showed is really bad (sorry to say that :-() - using label to let code execution to jump around is already bad enough practice, yet you have code execution jumps out of Do...Loop and then going into the loop again? You'd better re-structure the code again and do not use label to turn execution flow.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 7

a.kouchakzadeh
Advocate
Advocate

Dear Mr. Yuan

 

Thanks for the tip about labels and loops.

the main goal is Deleting a SelectionSet which some elements of it have to be removed out of the SelectionSet before being deleted.

when the userform is activated, the programs asks the user to pick the Items that should not be removed.

then when he/she are done, Confrim Delete button is pressed and it deletes the Items in the SelectionSet.

so I have to define a public SelectionSet to store the Items which has to be kept in it because I define a public EntityArray.

then I have to take set each element of that SelectionSet to an array and remove them From the main SelectionSet so I can delete the main SelectionSet

By the way the reason why Im Avoiding SelectionSet2.SelectOnScreen is that I want to have a tooltip on the mouse prompting "Pick Items you want to keep" which I cant while using SelectOnScreen method.

so this is the code:

 

Public SelectionSet2 As AcadSelectionSet


Sub userform_activate()

 

Dim entity As AcadEntity
Dim EntityArray() As AcadEntity
Dim i As Integer
i = 0
Dim condition As Boolean
condition = True

Do While condition
   ThisDrawing.Utility.GetEntity entity, bp, "pick an item"
   condition = False
   ReDim EntityArray(i)
   Set EntityArray(i) = entity
   i = i + 1
   If Not entity Is Nothing Then condition = True
Loop

For i = 0 To UBound(EntityArray)
Set SelectionSet2(i) = EntityArray(i)
Next

End Sub

Sub DeleteItems_click()

   Dim SelectionSet1 As AcadSelectionSet
   Set SelectionSet1 = ThisDrawing.SelectionSets.Add("set1")
   Dim block_filterdata(0) As Integer
   Dim block_filtertype(0) As Variant

   block_filterdata(0) = 2
   block_filtertype(0) = "xxx"

   SelectionSet1.Select acSelectionSetAll, , , block_filterdata, block_filtertype

   For i = 1 To SelectionSet2.Count - 1
      KeepArray(i) = SelectionSet2(i)
   Next

   SelectionSet1.RemoveItems KeepArray

   Dim elem As AcadEntity
   For Each elem In SelectionSet1
      elem.Delete
   Next

End Sub

can you give me a hand in the code Sir?

Thanks in advance

0 Likes
Message 5 of 7

a.kouchakzadeh
Advocate
Advocate

Hello Sir. Not really because I already know that my SelectionSet is empty and its not getting the entities which its suppose to.

but thanks anyways

0 Likes
Message 6 of 7

norman.yuan
Mentor
Mentor
Accepted solution

Let's critique the first portion of your code (in blue)

 

'' I assume this selectionset is created prior to the code below being executed

Public SelectionSet2 As AcadSelectionSet  

 

Sub userform_activate()

 

Dim entity As AcadEntity
Dim EntityArray() As AcadEntity
Dim i As Integer
i = 0
Dim condition As Boolean
condition = True

Do While condition
   ThisDrawing.Utility.GetEntity entity, bp, "pick an item"
   condition = False

   '' You missed keyword "Preserve", therefore, each "ReDim" makes previous picked entity lost
   ReDim Preserve EntityArray(i)  
   Set EntityArray(i) = entity
   i = i + 1
   If Not entity Is Nothing Then condition = True
Loop

'' You cannot set each element in a SelectionSet

'' Instead you use AcadSelectionSet.AddItems
''For i = 0 To UBound(EntityArray)
''Set SelectionSet2(i) = EntityArray(i)
''Next
SelectionSet2.AddItems EntityArray


End Sub

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 7 of 7

a.kouchakzadeh
Advocate
Advocate

I Fixed it with a little bit of change.

Thank you very much Sir.

0 Likes