Selection sets

Selection sets

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

Selection sets

Anonymous
Not applicable

The routine below was developed to erase everything in a drawing and worked beautifully in windows7 but windows10 doesn't like the Set ssetObj = statement after CREATE_SET:

 

Public Sub ClearDrawing()
' Clears AutoCad drawing of all objects. Used in each connection module.

Dim ssetObj As AcadSelectionSet
       
    On Error GoTo CREATE_SET
        ThisDrawing.SelectionSets("MSset").Delete
    GoTo SKIP_CREATE_SET

CREATE_SET:
    
     Set ssetObj = ThisDrawing.SelectionSets.Add("MSset")

SKIP_CREATE_SET:

    If ThisDrawing.ModelSpace.Count > 0 Then
        ReDim ssobjs(0 To ThisDrawing.ModelSpace.Count - 1) As AcadEntity
        Dim i As Integer
        For i = 0 To ThisDrawing.ModelSpace.Count - 1
        Set ssobjs(i) = ThisDrawing.ModelSpace.Item(i)
    Next
        ssetObj.AddItems ssobjs
        ssetObj.Erase
    End If

End Sub

 

Error code is Run-time error '91':

 

Object variable or With block variable not set.

 

Any ideas?

 

 

0 Likes
1,290 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

This line of code

 

CREATE_SET:
    
     Set ssetObj = ThisDrawing.SelectionSets.Add("MSset")

 

should not cause error of "Object varaiable or With block variable not set", be it running in Wndow7, 8 or 10.

 

From my observation, your code have logic error when the drawing DOES have a SelectionSet named "MSset" (for example, when you run your code the second time, because the first run creates the SelectionSet "MSset" and does not delete it).

 

Follow your code carefully:

 

1. When the drawing has a SelectionSet names as "MSset" (created by previous running your code), these lines of code executed correctly:

 

On Error GoTo CREATE_SET
        ThisDrawing.SelectionSets("MSset").Delete

GoTo SKIP_CREATE_SET

 

That is, the SelectionSet is deleted first, and then the next line asks the code to go to "SKIP_CREATE_SET" label, thus, these lines of code are skipped:

 

CREATE_SET:  
     Set ssetObj = ThisDrawing.SelectionSets.Add("MSset")

 

and directly go to the following If....End If. As you can see, in this case, ssetObj variable is NOT SET! So, this line of code raises the "91" error:

 

ssetObj.AddItems ssobjs

 

Excessively using GoTo labels in code is bad practice that leads to spaghetti code, difficult to read/follow/debug

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

Anonymous
Not applicable
Once you properly initialize the selectionset as told you by Norman, wouldn't it be simpler to use:

With ssetObj
.Select acSelectionSetAll
.Erase
End With
0 Likes