Selection Sets

Discussion in 'AutoCAD' started by ajtruckle, Nov 10, 2004.

  1. ajtruckle

    ajtruckle Guest

    My program creates a selection set and does it's stuff. Then it deletes it.

    But when debugging and if i make a mistake and it bombs out (for whatever reason) the selection set remains there.

    When I run it again, it crashes because the selection set already exists.

    i can't work how to test if a given selection set exists and delete it BEFORE creating mine so as to avoid this error.

    How do i do that? I tried using item but then that will crash if there is no selection set of that name present. so i can't win.

    Thanks.
     
    ajtruckle, Nov 10, 2004
    #1
  2. ajtruckle

    Jürg Menzi Guest

    Hi ajtruckle

    This one returns a new selection set by name. If a selection set with the
    given name exists, it will be deleted.
    Code:
    Public Function MeSelectionSet(SelNme As String) As AcadSelectionSet
    
    On Error Resume Next
    
    With ThisDrawing
    Set MeSelectionSet = .SelectionSets.Add(SelNme)
    If Err.Number <> 0 Then
    Err.Clear
    .SelectionSets.Item(SelNme).Delete
    Set MeSelectionSet = .SelectionSets.Add(SelNme)
    End If
    End With
    
    End Function
    
    Cheers
     
    Jürg Menzi, Nov 10, 2004
    #2
  3. This is a porting of a module I use to create
    all my selections sets and other items..Nice
    if you want to have error check off while
    debugging...

    Slower than Jürg's method of grabbing
    the error though...
    gl...
    Paul

    '****************************
    Public Function ssCreate(ByVal ssType As String) _
    As AcadSelectionSet

    Dim ssGen As AcadSelectionSet
    Dim gpCode(0) As Integer
    Dim GPData(0) As Variant
    Dim groupCode As Variant
    Dim dataCode As Variant
    Dim boolPassAdd As Boolean
    With ThisDrawing
    '***Begin Create Selection Set
    For Each ssGen In .SelectionSets
    If ssGen.Name = ssType Then
    ssGen.Clear
    Application.Update
    boolPassAdd = True
    Exit For
    End If
    Next ssGen

    Select Case boolPassAdd
    Case Is = True'don't add
    Set ssGen = .SelectionSets(ssType)
    Case Is = False'Add
    Set ssGen = .SelectionSets.Add(ssType)
    End Select
    '***End Create Selection Set
     
    Paul Richardson, Nov 10, 2004
    #3
  4. Hi Paul,

    You can handle the error checking with:

    With ThisDrawing
    Set MeSelectionSet = .SelectionSets.Add(SelNme)
    If Err.Number <> 0 Then
    Err.Clear
    .SelectionSets.Item(SelNme).Delete
    Set MeSelectionSet = .SelectionSets.Add(SelNme)
    End If
    End With
    On Error Goto 0 ' Additional to Jurg's code effectively turns error
    checking off at this point

    Why did you use a "Select Case" statement rather than an "If " statement ?

    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au
     
    Laurie Comerford, Nov 10, 2004
    #4
  5. ajtruckle

    ajtruckle Guest

    Thank you. I will try this tomorrow.

    Andrew
     
    ajtruckle, Nov 10, 2004
    #5
  6. ajtruckle

    Jürg Menzi Guest

    Hi Laurie

    Thx...
    I'm not a bright bulb in programming VB(A)... but in my knowledge a defined
    error handling will be reset at the end of a function - isn't it?

    Cheers
     
    Jürg Menzi, Nov 10, 2004
    #6
  7. Juerg, an "on error resume next" will bubble up to the calling function
    if the calling function has an error handler defined
     
    Jorge Jimenez, Nov 10, 2004
    #7
  8. Thanks Laurie, I like the other version for debugging so
    I can have all error check off..Probably a better way...
     
    Paul Richardson, Nov 10, 2004
    #8
  9. ajtruckle

    Ed Jobe Guest

    From Help:
    *****
    On Error Resume Next
    Specifies that when a run-time error occurs, control goes to the statement
    immediately following the statement where the error occurred where execution
    continues. Use this form rather than On Error GoTo when accessing objects.
    *****
    When you use the Resume keyword, execution continues after the Err object is
    reset. So no error would bubble up to the calling function. In order for an
    error to bubble up, you simply do not handle it. If no procedure handles it,
    the program terminates execution.
     
    Ed Jobe, Nov 10, 2004
    #9
  10. ajtruckle

    Ed Jobe Guest

    Hi Jurg,
    As long as you're using a With statement, why not use the following?
    With ThisDrawing.SelectionSets
    .Add()
    .Add()
    End With
     
    Ed Jobe, Nov 10, 2004
    #10
  11. Few other things...Always delete your selection
    sets when you done with them...Ugly crashes
    can happen...Not that all crashes arn't ugly I guess.

    If your code bugs before selection set gets deleted
    you can use imediate window while in debug mode
    to delete your selection set...then continue on..
    *ThisDrawing.SelectionSets("YourSelSet").delete' then hit enter

    Imediate window is also good for changing entities color
    while stepping thru code..so you can find the ent easy...

    gl
    Paul
     
    Paul Richardson, Nov 10, 2004
    #11
  12. Hi Paul,

    Another approach to this is:

    Public Const bDebug = True
    Public Const bDebug = False

    with one or the other commented out.

    Then create a function:

    Function DisplayForDebugging (oObj as AcadObject, iColour as Integer)

    If bDebug = True then
    oObj.Color = iColour
    oObj.Update
    End If
    End Function

    and use it like this:

    Dim oLine as AcadLine
    .....
    Set oLine = ThisDrawing.ModelSpace.AddLine (Pt1, Pt2)
    DisplayForDebugging oLine, 1

    You could make the more useful by:
    Doing a zoom centre on the object with scale adjusted to its bounding box,
    Change the Layer to "DeleteMeAfterDebuggingComplete" or whatever appeals
    Changing the lineweight
    Pausing to give you time to examine it etc. as your imagination allows.
    Deleting the object later.
    --

    Regards,


    Laurie Comerford
    www.cadapps.com.au
     
    Laurie Comerford, Nov 10, 2004
    #12
  13. Nice...tks...
     
    Paul Richardson, Nov 10, 2004
    #13
  14. Juerg wondered why the use of the on error goto 0
    at the end of the function, if the on error resume next
    will be out of scope when the function is done.

    Well he is right, the on error resume next is only active
    inside the function where it was used, so there is no need
    to place an on error goto 0 at the end of the function.
    But the err object will keep the value (at least in a VB module)
    so one needs to clear the err object using err.clear
    Any on error statement including the "on error goto 0"
    will also clear the err object, but it's misleading to use
    such a statement at the end of a function.

    Uhmm... don´t about the way VBA handles the error
    but in VB, if your calling procedure has an error handling routine
    and the called procedure has an "ON ERROR RESUME NEXT"
    (in which you do not clear the err object manually), the error WILL bubble
    up
    and be handled by the error handling routine in the calling procedure.

    So, if you enable the on error resume next, it's because you will
    be doing inline error checking, and should be clearing the err object
    (manually) after each check
     
    Jorge Jimenez, Nov 11, 2004
    #14
  15. ajtruckle

    Jürg Menzi Guest

    Hi Ed

    I've seen that after I post it... thanks anyway

    Cheers
     
    Jürg Menzi, Nov 11, 2004
    #15
  16. ajtruckle

    Jürg Menzi Guest

    Hi

    Thx Ed and Jorge...

    So, the syntax with Err.Clear after error checking will be the correct
    solution.

    Cheers
     
    Jürg Menzi, Nov 11, 2004
    #16
  17. ajtruckle

    ajtruckle Guest

    There are so many responses with mixed comments on usage of error handlers. Is the initial suggestion OK or should it be adjusted?

    Andrew
     
    ajtruckle, Nov 11, 2004
    #17
  18. ajtruckle

    mccad Guest

    Function CreateSelectionSet(Optional SSetName As String = "mjtd") As
    AcadSelectionSet
    On Error Resume Next
    ThisDrawing.SelectionSets(SSetName).Delete
    Set CreateSelectionSet = ThisDrawing.SelectionSets.Add(SSetName)
    End Function
     
    mccad, Nov 11, 2004
    #18
  19. ajtruckle

    Ed Jobe Guest

    This is from the help for the Err.Clear method:
    ****
    The Clear method is called automatically whenever any of the following
    statements is executed:
    Any type of Resume statement
    Exit Sub, Exit Function, Exit Property
    Any On Error statement
    ****
    Using the tests below, if you set a watch on the Err object in errtest1, you
    will see that there is a tiny exception to the help doc's statement. The On
    Error Resume Next does not actually Clear the Err object at that moment, but
    excution does continue. However, the Err object is cleared when the sub
    finishes and reaches the End statement, so errtest1 never sees an error. If
    you comment out the Resume Next line, an error does bubble up and the Resume
    statement in errtest1 does clear the Err object.

    Sub errtest1()
    On Error GoTo Err_Control
    errtest2
    Debug.Print "errtest1 completed"

    Exit_Here:
    Exit Sub
    Err_Control:
    Select Case Err.Number
    Case Else
    MsgBox Err.Number & ", " & Err.Description, , "errtest1"
    Err.Clear
    Resume Exit_Here
    End Select
    End Sub

    Sub errtest2()
    On Error Resume Next
    Err.Raise 1000, "errtest2", "This error was raised by errtest2."
    Debug.Print "errtest2 completed"
    End Sub
     
    Ed Jobe, Nov 12, 2004
    #19
  20. ajtruckle

    Jürg Menzi Guest

    Hi Andrew

    IMOH, this is a possible solution - there are many roads to travel to roma...
    And thx to Ed and McCad for their input...¦-)
    Code:
    Public Function MeSelectionSet(Optional SelNme As String = "TmpSet") _
    As AcadSelectionSet
    
    On Error Resume Next
    
    With ThisDrawing.SelectionSets
    Set MeSelectionSet = .Add(SelNme)
    If Err.Number <> 0 Then
    Err.Clear
    .Item(SelNme).Delete
    Set MeSelectionSet = .Add(SelNme)
    End If
    End With
    
    End Function
    
    Cheers
     
    Jürg Menzi, Nov 13, 2004
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.