Jump to content

lisp Phun tọa độ các điểm từ file txt vào CAD

To : khaosat2009

Bạn khảosát2009 thử xem cái này có đúng ý bạn không

Bạn chạy thử :

(defun c:RFT(/ data f h line pt pXY spc str ten val);Read File Txt
(vl-load-com)
(defun Split (Str Char / Lst pos)
(while (setq pos (vl-string-search Char Str))
(if (null Lst)
(setq Lst (list (substr Str 1 pos)))
(setq Lst (append Lst (list (read (substr Str 1 pos))))))
(setq Str (substr Str (+ pos 2)) ))
(setq Lst (append Lst (list (read Str)))))

(if (setq ten (getfiled "Chon File txt" (getvar "dwgprefix") "txt" 8))
(progn
(or (tblsearch "layer" "Point") (command "-layer" "n" "Point" "") )
(or (tblsearch "layer" "Sothutu") (command "-layer" "n" "Sothutu" "c" 3 "Sothutu" "") )
(or (tblsearch "layer" "Caodo") (command "-layer" "n" "Caodo" "c" 4 "Caodo" "") )
(setq spc (vla-get-ModelSpace (vla-get-ActiveDocument(vlax-get-Acad-Object))))
;(setq h 2)
(Setq h (getreal"\nChieu cao chu:"))
(if (= nil h)(setq h 2))
(setq kitu nil)
(initget "X Y")
(setq kitu (getkword "\nChon kieu xuat :"))
(cond
   ((= kitu "X") (setq kitu 0))
   ((= kitu "Y") (setq kitu 1))    
   )
(if (= nil kitu)(setq kitu 0))
;(* (getvar "dimtxt")(getvar "dimscale")))
(setq f (open (findfile ten) "r"))
(while (setq Line (read-line f))
(if (vl-string-search "\t" Line)
(progn
(setq data (split Line "\t" )
val (car data)
pt (cdr data))
(if (not(vl-catch-all-error-p (vl-catch-all-apply 'vlax-3d-point pt)))
(progn
;(setq pXY (list (car pt)(cadr pt)))
(if (= 0 kitu)
(setq pXY (list (car pt)(cadr pt)))
)
(if (= 1 kitu)
(setq pXY (list (cadr pt)(car pt)))
)
(vla-put-Layer (vla-addpoint spc (vlax-3d-point pXY)) "Point")
;kkkkkkkkkkkkk
(vla-put-Layer (setq str (vla-addtext spc val (vlax-3d-point pXY) h)) "Sothutu")
(vla-put-Alignment str 8)
(vla-put-TextAlignmentPoint str (vlax-3d-point pXY))
(vla-put-Layer (vla-addtext spc (caddr pt) (vlax-3d-point pXY) h) "Caodo") ))))) ))
(princ))

Cách dùng

  1. Tải tệp 97219_rft.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 RFT để chạy lisp.

Mã nguồn 97219_rft.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/20044-y%C3%AAu-c%E1%BA%A7u-lisp-phun-t%E1%BB%8Da-%C4%91%E1%BB%99-c%C3%A1c-%C4%91i%E1%BB%83m-t%E1%BB%AB-file-txt-v%C3%A0o-cad/
(defun c:RFT(/ data f h line pt pXY spc str ten val);Read File Txt
(vl-load-com)
(defun Split (Str Char / Lst pos)
(while (setq pos (vl-string-search Char Str))
(if (null Lst)
(setq Lst (list (substr Str 1 pos)))
(setq Lst (append Lst (list (read (substr Str 1 pos))))))
(setq Str (substr Str (+ pos 2)) ))
(setq Lst (append Lst (list (read Str)))))

(if (setq ten (getfiled "Chon File txt" (getvar "dwgprefix") "txt" 8))
(progn
(or (tblsearch "layer" "Point") (command "-layer" "n" "Point" "") )
(or (tblsearch "layer" "Sothutu") (command "-layer" "n" "Sothutu" "c" 3 "Sothutu" "") )
(or (tblsearch "layer" "Caodo") (command "-layer" "n" "Caodo" "c" 4 "Caodo" "") )
(setq spc (vla-get-ModelSpace (vla-get-ActiveDocument(vlax-get-Acad-Object))))
;(setq h 2)
(Setq h (getreal"\nChieu cao chu:"))
(if (= nil h)(setq h 2))
(setq kitu nil)
(initget "X Y")
(setq kitu (getkword "\nChon kieu xuat [X hay Y]:"))
(cond
   ((= kitu "X") (setq kitu 0))
   ((= kitu "Y") (setq kitu 1))    
   )
(if (= nil kitu)(setq kitu 0))
;(* (getvar "dimtxt")(getvar "dimscale")))
(setq f (open (findfile ten) "r"))
(while (setq Line (read-line f))
(if (vl-string-search "\t" Line)
(progn
(setq data (split Line "\t" )
val (car data)
pt (cdr data))
(if (not(vl-catch-all-error-p (vl-catch-all-apply 'vlax-3d-point pt)))
(progn
;(setq pXY (list (car pt)(cadr pt)))
(if (= 0 kitu)
(setq pXY (list (car pt)(cadr pt)))
)
(if (= 1 kitu)
(setq pXY (list (cadr pt)(car pt)))
)
(vla-put-Layer (vla-addpoint spc (vlax-3d-point pXY)) "Point")
;kkkkkkkkkkkkk
(vla-put-Layer (setq str (vla-addtext spc val (vlax-3d-point pXY) h)) "Sothutu")
(vla-put-Alignment str 8)
(vla-put-TextAlignmentPoint str (vlax-3d-point pXY))
(vla-put-Layer (vla-addtext spc (caddr pt) (vlax-3d-point pXY) h) "Caodo") ))))) ))
(princ))

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Yêu cầu] lisp Phun tọa độ các điểm từ file txt vào CAD. Xem đúng bài viết #97219 để đọc hướng dẫn và trao đổi thêm.

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