Jump to content

Lisp lệnh HA3 trong chủ đề “nhờ viết lisp vẽ đồ thị trên cad số liệu được lấy từ file excel”

Tặng chủ topic cái này, mới viết xong. Hy vọng hài lòng.

 

;Doan Van Ha - CADViet.com - Ngay 24/11/2014
;Muc dich: Ve Spline qua cac diem duoc ghi trong file txt (moi hang la 1 diem, X va Y cach nhau boi 1 ky tu tab; ve duong dong; ghi text cao do duong duoi am tren).
(defun C:HA3( / fn ss pr rl txt pt pd lst)
 (command "ucs" "w") (command "undo" "be")
 (setq fn (getfiled "Chon file de lay so lieu" "" "txt" 8))
 (or (and tlx (or (= (type tlx) 'int) (= (type tlx) 'real))) (setq tlx 200))
 (setq tlx (cond ((getreal (strcat "\nTi le theo phuong X <" (rtos tlx 2 0) ">: "))) (tlx)))
 (or (and tly (or (= (type tly) 'int) (= (type tly) 'real))) (setq tly 5000))
 (setq tly (cond ((getreal (strcat "\nTi le theo phuong Y <" (rtos tly 2 0) ">: "))) (tly)))
 (or (and hei (or (= (type hei) 'int) (= (type hei) 'real))) (setq hei 0.10))
 (setq hei (cond ((getreal (strcat "\nChieu cao chu <" (rtos hei 2 2) ">: "))) (hei)))
 (setq ss (ssadd))
 (setq pr (open fn "r"))
 (while (setq rl (read-line pr))
  (setq txt (HA:str->lst rl (chr 9)))
  (setq pt (list (/ (car txt) tlx) (/ (cadr txt) tly)))
  (setq pd (list (/ (car txt) tlx) 0))
  (MakeLine pd pt 5)
  (ssadd (entlast) ss)
  (if (>= (cadr txt) 0)
   (MakeText (polar pd (/ pi -2) hei) (rtos (cadr txt) 2 0) hei 2 "MR")
   (MakeText (polar pd (/ pi 2) hei) (rtos (cadr txt) 2 0) hei 2 "ML"))
  (ssadd (entlast) ss)
  (setq lst (cons pt lst)))
 (close pr)
 (MakeSpline (reverse lst) 1)
 (ssadd (entlast) ss)
 (MakeLine (list (cadr (last lst)) 0) (list (caar lst) 0) 3)
 (ssadd (entlast) ss)
 (command "move" ss "" (list (cadr (last lst)) 0) pause)
 (command "undo" "e") (princ))
;----- String to List, EX: (HA:str->lst "1,2,3,4,5" ",") => (1 2 3 4 5)
(defun HA:str->lst ( str del / pos )
 (if (setq pos (vl-string-search del str))
  (cons (atof (substr str 1 pos)) (HA:str->lst (substr str (+ pos 1 (strlen del))) del)) 
  (list (atof str))))
(defun MakeSpline (lst col)
 (entmake (append (list '(0 . "SPLINE") '(100 . "AcDbEntity") (cons 62 col) '(100 . "AcDbSpline") (cons 71 3) (cons 74 (length lst))) (mapcar '(lambda (p) (cons 11 p)) lst))))
(defun MakeLine (p1 p2 col)
 (entmake (list (cons 0 "LINE") (cons 62 col) (cons 10 p1) (cons 11 p2))))
