Visual LISP, AutoLISP and General Customization

Reply
Mentor
mid-awe
Posts: 1,155
Registered: ‎12-08-2004
Message 1 of 11 (377 Views)
Accepted Solution

is getstring input an integer or a distance?

377 Views, 10 Replies
02-21-2013 02:51 PM

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. 

*Expert Elite*
Posts: 2,369
Registered: ‎11-24-2009
Message 2 of 11 (351 Views)

Re: is getstring input an integer or a distance?

02-21-2013 10:43 PM 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)     
  )

 

*Expert Elite*
Kent1Cooper
Posts: 4,939
Registered: ‎09-13-2004
Message 3 of 11 (330 Views)

Re: is getstring input an integer or a distance?

02-22-2013 05:32 AM 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
Mentor
mid-awe
Posts: 1,155
Registered: ‎12-08-2004
Message 4 of 11 (316 Views)

Re: is getstring input an integer or a distance?

02-22-2013 10:21 AM in reply to: Kent1Cooper

Thank you, that'll do nicely :smileyvery-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.

*Expert Elite*
Kent1Cooper
Posts: 4,939
Registered: ‎09-13-2004
Message 5 of 11 (309 Views)

Re: is getstring input an integer or a distance?

02-22-2013 12:17 PM 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
*Expert Elite*
Posts: 2,369
Registered: ‎11-24-2009
Message 6 of 11 (293 Views)

Re: is getstring input an integer or a distance?

02-22-2013 10:00 PM 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

 

*Expert Elite*
scot-65
Posts: 2,081
Registered: ‎12-11-2003
Message 7 of 11 (260 Views)

Re: is getstring input an integer or a distance?

02-25-2013 03:48 PM 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
Dyslexia is a permanent physical disability that cannot be seen.
*Expert Elite*
Kent1Cooper
Posts: 4,939
Registered: ‎09-13-2004
Message 8 of 11 (242 Views)

Re: is getstring input an integer or a distance?

02-26-2013 05:36 AM 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
Mentor
mid-awe
Posts: 1,155
Registered: ‎12-08-2004
Message 9 of 11 (156 Views)

Re: is getstring input an integer or a distance?

06-06-2013 02:56 PM 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.

*Expert Elite*
Posts: 2,369
Registered: ‎11-24-2009
Message 10 of 11 (127 Views)

Re: is getstring input an integer or a distance?

06-07-2013 12:35 AM 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? 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community