Jump to content

Lisp lệnh E2P trong chủ đề “Chuyển tọa độ điểm từ file excel sang cad”

Nếu không quen dùng *.dll bạn có thể dùng Lisp này...

Gõ lệnh: e2p xuất hiện hộp thoại, sau đó bạn chọn file excel có chứa các tập hợp điểm của bạn.

 

(defun Add_point(ExcelFile / xlapp ex-wb CurReg sheet MaxRow cell ri mspace xx yy)
(VL-LOAD-COM)
(setq xlapp (vlax-create-object "Excel.Application"))
;(setq ex (vlax-put-property xlapp "Visible" nil))
(setq ex-wb (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Open ExcelFile))
(setq CurReg (vlax-get-property (vlax-get-property
(vlax-get-property xlapp "ActiveSheet") "Range" "A1") "CurrentRegion")
)
(setq MaxRow (vlax-get-property (vlax-get-property CurReg "Rows") "Count"))
(setq sheet (vlax-get-property ex-wb "ActiveSheet"))
(setq cell (vlax-get-property sheet "Cells"))
(setq ri 2)
(setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))
(while (<= ri MaxRow)
(setq xx (vlax-variant-value (vlax-get-property (vlax-variant-value (vlax-get-property cell "item" ri 1)) 'Value2)))
(setq yy (vlax-variant-value (vlax-get-property (vlax-variant-value (vlax-get-property cell "item" ri 2)) 'Value2)))
(vla-AddPoint mspace (vlax-3d-point xx yy))
(setq ri (1+ ri))
)
(vlax-invoke-method (vlax-get-property xlapp "ActiveWorkbook") 'Close :vlax-False)
(vlax-invoke-method xlapp 'Quit)
(vlax-release-object xlapp)(gc)
(setq xlapp nil)
(princ)
(princ)
)
(defun c:e2p(/ ExcelFile)
(setq ExcelFile (getfiled "Ch\U+1ECDn File Excel" (getvar "DWGPREFIX") "Xlsx;Xls" 8))
(if ExcelFile (Add_point ExcelFile))
)

Cách dùng

  1. Tải tệp 232929_e2p.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 E2P để chạy lisp.

Mã nguồn 232929_e2p.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/70542-chuy%E1%BB%83n-t%E1%BB%8Da-%C4%91%E1%BB%99-%C4%91i%E1%BB%83m-t%E1%BB%AB-file-excel-sang-cad/
(defun Add_point(ExcelFile / xlapp ex-wb CurReg sheet MaxRow cell ri mspace xx yy)(VL-LOAD-COM)(setq xlapp (vlax-create-object "Excel.Application"));(setq ex (vlax-put-property xlapp "Visible" nil))(setq ex-wb (vlax-invoke-method (vlax-get-property xlapp 'WorkBooks) 'Open ExcelFile))(setq CurReg (vlax-get-property (vlax-get-property(vlax-get-property xlapp "ActiveSheet") "Range" "A1") "CurrentRegion"))(setq MaxRow (vlax-get-property (vlax-get-property CurReg "Rows") "Count"))(setq sheet (vlax-get-property ex-wb "ActiveSheet"))(setq cell (vlax-get-property sheet "Cells"))(setq ri 2)(setq mspace (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))))(while (<= ri MaxRow)(setq xx (vlax-variant-value (vlax-get-property (vlax-variant-value (vlax-get-property cell "item" ri 1)) 'Value2)))(setq yy (vlax-variant-value (vlax-get-property (vlax-variant-value (vlax-get-property cell "item" ri 2)) 'Value2)))(vla-AddPoint mspace (vlax-3d-point xx yy))(setq ri (1+ ri)))(vlax-invoke-method (vlax-get-property xlapp "ActiveWorkbook") 'Close :vlax-False)(vlax-invoke-method xlapp 'Quit)(vlax-release-object xlapp)(gc)(setq xlapp nil)(princ)(princ))(defun c:e2p(/ ExcelFile)(setq ExcelFile (getfiled "Ch\U+1ECDn File Excel" (getvar "DWGPREFIX") "Xlsx;Xls" 8))(if ExcelFile (Add_point ExcelFile)))

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Chuyển tọa độ điểm từ file excel sang cad. Xem đúng bài viết #232929 để đọc hướng dẫn và trao đổi thêm.

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