Jump to content

Lisp lệnh QF trong chủ đề “Lisp Quick Filter: Lọc nhanh theo một số thuộc tính thông dụng”

Thay vì lệnh Filter (FI) mặc định chọn rồi xoá xoá. Lisp Quick Filter (QF) này sẽ chọn một số thuộc tính thông dụng để lọc, giảm bớt vài thao tác thừa không cần thiết:

;; List Box  -  Lee Mac
;; Displays a DCL list box allowing the user to make a selection from the supplied data.
;; msg -  Dialog label
;; lst -  List of strings to display
;; bit -  1=allow multiple; 2=return indexes
;; Returns:  List of selected items/indexes, else nil
(defun LM:listbox ( msg lst bit / dch des tmp rtn )
    (cond
        (   (not
                (and
                    (setq tmp (vl-filename-mktemp nil nil ".dcl"))
                    (setq des (open tmp "w"))
                    (write-line
                        (strcat "listbox:dialog{label=\"" msg "\";spacer;:list_box{key=\"list\";multiple_select="
                            (if (= 1 (logand 1 bit)) "true" "false") ";width=50;height=15;}spacer;ok_cancel;}"
                        )
                        des
                    )
                    (not (close des))
                    (< 0 (setq dch (load_dialog tmp)))
                    (new_dialog "listbox" dch)
                )
            )
            (prompt "\nError Loading List Box Dialog.")
        )
        (   t     
            (start_list "list")
            (foreach itm lst (add_list itm))
            (end_list)
            (setq rtn (set_tile "list" "0"))
            (action_tile "list" "(setq rtn $value)")
            (setq rtn
                (if (= 1 (start_dialog))
                    (if (= 2 (logand 2 bit))
                        (mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
                        (read (strcat "(" rtn ")"))
                      ;  (mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
                    )
                )
            )
        )
    )
    (if (< 0 dch)
        (unload_dialog dch)
    )
    (if (and tmp (setq tmp (findfile tmp)))
        (vl-file-delete tmp)
    )
    rtn
)
;====================================Main Lisp: Quick Filter (QF)======================================
(defun C:QF (/ ss ss1 ob lyrname colr blkname txth txtn lstQF lstDCL lstidx lstfi a)
  (setq ss1 (entsel "\nSelect Object: "))
  (while (or
	   (null ss1)
	   (= "" (cdr (assoc 0 (entget (car ss1)))))
	 )
  (setq ss1 (entsel "\nSelect Object Again: "))
  )
  (setq ss (entget (car ss1)))

  (setq ob (cdr (assoc 0 ss)))
  (setq lstQF (list (cons 0 ob)))
  (if (= "INSERT" ob)
  	(setq lstDCL (list (strcat "Object           : " "Block")))
	(setq lstDCL (list (strcat "Object           : " ob)))
  );endif
  (setq lyrname (cdr (assoc 8 ss)))
  (setq lstQF (append lstQF (list (cons 8 lyrname))))
  (setq lstDCL (append lstDCL (list (strcat "Layer            : " lyrname))))
  (if (= 62 (car (assoc 62 ss)))
     (progn	
    	(setq colr (cdr (assoc 62 ss)))
        (setq lstQF (append lstQF (list(cons 62 colr))))
	(setq lstDCL (append lstDCL (list (strcat "Color            : " (rtos colr 2 0)))))
     );progn
	(progn
	(setq lstQF (append lstQF '((62 . 256))))
	(setq lstDCL (append lstDCL (list (strcat "Color            : " "256"))))
	)
  )					;end if
  (if (= 2 (car (assoc 2 ss)))
    (progn	
    	(setq blkname (cdr (assoc 2 ss)))
	(setq lstQF (append lstQF (list (cons 2 blkname))))
	(setq lstDCL (append lstDCL (list (strcat "Block Name       : " blkname))))
    );end progn
  )					;end if
  (if (= 40 (car (assoc 40 ss)))
    (progn	
    	(setq txth (cdr (assoc 40 ss)))
	(setq lstQF (append lstQF (list (cons 40 txth))))
	(setq lstDCL (append lstDCL (list (strcat "Text Height      : " (rtos txth)))))
    );end progn
  )					;end if
  (if (= 7 (car (assoc 7 ss)))
    (progn	
    	(setq txtn (cdr (assoc 7 ss)))
	(setq lstQF (append lstQF (list (cons 7 txtn))))
	(setq lstDCL (append lstDCL (list (strcat "Text Style Name  : " txtn))))
    );end progn
  )					;end if
 (setq lstidx (LM:listbox "Selection to Filter:" lstDCL 1))
 (foreach a lstidx
	(setq lstfi (append lstfi (list (nth a lstQF))))
 )
(sssetfirst nil (ssget lstfi))
(Print "Write by: NghiaKieu")
(princ)
)

 

Cách dùng

  1. Tải tệp 448747_qf.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 QF để chạy lisp.

Mã nguồn 448747_qf.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/182351-lisp-quick-filter-l%E1%BB%8Dc-nhanh-theo-m%E1%BB%99t-s%E1%BB%91-thu%E1%BB%99c-t%C3%ADnh-th%C3%B4ng-d%E1%BB%A5ng/
;; List Box  -  Lee Mac
;; Displays a DCL list box allowing the user to make a selection from the supplied data.
;; msg - [str] Dialog label
;; lst - [lst] List of strings to display
;; bit - [int] 1=allow multiple; 2=return indexes
;; Returns: [lst] List of selected items/indexes, else nil
(defun LM:listbox ( msg lst bit / dch des tmp rtn )
    (cond
        (   (not
                (and
                    (setq tmp (vl-filename-mktemp nil nil ".dcl"))
                    (setq des (open tmp "w"))
                    (write-line
                        (strcat "listbox:dialog{label=\"" msg "\";spacer;:list_box{key=\"list\";multiple_select="
                            (if (= 1 (logand 1 bit)) "true" "false") ";width=50;height=15;}spacer;ok_cancel;}"
                        )
                        des
                    )
                    (not (close des))
                    (< 0 (setq dch (load_dialog tmp)))
                    (new_dialog "listbox" dch)
                )
            )
            (prompt "\nError Loading List Box Dialog.")
        )
        (   t     
            (start_list "list")
            (foreach itm lst (add_list itm))
            (end_list)
            (setq rtn (set_tile "list" "0"))
            (action_tile "list" "(setq rtn $value)")
            (setq rtn
                (if (= 1 (start_dialog))
                    (if (= 2 (logand 2 bit))
                        (mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
                        (read (strcat "(" rtn ")"))
                      ;  (mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
                    )
                )
            )
        )
    )
    (if (< 0 dch)
        (unload_dialog dch)
    )
    (if (and tmp (setq tmp (findfile tmp)))
        (vl-file-delete tmp)
    )
    rtn
)
;====================================Main Lisp: Quick Filter (QF)======================================
(defun C:QF (/ ss ss1 ob lyrname colr blkname txth txtn lstQF lstDCL lstidx lstfi a)
  (setq ss1 (entsel "\nSelect Object: "))
  (while (or
	   (null ss1)
	   (= "" (cdr (assoc 0 (entget (car ss1)))))
	 )
  (setq ss1 (entsel "\nSelect Object Again: "))
  )
  (setq ss (entget (car ss1)))

  (setq ob (cdr (assoc 0 ss)))
  (setq lstQF (list (cons 0 ob)))
  (if (= "INSERT" ob)
  	(setq lstDCL (list (strcat "Object           : " "Block")))
	(setq lstDCL (list (strcat "Object           : " ob)))
  );endif
  (setq lyrname (cdr (assoc 8 ss)))
  (setq lstQF (append lstQF (list (cons 8 lyrname))))
  (setq lstDCL (append lstDCL (list (strcat "Layer            : " lyrname))))
  (if (= 62 (car (assoc 62 ss)))
     (progn	
    	(setq colr (cdr (assoc 62 ss)))
        (setq lstQF (append lstQF (list(cons 62 colr))))
	(setq lstDCL (append lstDCL (list (strcat "Color            : " (rtos colr 2 0)))))
     );progn
	(progn
	(setq lstQF (append lstQF '((62 . 256))))
	(setq lstDCL (append lstDCL (list (strcat "Color            : " "256"))))
	)
  )					;end if
  (if (= 2 (car (assoc 2 ss)))
    (progn	
    	(setq blkname (cdr (assoc 2 ss)))
	(setq lstQF (append lstQF (list (cons 2 blkname))))
	(setq lstDCL (append lstDCL (list (strcat "Block Name       : " blkname))))
    );end progn
  )					;end if
  (if (= 40 (car (assoc 40 ss)))
    (progn	
    	(setq txth (cdr (assoc 40 ss)))
	(setq lstQF (append lstQF (list (cons 40 txth))))
	(setq lstDCL (append lstDCL (list (strcat "Text Height      : " (rtos txth)))))
    );end progn
  )					;end if
  (if (= 7 (car (assoc 7 ss)))
    (progn	
    	(setq txtn (cdr (assoc 7 ss)))
	(setq lstQF (append lstQF (list (cons 7 txtn))))
	(setq lstDCL (append lstDCL (list (strcat "Text Style Name  : " txtn))))
    );end progn
  )					;end if
 (setq lstidx (LM:listbox "Selection to Filter:" lstDCL 1))
 (foreach a lstidx
	(setq lstfi (append lstfi (list (nth a lstQF))))
 )
(sssetfirst nil (ssget lstfi))
(Print "Write by: NghiaKieu")
(princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Lisp Quick Filter: Lọc nhanh theo một số thuộc tính thông dụng. Xem đúng bài viết #448747 để đọc hướng dẫn và trao đổi thêm.

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