aloy Posted October 27, 2017 Share Posted October 27, 2017 Hi everybody, I want to make a value (a real number) attached to a variable as a default and ask the user to accept it or enter a different value. How can I achieve it with alisp/vlisp?. Thanking in advance. Aloy Quote Link to comment Share on other sites More sharing options...
Tharwat Posted October 27, 2017 Share Posted October 27, 2017 Hi, An example: (or *val* (setq *val* 1.0)) (setq *val* (cond ((getdist (strcat "\nSpecify Offset distance " (strcat "< " (rtos *val* 2 4) " > :")))) (*val*) ) ) Quote Link to comment Share on other sites More sharing options...
aloy Posted October 27, 2017 Author Share Posted October 27, 2017 Thanks a lot Tharwat. Aloy Quote Link to comment Share on other sites More sharing options...
Tharwat Posted October 27, 2017 Share Posted October 27, 2017 You are welcome anytime Aloy. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 27, 2017 Share Posted October 27, 2017 You might find this tutorial useful. Quote Link to comment Share on other sites More sharing options...
BIGAL Posted October 28, 2017 Share Posted October 28, 2017 Using a dcl example with user input ; Input Dialog box with variable title ; multiple lines of dcl input supported ; add extra lines if required by copying code defun ; By Alan H 2015 (vl-load-com) ; 1 line dcl ; sample code (ah:getval1 "Line 1" 5 4 "-") (defun AH:getval1 (title width limit def1 / fo fname) ; you can hard code a directory if you like for dcl file ;(setq fo (open (setq fname (vl-filename-mktemp "" "" ".dcl")) "w")) (setq fo (open (setq fname "c:\\acadtemp\\getval.dcl") "w")) (write-line "ddgetval : dialog {" fo) (write-line " : row {" fo) (write-line ": edit_box {" fo) (write-line (strcat " key = " (chr 34) "key1" (chr 34) ";") fo) (write-line (strcat " label = " (chr 34) title (chr 34) ";" ) fo) ; these can be replaced with shorter value etc (write-line (strcat " edit_width = " (rtos width 2 0) ";" ) fo) (write-line (strcat " edit_limit = " (rtos limit 2 0) ";" ) fo) (write-line " is_enabled = true;" fo) (write-line " }" fo) (write-line " }" fo) (write-line "ok_only;}" fo) (close fo) (setq dcl_id (load_dialog fname)) ; pt is a list 2 numbs -1 -1 centre ('(20 20)) ;(not (new_dialog "test" dch "" *screenpoint*)) (if (not (new_dialog "ddgetval" dcl_id)) (exit)) (set_tile "key1" (setq val1 def1)) (action_tile "key1" "(setq val1 $value)") (mode_tile "key1" 3) (start_dialog) (done_dialog) (unload_dialog dcl_id) ; returns the value of val1 as a string (vl-file-delete fname) ) ; defungetval1 (if (not ah:getval1)(load "getval1")) (ah:getval1 "Enter a value" 5 4 "6") GETVALS3.lsp Quote Link to comment Share on other sites More sharing options...
saim Posted October 28, 2017 Share Posted October 28, 2017 I was looking for a way to do it, thank aloy for asking and thank all the answers! I got confused with the "cond" statement. The first condition opens with twoo parenthesis and I can't find an explanation to it. I surely is required, for I tried the code with simple parenthesis and it didn't work. But WHY is it required? Btw, it was nice to read about notation of global variables. I alway try to use the good habits, while programming, but I'm a self-learner, so there is noone to teach me which good habits are there. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted October 28, 2017 Share Posted October 28, 2017 ..... and I can't find an explanation to it. I surely is required, for I tried the code with simple parenthesis and it didn't work. But WHY is it required? You can read THIS to know why. Quote Link to comment Share on other sites More sharing options...
saim Posted October 28, 2017 Share Posted October 28, 2017 I'll try to type in english what I read from the example code and you tell me ifI'm wrong, ok? Then, I think my doubt will be clearer. (cond ;if any of the following conditions is true, here's what will happen ((= s "Y") 1) ;if s equals "Y", then it'll return 1 ((= s "y") 1) ;if s equals "y", then it'll return 1 ((= s "N") 0) ;if s equals "N", then it'll return 1 ((= s "n") 0) ;if s equals "n", then it'll return 1 (t nil); if none of the above is true I'll test if "true" is true (it'll be) and return "nil" ) You see, in the example provided, the second parenthesis is there to test a condition. As I read this, I could have a global variable being true or nil and write something like this: (cond (*givenvar* returnVal) ;if the variable is a valid value, return "returnVal" (t defaulReturnVal) ;otherwise, return "defaulReturnVal" ) Now, for your code, there is no set value if the condition is true and no test (even testing if "true" is true) at the default condition. It is more concise, seems smarter. I'd like to understand how it works. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 28, 2017 Share Posted October 28, 2017 Per the documentation: Return Values The value of the last expression in the sublist. If there is only one expression in the sublist (that is, if result is missing), the value of the test expression is returned. If no arguments are supplied, cond returns nil. Quote Link to comment Share on other sites More sharing options...
saim Posted October 28, 2017 Share Posted October 28, 2017 Oops. It IS there. Must be one of those phrases that I couldn't translate right away (english is not my first language, as you probably already guessed) and considered to be unimportant. Sorry. I do that, sometimes. Still, I don't understand why I have to use 2 parenthesis on each condition. Perhaps the answer is "because it is a condition, not an argument, and conditions must be in parenthesis even if it is the only thing in each case". But if so, the last condition... the default condition... why is it parenthesis-free, then? Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 28, 2017 Share Posted October 28, 2017 Oops. It IS there. Must be one of those phrases that I couldn't translate right away (english is not my first language, as you probably already guessed) and considered to be unimportant. Sorry. I do that, sometimes. No worries; no need to apologise. Still, I don't understand why I have to use 2 parenthesis on each condition. You don't necessarily - it depends on whether the condition is an atom or an expression, as the test argument may be any AutoLISP data-type. For example, consider the following contrived example: (defun c:test ( / a b c ) (cond ( (progn (initget "Red Blue Green") (setq a (getkword "\nWhat's your favourite colour? [Red/Blue/Green] <more>: ")) ) ) ( (progn (initget "Yellow White Black") (setq b (getkword "\nWhat's your favourite colour? [Yellow/White/Black] <more>: ")) ) ) ( (progn (initget "Orange Purple Brown") (setq c (getkword "\nWhat's your favourite colour? [Orange/Purple/Brown] <I don't know!>: ")) ) ) ) (princ (strcat "\nYour favourite colour is " (cond ( a ) ( b ) ( c ) ("unknown") ) ) ) (princ) ) Quote Link to comment Share on other sites More sharing options...
saim Posted October 28, 2017 Share Posted October 28, 2017 Atom or expression! Now it makes sense! Thanks again, Lee! You're great! Quote Link to comment Share on other sites More sharing options...
Grrr Posted October 28, 2017 Share Posted October 28, 2017 Nice example Lee, I find the simpliest way to test conditionals from the user - by prompting for getpoint: (defun C:test ( / a ) (cond ( (setq a (getpoint "\nFirst try <enter to skip>: ")) (alert "First point specified.") ) ( (setq a (getpoint "\nSecond try <enter to skip>: ")) (alert "Second point specified.") ) ( (setq a (getpoint "\nThird try <enter to skip>: ")) (alert "Third point specified.") ) ( (alert "\nYou didn't tried enough - all I wanted was a point!") ) ); cond (princ) ); defun C:test Same goes for testing or / and functions - using the user's input for the getpoint : LMB to return a value or RMB to return nil. Quote Link to comment Share on other sites More sharing options...
BIGAL Posted October 28, 2017 Share Posted October 28, 2017 back to your code ((= s "Y") 1) ;if s equals "Y", then it'll return 1 ((= s "y") 1) ;if s equals "y", then it'll return 1 1 line only needed ((= (strcase s) "Y") 1) ;if s equals "Y", then it'll return 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.