CADWarrior Posted May 15, 2010 Posted May 15, 2010 Hey guys kinda new to this whole thing. And, well this is my first code I made by my self. A friend was telling me about error trapping and i was Planning on using this as a time saver later on. ;; Set var (defun setvars (vl nlv inout) (if(= inout 0) (progn (setq vlv (mapcar 'getvar vl)) (mapcar 'setvar vl nlv) ) ) (if(= inout 1) (progn (mapcar 'setvar vl vlv) ) ) ) ;;Refrence code for above (setvars '();|var names|; '();|var values|; 0;|0: save old vars and Input new Vars 1: restore old vars| ;; quick test (defun c:qc () (setvars '(ORTHOMODE OSMODE) '(0 16383) 0) (command "line") (setvars '(ORTHOMODE OSMODE) '(0 16383) 1) ) the only issue when i use this it doesnt use the (setvars) command it seemed like it would work to me but i cant figure out why its not. I can load 1 defun setvars in "VLIDE" then copy each line from defun c:qc and it works the way i want it to any help would be great. Quote
Lee Mac Posted May 15, 2010 Posted May 15, 2010 Welcome to CADTutor I'm not sure why your function doesn't work, beware of using 'qc' as a function syntax, as you should not use AutoCAD aliases as function syntax. This works for me: (defun setvars ( vl nlv inout / vlv ) (if (= inout 0) (progn (setq vlv (mapcar 'getvar vl)) (mapcar 'setvar vl nlv) ) (mapcar 'setvar vl nlv) ) vlv ) (defun c:test1 (/ *error* lst vlv ) (defun *error* ( msg ) (and vlv (setvars lst vlv 1)) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ) ) (setq lst '("ORTHOMODE" "OSMODE") vlv (setvars lst '(0 0) 0)) (command "_.line") (while (= 1 (logand 1 (getvar 'CMDACTIVE))) (command pause) ) (setvars lst vlv 1) (princ) ) But you could also approach it this way: (defun setvars ( vl nlv / ol ) (setq ol (mapcar 'getvar vl)) (mapcar 'setvar vl nlv) ol ) (defun c:test2 (/ *error* lst vlv ) (defun *error* ( msg ) (and vlv (setvars lst vlv)) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ) ) (setq lst '("ORTHOMODE" "OSMODE") vlv (setvars lst '(0 0))) (command "_.line") (while (= 1 (logand 1 (getvar 'CMDACTIVE))) (command pause) ) (setvars lst vlv) (princ) ) Lee Quote
CADWarrior Posted May 15, 2010 Author Posted May 15, 2010 Thank you very much Lee. I was reading through your code and found out why mine was not working. My lisp was working as intended but it had no pause for user input. It was finishing the lisp before i could even draw the line My new code (defun setvars (vl nlv inout) (if(= inout 0) (progn (setq vlv (mapcar 'getvar vl)) (mapcar 'setvar vl nlv) ) ) (if(= inout 1) (progn (mapcar 'setvar vl vlv) ) ) ) ;; quick test (defun c:test3 (/) (setq svin '(setvars '(ORTHOMODE OSMODE) '(1 16383) 0)) (setq svout '(setvars '(ORTHOMODE OSMODE) '(1 16383) 1)) (eval svin) (command "_.line") (while (= 1 (logand 1 (getvar 'CMDACTIVE))) (command pause) ) (eval svout) ) (defun *error* ( msg ) (eval svout) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ) ) Now anytime some one "tries to escape" it will reset all the values. Quote
alanjt Posted May 15, 2010 Posted May 15, 2010 I'm not sure why your function doesn't work, beware of using 'qc' as a function syntax, as you should not use AutoCAD aliases as function syntax. FYI, it doesn't hurt anything to use command aliases as command names. A command will always have priority over aliases defined in the acad.pgp and won't hurt any calls to the original, since they are always called with the full command name, not the alias. However, the problem resides in trying to define a command with the same name as an existing command. eg. Command: (defun c:L (/) (princ "HEY") (princ)) C:L Command: l HEY Command: Command: (defun c:Line (/) (princ "HEY") (princ)) C:LINE Command: line Specify first point: Specify next point or [undo]: 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.