Jump to content

Lisp lệnh KKO trong chủ đề “Nhờ viết LISP xuất dữ liệu ra file excel”

- hi mấy lsp dạng này trên 4rum mình nhiều lắm, tết rãnh nhoc luyện viết thử xem ^^


;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/119469-nho-viet-lisp-xuat-du-lieu-ra-file-excel/
(defun ss2ent (ss / i Le e) ;;;Convert ss to list of ename
(setq i 0)
(repeat (sslength ss)     
(setq e (ssname ss i)        
Le (append Le (list e))        
i (1+ i)    ))
Le)
;=================================================================================
(defun Length1(e) 
(vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)))
;=======================================================================
(defun c:kko (/ ss ds_ent ds_laydis layer dis info)
(prompt "Quet chon cac Multiline ")
(setq ss (ssget '( (0 . "MLINE"))))
(if ss 
	(progn 
		(setq ds_ent (ss2ent ss))
			(foreach k ds_ent
				(setq info (entget k))
				(setq layer (acet-dxf 8 info))
				(setq dis (add_mline info))
				(setq ds_laydis (append ds_laydis (list (list layer dis))))
			)
		)
)
(xls ds_laydis '("LAYER" "CHIEU DAI") nil "Thong ke")
;(princ ds_laydis)
(princ)
)
;==============================================================================================================================================
(vl-load-com)
(defun xls (Data-list	   header	  Colhide	 Name_list
	    /		   *aplexcel*	  *books-colection*
	    Currsep	   *excell-cells* *new-book*	 *sheet#1*
	    *sheet-collection*		  col		 iz_listo
	    row		   cell		  cols
	   )
  (defun Letter	(N / Res TMP)
    (setq Res "")
    (while (> N 0)
      (setq TMP	(rem N 26)
	    TMP	(if (zerop TMP)
		  (setq	N   (1- N)
			TMP 26
		  )
		  TMP
		)
	    Res	(strcat (chr (+ 64 TMP)) Res)
	    N	(/ N 26)
      )
    )
    Res
  )
  (if (null Name_list)
    (setq Name_list "")
  )
  (setq *AplExcel* (vlax-get-or-create-object "Excel.Application"))
  (if (setq *New-Book* (vlax-get-property *AplExcel* "ActiveWorkbook"))
    (setq *Books-Colection*  (vlax-get-property *AplExcel* "Workbooks")
	  *Sheet-Collection* (vlax-get-property *New-Book* "Sheets")
	  *Sheet#1*	     (vlax-invoke-method *Sheet-Collection* "Add")
    )
    (setq *Books-Colection*  (vlax-get-property *AplExcel* "Workbooks")
	  *New-Book*	     (vlax-invoke-method *Books-Colection* "Add")
	  *Sheet-Collection* (vlax-get-property *New-Book* "Sheets")
	  *Sheet#1*	     (vlax-get-property *Sheet-Collection* "Item" 1)
    )
  )
  (setq *excell-cells* (vlax-get-property *Sheet#1* "Cells"))
  (setq	Name_list (if (= Name_list "")
		    (vl-filename-base (getvar "DWGNAME"))
		    (strcat (vl-filename-base (getvar "DWGNAME"))
			    "&"
			    Name_list
		    )
		  )
	col	  0
	cols	  nil
  )
  (if (> (strlen Name_list) 26)
    (setq Name_list
	   (strcat (substr Name_list 1 10)
		   "..."
		   (substr Name_list (- (strlen Name_list) 13) 14)
	   )
    )
  )
  (vlax-for sh *Sheet-Collection*
    (setq cols (cons (strcase (vlax-get-property sh 'Name)) cols))
  )
  (setq row Name_list)
  (while (member (strcase row) cols)
    (setq row (strcat Name_list " (" (itoa (setq col (1+ col))) ")"))
  )
  (setq Name_list row)
  (vlax-put-property *Sheet#1* 'Name Name_list)
  (setq Currsep (vlax-get-property *AplExcel* "UseSystemSeparators"))
  (vlax-put-property
    *AplExcel*
    "UseSystemSeparators"
    :vlax-false
  ) ;_?? ???????????? ????????? ?????????
  (vlax-put-property *AplExcel* "DecimalSeparator" ".") ;_??????????? ??????? ? ????? ?????
  (vlax-put-property *AplExcel* "ThousandsSeparator" " ") ;_??????????? ???¤???
  (vla-put-visible *AplExcel* :vlax-true)
  (setq	row 1
	col 1
  )
  (if (null header)
    (setq header '("X" "Y" "Z"))
  )
  (repeat (length header)
    (vlax-put-property
      *excell-cells*
      "Item"
      row
      col
      (vl-princ-to-string (nth (1- col) header))
    )
    (setq col (1+ col))
  )
  (setq	row 2
	col 1
  )
  (repeat (length Data-list)
    (setq iz_listo (car Data-list))
    (repeat (length iz_listo)
      (vlax-put-property
	*excell-cells*
	"Item"
	row
	col
	(vl-princ-to-string (car iz_listo))
      )
      (setq iz_listo (cdr iz_listo)
	    col	     (1+ col)
      )
    )
    (setq Data-list (cdr Data-list))
    (setq col 1
	  row (1+ row)
    )
  )
  (setq	col (1+ (length header))
	row (1+ row)
  )
  (setq	cell (vlax-variant-value
	       (vlax-invoke-method
		 *Sheet#1*
		 "Evaluate"
		 (strcat "A1:" (letter col) (itoa row))
	       )
	     )
  ) ;_ end of setq
  (setq cols (vlax-get-property cell 'Columns))
  (vlax-invoke-method cols 'Autofit)
  (vlax-release-object cols)
  (vlax-release-object cell)
  (foreach item	ColHide
    (if	(numberp item)
      (setq item (letter item))
    )
    (setq cell (vlax-variant-value
		 (vlax-invoke-method
		   *Sheet#1*
		   "Evaluate"
		   (strcat item "1:" item "1")
		 )
	       )
    )
    (setq cols (vlax-get-property cell 'Columns))
    (vlax-put-property cols 'hidden 1)
    (vlax-release-object cols)
    (vlax-release-object cell)
  )
  (vlax-put-property *AplExcel* "UseSystemSeparators" Currsep)
  (mapcar 'vlax-release-object
	  (list	*excell-cells*	    *Sheet#1*
		*Sheet-Collection*  *New-Book*
		*Books-Colection*   *AplExcel*
	       )
  )
  (setq *AplExcel* nil)
  (gc)
  (gc)
  (princ)
)
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;https://www.cadviet.com/forum/topic/763-routine-tinh-tong-chieu-dai-cac-doi-tuong/
(defun add_mline ( elist / pt1 mline_len pt2 tot_len)
(setq tot_len 0.0)
  (foreach k	elist
    (cond ((= 10 (car k))
	   (setq pt1	   (cdr k)
		 mline_len 0.0
	   )
	  )
	  ((= 11 (car k))
	   (setq pt2	   (cdr k)
		 mline_len (+ mline_len (distance pt2 pt1))
		 pt1	   pt2
	   )
	  )
    )
  )
  (setq tot_len (+ tot_len mline_len))
 )

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 334067_kko.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 KKO để chạy lisp.

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

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/119469-nho-viet-lisp-xuat-du-lieu-ra-file-excel/
(defun ss2ent (ss / i Le e) ;;;Convert ss to list of ename
(setq i 0)
(repeat (sslength ss)     
(setq e (ssname ss i)        
Le (append Le (list e))        
i (1+ i)    ))
Le)
;=================================================================================
(defun Length1(e) 
(vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)))
;=======================================================================
(defun c:kko (/ ss ds_ent ds_laydis layer dis info)
(prompt "Quet chon cac Multiline ")
(setq ss (ssget '( (0 . "MLINE"))))
(if ss 
	(progn 
		(setq ds_ent (ss2ent ss))
			(foreach k ds_ent
				(setq info (entget k))
				(setq layer (acet-dxf 8 info))
				(setq dis (add_mline info))
				(setq ds_laydis (append ds_laydis (list (list layer dis))))
			)
		)
)
(xls ds_laydis '("LAYER" "CHIEU DAI") nil "Thong ke")
;(princ ds_laydis)
(princ)
)
;==============================================================================================================================================
(vl-load-com)
(defun xls (Data-list	   header	  Colhide	 Name_list
	    /		   *aplexcel*	  *books-colection*
	    Currsep	   *excell-cells* *new-book*	 *sheet#1*
	    *sheet-collection*		  col		 iz_listo
	    row		   cell		  cols
	   )
  (defun Letter	(N / Res TMP)
    (setq Res "")
    (while (> N 0)
      (setq TMP	(rem N 26)
	    TMP	(if (zerop TMP)
		  (setq	N   (1- N)
			TMP 26
		  )
		  TMP
		)
	    Res	(strcat (chr (+ 64 TMP)) Res)
	    N	(/ N 26)
      )
    )
    Res
  )
  (if (null Name_list)
    (setq Name_list "")
  )
  (setq *AplExcel* (vlax-get-or-create-object "Excel.Application"))
  (if (setq *New-Book* (vlax-get-property *AplExcel* "ActiveWorkbook"))
    (setq *Books-Colection*  (vlax-get-property *AplExcel* "Workbooks")
	  *Sheet-Collection* (vlax-get-property *New-Book* "Sheets")
	  *Sheet#1*	     (vlax-invoke-method *Sheet-Collection* "Add")
    )
    (setq *Books-Colection*  (vlax-get-property *AplExcel* "Workbooks")
	  *New-Book*	     (vlax-invoke-method *Books-Colection* "Add")
	  *Sheet-Collection* (vlax-get-property *New-Book* "Sheets")
	  *Sheet#1*	     (vlax-get-property *Sheet-Collection* "Item" 1)
    )
  )
  (setq *excell-cells* (vlax-get-property *Sheet#1* "Cells"))
  (setq	Name_list (if (= Name_list "")
		    (vl-filename-base (getvar "DWGNAME"))
		    (strcat (vl-filename-base (getvar "DWGNAME"))
			    "&"
			    Name_list
		    )
		  )
	col	  0
	cols	  nil
  )
  (if (> (strlen Name_list) 26)
    (setq Name_list
	   (strcat (substr Name_list 1 10)
		   "..."
		   (substr Name_list (- (strlen Name_list) 13) 14)
	   )
    )
  )
  (vlax-for sh *Sheet-Collection*
    (setq cols (cons (strcase (vlax-get-property sh 'Name)) cols))
  )
  (setq row Name_list)
  (while (member (strcase row) cols)
    (setq row (strcat Name_list " (" (itoa (setq col (1+ col))) ")"))
  )
  (setq Name_list row)
  (vlax-put-property *Sheet#1* 'Name Name_list)
  (setq Currsep (vlax-get-property *AplExcel* "UseSystemSeparators"))
  (vlax-put-property
    *AplExcel*
    "UseSystemSeparators"
    :vlax-false
  ) ;_?? ???????????? ????????? ?????????
  (vlax-put-property *AplExcel* "DecimalSeparator" ".") ;_??????????? ??????? ? ????? ?????
  (vlax-put-property *AplExcel* "ThousandsSeparator" " ") ;_??????????? ???¤???
  (vla-put-visible *AplExcel* :vlax-true)
  (setq	row 1
	col 1
  )
  (if (null header)
    (setq header '("X" "Y" "Z"))
  )
  (repeat (length header)
    (vlax-put-property
      *excell-cells*
      "Item"
      row
      col
      (vl-princ-to-string (nth (1- col) header))
    )
    (setq col (1+ col))
  )
  (setq	row 2
	col 1
  )
  (repeat (length Data-list)
    (setq iz_listo (car Data-list))
    (repeat (length iz_listo)
      (vlax-put-property
	*excell-cells*
	"Item"
	row
	col
	(vl-princ-to-string (car iz_listo))
      )
      (setq iz_listo (cdr iz_listo)
	    col	     (1+ col)
      )
    )
    (setq Data-list (cdr Data-list))
    (setq col 1
	  row (1+ row)
    )
  )
  (setq	col (1+ (length header))
	row (1+ row)
  )
  (setq	cell (vlax-variant-value
	       (vlax-invoke-method
		 *Sheet#1*
		 "Evaluate"
		 (strcat "A1:" (letter col) (itoa row))
	       )
	     )
  ) ;_ end of setq
  (setq cols (vlax-get-property cell 'Columns))
  (vlax-invoke-method cols 'Autofit)
  (vlax-release-object cols)
  (vlax-release-object cell)
  (foreach item	ColHide
    (if	(numberp item)
      (setq item (letter item))
    )
    (setq cell (vlax-variant-value
		 (vlax-invoke-method
		   *Sheet#1*
		   "Evaluate"
		   (strcat item "1:" item "1")
		 )
	       )
    )
    (setq cols (vlax-get-property cell 'Columns))
    (vlax-put-property cols 'hidden 1)
    (vlax-release-object cols)
    (vlax-release-object cell)
  )
  (vlax-put-property *AplExcel* "UseSystemSeparators" Currsep)
  (mapcar 'vlax-release-object
	  (list	*excell-cells*	    *Sheet#1*
		*Sheet-Collection*  *New-Book*
		*Books-Colection*   *AplExcel*
	       )
  )
  (setq *AplExcel* nil)
  (gc)
  (gc)
  (princ)
)
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;https://www.cadviet.com/forum/topic/763-routine-tinh-tong-chieu-dai-cac-doi-tuong/
(defun add_mline ( elist / pt1 mline_len pt2 tot_len)
(setq tot_len 0.0)
  (foreach k	elist
    (cond ((= 10 (car k))
	   (setq pt1	   (cdr k)
		 mline_len 0.0
	   )
	  )
	  ((= 11 (car k))
	   (setq pt2	   (cdr k)
		 mline_len (+ mline_len (distance pt2 pt1))
		 pt1	   pt2
	   )
	  )
    )
  )
  (setq tot_len (+ tot_len mline_len))
 )

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Nhờ viết LISP xuất dữ liệu ra file excel. Xem đúng bài viết #334067 để đọc hướng dẫn và trao đổi thêm.

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