Jump to content

Lisp lệnh CT trong chủ đề “Lisp đánh chữ theo thứ tự???”

Bạn dùng mã này:

(defun c:ct ( / ss entp gtlast)
 (defun read_text (ent)
   (cdr (assoc 1 (entget ent)))
 )
 (defun write_text (ent gt / tt old new)
   (setq tt  (entget ent)
  old (assoc 1 tt)
  new (cons 1 gt)
  tt  (subst new old tt)
   )
   (entmod tt)
 )
 (defun is_number(s)
   (wcmatch s "0,1,2,3,4,5,6,7,8,9")
 )
 (defun is_char(c )
   (wcmatch c "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,A,B,C,D,E,F,G,H,I,J,K,L,M

,N,O,P,Q,R,S,T,U,V,W,X,Y,")
 )
 (defun next_text (str / i)
   (cond
     ((is_number (substr str (strlen str) 1))
      (progn
 (setq i (strlen str))
 (while (and
	  (> i 1)
	  (is_number (substr str i 1)))
   (setq i (1- i))	
 )
 (strcat (substr str 1 i) (itoa (1+ (atoi (substr str (1+ i))))))
      )
     )
     ((is_char (substr str (strlen str) 1))
      (progn
 (strcat (substr str 1 (1- (strlen str))) (chr (1+ (ascii (substr str (strlen str))))))
      )
     )
     (t str)
   )

 )
 (setq SS (ssget ":S" '((0 . "TEXT"))))
 (if ss
   (progn
     (setq
       entp   (ssname ss 0)
dgoc   (cdr (assoc 10 (entget entp)))
gtlast (read_text entp)
     )
     (while (setq p (getpoint dgoc "\nVao diem: "))
(command ".copy" ss "" dgoc p)
(setq
  entl	 (entlast)
  gtlast (next_text gtlast)
)
(write_text (entlast) gtlast)
     )
   )
 )
 (princ)
)

Tên lệnh và cách thức giống hệt lệnh CT cũ nhưng khắc phục được nhược điểm số 100 và chữ Z.

 

 

Bác Hòanh đã check cái này chưa vậy? Bác thử với những số như là 26, 36, 16 chưa? Vì nếu lên đến 29 thì sau đó là 210, 211 (theo mình nghĩ nó sẽ là 30 hoặc 31 chứ)hoặc sau 39 sẽ là 310, 311 (40, 41)

Cách dùng

  1. Tải tệp 3707_ct.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh CT để chạy lisp.

Mã nguồn 3707_ct.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/83-lisp-%C4%91%C3%A1nh-ch%E1%BB%AF-theo-th%E1%BB%A9-t%E1%BB%B1/
(defun c:ct ( / ss entp gtlast)
 (defun read_text (ent)
   (cdr (assoc 1 (entget ent)))
 )
 (defun write_text (ent gt / tt old new)
   (setq tt  (entget ent)
  old (assoc 1 tt)
  new (cons 1 gt)
  tt  (subst new old tt)
   )
   (entmod tt)
 )
 (defun is_number(s)
   (wcmatch s "0,1,2,3,4,5,6,7,8,9")
 )
 [b](defun is_char(c )
   (wcmatch c "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,A,B,C,D,E,F,G,H,I,J,K,L,M

,N,O,P,Q,R,S,T,U,V,W,X,Y,")
 )[/b]
 (defun next_text (str / i)
   (cond
     ((is_number (substr str (strlen str) 1))
      (progn
 (setq i (strlen str))
 (while (and
	  (> i 1)
	  (is_number (substr str i 1)))
   (setq i (1- i))	
 )
 (strcat (substr str 1 i) (itoa (1+ (atoi (substr str (1+ i))))))
      )
     )
     ((is_char (substr str (strlen str) 1))
      (progn
 (strcat (substr str 1 (1- (strlen str))) (chr (1+ (ascii (substr str (strlen str))))))
      )
     )
     (t str)
   )

 )
 (setq SS (ssget ":S" '((0 . "TEXT"))))
 (if ss
   (progn
     (setq
       entp   (ssname ss 0)
dgoc   (cdr (assoc 10 (entget entp)))
gtlast (read_text entp)
     )
     (while (setq p (getpoint dgoc "\nVao diem: "))
(command ".copy" ss "" dgoc p)
(setq
  entl	 (entlast)
  gtlast (next_text gtlast)
)
(write_text (entlast) gtlast)
     )
   )
 )
 (princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Lisp đánh chữ theo thứ tự???. Xem đúng bài viết #3707 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 223)