Hi please help with strcat and more (Beginner question again!)

Hi please help with strcat and more (Beginner question again!)

vporrash141089
Advocate Advocate
1,081 Views
7 Replies
Message 1 of 8

Hi please help with strcat and more (Beginner question again!)

vporrash141089
Advocate
Advocate

Hi everybody 

 

I've been working this morning on a program to insert the following fl  floor number which would be 01,02,03 etc... and I came up with this which ofcourse is not working as desired and I would like some suggestions on how I could accomplish the followint 01.ROOM01 and +1 with every click.

 

 

(defun c:SVT ()
 
(setq F (getstring "\nEnter floor number"))
(setq stn (getint "Enter starting number"))
	
	(while (setq p (getpoint "\n Pick to insert text"))
		(entmake (list (cons 0 "text")
                       (cons 10 p) 
                       (cons 40 7) 
                       (cons 50 0.0)
                       (cons 1  F ".ROOM" stn ))
		)        
                       
		       (setq stn (+ stn 1))
 )  
)

 

Thanks in advanced,

 

 

 
0 Likes
Accepted solutions (2)
1,082 Views
7 Replies
Replies (7)
Message 2 of 8

paullimapa
Mentor
Mentor

Could you apply the knowledge you’ve learned from replies to your previous post

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-having-issues-with-lisp-beg...


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 8

ronjonp
Mentor
Mentor
Accepted solution

Try this:

 

(defun c:svt (/ f p stn)
  (if (and (/= "" (setq f (getstring "\nEnter floor number"))) (setq stn (getint "Enter starting number")))
    (while (setq p (getpoint "\n Pick to insert text"))
      (entmake
	(list (cons 0 "text") (cons 10 p) (cons 40 7) (cons 50 0.0) (cons 1 (strcat f ".ROOM" (itoa stn))))
      )
      (setq stn (+ stn 1))
    )
  )
  (princ)
)

 

You have to use STRCAT to join all your items and they all need to be text which is why ITOA is used for your getINT stn variable.

 

(strcat f ".ROOM" (itoa stn))

 

The (if (and at the beginning checks that all user input is correct then proceeds to the loop.

Message 4 of 8

vporrash141089
Advocate
Advocate

I've dug into the link for my previous questions and this might be silly but I can't figure out how to add the zeros...

The text should read like this 2.ROOM001 but with the option provided on this post zeros do not show up, I'd like to ask if you could maybe give me a hint on how I should make those zeros show up on the entmake text.

 

(defun c:SVT (/ f p stn)
  (setvar "clayer" "ROOM")
  (if (and (/= "" (setq f (getstring "\nEnter floor number"))) (setq stn (getint "Enter starting number")))
    (while (setq p (getpoint "\n Pick to insert text"))
      (entmake
		(list (cons 0 "text") (cons 7 "Arial") (cons 10 p) (cons 40 7) (cons 50 0.0) (cons 1 (strcat f ".ROOM" (itoa stn))))
      )
      (setq stn (+ stn 1))
    )
  )
  (princ)
)
0 Likes
Message 5 of 8

paullimapa
Mentor
Mentor
Accepted solution

sure..in my response to your first post I showed you the following code to test the length of the text string r:

(setq len (strlen r))
(cond
((= 1 len)(setq r (strcat "000" r))
((= 2 len)(setq r (strcat "00" r))
((= 3 len)(setq r (strcat "0" r))
(t (princ"\nNumber is greater than 4 digits"))
)

In that case r represented the value of the text string you wanted to use.

Now you have two text strings you need to test: f and stn:

(cons 1 F ".ROOM" stn) ; but this is incorrectly coded because again like before you need to convert stn from an integer to a string using function itoa and then connect the text strings together using function strcat:

(cons 1 (strcat F ".ROOM" (itoa stn)))

Also, this time you only need to test if the length of the text string is 1 character and then add the zero in front.

So you can either run this revised code 2x like this:

(setq len (strlen F))
(if(= 1 len)(setq F (strcat "0" F))) ; adds 0 in front only if character length is 1 which takes care of F

(setq G (itoa stn)) ; save the converted # to string as G

(setq len (strlen G)) ; get the length

(if(= 1 len)(setq G (strcat "0" G))) ; adds 0 in front only if character length is 1 which takes care of G

Now your entmake text string code should look like this:

(cons 1 (strcat F ".ROOM" G))

Now another more efficient way is to create a sub routine or another function that you will run like the above more than once that I'm calling chk_str. This function will pass the text string str as the argument to test and return text string with the added zero if needed:

(defun chk_str (str / len newstr)

 (setq len (strlen str))
 (if(= 1 len)

   (setq newstr (strcat "0" str)) ; adds 0 in front only if character length is 1

   (setq newstr str) ; no changes needed

 ) 

 newstr ; return

) ; defun

This time your entmake text string code would look like this:

(cons 1 (strcat (chk_str F) ".ROOM" (chk_str (itoa stn))))

putting it all together the updated code will look like this (not tested):

 

(defun c:SVT (/ chk_str F p stn) ; localize functions & symbols
; chk_str function passes the text string str as the argument to test and 
; return text string with the added zero if needed
 (defun chk_str (str / len newstr)
  (setq len (strlen str))
  (if(= 1 len)
   (setq newstr (strcat "0" str)) ; adds 0 in front only if character length is 1
   (setq newstr str) ; no changes needed
  )
  newstr ; return
 ) ; defun
 (setq F (getstring "\nEnter floor number: "))
 (setq stn (getint "Enter starting number: "))
  (while (setq p (getpoint "\n Pick to insert text"))
	(entmake 
          (list
            (cons 0 "text")
            (cons 10 p) 
            (cons 40 7) 
            (cons 50 0.0)
            (cons 1 (strcat (chk_str F) ".ROOM" (chk_str (itoa stn)))) ; revised code
          ) ; list
	) ; entmake                            
	(setq stn (+ stn 1))
  ) ; while
  (princ)
) ; defun

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 8

vporrash141089
Advocate
Advocate
Thanks that is very detailed, I will put into practice and hopefully I'll have this ready by EOD :D, it definitelt worthwhile thanks again for your time!
0 Likes
Message 7 of 8

paullimapa
Mentor
Mentor

You are welcome again. Now the real learning bregins when you start applying what you’ve learned...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 8

Kent1Cooper
Consultant
Consultant

Another little question/suggestion:

Do you ever have floor numbers less than one [presumably basement levels]?  If not, I would suggest that you use (getint) for the floor number, rather than (getstring).  That way, you can prevent certain kinds of errors.

 

(initget 7); for next (get...) function, no Enter [1]. no zero [2], no negative [4] -- 7 = 1+2+4

(setq

  F (getint "\nEnter floor number")

  F (itoa F); turn into string for further processing

)

 

The advantage is that it both requires input [no Enter], and requires an appropriate input.  If an inappropriate one is given, it does not just proceed anyway, which would happen if you missed the right key and typed, for example, "W" in other code here.  Instead, it asks again, until you do give it an appropriate input.

 

If you do use floor number 0 for a basement level, you can use (initget 5); if you use negatives, (initget 1).  With some additional code, it can also be made to remember the last floor number you gave it, and offer that as the default on subsequent use, in which Enter would also be allowed.  If all are allowed, (initget) wouldn't be needed, but using (getint) would still have the benefit of preventing non-whole-number entries.

Kent Cooper, AIA
0 Likes