Entity snaps cleared by a lisp command

Hi guys.

We have a lisp routine installed under a platinum version that is creating automatically an entity on my drawing. The problem is that when we activate this it automatically clears all the snaps we have selected. To use the entity snap again we have to reselect each option one by one again. Now, if I have to do this once in a while it would be OK but it happens everytime it is becoming a pain. Is there any way to reselect all entity snaps at onece - in the same time - with one click of the mouse?

We do have a "clear snaps" button which is cancelling all selections with one click why shouldn't we have a "select all snaps" button?

Or, is there any way not to have all snaps options deactivated when running the lisp command?

Comments

  • Hello,

    Add this simple code before and after your routine:
    [code](setq oldosmode (getvar "osmode")
    (your-lisp-routine)
    (setvar "osmode" oldosmode)[/code]

    Regards, Vaidas
  • Must be...
    [code](setq oldosmode (getvar "osmode") )[/code]

  • You could clear the snap (un)setting part of the lisp routine and remember to press F3 before and after running it.

    Many lisps don't work properly with osmode set to anything but zero. They used to, before Autocad had this bright idea.

    I use Vaidas' suggestion in many of mine, it works well unless the routine is interrupted. Then the osmode setting is lost.

    When that happens, I reset them using a routine called by pressing F12

    [code](Defun C:hfar ()
    (SETVAR "OSMODE" 255) - 255 is personal choice
    (COMMAND "dist")
    )[/code]

    So I have to measure some arbitrary distance just to reset osmode, but 'hfar' is used so often I don't mind.

    You could just leave out the 'dist' part, I'm economising on buttons.

    I also have to pick the original command from the menu again if I hadn't finished with it.

    I believe V12 uses F12, so when I upgrade I will have to think of a different key to use.

  • Hi all,

    with LISP there are two things to consider if your routine requires a change to an environment variable:
    1. Save and Reset the environment variable for cases where the LISP routine runs normally.
    2. What to do if the LISP routine fails to complete due to an error of some sort.
    Number one has been covered by the previous posts, but this won't work correctly for situations where there is an error in the code, or an error generated by the user. This second case is actually very common e.g. the user runs the routine, but then cancels it part way through, which is normal way to exit most standard commands.

    LISP routines that start, but don't complete a command are another (command "LINE") situation where environment variables can get mucked up. For example:
    [code](defun C:TEST ( / saveCMD)
     (setq *error* nil) ; undefine any existing error handler
     (setq saveCMD (getvar "CMDECHO")) ; save current command echo
     (setq saveOSM (getvar "OSMODE")) ; save current object snap
     (setvar "CMDECHO" 0) ; turn off command echo
     (setvar "OSMODE" 35) ; set OSNAP to ENDP, MIDP, INT
        (command "LINE") ; run the line command
     (setvar "CMDECHO" saveCMD) ; reset command echo
     (setvar "OSMODE" saveOSM) ; reset OSNAP
     (princ "\nTEST completed exiting")
     (prin1); make a quiet exit
    ); end TEST[/code]
    You can see the above routine saves environment variables, changes them to suit the routine, starts a command, then resets the save variables. If you run this you will see that the routine starts the LINE command, then carries on an exits with a message, resetting our variables on the way, which may not be quite what we expected. Changing the routine so that the command completes will give the expected result e.g.
    [code](defun C:TEST ( / saveCMD)
     (setq *error* nil) ; undefine any existing error handler
     (setq saveCMD (getvar "CMDECHO")) ; save current command echo
     (setq saveOSM (getvar "OSMODE")) ; save current object snap
     (setvar "CMDECHO" 0) ; turn off command echo
     (setvar "OSMODE" 35) ; set OSNAP to ENDP, MIDP, INT
        (command "LINE" (getpoint "\npick start point")(getpoint (getvar "LASTPOINT") "\npick end point") "") ; run the line command and draw a single line segment
     (setvar "CMDECHO" saveCMD) ; reset command echo
     (setvar "OSMODE" saveOSM) ; reset OSNAP
     (princ "\nTEST completed exiting")
     (prin1); make a quiet exit
    ); end TEST[/code]

    The message appears after you have drawn a single line segment. However you will notice that if you press escape (cancel) during the routine, that again the function doesn't complete, we get no message, and our environment variables aren't reset.

    What you need to have is an error handler that acts to clean up in situations where the routine fails to complete for one reason or another. LISP has a special function called *error* for this purpose e.g.
    [code]; *ERROR*
    ; Error handler should anything go wrong

    (defun *error* (error_msg)
     (if error_msg (princ (strcat "\n" error_msg ))) ; Print error message if given
     (gsvar T) ; reset any environment variables to their previous state
     (prin1)
    ) ; end *error*[/code]

    is an example of an error handler. This calls a routine called GSVAR that resets any environment variables. GSVAR is attached for anyone that wants to use it. GSVAR works by calling it at the start and end of your function e.g.
    [code](defun C:MYFUNCTION ()
        (gsvar '(cmdecho osmode)) ; save my vars
        .
        .
        (gsvar T) ; reset my vars
    )[/code]

    This coupled with an error handler pointing to gsvar should result in you environment variables being reset correctly.


    Regards,

    Jason Bourhill

    CAD Concepts



    gsvar.lsp

  • Thanks Jason, that's what some of mine always needed, but I was too dumb or impatient to work out how to do it.
This discussion has been closed.