Jump to content

Prompting for a default value


aloy

Recommended Posts

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

Link to comment
Share on other sites

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")

ScreenShot095.jpg

GETVALS3.lsp

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

..... 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...