Jump to content

Nh? Vi?t Lisp T?a ?? Theo File ?ính Kem

Các cột trong file csv không phải lúc nào cũng phân cách bằng ","

Tham khảo Lee Mac

(setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))

 

Nếu hiepttr chưa sửa lisp thì mở file csv bằng notepad, thay "," bằng ";" rồi save lại

Để phân cột file .csv, nó phụ thuộc vào Decimal symbol. Nghĩa là khi ô màu đỏ là dấu "." thì biến (setq sep ","), khi ô màu đỏ là dấu "," thì biến (setq sep ";")

tất nhiên ô màu xanh là ăn theo (nếu Decimal symbol là "." thì Digit grouping là "," là ngược lại)

Bạn Test thử Lisp của Lee khi thay đổi Decimal symbol để kiểm nghiệm trong trường hợp này thì có lẽ Lee đã nhầm lẫn :)

;; Write CSV  -  Lee Mac
;; Writes a matrix list of cell values to a CSV file.
;; lst - [lst] list of lists, sublist is row of cell values
;; Returns T if successful, else nil
(defun LM:writecsv ( lst csv / des sep )
(if (setq des (open csv "w")) (progn
(setq sep (cond ( (vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))    
(foreach row lst (write-line (LM:lst->csv row sep) des)) (close des) t)))
;; Concatenates a row of cell values to be written to a CSV file.
;; lst - [lst] list containing row of CSV cell values
;; sep - [str] CSV separator token
(defun LM:lst->csv (lst sep) 
(if (cdr lst) (strcat (LM:csv-addquotes (car lst) sep) sep (LM:lst->csv (cdr lst) sep))
(LM:csv-addquotes (car lst) sep) )) 
(defun LM:csv-addquotes (str sep / pos) 
(cond ((wcmatch str (strcat "*[`" sep "\"]*")) (setq pos 0)    
(while (setq pos (vl-string-position 34 str pos)) (setq str (vl-string-subst "\"\"" "\"" str pos)
pos (+ pos 2)) ) (strcat "\"" str "\"") ) (str) )) ;;;;;
(defun c:test ( / ss fn in lst)
(if (and (setq ss (ssget '((0 . "POINT"))))
(setq fn (getfiled "Create Output File" "" "csv" 1)) ) (progn (repeat (setq in (sslength ss)) 
(setq lst (cons (mapcar 'rtos (cdr (assoc 10 (entget (ssname ss (setq in (1- in))))))) lst)) )
(if (LM:WriteCSV (reverse lst) fn) (startapp "explorer" fn)) )) (princ)) 

127397_csv_1.png

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 388494_test.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 TEST để chạy lisp.

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

;; Write CSV  -  Lee Mac
;; Writes a matrix list of cell values to a CSV file.
;; lst - [lst] list of lists, sublist is row of cell values
;; Returns T if successful, else nil
(defun LM:writecsv ( lst csv / des sep )
(if (setq des (open csv "w")) (progn
(setq sep (cond ( (vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))    
(foreach row lst (write-line (LM:lst->csv row sep) des)) (close des) t)))
;; Concatenates a row of cell values to be written to a CSV file.
;; lst - [lst] list containing row of CSV cell values
;; sep - [str] CSV separator token
(defun LM:lst->csv (lst sep) 
(if (cdr lst) (strcat (LM:csv-addquotes (car lst) sep) sep (LM:lst->csv (cdr lst) sep))
(LM:csv-addquotes (car lst) sep) )) 
(defun LM:csv-addquotes (str sep / pos) 
(cond ((wcmatch str (strcat "*[`" sep "\"]*")) (setq pos 0)    
(while (setq pos (vl-string-position 34 str pos)) (setq str (vl-string-subst "\"\"" "\"" str pos)
pos (+ pos 2)) ) (strcat "\"" str "\"") ) (str) )) ;;;;;
(defun c:test ( / ss fn in lst)
(if (and (setq ss (ssget '((0 . "POINT"))))
(setq fn (getfiled "Create Output File" "" "csv" 1)) ) (progn (repeat (setq in (sslength ss)) 
(setq lst (cons (mapcar 'rtos (cdr (assoc 10 (entget (ssname ss (setq in (1- in))))))) lst)) )
(if (LM:WriteCSV (reverse lst) fn) (startapp "explorer" fn)) )) (princ))

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Nhờ Viết Lisp Tọa Độ Theo File Đính Kem. Xem đúng bài viết #388494 để đọc hướng dẫn và trao đổi thêm.

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