Default choice "enter" returns nil

Default choice "enter" returns nil

Anonymous
Not applicable
1,883 Views
11 Replies
Message 1 of 12

Default choice "enter" returns nil

Anonymous
Not applicable

Hi everyone,

I know this question has already been asked, but I'm totally unable to understand how to adapt answers to my problem :s

In my code, I'm using a getint to scale objects that will be drawn at the end.

I'd like to get the UserI1 variable as default choice (an integer), to simplify the use of my lisp routine if I have to use it again in the same drawing later.

For now, it works, the default choice is UserI1 as expected.

 

When I press Enter, it returns nil and then exit the routine. I've been searching on the web, I've read some tutorials (Lee Mac for example), but I don't understand what to do to allow pressing Enter, or right-clic (options are OK with that).

 

If someone has the solution for a dummie-lisp-developper, I'll be glad to learn from him/her 🙂

 

Thanks for reading !

 

(setvar "UserI1"
		(setq ech
			(getint 
				(strcat "\nQuelle sera l'échelle d'impression ? 100 pour 1:100 < "(rtos (getvar "UserI1") 2 0)" >: "))
		)
	)

 

0 Likes
Accepted solutions (1)
1,884 Views
11 Replies
Replies (11)
Message 2 of 12

CodeDing
Advisor
Advisor
Accepted solution

@Anonymous ,

 

I am not a big fan of the 'User..' variables. If your end user has more than 1 program that utilizes them, then they can become unreliable. Eventually you will need to look into Dictionaries & XRecords. This is where that type of information should potentially be stored. But to answer your question:

 

(setvar "UserI1"
  (setq ech
    (cond
      ((getint (strcat "\nQuelle sera l'échelle d'impression ? 100 pour 1:100 < " (rtos (getvar "UserI1") 2 0) " >: ")))
      ((getvar "UserI1"))
    )
  )
)

 

Best,

~DD

0 Likes
Message 3 of 12

Anonymous
Not applicable

Thanks a lot ! ❤️

May I ask you if it's possible to just get ech as default choice instead of using "UserI1" or the  dictionnary ?

I'll take a look there anyway, I'd like to do things right as soon as possible 😃

Thank you again for the answer and the tip !

0 Likes
Message 4 of 12

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

...

When I press Enter, it returns nil and then exit the routine...


 

There is no magic function to assign some 'default' value from (getint). You simply need to design your code the way that it ASKs whether (getint) returned nil or some True value.

 

Here is a sample code using the most simple functions:

(setq temp (getint (strcat "\nQuelle sera l'échelle d'impression? 100 pour 1:100 <" (itoa ech) ">: ")))
(if (= temp nil)  			
  (setq ech temp)) 

Yes, if we use the 'if' function then we need to use a 'temp' variable to temporarily store a value returned by (getint).

 

 

OR... we could use the trick with COND function, as @CodeDing did. Though it might be more difficult to understand because the logic is not so straight-forward.

 

(setq ech (cond ((getint (strcat "\nQuelle sera l'échelle d'impression? 100 pour 1:100 <" (itoa ech) ">: ")))  ; if (getint) returns some value (/= nil) it leaves the cond function right away at this line. 
		(ech)))												; Otherwise it evaluates following line - this one. 

 

Also, since we are converting an integer into a string, we can use (itoa) instead of (rtos). Thought (rtos) will work with both types.

 

 

 

 

0 Likes
Message 5 of 12

stevor
Collaborator
Collaborator

