Jump to content

Lisp lệnh EXPPNT IMPPNT trong chủ đề “AutoCAD với Excel”

Tại trang tin cũng đã có 1 ví dụ về chương trình LISP nhập xuất dữ liệu giữa AutoCAD và Excel.

https://www.cadviet.com/content/view/20/1/

 

Đó là chương trình eiPoint (Export Import Point), là chương trình khá đơn giản và cơ bản. Chúng ta có thể cải tiến nó để đáp ứng được từng mục đích riêng của mình.

 

Dưới đây là mã lệnh của chương trình eiPoint (lấy từ trang tin):

(defun c:exppnt	(/ fn fid i sdt tp ent p x y z)
 (princ "\neiPoint © 2007 Cadviet.com")
 (setq
   ssp	(ssget '((0 . "POINT")))
   fn	(getfiled "Ten tep de xuat toa do x y z"
	  (getvar "dwgprefix")
	  "txt"
	  1
)
   i	0
   sdt	(cond (ssp (sslength ssp))
      (t 0)
)
   tp	nil
   fid	(open fn "w")
 )

 (repeat sdt
   (setq ent (ssname ssp i)
  i   (1+ i)
  p   (cdr (assoc 10 (entget ent)))
  x   (car p)
  y   (cadr p)
  z   (caddr p)
   )
   (princ (strcat (rtos x) "\t" (rtos y) "\t" (rtos z) "\n")
   fid
   )
 )
 (close fid)
 (princ)
)

(defun c:imppnt	( / fn fid strht vt x y z)
 (defun pos (sub st / l1 l2 index)
   (setq index	1
  l1	(strlen sub)
  l2	(strlen st)
   )
   (while
     (and (<= (+ index l1 -1) l2) (/= sub (substr st index l1)))
      (setq index (1+ index))
   )
   (if	(= sub (substr st index l1))
     index
     nil
   )
 )
 (princ "\neiPoint © 2007 Cadviet.com")
 (setq	fn  (getfiled "Ten tep de nhap toa do x y z"
	      (getvar "dwgprefix")
	      "txt"
	      2
    )
fid (open fn "r")

 )
 (while (setq strht (read-line fid))
   (setq strht (strcat strht "\t\t\t"))
   (if	(/= strht "   ")
     (progn
(setq
  vt	(pos "\t" strht)
  x	(atof (substr strht 1 (1- vt)))
  strht	(substr strht (1+ vt))
  vt	(pos "\t" strht)
  y	(atof (substr strht 1 (1- vt)))
  strht	(substr strht (1+ vt))
  vt	(pos "\t" strht)
  z	(atof (substr strht 1 (1- vt)))
)
(entmake (list
	   (cons 0 "POINT")
	   (list 10 x y z)
	 )
)
     )
   )
 )
 (close fid)
)
(princ "\neiPoint da duoc nap. su dung 2 lenh: ExpPnt va ImpPnt de xuat va nhap Point")

 

Bản chất của quá trình trao đổi dữ liệu giữa AutoCAD và Excel trong ví dụ trên là tạo ra một tệp có đuôi .txt làm trung gian.

 

 

Trước hết mình xin chân thành cám ơn bạn "nguyen hoanh" vì sự nhiệt tình và sẳn sàng chia sẽ. Nhưng vấn đề là mình muốn làm việc trực tiếp vào các file Excel, không muố nhờ vào tep trung gian (*.txt). Xin bạn vui lòng hương dẫn.

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

  1. Chép mã nguồn, lưu thành tệp 1262_exppnt_imppnt.lsp.
  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 EXPPNT IMPPNT để chạy lisp.

Mã nguồn 1262_exppnt_imppnt.lsp (trích từ bài viết gốc)

(defun c:exppnt	(/ fn fid i sdt tp ent p x y z)
 (princ "\neiPoint © 2007 Cadviet.com")
 (setq
   ssp	(ssget '((0 . "POINT")))
   fn	(getfiled "Ten tep de xuat toa do x y z"
	  (getvar "dwgprefix")
	  "txt"
	  1
)
   i	0
   sdt	(cond (ssp (sslength ssp))
      (t 0)
)
   tp	nil
   fid	(open fn "w")
 )

 (repeat sdt
   (setq ent (ssname ssp i)
  i   (1+ i)
  p   (cdr (assoc 10 (entget ent)))
  x   (car p)
  y   (cadr p)
  z   (caddr p)
   )
   (princ (strcat (rtos x) "\t" (rtos y) "\t" (rtos z) "\n")
   fid
   )
 )
 (close fid)
 (princ)
)

(defun c:imppnt	( / fn fid strht vt x y z)
 (defun pos (sub st / l1 l2 index)
   (setq index	1
  l1	(strlen sub)
  l2	(strlen st)
   )
   (while
     (and (<= (+ index l1 -1) l2) (/= sub (substr st index l1)))
      (setq index (1+ index))
   )
   (if	(= sub (substr st index l1))
     index
     nil
   )
 )
 (princ "\neiPoint © 2007 Cadviet.com")
 (setq	fn  (getfiled "Ten tep de nhap toa do x y z"
	      (getvar "dwgprefix")
	      "txt"
	      2
    )
fid (open fn "r")

 )
 (while (setq strht (read-line fid))
   (setq strht (strcat strht "\t\t\t"))
   (if	(/= strht "   ")
     (progn
(setq
  vt	(pos "\t" strht)
  x	(atof (substr strht 1 (1- vt)))
  strht	(substr strht (1+ vt))
  vt	(pos "\t" strht)
  y	(atof (substr strht 1 (1- vt)))
  strht	(substr strht (1+ vt))
  vt	(pos "\t" strht)
  z	(atof (substr strht 1 (1- vt)))
)
(entmake (list
	   (cons 0 "POINT")
	   (list 10 x y z)
	 )
)
     )
   )
 )
 (close fid)
)
(princ "\neiPoint da duoc nap. su dung 2 lenh: ExpPnt va ImpPnt de xuat va nhap Point")

Thảo luận gốc

Lisp được chia sẻ trong chủ đề AutoCAD với Excel. Xem đúng bài viết #1262 để đọc hướng dẫn và trao đổi thêm.

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