Jump to content

initget/getkword with previous entry option


Recommended Posts

Posted

Hey All,

 

I'm trying to create a lisp routine that will prompt the user for a dimension precision that will allow the user to use the previous precision entry only if one exsits. Here's what I've got so far:

 

(defun C:dd (/ old_osm)
 (setq old_clr (getvar "clayer")
old_osm (getvar "osmode"))
 (if (eq prs "")
   (progn
     (initget 1 "1 2 4 8 16 32")
     (setq prs (getkword "\nDimension Precision: ")))
   (progn
     (setq old_prs prs)
     (initget "1 2 4 8 16 32")
     (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: ")))))
 (princ))

 

But its not working. Any ideas?

Posted

Try replacing

(if (eq prs "")

with

(if (= prs nil)

Posted

Wow, that's upsetting... lol, thanks a ton.

Posted

Actually I think (eq prs nil) would work, but the results from eq can be squirrely; I'm sure it has it's purpose, but I try to avoid it.

Posted

Gotcha, another quick question... I've changed it a little to include a default value when prs is equal to nil, and now I just realized another issue... it accepts "3" as an input for "32". Is it possible to stop this, I would like it to allow only "32". Here's what I got:

 

(defun C:dd (/ old_osm old_osm old_prs)
 (setq old_clr (getvar "clayer")
old_osm (getvar "osmode"))
 (if (= prs nil)
   (progn
     (setq old_prs "4")
     (initget "1 2 4 8 16 32")
     (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: "))))
   (progn
     (setq old_prs prs)
     (initget "1 2 4 8 16 32")
     (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: ")))))
 (princ))

Posted
Gotcha, another quick question... I've changed it a little to include a default value when prs is equal to nil, and now I just realized another issue... it accepts "3" as an input for "32". Is it possible to stop this, I would like it to allow only "32". Here's what I got:

 

(defun C:dd (/ old_osm old_osm old_prs)
 (setq old_clr (getvar "clayer")
old_osm (getvar "osmode"))
 (if (= prs nil)
   (progn
     (setq old_prs "4")
     (initget "1 2 4 8 16 32")
     (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: "))))
   (progn
     (setq old_prs prs)
     (initget "1 2 4 8 16 32")
     (setq prs (getkword (strcat "\nDimension Precision <" old_prs ">: ")))))
 (princ))

 

 

Try something like (while (not (member prs '("1" "2"....

Posted

I would approach it like this:

 

(defun c:test nil
 
 (or prs (setq prs "4"))

 (initget "1 2 4 8 16 32")
 (setq prs (cond ((getkword (strcat "\nDimension Precision <" prs "> : ")))
                 (prs)))

 )
 

 

Bear in mind that if the user enters '3' the getkword will still return 32.

Posted
Bear in mind that if the user enters '3' the getkword will still return 32.

Hence my above statement.

 

BTW, I agree with you. :)

  • 5 months later...
Posted

I know this is a old one, but I have a related question that I can't figure out...

 

Here's the code I have now:

 

(while
(not
  (and
    (= (strlen rvl) 1)
    (>= (ascii rvl) (ascii "A"))
    (<= (ascii rvl) (ascii "Z"))))
(setq rvl
       (strcase
  (cond
    ((getstring "\nEnter Revision <A>:"))
    (T "A")))))

 

What I want it to do is prompt the user for a 1 letter uppercase string and keep asking until this is fufilled and also allow the user to press enter to accept the default "A". But when I run this code and try to press the enter button to accept the default, it loops and asks me again. :x

 

Any help would be greatly appriciated.

Posted
(defun _RevNo (/ str)
 (while
   (not
     (< 64
        (ascii (setq str (strcase
                           (cond
                             (((lambda (/ s)
                                 (if (/= ""
                                         (setq s (substr
                                                   (getstring "\nSpecify revision <A>: ")
                                                   1
                                                   1
                                                 )
                                         )
                                     )
                                   s
                                 )
                               )
                              )
                             )
                             ("A")
                           )
                         )
               )
        )
        91
     )
   )
 )
 str
)

Posted

Would you mind explaining why mine didn't work and why your's does?

Posted

Actually, since you only want A-Z, this should work just fine:

 

(initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 ""))
(setq rev (cond ((getkword "\nSpecify revision <A>: "))
               ("A")
         )
)

Posted

Ah, I see it now... never thought to use lambda to create a list of acceptable values for a getkword... I still don't get why mine didn't work tho:?

Posted
Ah, I see it now... never thought to use lambda to create a list of acceptable values for a getkword... I still don't get why mine didn't work tho:?

You were trying to validate a nil statement as a string. You have set the rvl variable first. You were also going about your cond wrong since getstring will return "" if the user hits enter (see my code).

 

Instead of using a function to create the string, you could have just typed "A B C..."

Posted (edited)

Yeah, I just figured it'd be better coding to have a function...

 

Also, when I try this one, it doesn't accept any input but pressing enter:

 

(initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 ""))
(setq rev (cond ((getkword "\nSpecify revision <A>: "))
               ("A")
         )
)

 

But I was able to figure out my method...

 

(while
(not
  (and
    rvl
    (= (strlen rvl) 1)
    (>= (ascii "Z") (ascii rvl) (ascii "A"))))
(if (= "" (setq rvl (strcase (getstring "\nEnter Revision <A>: "))))
  (setq rvl "A")))

Edited by KRBeckman
Updated code to fix bug
Posted
Yeah, I just figured it'd be better coding to have a function...

 

Also, when I try this one, it doesn't accept any input but pressing enter:

 

(initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 ""))
(setq rev (cond ((getkword "\nSpecify revision <A>: "))
               ("A")
         )
)

 

Yes it does. That's what the cond statement is for. Same as what you were trying to replicate with getstring.

Posted
Yes it does. That's what the cond statement is for. Same as what you were trying to replicate with getstring.

 

Maybe I'm doing something wrong, but when I run this and try to input "A" for the prompt I get "Invalid option keyword." and it prompts me again, and continues to do it unitl i just press enter.

Posted
Maybe I'm doing something wrong, but when I run this and try to input "A" for the prompt I get "Invalid option keyword." and it prompts me again, and continues to do it unitl i just press enter.

Do you have this in code or are you just pasting it to the command line? If the latter, then you have to wrap it in a progn function or something of the like.

 

(defun foo (/ rev)
 (initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 ""))
 (setq rev (cond ((getkword "\nSpecify revision <A>: "))
                 ("A")
           )
 )
)

 

 

I didn't do it this way because I wanted to make sure you understood it wasn't required.

Posted
Do you have this in code or are you just pasting it to the command line? If the latter, then you have to wrap it in a progn function or something of the like.

 

(defun foo (/ rev)
 (initget 0 ((lambda (i s) (while (< i 91) (setq s (strcat s (chr (setq i (1+ i))) " ")))) 64 ""))
 (setq rev (cond ((getkword "\nSpecify revision <A>: "))
                 ("A")
           )
 )
)

 

 

I didn't do it this way because I wanted to make sure you understood it wasn't required.

 

There we go.... never knew that mattered. Thanks a ton for your help.

Posted
There we go.... never knew that mattered. Thanks a ton for your help.

You're welcome. :)

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