leonucadomi Posted Friday at 10:00 PM Posted Friday at 10:00 PM hello : I use this code to quickly change a variable on or off. I would like to know if someone can improve it in appearance or functionality. Quote (defun c:SF (/ gira) (repeat 100 (setq gira (getreal "\nActiva UCS FOLLOW <1>: ")) (if (equal gira nil)(setq gira 1)) (if (= gira 1) (Command "_UCSFOLLOW" "1" "") (princ "\nUCS Follow Activado") ) (setq gira (getreal "\nDesactiva UCS FOLLOW <1>: ")) (if (equal gira nil)(setq gira 1)) (if (= gira 1) (Command "_UCSFOLLOW" "0" "") (princ "\nUCS Follow Desactivado") ) );repeat );fin defun Quote
GLAVCVS Posted Friday at 10:52 PM Posted Friday at 10:52 PM 'repeat 100...'? Changing the same variable 100 times...why? I guess that's a mistake. Something simple: every time the command sf is called, UCSFOLLOW changes state, without asking (defun c:sf (/ act) (setvar "UCSFOLLOW" (if (setq act (= (getvar "UCSFOLLOW") 0)) 1 0)) (princ (strcat "\n*** Variable UCSFOLLOW " (if act "activada" "desactivada"))) (princ) ) 1 Quote
Tsuky Posted Friday at 11:06 PM Posted Friday at 11:06 PM Or this, for toggle (defun c:SF ( / ) (if (zerop (boole 1 (getvar "UCSFOLLOW") 1)) (progn (setvar "UCSFOLLOW" (1+ (getvar "UCSFOLLOW"))) (princ "\nUCS Follow Activado") ) (progn (setvar "UCSFOLLOW" (1- (getvar "UCSFOLLOW"))) (princ "\nUCS Follow Desactivado") ) ) (prin1) ) 1 1 Quote
Lee Mac Posted Saturday at 11:27 AM Posted Saturday at 11:27 AM Another - (defun c:sf ( ) (setvar 'ucsfollow (- 1 (getvar 'ucsfollow))) (princ) ) 1 1 Quote
leonucadomi Posted yesterday at 03:06 PM Author Posted yesterday at 03:06 PM On 9/26/2025 at 4:52 PM, GLAVCVS said: 'repeat 100...'? Changing the same variable 100 times...why? I guess that's a mistake. Something simple: every time the command sf is called, UCSFOLLOW changes state, without asking (defun c:sf (/ act) (setvar "UCSFOLLOW" (if (setq act (= (getvar "UCSFOLLOW") 0)) 1 0)) (princ (strcat "\n*** Variable UCSFOLLOW " (if act "activada" "desactivada"))) (princ) ) On 9/26/2025 at 4:52 PM, GLAVCVS said: 'repeat 100...'? Changing the same variable 100 times...why? I guess that's a mistake. Something simple: every time the command sf is called, UCSFOLLOW changes state, without asking (defun c:sf (/ act) (setvar "UCSFOLLOW" (if (setq act (= (getvar "UCSFOLLOW") 0)) 1 0)) (princ (strcat "\n*** Variable UCSFOLLOW " (if act "activada" "desactivada"))) (princ) ) I do it by repeating it 100 times because I don't know how to make a routine repeat itself indefinitely until I execute escape. Quote
leonucadomi Posted yesterday at 03:09 PM Author Posted yesterday at 03:09 PM What I'm looking for is not to have a routine that turns it on and one that turns it off, but rather a single routine that, like a roulette wheel, allows me to change the state of the variable just with enter Quote
GLAVCVS Posted yesterday at 05:38 PM Posted yesterday at 05:38 PM (edited) Another way, taking advantage of @Lee Mac's idea and condensing an output message for the user (defun c:sf (/ v) (princ (strcat "\n*** " (setq v "UCSFOLLOW") (nth (setvar v (- 1 (getvar v))) '(" desactivado" " activado")))) (princ) ) Edited yesterday at 05:40 PM by GLAVCVS 1 Quote
mhupp Posted 2 hours ago Posted 2 hours ago Another way not as compact but a little easier to read/follow. Would also handle if uscfollow wasn't 1 or 0 Tho i don't know when/if that would ever happen. ;; Toggle UCSFOLLOW using COND (defun c:foo (/ v) (setq v (getvar "UCSFOLLOW")) (cond ((= v 1) ;; If it's currently ON turn it OFF (setvar 'UCSFOLLOW 0) (princ "\nUCSFOLLOW Desactivado") ) ((= v 0) ;; If it's currently OFF turn it ON (setvar "UCSFOLLOW" 1) (princ "\nUCSFOLLOW Activado") ) (T ;; If it’s some unexpected value, default to OFF (setvar "UCSFOLLOW" 0) (princ "\nUCSFOLLOW Desactivado") ) ) (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.