Romero Posted April 30, 2019 Share Posted April 30, 2019 How are you. I'm pretty simple with Lisp programming, but develop a routine for my needs I'm having problems with restoring variables; since if the user presses the esc key the variables are not reset at the end of the routine. Can someone help me with error control? I would be very grateful. I attached the routine. ;guarda y establece variables origen________________________________________________________________ (defun s_var (/ gvtho gvomo gvlco) (setq gvtho (getvar "textsize"));Guarda variable text h (setq gvomo (getvar "orthomode"));Guarda variable orthomode (setq gvlco (getvar "clayer"));Guarda variable layer current (setvar 'orthomode 1) (setvar 'tspacefac 1) (setvar 'luprec 2) );endd ;restaura variables de origen (defun r_var () (setvar 'textsize gvtho) (setvar 'orthomode gvomo) (setvar 'clayer gvlco));endd ;Crea y/o establece el layer actual en "_OFFSET Y NIVELES" ____________________________________________ (defun n_lay ( ) (command "._layer" "_M" "_OFFSET Y NIVELES" "_C" "7" "" "") (setvar "clayer" "_OFFSET Y NIVELES"));end ;Preguntar al usuario si el estio tiene Altura_____________________________________________________________________________ (defun q_estion () (initget "Si No") (setq eath (getkword "\nESTILO DE TEXTO ACTUAL TIENE ALTURA? [Si/No] <Si>: "));Estilo actual tiene H (if (= eath "No") (ts) );end if );end ;establece altura de texto cuando el estilo no lo tiene______________________________________________ (defun ts (/ ph) (setq dnht (getdist "\nAltura de Texto: <0.18>: "); Define Nueva H texto dnht (if (null ph) 0.18 ph)) (setvar 'textsize dnht) );end ;if YTN=____________________________________________________________________________________ (defun ytn ( ) (while (setq wpy (getpoint "\nQUE PUNTO: ")) (setq p1y (cadr wpy)) ;coordinada en y (setq v1y (rtos p1y 2 2)) (setq m1y (strcat "TN= " v1y )) (setq t1y (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1y "j" "ml" "r" "90" "w" "0" m1y "") (command "_.move" "l" "" t1y (cons (- (car t1y) -0.17) (cdr t1y))) );while );end ;if YG=____________________________________________________________________________________ (defun yg ( ) (while (setq wpy (getpoint "\nQUE PUNTO: ")) (setq p1y (cadr wpy)) ;coordinada en y (setq v1y (rtos p1y 2 2)) (setq m1y (strcat "G= " v1y )) (setq t1y (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1y "j" "ml" "r" "90" "w" "0" m1y "") (command "_.move" "l" "" t1y (cons (- (car t1y) -0.17) (cdr t1y))) );while );end ;if YB=____________________________________________________________________________________ (defun yb ( ) (while (setq wpy (getpoint "\nQUE PUNTO: ")) (setq p1y (cadr wpy)) ;coordinada en y (setq v1y (rtos p1y 2 2)) (setq m1y (strcat "B= " v1y )) (setq t1y (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1y "j" "ml" "r" "90" "w" "0" m1y "") (command "_.move" "l" "" t1y (cons (- (car t1y) -0.17) (cdr t1y))) );while );end ;if YNC=____________________________________________________________________________________ (defun ync ( ) (while (setq wpy (getpoint "\nQUE PUNTO: ")) (setq p1y (cadr wpy)) ;coordinada en y (setq v1y (rtos p1y 2 2)) (setq m1y (strcat "NC= " v1y )) (setq t1y (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1y "j" "ml" "r" "90" "w" "0" m1y "") (command "_.move" "l" "" t1y (cons (- (car t1y) -0.17) (cdr t1y))) );while );end ;if YMC=____________________________________________________________________________________ (defun ymc ( ) (while (setq wpy (getpoint "\nQUE PUNTO: ")) (setq p1y (cadr wpy)) ;coordinada en y (setq v1y (rtos p1y 2 2)) (setq m1y (strcat "MC= " v1y )) (setq t1y (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1y "j" "ml" "r" "90" "w" "0" m1y "") (command "_.move" "l" "" t1y (cons (- (car t1y) -0.17) (cdr t1y))) );while );end ;if YOTRO_________________________________________________________________________________ (defun yotro (/ dpry ) (setq dpry (getstring t "\nQue prefijo: <R>: ") dpry (if (null dpry)R dpry)) (while (setq wpy (getpoint "\nQUE PUNTO: ")) (setq p1y (cadr wpy)) ;coordinada en y (setq v1y (rtos p1y 2 2)) (setq m1y (strcat dpry "= " v1y )) (setq t1y (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1y "j" "ml" "r" "90" "w" "0" m1y "") (command "_.move" "l" "" t1y (cons (- (car t1y) -0.17) (cdr t1y))) );while );end ;lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll ;if TN=____________________________________________________________________________________ (defun xtn ( ) (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq p2x (cadr wpx)) ;coordinada en y (setq v1x (rtos p1x 2 2)) (setq v2x (rtos p2x 2 2)) (setq m1x (strcat "OFF= " v1x )) (setq m2x (strcat "TN= " v2x )) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x m2x "") );while );end ;if G=____________________________________________________________________________________ (defun xg ( ) (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq p2x (cadr wpx)) ;coordinada en y (setq v1x (rtos p1x 2 2)) (setq v2x (rtos p2x 2 2)) (setq m1x (strcat "OFF= " v1x )) (setq m2x (strcat "G= " v2x )) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x m2x "") );while );end ;if B=____________________________________________________________________________________ (defun xb ( ) (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq p2x (cadr wpx)) ;coordinada en y (setq v1x (rtos p1x 2 2)) (setq v2x (rtos p2x 2 2)) (setq m1x (strcat "OFF= " v1x )) (setq m2x (strcat "B= " v2x )) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x m2x "") );while );end ;if NC=____________________________________________________________________________________ (defun xnc ( ) (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq p2x (cadr wpx)) ;coordinada en y (setq v1x (rtos p1x 2 2)) (setq v2x (rtos p2x 2 2)) (setq m1x (strcat "OFF= " v1x )) (setq m2x (strcat "NC= " v2x )) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x m2x "") );while );end ;if NU=____________________________________________________________________________________ (defun xnu ( ) (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq v1x (rtos p1x 2 2)) (setq m1x (strcat "OFF= " v1x )) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x "") (command "_.move" "l" "" t1x (cons (- (car t1x) 0.14) (cdr t1x))) );while );end ;if MC=____________________________________________________________________________________ (defun xmc ( ) (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq p2x (cadr wpx)) ;coordinada en y (setq v1x (rtos p1x 2 2)) (setq v2x (rtos p2x 2 2)) (setq m1x (strcat "OFF= " v1x )) (setq m2x (strcat "MC= " v2x )) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x m2x "") );while );end ;if XOTRO=____________________________________________________________________________________ (defun xotro (/ dprx ) (setq dprx (getstring t "\nQue prefijo: <R>: ") dprx (if (null dprx) R= dprx)) (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq p2x (cadr wpx)) ;coordinada en y (setq v1x (rtos p1x 2 2)) (setq v2x (rtos p2x 2 2)) (setq m1x (strcat "OFF= " v1x )) (setq m2x (strcat dprx "= " v2x )) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x m2x "") );while );end ;OK_Y___________________________________ (defun C:vy (/ opry pwy p1y v1y m1y t1y) (s_var) (n_lay) (initget "TN R G B NC MC OTRO") (setq opry (getkword "\nPREFIJO EN Y [TN/R/G/B/NC/MC/OTRO] <R>: ")) (q_estion) (if (= opry "TN") (ytn) );end if (if (= opry "G") (yg) );end if (if (= opry "B") (yb) );end if (if (= opry "NC") (ync) );end if (if (= opry "MC") (ymc) );end if (if (= opry "OTRO") (yotro) );end if (while (setq wpy (getpoint "\nQUE PUNTO: ")) (setq p1y (cadr wpy)) ;coordinada en y (setq v1y (rtos p1y 2 2)) (setq m1y (strcat "R= " v1y )) (setq t1y (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1y "j" "ml" "r" "90" "w" "0" m1y "") (command "_.move" "l" "" t1y (cons (- (car t1y) -0.17) (cdr t1y))) );while (r_var) (princ) );end ;OK_XY___________________________________________________ (defun C:vxy (/ oprx wpx p1x p2x v1x v2x m1x m2x t1x t2x) (s_var) (n_lay) (initget "TN NU R G B NC MC OTRO") (setq oprx (getkword "\nOFFEST + PREFIJO EN Y [TN/NU/R/G/B/NC/MC/OTRO] <R=>: ")) (q_estion) (if (= oprx "TN") (xtn) );end if (if (= oprx "R") (xr) );end if (if (= oprx "G") (xg) );end if (if (= oprx "B") (xb) );end if (if (= oprx "NC") (xnc) );end if (if (= oprx "MC") (xmc) );end if (if (= oprx "NU") (xnu) );end if (if (= oprx "OTRO") (xotro) );end if (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq p2x (cadr wpx)) ;coordinada en y (setq v1x (rtos p1x 2 2)) (setq v2x (rtos p2x 2 2)) (setq m1x (strcat "OFF= " v1x )) (setq m2x (strcat "R= " v2x )) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x m2x "") );while (r_var) (princ) );end Quote Link to comment Share on other sites More sharing options...
ronjonp Posted April 30, 2019 Share Posted April 30, 2019 (edited) Give this a try .. you should use the built in *error* handler: ;; OK_Y___________________________________ (defun c:vy (/ *error* opry pwy p1y v1y m1y t1y vars vals) ;; Error handler (defun *error* (msg) (mapcar 'setvar vars vals) (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")) (princ (strcat "\nError: " msg)) ) (princ) ) ;; Save values and variables (setq vals (mapcar 'getvar (setq vars '(clayer orthomode luprec textsize tspacefac orthomode)))) (s_var) (n_lay) (initget "TN R G B NC MC OTRO") (setq opry (getkword "\nPREFIJO EN Y [TN/R/G/B/NC/MC/OTRO] <R>: ")) (q_estion) (if (= opry "TN") (ytn) ) ;end if (if (= opry "G") (yg) ) ;end if (if (= opry "B") (yb) ) ;end if (if (= opry "NC") (ync) ) ;end if (if (= opry "MC") (ymc) ) ;end if (if (= opry "OTRO") (yotro) ) ;end if (while (setq wpy (getpoint "\nQUE PUNTO: ")) (setq p1y (cadr wpy)) ;coordinada en y (setq v1y (rtos p1y 2 2)) (setq m1y (strcat "R= " v1y)) (setq t1y (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1y "j" "ml" "r" "90" "w" "0" m1y "") (command "_.move" "l" "" t1y (cons (- (car t1y) -0.17) (cdr t1y))) ) ;while ;; Reset variables (mapcar 'setvar vars vals) (princ) ) ;end ;; OK_XY___________________________________________________ (defun c:vxy (/ *error* oprx wpx p1x p2x v1x v2x m1x m2x t1x t2x vars vals) ;; Error handler (defun *error* (msg) (mapcar 'setvar vars vals) (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")) (princ (strcat "\nError: " msg)) ) (princ) ) ;; Save values and variables (setq vals (mapcar 'getvar (setq vars '(clayer orthomode luprec textsize tspacefac orthomode)))) (s_var) (n_lay) (initget "TN NU R G B NC MC OTRO") (setq oprx (getkword "\nOFFEST + PREFIJO EN Y [TN/NU/R/G/B/NC/MC/OTRO] <R=>: ")) (q_estion) (if (= oprx "TN") (xtn) ) ;end if (if (= oprx "R") (xr) ) ;end if (if (= oprx "G") (xg) ) ;end if (if (= oprx "B") (xb) ) ;end if (if (= oprx "NC") (xnc) ) ;end if (if (= oprx "MC") (xmc) ) ;end if (if (= oprx "NU") (xnu) ) ;end if (if (= oprx "OTRO") (xotro) ) ;end if (while (setq wpx (getpoint "\nQUE PUNTO: ")) (setq p1x (car wpx)) ;coordinada en x (setq p2x (cadr wpx)) ;coordinada en y (setq v1x (rtos p1x 2 2)) (setq v2x (rtos p2x 2 2)) (setq m1x (strcat "OFF= " v1x)) (setq m2x (strcat "R= " v2x)) (setq t1x (getpoint "\nDONDE TEXTO: ")) (command "mtext" t1x "j" "ml" "r" "90" "w" "0" m1x m2x "") ) ;while ;; Reset variables (mapcar 'setvar vars vals) (princ) ) Also .. not getting too far in the weeds but ... ;; This (if (= opry "TN") (ytn) ) ;end if (if (= opry "G") (yg) ) ;end if (if (= opry "B") (yb) ) ;end if (if (= opry "NC") (ync) ) ;end if (if (= opry "MC") (ymc) ) ;end if (if (= opry "OTRO") (yotro) ) ;end if ;; Could be this (cond ((= opry "TN") (ytn)) ((= opry "G") (yg)) ((= opry "B") (yb)) ((= opry "NC") (ync)) ((= opry "MC") (ymc)) ((= opry "OTRO") (yotro)) ) Edited April 30, 2019 by ronjonp Added suggestion 1 Quote Link to comment Share on other sites More sharing options...
Romero Posted April 30, 2019 Author Share Posted April 30, 2019 Thank you very much @ronjonp . I really appreciate the hassle in helping me, now the code works perfectly, I have also edited it to not use as many if and use the cond function and the code is cleaner. I need to learn a lot more, but there I go little by little with some routines for my personal use. Error control is something I must learn soon. Your help came to me wonderfully. Thank you. 1 Quote Link to comment Share on other sites More sharing options...
ronjonp Posted April 30, 2019 Share Posted April 30, 2019 (edited) 1 hour ago, Romero said: Thank you very much @ronjonp . I really appreciate the hassle in helping me, now the code works perfectly, I have also edited it to not use as many if and use the cond function and the code is cleaner. I need to learn a lot more, but there I go little by little with some routines for my personal use. Error control is something I must learn soon. Your help came to me wonderfully. Thank you. Glad to help One thing to note is when using the *error* handler be sure to localize it within your routines: (defun c:vy (/ *error* opry pwy p1y v1y m1y t1y vars vals) Edited April 30, 2019 by ronjonp 1 Quote Link to comment Share on other sites More sharing options...
Grrr Posted May 1, 2019 Share Posted May 1, 2019 2 cents, about a more general error-trapping-wrapping - ;; Reset variables (mapcar 'setvar vars vals) (princ) ) can become: ;; Reset variables (*error* nil) (princ) ) I saw that from Lee Mac, Hence you can do whatever you want within the *error* function definition, except for restoring variables also including EndUndoMark or erasing a temporarily created .dcl file. So that way can be avoided some repetitive code. 1 Quote Link to comment Share on other sites More sharing options...
Romero Posted May 1, 2019 Author Share Posted May 1, 2019 8 hours ago, ronjonp said: Encantado de ayudar Una cosa a tener en cuenta es que al usar el controlador * error *, asegúrese de localizarlo dentro de sus rutinas: If I observed that. Thank you very much for your appreciable help, I will try to handle more error control Quote Link to comment Share on other sites More sharing options...
BIGAL Posted May 1, 2019 Share Posted May 1, 2019 Grr if you changed some of the variables during a program you would still need to reset back to what they were before the program was run so I think the mapcar is still applicable. Suggest use cond instead of all the if's 1 Quote Link to comment Share on other sites More sharing options...
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.