(defun MakeText (pt str hei col jus / lst)
 (setq lst (list '(0 . "TEXT") (cons 62 col) (cons 10 pt) (cons 40 hei) (cons 1 str) (cons 50 (/ pi 2))))
 (cond
  ((= jus "ML") (setq lst (append lst (list (cons 72 0) (cons 11 pt) (cons 73 2)))))
  ((= jus "MR") (setq lst (append lst (list (cons 72 2) (cons 11 pt) (cons 73 2))))))
 (entmake lst))
 

không còn gì tuyệt vời hơn!

cảm ơn bác nhiều nhiều !122241_ok_men.png

Cách dùng

  1. Tải tệp 323792_ha3.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 HA3 để chạy lisp.

Mã nguồn 323792_ha3.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/114704-nh%E1%BB%9D-vi%E1%BA%BFt-lisp-v%E1%BA%BD-%C4%91%E1%BB%93-th%E1%BB%8B-tr%C3%AAn-cad-s%E1%BB%91-li%E1%BB%87u-%C4%91%C6%B0%E1%BB%A3c-l%E1%BA%A5y-t%E1%BB%AB-file-excel/
;Doan Van Ha - CADViet.com - Ngay 24/11/2014
;Muc dich: Ve Spline qua cac diem duoc ghi trong file txt (moi hang la 1 diem, X va Y cach nhau boi 1 ky tu tab; ve duong dong; ghi text cao do duong duoi am tren).
(defun C:HA3( / fn ss pr rl txt pt pd lst)
 (command "ucs" "w") (command "undo" "be")
 (setq fn (getfiled "Chon file de lay so lieu" "" "txt" 8))
 (or (and tlx (or (= (type tlx) 'int) (= (type tlx) 'real))) (setq tlx 200))
 (setq tlx (cond ((getreal (strcat "\nTi le theo phuong X <" (rtos tlx 2 0) ">: "))) (tlx)))
 (or (and tly (or (= (type tly) 'int) (= (type tly) 'real))) (setq tly 5000))
 (setq tly (cond ((getreal (strcat "\nTi le theo phuong Y <" (rtos tly 2 0) ">: "))) (tly)))
 (or (and hei (or (= (type hei) 'int) (= (type hei) 'real))) (setq hei 0.10))
 (setq hei (cond ((getreal (strcat "\nChieu cao chu <" (rtos hei 2 2) ">: "))) (hei)))
 (setq ss (ssadd))
 (setq pr (open fn "r"))
 (while (setq rl (read-line pr))
  (setq txt (HA:str->lst rl (chr 9)))
  (setq pt (list (/ (car txt) tlx) (/ (cadr txt) tly)))
  (setq pd (list (/ (car txt) tlx) 0))
  (MakeLine pd pt 5)
  (ssadd (entlast) ss)
  (if (>= (cadr txt) 0)
   (MakeText (polar pd (/ pi -2) hei) (rtos (cadr txt) 2 0) hei 2 "MR")
   (MakeText (polar pd (/ pi 2) hei) (rtos (cadr txt) 2 0) hei 2 "ML"))
  (ssadd (entlast) ss)
  (setq lst (cons pt lst)))
 (close pr)
 (MakeSpline (reverse lst) 1)
 (ssadd (entlast) ss)
 (MakeLine (list (cadr (last lst)) 0) (list (caar lst) 0) 3)
 (ssadd (entlast) ss)
 (command "move" ss "" (list (cadr (last lst)) 0) pause)
 (command "undo" "e") (princ))
;----- String to List, EX: (HA:str->lst "1,2,3,4,5" ",") => (1 2 3 4 5)
(defun HA:str->lst ( str del / pos )
 (if (setq pos (vl-string-search del str))
  (cons (atof (substr str 1 pos)) (HA:str->lst (substr str (+ pos 1 (strlen del))) del)) 
  (list (atof str))))
(defun MakeSpline (lst col)
 (entmake (append (list '(0 . "SPLINE") '(100 . "AcDbEntity") (cons 62 col) '(100 . "AcDbSpline") (cons 71 3) (cons 74 (length lst))) (mapcar '(lambda (p) (cons 11 p)) lst))))
(defun MakeLine (p1 p2 col)
 (entmake (list (cons 0 "LINE") (cons 62 col) (cons 10 p1) (cons 11 p2))))
(defun MakeText (pt str hei col jus / lst)
 (setq lst (list '(0 . "TEXT") (cons 62 col) (cons 10 pt) (cons 40 hei) (cons 1 str) (cons 50 (/ pi 2))))
 (cond
  ((= jus "ML") (setq lst (append lst (list (cons 72 0) (cons 11 pt) (cons 73 2)))))
  ((= jus "MR") (setq lst (append lst (list (cons 72 2) (cons 11 pt) (cons 73 2))))))
 (entmake lst))
 

Thảo luận gốc

Lisp được chia sẻ trong chủ đề nhờ viết lisp vẽ đồ thị trên cad số liệu được lấy từ file excel. Xem đúng bài viết #323792 để đọc hướng dẫn và trao đổi thêm.

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