TunzaGibbo Posted January 20, 2019 Posted January 20, 2019 Hi All I would like a little help to finish this Lisp routine Creates an "XLine" and copies it as many times as I press enter (defun c:qqq (/ Pt1 Pt2 obline) (savevartoold) ;Saves system variables (constructionlayer) ;Creates a Construction Layer (setq Pt1 (getpoint "\nPick First Point:") Pt2 (getpoint Pt1 "\nPick Second Point:")) (command "_xline" Pt1 Pt2 "") (setq obline (entlast)) (while 1 (command "copy" obline "" Pt2 pause)) (resetoldvar) (princ) ) It works all the way through except it won't reset the system variables if I push "Escape" Regards Tony Quote
marko_ribar Posted January 20, 2019 Posted January 20, 2019 (edited) Implement into routine error handler in which you should put call to reset variables sub... Edited January 20, 2019 by marko_ribar Quote
Alex_Miller Posted January 20, 2019 Posted January 20, 2019 When you are pressing Escape, the program is interrupted and function (resetoldvar) does not work. The cycle of copy should be other. For example: (setq flag T) (while flag (setq obline (entlast)) (command "_.copy" obline "" pt2 pause) (initget 1 "Y N") (setq reply (getkword "\nRepeat? [Y/N]: ")) (setq flag (= (strcase reply) "Y")) ) Quote
Lee Mac Posted January 20, 2019 Posted January 20, 2019 Change the while loop to: (while (vl-cmdf "_.copy" obline "" "_non" Pt2 pause)) vl-cmdf will evaluate the supplied input before passing it to the command; Esc will therefore exit the loop cleanly. 1 Quote
TunzaGibbo Posted January 21, 2019 Author Posted January 21, 2019 Thanks Lee Using your line I have this but it still won't reset the variables (defun c:sss (/ Pt1 Pt2 obline) (savevartoold) (constructionlayer) (setq Pt1 (getpoint "\nPick First Point:") Pt2 (getpoint Pt1 "\nPick Second Point:")) (command "_xline" Pt1 Pt2 "") (setq obline (entlast)) (while (vl-cmdf "_.copy" obline "" "_non" Pt2 pause)) (resetoldvar) (princ) ) Hope you can help Regards Quote
Tharwat Posted January 21, 2019 Posted January 21, 2019 (defun c:sss (/ *error* Pt1 Pt2 obline) (defun *error* (x) (resetoldvar) (princ "\n*Cancel*") ) (savevartoold) (constructionlayer) ;;; ....etc 1 Quote
TunzaGibbo Posted January 21, 2019 Author Posted January 21, 2019 Thanks Tharwat I tried this but with no luck at getting the variables to reset (defun c:sss (/ *error* Pt1 Pt2 obline) (defun *error* (x) (resetoldvar) (princ "\n*Cancel*") ) (savevartoold) (constructionlayer) (setq Pt1 (getpoint "\nPick First Point:") Pt2 (getpoint Pt1 "\nPick Second Point:")) (command "_xline" Pt1 Pt2 "") (setq obline (entlast)) (while (vl-cmdf "_.copy" obline "" "_non" Pt2 pause)) (resetoldvar) (princ) ) Quote
Tharwat Posted January 21, 2019 Posted January 21, 2019 3 minutes ago, TunzaGibbo said: I tried this but with no luck at getting the variables to reset Your problem is related to one of your previous threads that I already indicated to you and it seems is that you did not fix it and still repeating the same process and due to this silly and ridicules software of this forum, I can't find that thread. Quote
TunzaGibbo Posted January 21, 2019 Author Posted January 21, 2019 I can't find it either What can I do from here Quote
Tharwat Posted January 21, 2019 Posted January 21, 2019 1 minute ago, TunzaGibbo said: I can't find it either What can I do from here Post the codes of the two functions savevartoold and restoldvar to check them out for you. Quote
TunzaGibbo Posted January 21, 2019 Author Posted January 21, 2019 (defun savevartoold () (setq *error* DeltaError) (setq oldlayer (getvar "clayer") oldcolor (getvar "cecolor") oldltype (getvar "celtype") oldltscale (getvar "ltscale") olddimscale (getvar "dimscale") oldattdia (getvar "attdia") oldosmode (getvar "osmode") oldcmdecho (getvar "cmdecho")) (princ) ) (defun resetoldvar () (setvar "clayer" oldlayer) (setvar "cecolor" oldcolor) (setvar "celtype" oldltype) (setvar "ltscale" oldltscale) (setvar "dimscale" olddimscale) (setvar "attdia" oldattdia) (setvar "osmode" oldosmode) (setvar "cmdecho" oldcmdecho) (setq *error* nil) (princ) ) (defun DeltaError () (resetoldvar) (princ ("\nCommand has been canceled:")) (princ "\nOrignal values are reset.\n") (princ) ) Quote
marko_ribar Posted January 21, 2019 Posted January 21, 2019 (edited) Remove this line from (savevartoold) : (setq *error* DeltaError) Remove this line from (resetoldvar) : (setq *error* nil) Don't use (deltaerror) sub... HTH., M.R. Edited January 21, 2019 by marko_ribar Quote
TunzaGibbo Posted January 21, 2019 Author Posted January 21, 2019 Thank you Marko It works like a dream now Thnks to everyone else as well Quote
Tharwat Posted January 21, 2019 Posted January 21, 2019 Try it this way. (defun c:sss (/ *error* lst Pt1 Pt2 obline) (defun *error* (x) (if lst (resetoldvar lst)) (princ "\n*Cancel*") ) ;; ;; (defun savevartoold nil (mapcar 'getvar '("clayer" "cecolor" "celtype" "ltscale" "dimscale" "attdia" "osmode" "cmdecho")) ) ;; ;; (defun resetoldvar (vars) (mapcar 'setvar '("clayer" "cecolor" "celtype" "ltscale" "dimscale" "attdia" "osmode" "cmdecho") vars) ) ;; ;; (constructionlayer) (setq lst (savevartoold)) (if (and (setq Pt1 (getpoint "\nPick First Point:")) (setq Pt2 (getpoint Pt1 "\nPick Second Point:")) ) (progn (command "_xline" Pt1 Pt2 "") (setq obline (entlast)) ) ) (while (and obline (vl-cmdf "_.copy" obline "" "_non" Pt2 pause))) (if lst (resetoldvar lst)) (princ) ) Although you can localise the sub-functions as well and its up to you. Quote
Lee Mac Posted January 21, 2019 Posted January 21, 2019 (edited) 13 hours ago, TunzaGibbo said: Thanks Lee Using your line I have this but it still won't reset the variables (defun c:sss (/ Pt1 Pt2 obline) (savevartoold) (constructionlayer) (setq Pt1 (getpoint "\nPick First Point:") Pt2 (getpoint Pt1 "\nPick Second Point:")) (command "_xline" Pt1 Pt2 "") (setq obline (entlast)) (while (vl-cmdf "_.copy" obline "" "_non" Pt2 pause)) (resetoldvar) (princ) ) If you are using vl-cmdf, a local error function (*error*) is not required, as vl-cmdf will account for the user pressing ESC during command evaluation, flag this as invalid input (by returning nil), and the remainder of the program will continue to be evaluated. With that said, I do not know why you are saving & resetting system variables that you are not changing in your program; presumably you are blindly copying & pasting the same functions for each of your programs. Edited January 21, 2019 by Lee Mac Quote
Tharwat Posted January 21, 2019 Posted January 21, 2019 2 hours ago, Lee Mac said: If you are using vl-cmdf, a local error function (*error*) is not required, as vl-cmdf will account for the user pressing ESC during command evaluation, flag this as invalid input (by returning nil), and the remainder of the program will continue to be evaluated. I guess the vl-cmdf will always return true once the arguments are valid and to while function as in the above case and even if the user tries to hit ESC, the command won't stop at all until the user forcing AutoCAD to exit with the use of Windows Task Manager as I tried this myself just now with AutpCAD 2017. So even though the error function would not useful in this case because it is not reachable and I don't know how did that work with the user anyway. Quote
Lee Mac Posted January 21, 2019 Posted January 21, 2019 1 hour ago, Tharwat said: I guess the vl-cmdf will always return true once the arguments are valid and to while function as in the above case and even if the user tries to hit ESC, the command won't stop at all until the user forcing AutoCAD to exit with the use of Windows Task Manager as I tried this myself just now with AutpCAD 2017. So even though the error function would not useful in this case because it is not reachable and I don't know how did that work with the user anyway. It would seem that Autodesk have broken this function with later versions of AutoCAD Quote
Tharwat Posted January 21, 2019 Posted January 21, 2019 4 minutes ago, Lee Mac said: It would seem that Autodesk have broken this function with later versions of AutoCAD You may encounter the same issue since you are using later version than mine as shown into your profile if you have time to test that out. Quote
TunzaGibbo Posted January 22, 2019 Author Posted January 22, 2019 10 hours ago, Lee Mac said: If you are using vl-cmdf, a local error function (*error*) is not required, as vl-cmdf will account for the user pressing ESC during command evaluation, flag this as invalid input (by returning nil), and the remainder of the program will continue to be evaluated. With that said, I do not know why you are saving & resetting system variables that you are not changing in your program; presumably you are blindly copying & pasting the same functions for each of your programs. You make a good point about saving and resetting variables. In some routines I change the layers and linetypes many times, so I thought it was easier to have those 2 functions running rather than having separate reset functions each time I change a variable. I guess that's just the way I think. What would be a better way? Quote
BIGAL Posted January 22, 2019 Posted January 22, 2019 Maybe an old fashioned way (while (= (setq test (getstring "Press any key to exit <Cr> to continue")) nil) 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.