Error in error function

Discussion in 'AutoCAD' started by jlspartz, Feb 4, 2005.

  1. jlspartz

    jlspartz Guest

    I'm not sure exactly how the error function works, but I am trying to reference the "setback" function that I created for an error also. What am I doing wrong here?

    Here is the main function for my code:

    (defun c:lstate-fp (/ lay rors)
    (setvar "cmdecho" 0)
    (command "undo" "end")
    (command "undo" "group")
    (setq *ERROR* setback)
    (setq lay(getvar "clayer"))
    (setvar "clayer" "0")
    (prompt "\nLayer State FLOOR_PLAN: ")
    (setq rors (strcase (getstring "\nRestore or Save? [R/S]: ")))
    (cond
    ((= rors "R")(r-fp))
    ((= rors "S")(s-fp))
    (t(setback))
    )
    )

    and here's my setback function:

    (defun setback (strErr)
    (if
    (= 1 (logand (cdr (assoc 70 (entget (tblobjname "layer" lay))))1))
    (setvar "clayer" "0")
    (setvar "clayer" lay)
    )
    (command "undo" "end")
    (setvar "cmdecho" 1)
    (princ)
    )
     
    jlspartz, Feb 4, 2005
    #1
  2. jlspartz

    ECCAD Guest

    Try this approach.
    (setq OLDERR *error*)
    (defun *error* (errmes)
    (princ (strcat "\nExecution halted by the following error: " ERRMES))
    (reset_settings)
    (setq *error* OLDERR)
    (princ)
    ); function

    (defun get_settings ()
    (setq lay (getvar "clayer"))
    (setq OS (getvar "OSMODE"))
    ); function

    (defun reset_settings ()
    (if
    (= 1 (logand (cdr (assoc 70 (entget (tblobjname "layer" lay))))1))
    (setvar "clayer" "0")
    (setvar "clayer" lay)
    )
    (command "undo" "end")
    (setvar "cmdecho" 1)
    (if OS (setvar "OSMODE" OS))
    (princ)
    ); function

    (defun c:lstate-fp (/ lay rors)
    (get_settings); get current settings
    (setvar "cmdecho" 0)
    (command "undo" "end")
    (command "undo" "group")
    (setvar "clayer" "0")
    (prompt "\nLayer State FLOOR_PLAN: ")
    (setq rors (strcase (getstring "\nRestore or Save? [R/S]: ")))
    (cond
    ((= rors "R")(r-fp))
    ((= rors "S")(s-fp))
    )
    (reset_settings)
    (princ)
    )
     
    ECCAD, Feb 4, 2005
    #2
  3. jlspartz

    Tom Smith Guest

    I'm not sure exactly how the error function works, but I am trying to
    reference the "setback" function that I created for an error also. What am
    I doing wrong here?
    Your setback function takes a message argument, as error handlers should,
    but you're calling it without one. Change the call to (setback "") or
    (setback nil) and it should work.
     
    Tom Smith, Feb 4, 2005
    #3
  4. jlspartz

    jlspartz Guest

    So I would change my lines to:

    (setq *ERROR* setback nil)

    and

    (t(setback nil))

    ?
     
    jlspartz, Feb 5, 2005
    #4
  5. jlspartz

    Tom Smith Guest

    No, just change the function call to (t(setback nil)) instead of (t(setback)). Your setback function expects one argument and you are providing none.
     
    Tom Smith, Feb 5, 2005
    #5
  6. jlspartz

    Tom Smith Guest

    Let me back up a little. On second thought, my suggestion on the invalid error call isn't going to prevent problems.

    If you're going to do a non-local error handler, you need to save the built-in *error*, and your custom *error*, when executed, needs to restore it. A simpler method is to declare your *error* local to your function, to avoid this need. This has been discussed here in vast detail, so I'm not going to rehash. Just to point out, your code as written will overwrite and defeat the normal error handling mechanism in a drawing session.

    I don't think a clayer-restore function can be guaranteed to work in this context without a lot more elaboration. For instance, the saved clayer may be frozen in the restored layer state, and therefore can't be set current again. So a clayer-restore function would need to address this, and might be more trouble than it's worth.

    I don't think you need an error handler here at all, except to close the undo group, and I'm not sure yiou even need the undo group.

    You're only calling the setback function if the user fails to make one of two choices. You can eliminate this by using initget and getkword instead of getstring, to prevent invalid user input. You should set one of the choices as the default and let the user choose it with a return. It's more an input handling question than an error handling issue.

    I'm not sure where you're going with this, but I prefer to manage layer states using the built-in layer state manager. Our template drawings already include the normal layer states we use. As a fallback, for dealing with older drawings or in case layers have been prematurely purged, we have a button to import the standard layer states from a file. To switch states, we just use the layer command.

    I haven't had a request for a different interface for restoring layer states, but I can predict from past experience what my users would want. They'd be very impatient with a command/button/whatever which did nothing but ask them which of two functions to run. They'd want to have access to those two functions directly, and skip the front end function. For instance, they'd want a (c:rfp) keyboard command to run the (r-fp) function directly. They don't like having to answer a question in order to do what they already know they want to do.

    Having said that, I'd write your function as follows:

    (defun c:lstate-fp (/ kword)
    (princ "\nLayer State FLOOR_PLAN")
    (initget "Save Restore")
    (if (setq kword (getkword "\nSave/<Restore> :"))
    (setq kword (strcase kword))
    (setq kword "RESTORE"))
    (if (= kword "SAVE")
    (s-fp)
    (r-fp))
    (princ))

    In other words, I'd reduce it what it essentially seems to be: a choice of which routine to run. I'd put all the layer-setting, undo-handling, etc. in those routines, and then you don't need that stuff here. And I'd also provide:

    (defun c:sfp ()
    (s_fp)
    (princ))

    (defun c:rfp ()
    (r_fp)
    (princ))

    If those are going to work as standalone routines, as I think they should, then they will have to handle all of their own overhead anyway.

    Hope this helps.
     
    Tom Smith, Feb 6, 2005
    #6
  7. jlspartz

    jlspartz Guest

    Well, we have an option for users to import standard layer states from a pull-down too. But, they hate even going to the layer state manager within the layers dialog box. And then to save a layer state, they have to retype each name exactly the way it shows in the manager to overwrite it. So, I made buttons, each one for each state. If they restore a state that doesn't exist in their drawing, it will import the standard state and restore it. All they have to do it click the button and then type r or s for save or restore, but I like the idea of setting restore as the default. Thanks!

    Jamie
     
    jlspartz, Feb 7, 2005
    #7
  8. jlspartz

    Tom Smith Guest

    No problem. If you want to venture into error handling, do a search in this
    NG. It's been discussed in great detail a number of times.
     
    Tom Smith, Feb 7, 2005
    #8
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.