Selection Sets – Erase and Un-Erase

Selection Sets have one very undesirable behavior, in my experience. i hope i stumble across the reason for this, or maybe somebody will tell me. If you create a selection set of entities, then erase part or all of the entities through the normal autocad interface, then erase the entities again through the selection set program, the entities are actually restored to the drawing.

Our XY Axis program will have equation labels. The program will erase the old ones and put new ones in the same place. While debugging it and occasionally manually erasing the labels, i was getting two labels, one old one new, in the same place. i believe it is solved but i was unable to find any official documentation this was normal behavior. sset.erase seems to be a condition negater, if it exists it erases, if it no longer exists but still exists in the selection set, it un-erases it. however iterating the set does erase, if error reporting is turned off. btw i tested with lineobj.erase and it did not follow this pattern. the line was not restored.

here is a test to confirm and the fix.
run test1, then run test2, everything is fine.
run test1, then run test3, same behavior as above.
run test1, now delete two of the lines manually, then run test2, the lines are restored.
run test1, delete two lines, run test3, everything is fine.

Sub test1_ss_erase()
Call connect_acad
Dim s_set As AcadSelectionSet

On Error Resume Next
Set s_set = acadDoc.SelectionSets.Item("test_ss")
s_set.Clear
s_set.Delete
On Error GoTo 0

Set s_set = acadDoc.SelectionSets.Add("test_ss")

Debug.Print "Mark1 " & s_set.Count

Call line(0, 0, 6, 1)
s_set.Select acSelectionSetLast

Call line(7, 2, 9, 7)
s_set.Select acSelectionSetLast

Call line(10, 8, 1, 9)
s_set.Select acSelectionSetLast

Call line(-1, 12, -3, 1)
s_set.Select acSelectionSetLast

Debug.Print "Mark2 " & s_set.Count
End Sub

Sub test2_ss_erase()
'before running this sub, manually delete two of the lines
'created by the sub above
'this routine will un-erase the erased entities

Call connect_acad
Dim s_set As AcadSelectionSet

On Error Resume Next
Set s_set = acadDoc.SelectionSets.Item("test_ss")

Debug.Print "Mark3 " & s_set.Count

s_set.Erase
On Error GoTo 0

Update
Debug.Print "Mark4 " & s_set.Count
End Sub


Sub test3_ss_erase()
'before running this sub, manually delete two of the lines
'created by the sub test1 above
'this routine will erase and not un-erase

Call connect_acad
Dim s_set As AcadSelectionSet
Dim i As Integer

On Error Resume Next
Set s_set = acadDoc.SelectionSets.Item("test_ss")
Debug.Print "Mark5 " & s_set.Count

i = s_set.Count
Do While i > 0
i = i - 1 'because ss index is zero based
s_set.Item(i).Erase
Loop

'if you move the error reset up
' you get a catastrophic error on erasing non-existent entities
'but down here everything seems to go well
On Error GoTo 0

Update
Debug.Print "Mark5 " & s_set.Count
End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s