Jump to content

Combine the Text and Number


Mesut

Recommended Posts

Hi friends, i want to write lisp codes about coordinates on drawing but i get error message.  strcat command is not working and I didnt understand. Somebody can help to me?

Thanks in advance.

 

(defun c:xy()
(setq yazi 1)
(setq str1 (getstring "Enter the Word: " ))
  (while
   
  (setq ntxx (strcat ( str1 yazi)))
  (setvar "osmode" 1)
  (setq a1 (getpoint "\nSelect the Point"))
  (setq x (rtos (car a1) 2 3))
  (setq y (rtos (cadr a1) 2 3))

  (command "_.text" a1 2.5 "0" x)
  (setvar "osmode" 0)
  (setq a2 (polar a1 (* (/ pi 2) 3) 3.5))
  (command "_.text" a2 2.5 "0" y)
  
  (setq a3 (polar a1  (/ pi 2)  3.5))
  (command "_.text" a3 2.5 "0" ntxx)
  (setq yazi (+ yazi 1))
  (command "_.regen")
  )
 )

 

Link to comment
Share on other sites

Yes the line with strcat isn't working.

 

This might be better:

  (setq ntxx (strcat str1 (itoa yazi)))

 

(no bracket needed after strcat, a bracket means it looks at the next word to be a command which str1 isn't. Then strcat wants to combine strings and doesn't like combining strings and numbers... so change the number, yazi to a string with itoa or rtos )

  • Like 1
  • Agree 1
Link to comment
Share on other sites

With an open ended while the only way to exit this lisp is to hit the Esc button. This will also generate and error message on exit. ; error : Function cancelled

Its also good practice to declare all your variables so they are localized in the lisp.

 

with the changes made the lisp will end with a right click/enter & without an error.  because your ending the lisp it will restore your old osmode settings before you changed it to only endpoints.

 

