Jump to content

Recommended Posts

Posted

How can I return the osmode to user's previous mode after running any of my lisp?

 

Could you please help?

 

Thanks

Posted (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 by marko_ribar
Posted

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.

Posted

I cannot believe my eyes!

An answer but the living legend himself!

giphy.gifgiphy.gif

I will see your Error handling tutorial.

 

Thanks Lee Mac

and thank you Marko

 

Posted

But how do I restore it to previous and not something I define?

 

Maybe I am not getting it - please help

help me new girl quotes GIF (GIF Image)

Posted
(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.

Posted

Is this how I should be using it? sorry for being dumb...

giphy.gif

(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)
)

 

Posted

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)
)

 

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...