(DEFUN Get_I (D S / A ) ; default is 0, spaces not included
(setq D (if (and d (= 'INT (type D))) D 0)
a (getint (strcat " " s " < " (itoa d) " > " )) )
(if A A D ) ) ; d

 

(setq YourVar  (if   YourVar   YourVar  9) 

          YourVar    (Get_I  YourVar  "  Enter Your Integer") )

 

About 30 years old.

 

 

 

 

 

 

S
0 Likes
Message 6 of 12

Kent1Cooper
Consultant
Consultant

I agree that you need to be careful about the USER System Variables if you might have more than one routine that uses the same one.  Maybe you use a further-along one like UserI5.  But if you can control that, the fact that the numerical ones [not the String ones, for some reason] are stored in the drawing is an advantage.  However, since the UserI... and UserR... System Variables have a value of zero [not nil]  until you set them to something else, on first use in the accepted Solution, you will be offered zero  as a default value, and if you hit Enter, you will surely have a problem when that is used as a scale factor.

 

Here's a way around that, and it also shows a way to offer an initial default on first use when you have not yet set a value, if you have a common value that you are likely to use often.  In this case, it offers 100 only if there is no [non-zero] prior value, but you can change that to whatever you prefer.  If you type in any integer value, it will use it; if you have a prior value and you hit Enter, it will use that prior value; if you don't have a prior value and you hit Enter, it will use 100.

 

 

(setvar 'UserI1
  (cond
    ( (getint ; User input [returns nil on Enter]
        (strcat
          "\nQuelle sera l'échelle d'impression? 100 pour 1:100 <"
          (if (/= (getvar 'UserI1) 0) (itoa (getvar 'UserI1)) "100")
            ;; offer prior value as default if present; if not, offer 100 as initial default
          ">: "
        ); strcat
      ); getint
    ); end User-input condition
    ((/= (getvar 'UserI1) 0) (getvar 'UserI1)); on Enter if prior non-zero value exists
    (100); on Enter if no prior value exists [i.e. UserI1 = 0]
  ); cond
); setvar

 

 

Others have already done so, but note the use of (itoa) instead of (rtos) with no decimal places, when the value is an integer.

 

Also [a small thing], you can use the double-quotes-at-both-ends approach with System Variable names in (getvar ) and (setvar), but the apostrophe-at-the-front-only approach also works in both functions.

Kent Cooper, AIA
0 Likes
Message 7 of 12

Anonymous
Not applicable

Thanks a lot again for all those tips...

 

I don't know how much years I'll need to reach your knowledge, things seem so easy for you x)

I was using the userI1 var because it's the first solution I understood when I was looking on the web to grab some answers about "how to get a variable as default choice".

Now, I know this is not the best way and I have some other choices.

 

Thanks for your help, and thanks for the LISP lesson ❤️

Have a nice day/night everyone, I wish you the best 😃

0 Likes
Message 8 of 12

Sea-Haven
Mentor
Mentor

This is something I did to replace getint, I have attached the library routine and made sample code to suit add or subtract as required. Other would mean you could do extra scales. Change the (setq but 3) for 1:100

 

 

(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (= but nil)(setq but 1))
(setq ans (atoi(ah:butts but "V" '("Choose scale 1:X " "25" "50" "100" "200" "250" "500" "Other")))) ; ans holds the button picked as an integer value

 

 

0 Likes
Message 9 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....

I was using the userI1 var .... Now, I know this is not the best way and I have some other choices.

 

....


The same approach can be used with a variable name:

(setq ech
  (cond
    ( (getint ; User input [returns nil on Enter]
        (strcat
          "\nQuelle sera l'échelle d'impression? 100 pour 1:100 <"
          (if ech (itoa ech) "100")
            ;; offer prior value as default if present; if not, offer 100 as initial default
          ">: "
        ); strcat
      ); getint
    ); end User-input condition
    (ech); on Enter if prior non-zero value exists
    (100); on Enter if no prior value exists
  ); cond
); setvar

and in some ways is a little easier, because that variable would presumably never be set to zero [as UserI1 will be if not set to something else], so you don't need to evaluate whether it's zero, but simply whether it exists.

 

But yes, if you want it stored in the drawing, you need to do something more complicated like a dictionary.

Kent Cooper, AIA
0 Likes
Message 10 of 12

ronjonp
Advisor
Advisor

Another using a global variable ( although I prefer the COND examples ).

 

*Code removed .. 😳 As @ВeekeeCZ mentioned it was horribly flawed.

 

 

0 Likes
Message 11 of 12

ВeekeeCZ
Consultant
Consultant

@ronjonp wrote:

Another using a global variable ( although I prefer the COND examples ).

(setq *ech* (if (not (getint (strcat "\nQuelle sera l'échelle d'impression ? 100 pour 1:100 < "
				   (itoa (if *ech* *ech* 100))
				   " >: "
			   )
		   )
	      )
	    100
	  )
)

 

This can't be right!

0 Likes
Message 12 of 12

ronjonp
Advisor
Advisor

Should have read this thread a bit closer .. does not get much simpler than THIS. Sorry for the inconvenience. 🍺

0 Likes