Jody Posted August 23, 2023 Posted August 23, 2023 Hi all, Recently got sent some lisps from a colleague of a simple door where you snap your own widths however whenever the lisp is inserted, the OS snap turns off and resets every snap to turn off. Assuming it's something in the coding but I'm not sure what it could be. Thanks in advance. Quote
SLW210 Posted August 23, 2023 Posted August 23, 2023 Hard to tell without the code. Most likely, it starts by turning off OSNAP and doesn't save the original settings and resets it after the code runs. 1 Quote
j2lstaples Posted August 23, 2023 Posted August 23, 2023 2 hours ago, SLW210 said: Hard to tell without the code. Most likely, it starts by turning off OSNAP and doesn't save the original settings and resets it after the code runs. I agree with this sentiment. Although I will still try to pose a solution. Look for the lisp routine .lsp file that is being used and read it with a code editor (Notepad++, VSCode). In the file, and more specifically in the function being called, search for "osmode" using Ctrl+F. Somewhere along the code, there's probably a code that looks like this: (setq osmode 66) (setvar "osmode" osmode) This can be remedied by adding a line before the setvar line to get the previous object snap settings: (setq osmode_return (getvar "OSMODE")) At the end of the code, just reset the OSNAP settings back: (setvar "osmode" osmode_return) Quote
Jody Posted August 25, 2023 Author Posted August 25, 2023 (edited) On 8/23/2023 at 7:04 AM, SLW210 said: Hard to tell without the code. Most likely, it starts by turning off OSNAP and doesn't save the original settings and resets it after the code runs. I've managed to find out why it was turning snap off (the second SETVAR "OSMODE" was on 0 not 1) but that only retains one snap, the end point snap. Is there a way to make it retain the snaps the user had selected before using the lisp routine? ;--RIGHTHAND DOOR-- (DEFUN C:DOORRH (/ A L W P1 endP rtA txtA ) (setvar "cmdecho" 0) (graphscr) (SETVAR "OSMODE" 1) (command ".LAYER" "M" "RDG_GS_Dor" ^C) (PROMPT "RIGHT HAND DOOR - Enter width <0.800>: ") (INITGET (+ 2 4)) (SETQ W (GETDIST)) (IF (= W NIL) (SETQ W 0.8)) (SETQ P1 (GETPOINT "\nFirst point: ")) (SETQ endP (GETPOINT P1 "\nNext point: ")) (SETVAR "OSMODE" 0) (IF (/= endP NIL) (PROGN (SETQ L (DISTANCE P1 endP)) (SETQ A (ANGLE P1 endP)) (IF (OR (<= A (/ PI 2.0)) (>= A (+ PI (/ PI 2.0)))) (SETQ txtA (ANGTOS A 0 3)) (SETQ txtA (ANGTOS (- A PI) 0 3)) ) ) ) (SETQ rtA (- (ANGLE P1 endP) (/ PI 2.0))) (IF (< rtA 0) (+ rtA (/ PI 2.0))) (command ".PLINE" P1 (POLAR P1 rtA W) "A" "R" W (POLAR P1 A W) "") (princ) ) Edited August 25, 2023 by SLW210 Added Code Tags! Quote
Jody Posted August 25, 2023 Author Posted August 25, 2023 (edited) On 8/23/2023 at 9:19 AM, jonathann3891 said: Post the lisp in question. I've managed to find out why it was turning snap off (the second SETVAR "OSMODE" was on 0 not 1) but that only retains one snap, the end point snap. Is there a way to make it retain the snaps the user had selected before using the lisp routine? ;--RIGHTHAND DOOR-- (DEFUN C:DOORRH (/ A L W P1 endP rtA txtA ) (setvar "cmdecho" 0) (graphscr) (SETVAR "OSMODE" 1) (command ".LAYER" "M" "RDG_GS_Dor" ^C) (PROMPT "RIGHT HAND DOOR - Enter width <0.800>: ") (INITGET (+ 2 4)) (SETQ W (GETDIST)) (IF (= W NIL) (SETQ W 0.8)) (SETQ P1 (GETPOINT "\nFirst point: ")) (SETQ endP (GETPOINT P1 "\nNext point: ")) (SETVAR "OSMODE" 0) (IF (/= endP NIL) (PROGN (SETQ L (DISTANCE P1 endP)) (SETQ A (ANGLE P1 endP)) (IF (OR (<= A (/ PI 2.0)) (>= A (+ PI (/ PI 2.0)))) (SETQ txtA (ANGTOS A 0 3)) (SETQ txtA (ANGTOS (- A PI) 0 3)) ) ) ) (SETQ rtA (- (ANGLE P1 endP) (/ PI 2.0))) (IF (< rtA 0) (+ rtA (/ PI 2.0))) (command ".PLINE" P1 (POLAR P1 rtA W) "A" "R" W (POLAR P1 A W) "") (princ) ) Edited August 25, 2023 by SLW210 Added Code Tags! Quote
j2lstaples Posted August 25, 2023 Posted August 25, 2023 (edited) ;--RIGHTHAND DOOR-- (DEFUN C:DOORRH (/ A L W P1 endP rtA txtA) (setvar "cmdecho" 0) (graphscr) (setq osmode_return (getvar "OSMODE")) ;; Get Current OSNAP setting (SETVAR "OSMODE" 1) (command ".LAYER" "M" "RDG_GS_Dor" ^C) (PROMPT "RIGHT HAND DOOR - Enter width <0.800>: ") (INITGET (+ 2 4)) (SETQ W (GETDIST)) (IF (= W NIL) (SETQ W 0.8)) (SETQ P1 (GETPOINT "\nFirst point: ")) (SETQ endP (GETPOINT P1 "\nNext point: ")) (SETVAR "OSMODE" 0) (IF (/= endP NIL) (PROGN (SETQ L (DISTANCE P1 endP)) (SETQ A (ANGLE P1 endP)) (IF (OR (<= A (/ PI 2.0)) (>= A (+ PI (/ PI 2.0)))) (SETQ txtA (ANGTOS A 0 3)) (SETQ txtA (ANGTOS (- A PI) 0 3)) ) ) ) (SETQ rtA (- (ANGLE P1 endP) (/ PI 2.0))) (IF (< rtA 0) (+ rtA (/ PI 2.0))) (command ".PLINE" P1 (POLAR P1 rtA W) "A" "R" W (POLAR P1 A W) "" ) (setvar "OSMODE" osmode_return) ;; Added this to return to user's previous OSNAP setting (princ) ) It's not that hard to figure out this from the previous replies. Edited August 25, 2023 by j2lstaples Quote
BIGAL Posted August 26, 2023 Posted August 26, 2023 In some cases you want to reset multiple variables this can be done using a list of variable names and their new values. I just use a dumb approach. (setq oldsnap (getvar 'osmode)) (setvar 'osmode 0) (setq oldangdir (getvar 'angdir)) (setvar 'angdir 1) ; bottom of code (setvar 'osmode oldsnap) (setvar 'angdir oldangdir) If you have error trap defun a good idea is reset the variables there also. Quote
Steven P Posted August 26, 2023 Posted August 26, 2023 So adding to what was said above, if you set a variable in a LISP then you should set it back again - which is what you are asking. BigAl example above is clear what to add where and when and j2lstaples has added it to the code as an example. This is good if you have only a couple of variables to temporarily change, more and it might be more efficient to use a list (as BigAl suggests), something like this: ;;At the beginning of the LISP... best to use as close to the point where you need the variables changed (in case of errors) ;;3 lines to disable command prompts and echo (setq vars '("CMDECHO" "FILEDIA" "NOMUTT")) ; list "vars" holding the variable names to change (setq oldvars (mapcar 'getvar vars)) ; list "oldvars" with their current values (mapcar 'setvar vars '(0 0 0)) ; set the list to the values you want ;;and at the end of the LISP - or even better as soon after when you no longer need the variables changed add this (if oldvars (mapcar 'setvar vars oldvars)) ; if the lisp variable oldvars exists, reset the variables. Don't localise vars or oldvars (don't add them to the defun line) and then you can use that in errors, which might be something like this: (defun trap ( msg ) (setq *error* tmperr) (if (not (member msg '("Function cancelled" "quit / exit abort" "bad argument type: lentityp nil"))) (princ (strcat "\nError: " msg)) (princ "-Cancelled-") ) (command-s "undo" "end") (if oldvars (mapcar 'setvar vars oldvars)) (setq *error* tmperr) (princ) ) appreciating that this might be a little beyond your LISPs ability just now, but if you remember that you can do this a few ways and there is a way to reset it all in the case of errors you can always come and ask about it if you need to. Final thing. osnaps variable is a number, adding up the value for each snap you want to use gives the osnap number. I have a LISP that runs at startup to set the snaps as I like them - that way whatever state I leave it all in, open a drawing and all is good again You could use this list to work out the value that you like your snaps to be and when you reset your snap setting just hard code the value as you like it to be (based on that most people have LISPs for their use rather than company wide LISPs). Save this LISP away, adjust to suit your preferences and if the snaps mess up ever just run "setsnaps" (defun c:setsnaps ( / snaps ) ;;Snaps. ( * 1.. to use snap, ( * 0 to turn off snap. (setq snaps 0) (setq snaps (+ snaps (* 1 0))) ;None (setq snaps (+ snaps (* 1 1))) ;End Point (setq snaps (+ snaps (* 1 2))) ;Mid Point (setq snaps (+ snaps (* 1 4))) ;Centre (setq snaps (+ snaps (* 0 8))) ;Node (setq snaps (+ snaps (* 0 16))) ;Quadrant (setq snaps (+ snaps (* 1 32))) ;Intersection (setq snaps (+ snaps (* 0 64))) ;Insertion (setq snaps (+ snaps (* 0 128))) ;Perpendicular (setq snaps (+ snaps (* 1 256))) ;Tangent (setq snaps (+ snaps (* 0 512))) ;Nearest (setq snaps (+ snaps (* 0 1024))) ;Geometric Centre (setq snaps (+ snaps (* 1 2048))) ;Apparent Intersection (setq snaps (+ snaps (* 0 4096))) ;Extrnsion (setq snaps (+ snaps (* 0 8192))) ;Parallel (setq snaps (+ snaps (* 0 16348))) ;Superess all running snaps (setvar 'osmode snaps) Quote
BIGAL Posted August 27, 2023 Posted August 27, 2023 I just do Osnap set the snaps I want to use then get a Pencil you know that thing that marks paper, then type OSMODE write down the number. 1 Quote
Steven P Posted August 27, 2023 Posted August 27, 2023 Pencil? No,. No idea, have to explain that one! Quote
BIGAL Posted August 27, 2023 Posted August 27, 2023 (edited) Sorry forgot you know the red crayon. Edited August 27, 2023 by BIGAL Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.