Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

is getstring input an integer or a distance?

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
mid-awe
684 Views, 10 Replies

is getstring input an integer or a distance?

Hi all,

 

I'm a little puzzled. I would like to eliminate a mouse click in my program. I use getstring to allow the user to either enter in an integer or a distance used for labeling in a drawing, but I could eliminate the need for the user to then click an option that completes the label if I only knew how to determine if the entry is an integer (such as 5 or 15) or a distance (such as 24" or 36").

 

I could swear I did this before but I don't remember how. Maybe something like "isdist" but again, I don't remember exactly how I did it.

 

Any help is greatly appreciated. Thank you in advance. 

10 REPLIES 10
Message 2 of 11
pbejse
in reply to: mid-awe


@mid-awe wrote:

Hi all,

 

I'm a little puzzled. I would like to eliminate a mouse click in my program. I use getstring to allow the user to either enter in an integer or a distance used for labeling in a drawing, but I could eliminate the need for the user to then click an option that completes the label if I only knew how to determine if the entry is an integer (such as 5 or 15) or a distance (such as 24" or 36").

 

I could swear I did this before but I don't remember how. Maybe something like "isdist" but again, I don't remember exactly how I did it.

 

Any help is greatly appreciated. Thank you in advance. 


Maybe.

 

(defun _correctVal ( / val )
  	(setq val (getstring "\nEnter Value: "))
  	(if (or (eq val "")
                (not (eq (type (read val)) 'INT))
                )	
		  (progn
		    (princ "Null Input Try again")
		  (_correctVal))
		(print (if
                  (wcmatch val "*\"")
                  	(list "Distance" val)
                        (list "Integer" val)))
          )
	 (princ)     
  )

 

Message 3 of 11
Kent1Cooper
in reply to: mid-awe


@mid-awe wrote:

.... I would like to eliminate a mouse click in my program. I use getstring to allow the user to either enter in an integer or a distance used for labeling in a drawing, but I could eliminate the need for the user to then click an option that completes the label if I only knew how to determine if the entry is an integer (such as 5 or 15) or a distance (such as 24" or 36").

....


You can turn the string into an integer, and the integer back into a string, and if they're the same string, the Text represents an integer.

 

(defun TextInt (txt) (= txt (itoa (atoi txt))))

 

Command: (setq x (getstring))
[type in:] 24"
"24\""

Command: (TextInt x)
nil

Command: (setq x (getstring))
[type in:] 5
"5"

Command: (TextInt x)
T

 

Or directly, without the variable:

 

Command: (TextInt "15")
T

Command: (TextInt "3'-4 1/2")
nil

Command: (TextInt "13.25")
nil

Command: (TextInt "13579")
T

Command: (TextInt "this is a test")
nil

Kent Cooper, AIA
Message 4 of 11
mid-awe
in reply to: Kent1Cooper

Thank you, that'll do nicely Smiley Very Happy

 

I believe originally I used an initget bit or something, but this is a great solution that I never considered and it is simple enough for my taste. Thanks again.

Message 5 of 11
Kent1Cooper
in reply to: mid-awe


@mid-awe wrote:

Thank you, that'll do nicely.... 


You're welcome.  And I thought of another way to do it:
 

(defun TextInt (txt) (not (wcmatch txt "*@*,*.*")))

 

It just checks whether there are no alphabetic or non-alphanumeric characters in the string, returning T only if all characters are numeric.

Kent Cooper, AIA
Message 6 of 11
pbejse
in reply to: mid-awe

For a while there i thought you want to prevent the user from supplying the wrong value. where Integers and unit inch is the only valid value.

 

(defun _correctVal ( / val )
  	(setq val (getstring "\nEnter Value: "))
  	(if (or (eq val "")
                (not (eq (type (read val)) 'INT))
                )	
		  (progn
		    (princ "Null Input Try again")
		  (_correctVal))
		(setq val (if
                  (wcmatch val "*\"")
                  	(list "Distance" val)
                        (list "Integer" val)))
          )
	      
  )

(defun c:test ()
  	(setq ValueToUSe (_correctVal))
  	(if (eq (car ValueToUSe) "Integer")
          	(princ "\nDo this for Integer")
          	(princ "\nDo this for Distance")
          )
  (princ)
  )
       

 

If you dont have any use for the inch input (") why not stick with getint

 

Message 7 of 11
scot-65
in reply to: mid-awe

One more suggestion:

ATOI of a string that contains other than numbers will return 0 (which is true).

However DISTOF works well especially in architectural mode.

Don't bother with ATOF.

 

?

 


Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.


Message 8 of 11
Kent1Cooper
in reply to: scot-65


@scot-65 wrote:

One more suggestion:

ATOI of a string that contains other than numbers will return 0 (which is true).

....


Not quite true, actually....  When applied to a string that starts with other than numbers, (atoi) always returns 0.  But a string that starts with any numerical character(s), even if it contains other than numbers later on, will return an integer of just the initial number(s), ignoring the rest.

 

Command: (atoi "abc123")
0

 

Command: (atoi "123abc")

123

Kent Cooper, AIA
Message 9 of 11
mid-awe
in reply to: pbejse

I'm using this for many application but the one that triggered my question was on an "insert" block with label for plant purchase sizes are either in inches for boxed or gallons. If 24", for example, is typed for the size then I can deduce that it is a boxed order or if an integer is entered then I know it is in gallons.

Message 10 of 11
pbejse
in reply to: mid-awe


@mid-awe wrote:

I'm using this for many application but the one that triggered my question was on an "insert" block with label for plant purchase sizes are either in inches for boxed or gallons. If 24", for example, is typed for the size then I can deduce that it is a boxed order or if an integer is entered then I know it is in gallons.


Comment or Question? 

Message 11 of 11
mid-awe
in reply to: pbejse

Just a comment to explain what I use this for in reply to your comment.
"If you dont have any use for the inch input (") why not stick with getint?"

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost