Viết lisp xử lý số liệu từ .txt vào trong cad
Chúc bạn thành công.
(defun c:itext(/ blk *tH* lstDis lstDisX lstVal fn mLstPnt rl i j p mtext)(vl-load-com)
(setq blk (vla-get-block (vla-get-activelayout(vla-get-activedocument (vlax-get-acad-object)))))
(setq *tH* 1)
(cond
((setq fn (getfiled "Chon file so lieu" "" "txt" 8))
(defun mtext (str p blk h / obj)
(vla-put-AttachmentPoint (setq obj (vla-addmtext blk (vlax-3d-point p) h str)) 4)
(vla-put-rotation obj (* pi 0.5))
)
(defun mLstPnt (p lstDisX lstVal)
(mapcar '(lambda(y z)(mtext y z blk *tH*))
lstVal
(cons p (mapcar '(lambda(x)(setq p (cons (+ (car p) x) (cdr p)))) lstDisX))
)
)
(setq fn (open fn "r"))
(while (setq rl (read-line fn))
(setq i 1 j 0
p (getpoint (strcat "\nDiem dat Text dong thu "(itoa (setq j (1+ j)))" :"))
lstVal (mapcar 'vl-princ-to-string (read (strcat "(" rl ")")))
lstDisX
(reverse
(repeat (1- (length lstVal))
(setq lstDis (cons (getdist (strcat "\nKhoang cach tu cot H" (itoa i) "-> H" (itoa (setq i (1+ i))) ":")) lstDis))
)
)
)
(mLstPnt p lstDisX lstVal)
(setq lstDis '())
)
(close fn)
)
)
)
(defun c:itext(/ blk *tH* lstDis lstDisX lstVal fn mLstPnt rl i j p mtext)(vl-load-com)
(setq blk (vla-get-block (vla-get-activelayout(vla-get-activedocument (vlax-get-acad-object)))))
(setq *tH* 1)
(cond
((setq fn (getfiled "Chon file so lieu" "" "txt" 8))
(defun mtext (str p blk h / obj)
(vla-put-AttachmentPoint (setq obj (vla-addmtext blk (vlax-3d-point p) h str)) 4)
(vla-put-rotation obj (* pi 0.5))
)
(defun mLstPnt (p lstDisX lstVal)
(mapcar '(lambda(y z)(mtext y z blk *tH*))
lstVal
(cons p (mapcar '(lambda(x)(setq p (cons (+ (car p) x) (cdr p)))) lstDisX))
)
)
(setq fn (open fn "r"))
(while (setq rl (read-line fn))
(setq i 1 j 0
p (getpoint (strcat "\nDiem dat Text dong thu "(itoa (setq j (1+ j)))" :"))
lstVal (mapcar 'vl-princ-to-string (read (strcat "(" rl ")")))
lstDisX
(reverse
(repeat (1- (length lstVal))
(setq lstDis (cons (getdist (strcat "\nKhoang cach tu cot H" (itoa i) "-> H" (itoa (setq i (1+ i))) ":")) lstDis))
)
)
)
(mLstPnt p lstDisX lstVal)
(setq lstDis '())
)
(close fn)
)
)
)
Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.
Cách dùng
- Chép mã nguồn, lưu thành tệp 202410_itext.lsp.
- 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ẽ.
- Gõ lệnh ITEXT để chạy lisp.
Mã nguồn 202410_itext.lsp (trích từ bài viết gốc)
(defun c:itext(/ blk *tH* lstDis lstDisX lstVal fn mLstPnt rl i j p mtext)(vl-load-com)
(setq blk (vla-get-block (vla-get-activelayout(vla-get-activedocument (vlax-get-acad-object)))))
(setq *tH* 1)
(cond
((setq fn (getfiled "Chon file so lieu" "" "txt" 8))
(defun mtext (str p blk h / obj)
(vla-put-AttachmentPoint (setq obj (vla-addmtext blk (vlax-3d-point p) h str)) 4)
(vla-put-rotation obj (* pi 0.5))
)
(defun mLstPnt (p lstDisX lstVal)
(mapcar '(lambda(y z)(mtext y z blk *tH*))
lstVal
(cons p (mapcar '(lambda(x)(setq p (cons (+ (car p) x) (cdr p)))) lstDisX))
)
)
(setq fn (open fn "r"))
(while (setq rl (read-line fn))
(setq i 1 j 0
p (getpoint (strcat "\nDiem dat Text dong thu "(itoa (setq j (1+ j)))" :"))
lstVal (mapcar 'vl-princ-to-string (read (strcat "(" rl ")")))
lstDisX
(reverse
(repeat (1- (length lstVal))
(setq lstDis (cons (getdist (strcat "\nKhoang cach tu cot H" (itoa i) "-> H" (itoa (setq i (1+ i))) ":")) lstDis))
)
)
)
(mLstPnt p lstDisX lstVal)
(setq lstDis '())
)
(close fn)
)
)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề [Yêu cầu] Viết lisp xử lý số liệu từ .txt vào trong cad. Xem đúng bài viết #202410 để đọc hướng dẫn và trao đổi thêm.