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

getstring if ascii / if integer

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
smaher12
2868 Views, 8 Replies

getstring if ascii / if integer

I am messing around with a lisp that will draw grid bubbles and was wonder if instead of doing (setq an (getkword "\nSpecify grid type [Alpha/Numeric]: ")) can the getstring fuction be used to determine if the user enters ascii do this and if the user entered a integer do that? Recommendations?

 

 

8 REPLIES 8
Message 2 of 9
dgorsman
in reply to: smaher12

Everything entered at the keyboard is ASCII (it includes numbers, letters, and a number of non-printing characters).  If you mean differentiating between numeric and alphanumeric content, yes - there's a couple of ways of doing that.  You can try converting the input into an integer via (atoi ...), but there's a few limitiations.  And you can convert the input string to a list of ASCII codes; ASCII groups its characters together, so you can range-test each ASCII value to see if its a number or not.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 3 of 9
Kent1Cooper
in reply to: smaher12


@smaher12 wrote:

I am messing around with a lisp that will draw grid bubbles and was wonder if instead of doing (setq an (getkword "\nSpecify grid type [Alpha/Numeric]: ")) can the getstring fuction be used to determine if the user enters ascii do this and if the user entered a integer do that? Recommendations?


One way to do that, if the return of (getstring) is saved to a variable called 'str':

 