(defun C:XY (/ # txt x y pt1 pt2 pt3)
  (setq oldsnap (getvar 'osmode) ;saves osmode
        # 1
        pt1 t ;pass by while the first time.
        str (getstring "Enter the Word: ")
  )
  (setvar 'osmode 1)
  (while pt1
    (setq txt (strcat str (itoa #)))
    (if (setq pt1 (getpoint "\nSelect the Point"))
      (progn
        (setq pt2 (polar pt1 (DtR 270) 3.5)
              pt3 (polar pt2 (DtR 270) 3.5)
              x (rtos (car pt1) 2 3)
              y (rtos (cadr pt1) 2 3)
        )
        (command "_.Text" "_non" pt1 2.5 "0" txt) ;use non infront of points to prevent snaping
        (command "_.Text" "_non" pt2 2.5 "0" x)
        (command "_.Text" "_non" pt3 2.5 "0" y)
        (setq # (+ # 1))
        ;(command "_.Regen") ;not used
      )
    )
  )
  (setvar 'osmode oldsnap) ;restores old snaps
  (princ)
)
(defun DtR (d) (* pi (/ d 180.0))) ;degree to radian
(defun RtD (r) (* 180.0 (/ r pi))) ;radian to degree

 

Welcome to the forums.

Edited by mhupp
Tharwat changes
Link to comment
Share on other sites

@mhupp if you relocate getpoint function in place of pt1 in front of while function specifically then this would be the best way to exit the program with enter to avoid the fore-said error you already demonstrated.

Also you don't need to set the system variable OSMODE every time a user specify a point so you can move it above while function since you need it set only once.

I don't know what's the use of regen in this routine !

 

  • Agree 1
Link to comment
Share on other sites

19 minutes ago, Tharwat said:

@mhupp if you relocate getpoint function in place of pt1 in front of while function specifically then this would be the best way to exit the program with enter to avoid the fore-said error you already demonstrated. Also you don't need to set the system variable OSMODE every time a user specify a point so you can move it above while function since you need it set only once. I don't know what's the use of regen in this routine !

 

Made your changes except the getpoint one. I (setq pt1 t) so it will enter into the while function. As long as you keep selecting points the loop will continue, stop and it will exit. or am i miss understanding what your saying?

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

I don't like modifying others' codes but this is just an example of avoiding error from the use of While with true statement although adding an error function is recommended in this case to reset the system variable used in the routine and to exit calmly / safely.

 

(defun C:XY (/ # x y pt1 pt2)
  (setq oldsnap (getvar 'osmode)        ;saves osmode
        #       1
  )
  (if (/= (setq str (getstring "\nEnter the Word: ")) "")
    (progn
      (setvar 'osmode 1)
      (while (setq pt1 (getpoint "\nSelect the Point"))
        (setq pt2 (polar pt1 (DtR 270) 3.5)
              x (rtos (car pt1) 2 3)
              y (rtos (cadr pt1) 2 3)
        )
        (command "_.Text" "_non" pt1 2.5 "0" (strcat str (itoa #)))
                                        ;use non infront of points to prevent snaping
        (command "_.Text" "_non" pt2 2.5 "0" x)
        (command "_.Text" "_non" (polar pt2 (DtR 270) 3.5) 2.5 "0" y)
        (setq # (+ # 1))
      )
      (setvar 'osmode oldsnap)          ;restores old snaps
      (princ)
    )
  )
  (princ)
)
(defun DtR (d) (* pi (/ d 180.0)))      ;degree to radian
(defun RtD (r) (* 180.0 (/ r pi)))      ;radian to degree

 

  • Like 2
Link to comment
Share on other sites

Ok i see what you meant now. didn't quite understand what you meant by "in front of "

 

--edit--

Was thinking about this and you might want to ask for input on the count. like your labeling something 1-12 exit command and you missed one. no real way to start back at 13. This will ask the user to set the count so you can pick up where you left off. defaults to 1 with enter key

(defun C:XY (/ # x y pt1 pt2)
  (setq oldsnap (getvar 'osmode))        ;saves osmode
  (or (setq # (getint "\nSet Count <1>: ")) (setq # 1))
  (if (/= (setq str (getstring "\nEnter the Word: ")) "")
    (progn
      (setvar 'cmdecho 0)
      (setvar 'osmode 1)
      (while (setq pt1 (getpoint "\nSelect the Point"))
        (setq pt2 (polar pt1 (DtR 270) 3.5)
              x (rtos (car pt1) 2 3)
              y (rtos (cadr pt1) 2 3)
        )
        (command "_.Text" "_non" pt1 2.5 "0" (strcat str (itoa #)))
        (command "_.Text" "_non" pt2 2.5 "0" x)
        (command "_.Text" "_non" (polar pt2 (DtR 270) 3.5) 2.5 "0" y)
        (setq # (+ # 1))
      )
    )
  )
  (setvar 'osmode oldsnap)          ;restores old snaps
  (setvar 'cmdecho 1)
  (princ)
)
(defun DtR (d) (* pi (/ d 180.0)))      ;degree to radian
(defun RtD (r) (* 180.0 (/ r pi)))      ;radian to degree

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

An mtext alternative example

 

(setq t1 "Northing ")
(setq t2 "Easting ")
(setq t3 "Southing ")
(setq t4 "Westing ")
(setq pt1 (getpoint "\nPick Top left for text"))
(setq pt2 (getpoint pt1 "\nPick Bottom right"))
(command "mtext" pt1 pt2 t1 t2 t3 t4 "")

 

  • Like 1
Link to comment
Share on other sites

Thank you @BIGAL for this solution,  I can write now in one row all i need.

(defun c:xy()
(setq yazi 1)
(setq str1 (getstring "Kisaltmayi Giriniz: " ))
  (while
  (setq ntxx (strcat str1 "- " (itoa yazi)))
  (setvar "osmode" 1)
  (setq a1 (getpoint "\nNoktayi Seçiniz.."))
  (command "_.point" a1 )
  (setq x (rtos (car a1) 2 3))
  (setq y (rtos (cadr a1) 2 3))
  ;(command "_.text" a1 2.5 "0" x)
  (setvar "osmode" 0)
  (setq a2 (polar a1 (* (/ pi 2) 3) 3.5))
  ;(command "_.text" a2 2.5 "0" y)
  (setq a3 (polar a1  (/ pi 2)  3.5))
  (command "mtext" a1 a3 ntxx x y "")
  ;(command "_.text" a3 2.5 "0" ntxx)
  (setq yazi (+ yazi 1))
  (command "_.regen")
  ) ;end while
 ) ;end

 

Edited by Mesut
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...