default option in lisp

Discussion in 'AutoCAD' started by jlspartz, Feb 23, 2005.

  1. jlspartz

    jlspartz Guest

    Here I have two options, which will happen about 4 times in my command, and right now you have to choose one or the other, but what if I want to have one set as a default, so that enter is considered picking that option?

    (initget 1 "Y N")
    (setq repos (strcase (getkword "\nReposition Grid? Y or N: ")))
    (cond
    ((= repos "N")(command))
    ((= repos "Y")
    (prompt "\nPick new ceiling grid origin: ")
    (command "hatchsetorigin" "last" "")
    (while (= (logand (getvar "cmdactive") 1) 1)
    (command pause)
    )
    )
    (t(ceiling-end nil))
    )

    Let's say I want "N" (no) as the default. How would I go about doing that?
     
    jlspartz, Feb 23, 2005
    #1
  2. jlspartz

    Jim Claypool Guest

    Remove the 1 from the initget so that enter is allowed.
    (initget "Y N")

    Remove (strcase from the (getkword so that it won't error on enter
    (setq repos (getkword "\nReposition Grid? Y or N <N>: "))

    and before the (cond, add
    (if (not repos) (setq repos "N"))

    Just an additional note:
    The (t (ceiling-end nil)) will never happen because one of the other two
    conditions will always be met.
     
    Jim Claypool, Feb 23, 2005
    #2
  3. jlspartz

    Tom Smith Guest

    Here I have two options, which will happen about 4 times in my command,
    and right now you have to choose one or the other, but what if I want to
    have one set as a default, so that enter is considered picking that option?

    I'd write a function to do that and then call it four times. This might be a
    good candidate for a general purpose "library" function.

    (defun getkey (prmpt keystr default)
    (initget 0 keystr)
    (cond
    ((getkword prmpt))
    (default)))

    (defun c:test1 ()
    (getkey "\nReposition grid? Y/<N>: " "Yes No" "No"))

    (defun c:test2 ()
    (getkey "\nWhich? Rock/Paper/<Scissors>: " "Rock Paper Scissors"
    "Scissors"))

    Command: TEST1
    Reposition grid? Y/<N>: (hit return)
    "No"

    Command:
    TEST2
    Which? Rock/Paper/<Scissors>: p (not case sensitive)
    "Paper"

    Command:
    TEST2
    Which? Rock/Paper/<Scissors>: (hit return)
    "Scissors"
     
    Tom Smith, Feb 23, 2005
    #3
  4. jlspartz

    jlspartz Guest

    Thanks everyone! I haven't put it in yet, but by looking at it, I know it will work. But, one question, since I'm taking out strcase so I don't get an error on a return, wouldn't that mean that I will have to make also lowercase conditions, ex. "Y" and "y"?
     
    jlspartz, Feb 23, 2005
    #4
  5. jlspartz

    jlspartz Guest

    Nevermind, I just tried a lowercase letter and it worked. Great! :-D
     
    jlspartz, Feb 23, 2005
    #5
  6. jlspartz

    Tom Smith Guest

    As my examples showed, user input to a getkword prompt is not case sensitive.

    Getkword returns whatever keystring is attached to the getkword option. If the string includes a "No" choice, then the user can enter the abbreviation N or n,or the whole word, in any case. What's returned is the exact string that was included in the getkword call, in this case "No" just as given.

    If you limit your kword choices to "Y N" (upper case) then that is what will be returned. The user's input is never case sensitive.

    It's good practice to conform to Acad's usual typographic conventions on prompts, such as enclosing the default in < >, for user friendliness.
     
    Tom Smith, Feb 23, 2005
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.