ktbjx Posted November 10, 2016 Posted November 10, 2016 (defun c:xLucky(/ hcol) (command "select" pause) (setq hcol (getint "\nEnter Number: ")) (command "chprop" "p" "" "c" hcol "")) How can i make the previous number i entered to be the value of "hcol" so i will not type the value over and over, unless i type different value and use it over again.. Quote
Grrr Posted November 10, 2016 Posted November 10, 2016 Like this? (defun c:xLucky( / ) (command "_.select" pause) (or *hcol* (setq *hcol* 1)) (or (setq *hcol* (getint (strcat "\nEnter Number: <" (itoa *hcol*) ">: "))) *hcol*) (command "_.chprop" "p" "" "c" *hcol* "") (princ) ) Note that *hcol* is now a global variable, and it should not be included in the arguments list. Quote
Lee Mac Posted November 10, 2016 Posted November 10, 2016 (or (setq *hcol* (getint (strcat "\nEnter Number: <" (itoa *hcol*) ">: "))) *hcol*) Careful - this will set *hcol* to nil should the user press enter at the prompt. Quote
Grrr Posted November 10, 2016 Posted November 10, 2016 Careful - this will set *hcol* to nil should the user press enter at the prompt. Correct, just tested it - it was harder than I thought, so here are 2 solutions I found: 1.Using the last example from your "Prompting with a Default Option" tutorial http://www.lee-mac.com/promptwithdefault.html (gotta love it) (defun c:test ( / ) (command "_.select" pause) (or (numberp *hcol*) (setq *hcol* 1)) (setq *hcol* (cond ( (getint (strcat "\nChoose a Number <" (itoa (setq *hcol* (cond ( *hcol* ) ( 1 )))) ">: " ))) ( *hcol* ))) (command "_.chprop" "p" "" "c" *hcol* "") (princ) ) 2. Using some pseudo _OR function that I had in mind the last week: (defun c:test ( / ) (command "_.select" pause) (or (numberp *hcol*) (setq *hcol* 1)) (setq *hcol* (_or (list (getint (strcat "\nEnter Number: <" (itoa *hcol*) ">: ")) *hcol*))) (command "_.chprop" "p" "" "c" *hcol* "") (princ) ) ; (_or (list (getpoint "\nFirst try: ") (getpoint "\nSecond try: ") (getpoint "\nThird try: ") )) (defun _or ( LstOfAnythings / r ); return the first non-nil value, instead of just T or nil, seems to work (vl-some (function (lambda (x) (if (not (vl-catch-all-error-p (vl-catch-all-apply 'eval (list 'x)))) (setq r x) ) ) ) LstOfAnythings ) r ) BTW after testing the code, the result performance seems to be similar for another request on recent thread here, on cadtutor. I am unsure how does cond really works, I tested with and/or/if functions without any success. I think that cond will evaluate as much test statements as you provide, and if everything is evaluated to nil (or the test statement is completely unrelated) it will run the last statement (T ). I "use/know" it since I'm on this forum, and everytime I feel weird/unsure while using it. gotta experiment more with it. Quote
Roy_043 Posted November 10, 2016 Posted November 10, 2016 @Grrr: Your last post's first code sample contains a superfluous line... Quote
Lee Mac Posted November 10, 2016 Posted November 10, 2016 2. Using some pseudo _OR function that I had in mind the last week:; (_or (list (getpoint "\nFirst try: ") (getpoint "\nSecond try: ") (getpoint "\nThird try: ") )) Be careful, as this expression will not evaluate as you intend: all of the getpoint expressions will be evaluated (regardless of previous responses) before the list of the returned values is constructed and passed to your "_or" function. This behaviour differs to that of the AutoLISP or function, which is a Special Form - this means the evaluation of each successive argument is dependent upon the evaluation of the function with previous arguments. To replicate this behaviour, the list of getpoint expression should be passed as an unevaluated (literal) list, with the expressions evaluated within the vl-some expression. Quote
BIGAL Posted November 11, 2016 Posted November 11, 2016 (edited) Another check for hcol is nil set hcol to a start value if hcol /= nil then prompt has current value press to accept current or enter new value. ; found this (if (= vert nil)(setq vert 50)) ; sets an initial value for message (prompt "\nEnter Vertical scale:<") (prin1 vert) (prompt ">:") (setq newvert (getint)) (if (= newvert nil) (princ); use current vert value (setq vert newvert) ; set vert to new value ) Edited November 14, 2016 by BIGAL Quote
Grrr Posted November 11, 2016 Posted November 11, 2016 Be careful, as this expression will not evaluate as you intend: all of the getpoint expressions will be evaluated (regardless of previous responses) before the list of the returned values is constructed and passed to your "_or" function. This behaviour differs to that of the AutoLISP or function, which is a Special Form - this means the evaluation of each successive argument is dependent upon the evaluation of the function with previous arguments. To replicate this behaviour, the list of getpoint expression should be passed as an unevaluated (literal) list, with the expressions evaluated within the vl-some expression. Yes, I know that it evaluates all the getpoint expressions, couldn't figure out how to fix this, until following your advice, passing list of unevaluated arguments: (defun _or ( LstOfAnythings / r ); return the first non-nil value, instead of just T or nil, seems to work (apply 'and (mapcar '(lambda (x) (= 'STR (type x))) LstOfAnythings)) (vl-some (function (lambda (x) (and (not (vl-catch-all-error-p (setq r (vl-catch-all-apply 'eval (list (read x)))))) (not (eq r nil)) ) ) ) LstOfAnythings ) r ) Test function: (if ; test function (setq p (_or (list "(getpoint \"\nFirst try: \")" "(getpoint \"\nSecond try: \")" "(getpoint \"\nThird try: \")" ) ) ) (entmakex (list (cons 0 "POINT") (cons 62 1) (cons 10 p))) ) Thanks! I feel that there might another way with apply. @Grrr: Your last post's first code sample contains a superfluous line... Sorry Roy, I can't see a thing.. any hints? Quote
Lee Mac Posted November 11, 2016 Posted November 11, 2016 Yes, I know that it evaluates all the getpoint expressions, couldn't figure out how to fix this, until following your advice, passing list of unevaluated arguments... Quoted literal expressions would also suffice, e.g.: (defun _or ( lst ) (vl-some '(lambda ( x / r ) (if (not (vl-catch-all-error-p (setq r (vl-catch-all-apply 'eval (list x))))) r)) lst) ) (if (setq p (_or '((getpoint "\n1st try: ") (getpoint "\n2nd try: ") (getpoint "\n3rd try: ")))) (entmakex (list (cons 0 "POINT") (cons 62 1) (cons 10 p))) ) Quote
Roy_043 Posted November 11, 2016 Posted November 11, 2016 Sorry Roy, I can't see a thing.. any hints? Assuming *hcol* is either nil or an integer, this line is not required: (or (numberp *hcol*) (setq *hcol* 1)) The next line already takes care of things: (itoa (setq *hcol* (cond ( *hcol* ) ( 1 )))) Quote
Grrr Posted November 11, 2016 Posted November 11, 2016 Quoted literal expressions would also suffice, e.g.:(defun _or ( lst ) (vl-some '(lambda ( x / r ) (if (not (vl-catch-all-error-p (setq r (vl-catch-all-apply 'eval (list x))))) r)) lst) ) (if (setq p (_or '((getpoint "\n1st try: ") (getpoint "\n2nd try: ") (getpoint "\n3rd try: ")))) (entmakex (list (cons 0 "POINT") (cons 62 1) (cons 10 p))) ) Nice! I just wrote something similar about and function: (defun _and ( lst / r rtn ) (not (vl-some '(lambda ( x ) (or (vl-catch-all-error-p (setq r (vl-catch-all-apply 'eval (list x)))) (not r))) lst)) (setq rtn (cons r rtn)) (reverse rtn) ) (defun _and ( lst ) (vl-every '(lambda ( x / r ) (if (not (vl-catch-all-error-p (setq r (vl-catch-all-apply 'eval (list x))))) r)) lst) ) (if (setq p (_and '((getpoint "\n1st try: ") (getpoint "\n2nd try: ") (getpoint "\n3rd try: ")))) p ) Although the performance and the returns are same as and function, I was expecting instead of T to get a return of list of evaluations, so it could be incorporated like this: (if (_and (setq lst '( (getpoint "\nSpecify circle's center: ") (getpoint "\nSpecify second point for radius: ") ) ) ) (command "_.CIRCLE" "_non" (car lst) "_non" (cadr lst)) ) Do you have any ideas Lee? Quote
Roy_043 Posted November 11, 2016 Posted November 11, 2016 According to the docs the (vl-every) function returns T or nil. Quote
Grrr Posted November 11, 2016 Posted November 11, 2016 According to the docs the (vl-every) function returns T or nil. I tried collecting the results from catch-apply function into separate list so it could be returned, but it affects the overall performance, so the closest I got is this: (defun MakeInputLst ( key func arglst mainALst / mainALst expr ) (if (and (= 'STR (type key)) (= 'SUBR (type func)) (listp arglst) (listp mainALst) (not (vl-catch-all-error-p (setq expr (vl-catch-all-apply 'func arglst)))) ) (setq mainALst (cons (cons key expr) mainALst)) ); if mainALst );| defun MakeInputLst |; (or vlax-get-acad-object (vl-load-com)) (princ) (and (not (setq InputList (list))) (apply 'and (mapcar 'cdr (setq InputList (MakeInputLst "FirstPt" getpoint (list "\nSpecify first point: ") InputList)))) (apply 'and (mapcar 'cdr (setq InputList (MakeInputLst "SecondPt" getpoint (list "\nSpecify second point: ") InputList)))) (apply 'and (mapcar 'cdr (setq InputList (MakeInputLst "ThirdPt" getpoint (list "\nSpecify third point: ") InputList)))) (apply 'and (mapcar 'cdr (setq InputList (MakeInputLst "ent" entsel (list "\nSelect an entity to get its layer: ") InputList)))) (mapcar '(lambda (x) (and (member (car x) '("FirstPt" "SecondPt" "ThirdPt")) (entmakex (list (cons 0 "POINT") (assoc 8 (entget (car (cdr (assoc "ent" InputList))))) (cons 10 (cdr x)))) ) ) InputList ) ); and I thought there was a simplier way. Maybe I'll create a new thread for this, since OP's task is solved and I'm redirecting the question too much. But as you can see, the idea was to skip any additional setq's and deal with one assoc list instead - where each dotted pair represents a setq (Why? - why not?). Quote
Lee Mac Posted November 11, 2016 Posted November 11, 2016 (edited) I just wrote something similar about and function: I was expecting instead of T to get a return of list of evaluations, so it could be incorporated like this: (if (_and (setq lst '( (getpoint "\nSpecify circle's center: ") (getpoint "\nSpecify second point for radius: ") ) ) ) (command "_.CIRCLE" "_non" (car lst) "_non" (cadr lst)) ) Do you have any ideas Lee? Firstly, note that the variable assignment should be evaluated after evaluation of the '_and' function in order to use the values returned by this function: (if (setq lst (_and '( (getpoint "\nSpecify circle's center: ") (getpoint "\nSpecify second point for radius: ") ) ) ) (command "_.circle" "_non" (car lst) "_non" (cadr lst)) ) As for the '_and' function, there are many possibilities: (defun _and ( lst / rtn ) (vl-every '(lambda ( x ) (if (setq x (catcheval x)) (setq rtn (cons x rtn)))) lst) (reverse rtn) ) (defun _and ( lst / val ) (if (and lst (setq val (catcheval (car lst)))) (cons val (_and (cdr lst)))) ) (defun _and ( lst / rtn val ) (while (and lst (setq val (catcheval (car lst)))) (setq rtn (cons val rtn) lst (cdr lst) ) ) (reverse rtn) ) (defun catcheval ( x / r ) (if (not (vl-catch-all-error-p (setq r (vl-catch-all-apply 'eval (list x))))) r) ) Revised: (defun _and ( lst / rtn ) (if (vl-every '(lambda ( x ) (if (setq x (catcheval x)) (setq rtn (cons x rtn)))) lst) (reverse rtn) ) ) (defun _and ( lst / foo ) (defun foo ( lst acc / val ) (if lst (if (setq val (catcheval (car lst))) (foo (cdr lst) (cons val acc)) ) (reverse acc) ) ) (foo lst nil) ) (defun _and ( lst / rtn val ) (while (and lst (setq val (catcheval (car lst)))) (setq rtn (cons val rtn) lst (cdr lst) ) ) (if (null lst) (reverse rtn)) ) (defun catcheval ( x / r ) (if (not (vl-catch-all-error-p (setq r (vl-catch-all-apply 'eval (list x))))) r) ) Edited November 12, 2016 by Lee Mac Fixed typo! Quote
Grrr Posted November 11, 2016 Posted November 11, 2016 Lee, this seems to work. I noticed that it collects all the non-nil evaluations, until theres a nil evaluation or the list ends: _$ (setq pts (_and '((getpoint "\n1st try: ") (getpoint "\n2nd try: ") (getpoint "\n3rd try: ")))) nil [color="green"]; hit enter (no points specified)[/color] _$ (setq pts (_and '((getpoint "\n1st try: ") (getpoint "\n2nd try: ") (getpoint "\n3rd try: ")))) ((140.658 116.678 0.0)) [color="green"]; first point specified, then pressed enter[/color] _$ (setq pts (_and '((getpoint "\n1st try: ") (getpoint "\n2nd try: ") (getpoint "\n3rd try: ")))) ((140.658 116.678 0.0) (156.932 167.24 0.0)) [color="green"]; first and second point specified, then pressed enter[/color] _$ (setq pts (_and '((getpoint "\n1st try: ") (getpoint "\n2nd try: ") (getpoint "\n3rd try: ")))) ((94.3117 156.279 0.0) (101.034 182.797 0.0) (188.065 174.665 0.0)) [color="green"]; all points specified[/color] _$ So by comparing the list's length its possible to determine if everything prompted is non-nil value, so the code proceeds: (defun C:test ( / lst ) (if (= (length (setq lst '( (getpoint "\nFirst point: ") (getpoint "\nSecond point: ")))) (length (setq lst (_and lst))) ) (command "_.LINE" "_non" (car lst) "_non" (cadr lst) "") ) (princ) ) And just a tiny fix I noticed: (defun catcheval ( [b][color="red"]x [/color][/b]/ r ) (if (not (vl-catch-all-error-p (setq r (vl-catch-all-apply 'eval (list [b][color="red"]x[/color][/b]))))) r) ) Thanks alot Lee! I'll try to include your name in the code everytime I play/experiment with this. Quote
rkent Posted November 12, 2016 Posted November 12, 2016 How about just pressing the up arrow key? Quote
Grrr Posted November 12, 2016 Posted November 12, 2016 Thanks for spotting the typo! No worries Lee, I highly appreciate your help! Quote
Roy_043 Posted November 12, 2016 Posted November 12, 2016 Lee, this seems to work. I noticed that it collects all the non-nil evaluations, until theres a nil evaluation or the list ends:... So by comparing the list's length its possible to determine if everything prompted is non-nil value, so the code proceeds: IMO it makes more sense to revise Lee's 'vl-every' suggestion to: (defun _and ( lst / rtn ) (if (vl-every '(lambda ( x ) (if (setq x (catcheval x)) (setq rtn (cons x rtn)))) lst) (reverse rtn) ) ) Quote
halam Posted November 12, 2016 Posted November 12, 2016 How about just pressing the up arrow key? It used to work but i'm wondering when and why was screwed in what new AutoCAD release? Lost track and I never was able to figure it out. Is there any setvar that controls the arrow key behavior?? 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.