Sambuddy Posted November 19, 2019 Posted November 19, 2019 How can I return the osmode to user's previous mode after running any of my lisp? Could you please help? Thanks Quote
marko_ribar Posted November 19, 2019 Posted November 19, 2019 (edited) The best way is through error handler... If program is terminated ESC, error handler is initialized and there if defined - variable "osmode" can be restored... Simple way : (defun c:foo ( / osm ) (setq osm (getvar 'osmode)) (setvar 'osmode 0) ; I set it to 0, but you should find what's the best for program processing ... do your stuff ... (setvar 'osmode osm) ; restore old osmode at the end of routine... (princ) ; silent exit without nil prompt ) With error handler : (defun c:foo ( / *error* osm ) (defun *error* ( m ) (if osm (setvar 'osmode osm) ) (if m (prompt m) ) (princ) ) (setq osm (getvar 'osmode)) (setvar 'osmode 0) ; again set it to preferable by routine processing ... do your stuff ... (*error* nil) ) ; calling error handler at the end which restores old osnap stored in variable "osm"; at the same time silent exit without nil prompt as last line in error definition is (princ) Like I stated through error handler is better as whenever routine is unexpectidly terminated, routine will exit silently with old osmode restored... HTH., M.R. Edited November 19, 2019 by marko_ribar Quote
Lee Mac Posted November 19, 2019 Posted November 19, 2019 You may wish to refer to my tutorial on Error Handling; the tutorial specifically includes an example demonstrating how to restore the OSMODE system variable value in the event of an error or on completion of the program. Quote
Sambuddy Posted November 19, 2019 Author Posted November 19, 2019 I cannot believe my eyes! An answer but the living legend himself! I will see your Error handling tutorial. Thanks Lee Mac and thank you Marko Quote
Sambuddy Posted November 19, 2019 Author Posted November 19, 2019 But how do I restore it to previous and not something I define? Maybe I am not getting it - please help Quote
Lee Mac Posted November 19, 2019 Posted November 19, 2019 (defun c:test ( / *error* osm ) (defun *error* ( msg ) (if osm (setvar 'osmode osm)) ;; <--- Restoring OSMODE to the original value in the case of an error (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)) ) (princ) ) (setq osm (getvar 'osmode)) ;; <--- Storing the current value of OSMODE (setvar 'osmode 0) ;; <--- Setting OSMODE to something new (rtos (getreal "\nPress Esc to exit, press Enter to force an error ...")) (setvar 'osmode osm) ;; <--- Restoring OSMODE to the original value (princ) ) Consider the above example from my tutorial - note the comments indicating where the original OSMODE value is stored and then restored. Quote
Sambuddy Posted November 19, 2019 Author Posted November 19, 2019 Is this how I should be using it? sorry for being dumb... (defun c:4 ( / *error* osm ) (setvar 'LUPREC 8) (setvar 'dynmode 3) (setvar 'dynprompt 1) (setq d (getreal "valeur d'entrée en - Input (ft.): "))(terpri) (setq e (getreal "valeur d'entrée en - Input (in.): "))(terpri) (setq f (getreal "valeur d'entrée en - Input (fraction; Example: 5/8 or 0.625): "))(terpri) (command (+ (* d 304.8 )(* e 25.4)(* f 25.4))) (setvar 'dynmode 0) (setvar 'dynprompt 0) (defun *error* ( msg ) (if osm (setvar 'osmode osm)) ;; <--- Restoring OSMODE to the original value in the case of an error (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)) ) (princ) ) (setq osm (getvar 'osmode)) ;; <--- Storing the current value of OSMODE (setvar 'osmode 0) ;; <--- Setting OSMODE to something new (rtos (getreal "\nPress Esc to exit, press Enter to force an error ...")) (setvar 'osmode osm) ;; <--- Restoring OSMODE to the original value (princ) ) Quote
Lee Mac Posted November 19, 2019 Posted November 19, 2019 Consider the following example: (defun c:4 ( / *error* d e f sysvals sysvars ) (defun *error* ( msg ) (mapcar 'setvar sysvars sysvals) ;; Restore original system variable values in the event of an error (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)) ) (princ) ) (setq sysvars '(luprec dynmode dynprompt) ;; System variables whose values will be changed sysvals (mapcar 'getvar sysvars) ;; Current system variable values ) (mapcar 'setvar sysvars '(8 3 1)) ;; Set system variables to new values (if (and (setq d (getreal "\nvaleur d'entrée en - Input (ft.): ")) (setq e (getreal "\nvaleur d'entrée en - Input (in.): ")) (setq f (getreal "\nvaleur d'entrée en - Input (fraction; Example: 5/8 or 0.625): ")) ) (princ (+ (* d 304.8) (* e 25.4) (* f 25.4))) ) (mapcar 'setvar sysvars sysvals) ;; Restore original system variable values (princ) ) 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.