getstring prompt

getstring prompt

dvertz
Collaborator Collaborator
511 Views
15 Replies
Message 1 of 16

getstring prompt

dvertz
Collaborator
Collaborator

Is there a way to use getstring and have a default that will also accept a null string?

For example, if issued the normal way as:

(setq pre (getstring "\nPrefix <C->: "))

Then the normal way is check if the variable pre is null and if it is then set the variable pre to "C-1"

But what I am wanting to do is issue the prompt as:

(setq pre (getstring "\nPrefix: C- "))

Then if the C- is removed the variable pre would be a null string, but if not removed then the variable is filled with "C-"

 

Is there a way to do that?

 

Thanks

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Accepted solutions (1)
512 Views
15 Replies
Replies (15)
Message 2 of 16

ВeekeeCZ
Consultant
Consultant

getkword with arbitrary input could help. 

 

(progn (initget 128) (getkword "\nPrefix <C->: "))

0 Likes
Message 3 of 16

dvertz
Collaborator
Collaborator

Good Thought, but not quite there. When I enter that and just press enter, NIL is returned. I could process NIL and set the variable to "C-" but the result is the same as with getstring. So I thought maybe if I included "C-" in the initget like (initget 128 "C-") that it might return "C-", but it does not. I am thinking that what I want may not be able to be done. As the only thing I can think of would be to have the getstring issue the prompt and then a princ to add the "C-" as an answer. That way the "C-" could be deleted and a NULL string could be returned, but if the "C-" is left as the answer to getstring then the "c-" would be returned. However, a princ would not place the "C-" as answer to getstring until getstring IS answered.

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Message 4 of 16

ВeekeeCZ
Consultant
Consultant

Lost, sorry.

 

One more tip, though,

(lisped "C-")

0 Likes
Message 5 of 16

DGCSCAD
Collaborator
Collaborator

If I'm following you correctly, maybe something like this:

 

(initget "Yes No")

(setq pre (getstring "\nDo you want a Prefix of C- ? [Yes/No] <Y>"))

(if (or (= pre "") (= pre "Y"))

   (setq pre "C-")

   (setq pre nil)

)

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 6 of 16

dvertz
Collaborator
Collaborator
Accepted solution

Perfect, you got me there.

I used:

(initget "Yes No")
(setq pre (getstring "\nDo you want a Prefix of \"C-\"? [Yes]"))
(if (= pre "Y") (setq pre "C-"))

 

The results were:

Command: (setq pre (getstring "\nDo you want a Prefix of \"C-\"? [Yes]"))
Do you want a Prefix of "C-"? [Yes]V- (the "V-" was entered)
"V-"
Command: (setq pre (getstring "\nDo you want a Prefix of \"C-\"? [Yes]"))
Do you want a Prefix of "C-"? [Yes] (nothing was enter and just pressed enter)
""
Command: (setq pre (getstring "\nDo you want a Prefix of \"C-\"? [Yes]"))
Do you want a Prefix of "C-"? [Yes]Y (clicked the Yes)
"Y"

 

This worked perfectly, I am able to process all three answers.

I am curious why only the "Y" was returned instead of the whole word "Yes".

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Message 7 of 16

-didier-
Advisor
Advisor

Bonjour @dvertz 

oops! When editing my post, some answers were posted. I’m not fast enough!

I am not sure to understand correctly your question.

I propose that if press “return” at the prompt without giving an answer, you concatenate the prefix and the number 1.

2025-05-20_17-52-50.png

Amicalement

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

0 Likes
Message 8 of 16

DGCSCAD
Collaborator
Collaborator

Apparently, the INITGET isn't required, and I've also found something I didn't know when looking into this:

 

Using GETSTRING where GETKWORD would normally be used, if you supply brackets the first capitalized letter within the brackets is used as a selectable character and will return that as a string if selected.

 

Command: (setq pre (getstring "\nDo you want a Prefix of \"C-\"? [ZeRo]"))
Do you want a Prefix of "C-"? [ZeRo]Z
"Z"

 

 

Command: (setq pre (getstring "\nDo you want a Prefix of \"C-\"? [zeRo]"))
Do you want a Prefix of "C-"? [zeRo]R
"R"

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 9 of 16

doni49
Mentor
Mentor

@dvertz wrote:

 

I am curious why only the "Y" was returned instead of the whole word "Yes".


It uses the CAPITALIZED letters from the initget function.  Try this version and it'll return YES -- but you'll have to actually type YES (you don't have to type it in caps though).

 

(initget "YES NO")
(setq pre (getstring "\nDo you want a Prefix of C- ? [Yes/No] <Y>"))

 

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

0 Likes
Message 10 of 16

Kent1Cooper
Consultant
Consultant

@dvertz wrote:

....

(setq pre (getstring "\nPrefix <C->: "))

Then the normal way is check if the variable pre is null ....

....

I am curious why only the "Y" was returned instead of the whole word "Yes".


Bear in mind that unlike other (get...) functions, (getstring) does not return nil if you press Enter, but rather an empty text string "".  [I'm never quite sure when people use the word "null" whether they mean "nil."]

....

You would need to do the (initget) again before each (get...) function -- once at the top will not cover more than one of them.

 

Kent Cooper, AIA
0 Likes
Message 11 of 16

dvertz
Collaborator
Collaborator

Well, after all these years, I learned something new here.

The getstring returns only the "Y"

But the getkword returns the entire work "Yes" as it appears in the initget function

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Message 12 of 16

Kent1Cooper
Consultant
Consultant

I wouldn't bother with the Yes/No choice.  Does this do what you want?

(setq pre (getstring "\nPrefix <C->: "))

(if (= pre "") (setq pre "C-"))

Kent Cooper, AIA
0 Likes
Message 13 of 16

dvertz
Collaborator
Collaborator

Thanks.

The null string is exactly what I wanted to have return when nothing is entered. I could have also used having it return NIL. With NIL I could assign the NULL string. The key was having the option of the default value of "C-". But yet also have an option of typing in a different value for example "V-". Just as DGCSCAD got me to.

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Message 14 of 16

dvertz
Collaborator
Collaborator

Kent,

As pointed out in post 1, it does not.

As pointed out in post 6 with the help of the forum, I got there.

Thanks though. I always appreciate all who help.

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Message 15 of 16

komondormrex
Mentor
Mentor

one using cond

(setq pre (cond ((= "" (setq pre (getstring "\nEnter prefix {C}-: "))) "C-") (t (strcat pre "-"))))
0 Likes
Message 16 of 16

ВeekeeCZ
Consultant
Consultant

Here's the getkword version

 

(defun c:test ()
  (initget 128)
  (setq o (cond ((getkword "\nPrefix <C->: ")) ("C-")))
  (setq o (vl-string-trim "\"" o))
  (princ (strcat "Prefix: " o))
  (princ)
  )

 

Simple <enter> to get "C-"

"" (double quotes) to get an empty string ""

"C :"  - quotes have to be typed to set anything with a space.

0 Likes