(if

  (= str (itoa (atoi str))); convert to integer and back to string -- the same?

  (...then, it's an integer...)

  (...else, it's something else, presumably a letter...)

)

 

The "something else" could include an empty string [Enter in response to (getstring)], or a combination such as "3B", as well as the expected things like "A".  So if you want to protect against certain kinds of invalid input, you might need to make it more sophisticated.

Kent Cooper, AIA
Message 4 of 9
smaher12
in reply to: Kent1Cooper

I managed to patch this together. Suggestions?

 

(defun C:test (/ str glt pt)

  (setq str (getstring "\nSpecify grid line tag: ")
        glt (atoi str)
  )
  (while
    (setq pt (getpoint "\nSpecify point: " ))
    (if (/= glt 0)
      (progn
	(command "TEXT" "M" pt "" "0" glt)
	(setq glt (1+ glt))
      )
    )
    (if (= glt 0)
      (progn
        (command "TEXT" "M" pt "" "0" str)
	 (setq str
		(cond ((= "Z" str) "AA")
		      ((= "z" str) "aa")
		      ((= 1 (strlen str)) (chr (1+ (ascii str))))
		      ((= 2 (strlen str)) (strcat (chr (1+ (ascii (substr str 1 1))))
						 (chr (1+ (ascii (substr str 2 1)))))
		      )
		); cond
         )
     ); progn
   ); if
 ); while
 (princ)
)
  

 

Message 5 of 9
Kent1Cooper
in reply to: smaher12


@smaher12 wrote:

I managed to patch this together. Suggestions? 


Do you think you need to do anything to prevent an entry such as "3B"?  If someone typed that in, 'glt' would be 3, and it would proceed as if they had typed in just "3".  Similarly, if they typed "B8", it would I think use that for the first one, then "C9", then "D:", then "E;", etc.  It would be possible [as in another recent thread] to limit it to only numbers or only alphabetic characters, place a limit on the number or characters, by testing what the User typed with (wcmatch), and asking again if they made an invalid entry.

 

Apart from the possibility of the User answering the prompt with Escape, 'glt' is going to be either 0 or not, so you can consolidate your two (if) functions into one, with 'then' and 'else' expressions:

 

....

    (if (/= glt 0)
      (progn ; then
        (command "TEXT" "M" pt "" "0" glt)
        (setq glt (1+ glt))
      ); progn [then]
      (progn ; else
        (command "TEXT" "M" pt "" "0" str)
....

     ); progn [else]
   ); if
....

Kent Cooper, AIA
Message 6 of 9
Lee_Mac
in reply to: smaher12


@smaher12 wrote:

Suggestions?


I would suggest the following for your consideration:

 

(defun c:tag ( / fun ins ocs str uxa )
    (while
        (not
            (or (= "" (setq str (getstring "\nSpecify grid line tag: ")))
                (wcmatch str "~*[~0-9]*")
                (wcmatch str "~*[~a-zA-Z]*")
            )
        )
        (princ "\nPlease enter either letters or numbers.")
    )
    (if (/= "" str)
        (progn
            (if (wcmatch str "~*[~0-9]*")
                (setq fun (lambda ( x ) (itoa (1+ (atoi x)))))
                (setq fun LM:A++)
            )
            (setq ocs (trans '(0.0 0.0 1.0) 1 0 t)
                  uxa (angle '(0.0 0.0 0.0) (trans (getvar 'ucsxdir) 0 ocs t))
            )
            (while (setq ins (getpoint "\nSpecify point <Done>: "))
                (entmake
                    (list
                       '(00 . "TEXT")
                       '(72 . 4)
                       '(73 . 0)
                        (cons 001 str)
                        (cons 050 uxa)
                        (cons 007 (getvar 'textstyle))
                        (cons 040 (getvar 'textsize))
                        (cons 010 (trans ins 1 ocs))
                        (cons 011 (trans ins 1 ocs))
                        (cons 210 ocs)
                    )
                )
                (setq str (fun str))
            )
        )
    )
    (princ)
)

;; Alpha++  -  Lee Mac
;; Increments an alphabetical string by one, e.g. AZ => BA
;; a - [str] alphabetical string

(defun LM:A++ ( a )
    (   (lambda ( f ) (vl-list->string (reverse (f (reverse (vl-string->list a)) t))))
        (lambda ( l x )
            (cond
                (   (null l) (if x '(65) '(97)))
                (   (= 090 (car l)) (cons 65 (f (cdr l)  t )))
                (   (= 122 (car l)) (cons 97 (f (cdr l) nil)))
                (   (cons (1+ (car l)) (cdr l)))
            )
        )
    )
)
(princ)

 

On a related note, you might also be interested in my Incremental Numbering Suite program.

 

Message 7 of 9
bkp
Contributor
in reply to: Lee_Mac

We have made a program in our company to draw grids for us. If not wrong the similar way, you are thinking.

 

Now we are trying to connect excel with autocad via script file, as we still dont know how to connect both of them directly.

 

Any help from the experts on this will be highly appreciated.

And yes, how to write the program in short is also to be learnt, as we are very much new to autolisp.

(setq sc (getreal "\nEnter Drawing scale: "))
(setq radi (* sc 3.0))
(setq hi (* sc 3.0))
(setq p1 (getpoint "\nBottom left point:"))
(setq x (getint "\nEnter No. of Grids in X-Direction:"))
(setq x1 (getreal "\nTotal length of grid line in X-Direction:"))
(setq e3 (getstring "\nSelect option for notation for x-axis (A-number,B-alphabets [a/b] <a>:"))
(if (= e3 "a") (setq s (getint "\nEnter Starting Number:")) (setq s (getint "Enter Alphabte unicode number(i.e FOR A=65,B=66...) Number: ")))
(setq y (getint "Enter No. of Grids in Y-Direction\n"))
(setq y1 (getreal "total length of grid line in Y-Direction\n"))
(if (= e3 "a") (setq s1 (getint "Enter Alphabet unicode number(i.e FOR A=65,B=66...) Number: ")) (setq s1 (getint "\nEnter Starting Number:")))
;;;;
(setq osm (getvar "osmode"))
(setvar "osmode" 0)
(setq p0v (list (car p1) (- (cadr p1) 200)))
(setq p1v (list (car p1) (+ (cadr p1) (+ y1 200))))

(setq x2 (- x 1))
(setq p2 (list (car p0v) (- (cadr p0v) radi)))
;
(command "layer" "s" "LW3" "")
(command "circle" p2 radi "")
(command "layer" "s" "TXT4" "")
(if (= e3 "b") (setq a1 (chr s)) (setq a1 s))
(command "text" "s" "rs" "j" "MC" p2 hi 0 a1 "")
(command "layer" "s" "center" "")
(command "line" p0v p1v "")
(repeat x2
(setq d1 (getreal "\nEnter Offset distances from 1st grid line: "))
(setq x3 (list (+ (car p0v) d1) (cadr p0v)))
(setq x4 (list (+ (car p1v) d1) (cadr p1v)))
(command "layer" "s" "LW3" "")
(setq x5 (list (car x3) (- (cadr x3) radi)))
(command "circle" x5 radi "")
(command "layer" "s" "TXT4" "")
(setq s (+ s 1))
(if (= e3 "b") (setq a1 (chr s)) (setq a1 s))
(command "text" "s" "rs" "j" "MC" x5 hi 0 a1 "")
(command "layer" "s" "center" "")
(command "line" x3 x4 "")
);repeat
;;;;
(setq p0h (list (- (car p1) 200) (cadr p1)))
(setq p1h (list (+ (car p1) (+ x1 200)) (cadr p1)))
(setq y2 (- y 1))
(setq q2 (list (- (car p0h) radi) (cadr p0h)))
(command "layer" "s" "LW3" "")
(command "circle" q2 radi "")
(command "layer" "s" "TXT4" "")
(if (= e3 "a") (setq a2 (chr s1)) (setq a2 s1))
(command "text" "j" "MC" q2 hi 0 a2 "") 
(command "layer" "s" "center" "")
(command "line" p0h p1h "")
;
(repeat y2
	(setq d2 (getreal "\nEnter Offset distances from 1st grid line:"))
		(setq y3 (list (car p0h) (+ (cadr p0h) d2)))
		(setq y4 (list (car p1h) (+ (cadr p1h) d2)))
	(command "layer" "s" "LW3" "")
		(setq y5 (list (- (car y3) radi) (cadr y3)))
	(command "circle" y5 radi "")
	(command "layer" "s" "TXT4" "")
		(setq s1 (+ s1 1))
		(if (= e3 "a") (setq a2 (chr s1)) (setq a2 s1))
	(command "text" "j" "MC" y5 hi 0 a2 "")
	(command "layer" "s" "center" "")
	(command "line" y3 y4 "")
);repeat
(setvar "osmode" osm)
);defun

 

Message 8 of 9
smaher12
in reply to: Kent1Cooper

Thank you ALL for your help. Thanks for getting me on the right path and cleaning up my mess. Lee - as always very nice job! Above and beyond what I was thinking. You provided something to really consider.

Message 9 of 9
Lee_Mac
in reply to: smaher12

Thank you smaher! Smiley Happy

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

Post to forums  

Autodesk Design & Make Report

”Boost