Jump to content

Lisp lệnh DWGCLEANUP trong chủ đề “Dọn dẹp và lưu trữ file với lisp”

Vừa viết 1 chương trình theo yêu cầu của bạn kts.ngocquan
Chương trình này cho phép bạn chọn để xóa các file trong cùng thư mục với file bản vẽ hiện hành.


Sử dụng lệnh DWGCleanUp để xóa các file trong thư mục của file bản vẽ hiện hành.

http://img.photobucket.com/albums/v672/nguyenhoanh/CVCLN_Main.gif

Màn hình xuất hiệ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 1925_dwgcleanup.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 DWGCLEANUP để chạy lisp.

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

;;; DWGCleanUp is freeware    
;;; © 2007 CADViet.com
;;; please visit [url="https://www.cadviet.com/forum"][url="https://www.cadviet.com/forum"][url="https://www.cadviet.com/forum"]https://www.cadviet.com/forum[/url][/url][/url] for more information

(defun str2lst (str / kq)
 (defun pos (sub st / l1 l2 index)
   (setq index	1
  l1	(strlen sub)
  l2	(strlen st)
   )
   (while
     (and (       (setq index (1+ index))
   )
   (if	(= sub (substr st index l1))
     index
     nil
   )
 )
 (setq kq nil)
 (while (/= str "")
   (if	(not (pos " " str))
     (setq str (strcat str " "))
   )
   (setq
     kq  (append kq (list (substr str 1 (1- (pos " " str)))))
     str (substr str (1+ (pos " " str)))
   )
 )
 (setq kq (append kq (list str)))
 (reverse (cdr (reverse kq)))
)

(defun GetfromList (info-list / fl ret dcl_id)
 (vl-load-com)  
 (setq fl (vl-filename-mktemp "mip" nil ".dcl"))
 (setq ret (open fl "w"))
 (mapcar '(lambda (x) (write-line x ret))
  (list	"mip_msg : dialog { "
	(strcat "label=\"" "CADViet Cleanup © 2007 CADViet.com" "\";")
	":text{"
	"label=\"Hay chon file de xoa: \";"
	"}"
	":list_box {"
	"alignment=top ;"
	"multiple_select=true ;"
	"width=50 ;"
	(if (> (length info-list) 26)
	  "height= 30;"
	  (strcat "height= " (itoa (+ 3 (length info-list))) ";")
	) 
	"is_tab_stop = false ;"
	"key = \"info\";}"

	":row{"
	":button{"
	"label = \"Xoa\";"
	"is_default=true;"
	"key=xoa;"
	"}"
	":button{"
	"label = \"Huy bo\";"
	"is_cancel=true;"
	"key=huybo;"
	"}"
	"}"
	"}"
  ) 
 ) 
 (setq ret (close ret))
 (if (setq dcl_id (load_dialog fl))
   (if	(new_dialog "mip_msg" dcl_id)
     (progn
(start_list "info")
(mapcar 'add_list info-list)
(end_list)
(action_tile
  "info"
  "(setq ret $value)"
)
(action_tile
  "huybo"
  "(progn(setq ret nil)(done_dialog 0))"
)
(action_tile "xoa" "(setq cochon t)(done_dialog 1)")
(start_dialog)
     ) 
   ) 
 ) 
 (unload_dialog dcl_id)
 (vl-file-delete fl)

 (if ret
   (progn
     (setq ret	(str2lst ret)
    ret	(mapcar
	  '(lambda (x)
	     (nth (atoi x) info-list)
	   )
	  ret
	)
     )
   )
 )
 ret
)

(defun tablelist (s / d r)
 (while (setq d (tblnext s (null d)))
   (setq r (cons (cdr (assoc 2 d)) r))
 )
)
(defun C:DWGCleanUp (/ *error* lay att ed blk blkdef doc)
 (vl-load-com)
 (setq	path  (getvar "dwgprefix")
flist (vl-directory-files path "*.dwg")
flist (vl-remove  (getvar "dwgname") flist)
 )

 (if (setq filenamelist (GetfromList flist))
   (progn
     (setq sofilexoa 0)
     (princ "\n-----------------------------")      
     (foreach pp filenamelist
(setq fdname (strcat (getvar "dwgprefix") pp))
(if (vl-file-delete  fdname)
  (progn (princ (strcat "\nDa xoa file: " fdname " thanh cong")) (setq sofilexoa (1+ sofilexoa)))
  (princ (strcat "\nXoa file: " fdname " that bai"))
)
     )
     (princ "\n-----------------------------")
     (princ (strcat "\nTong cong da xoa " (itoa sofilexoa) " file."))
   )
   (if cochon (alert "\nBan khong chon file nao de xoa!"))
 )
 (princ)
)
(princ "\nGoi lenh DWGCleanUp de chay!")

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Dọn dẹp và lưu trữ file với lisp. Xem đúng bài viết #1925 để đọc hướng dẫn và trao đổi thêm.

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