Jump to content

Lisp lệnh 34 12 23 trong chủ đề “lisp chuyển layer”


Đó là lệnh layiso của Autocad (nếu cad cài tools express).
Còn nếu chưa cài thì dùng lisp này:
- 12: Hiện tất cả các layer (Lệnh layon của Cad khi cài tools express)
- 23: Kích chọn đối tượng có layer cần ẩn (Lệnh layoff của Cad khi cài tools express)
- 34: Kích chọn đối tượng có layer giữ lại còn các layer khác ẩn (Lệnh layiso của Cad khi cài tools express)

Cách dùng

  1. Tải tệp 165393_34_12_23.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 34 12 23 để chạy lisp.

Mã nguồn 165393_34_12_23.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/31222-lisp-chuy%E1%BB%83n-layer/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun restore_old_error ( / )

(setq bonus_alive (- bonus_alive 1))
(if (>= bonus_alive 0)
   (b_restore_sysvars)
   (setq bonus_varlist nil)
);if
(if (<= bonus_alive 0)
   (progn
    (b_restore_undo)
    (if bonus_old_error
        (setq *error* bonus_old_error);put the old error routine back.
    );if
   );progn then
);if

(princ)
);defun restore_old_error
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun b_restore_sysvars ( / lst n a B)

(if (<= bonus_alive 0)
    (setq           lst (car bonus_varlist)
          bonus_varlist (list lst)
    );setq 
    (setq lst (last bonus_varlist)) 
);if

(setq n 0);setq
(repeat (length lst)
(setq a (nth n lst)
      b (cadr a)
      a (car a)
)
(setvar a B)
(setq n (+ n 1));setq
);repeat
(setq bonus_varlist (reverse (cdr (reverse bonus_varlist))))

);defun b_restore_sysvars

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun b_restore_undo ()

(if bonus_undoctl
   (progn
     (b_set_sysvars (list "cmdecho" 0))

     (while (equal 8 (logand 8 (getvar "undoctl")))
        (command "_.undo" "_end")
     );while

     (if (not (equal bonus_undoctl (getvar "undoctl")))
         (progn
          (cond 
           ((equal 0 bonus_undoctl) 
            (command "_.undo" "_control" "_none")
           )
           ((equal 2 (logand 2 bonus_undoctl))
            (command "_.undo" "_control" "_one")
           )	
          );;cond 
          (if (equal 4 (logand 4 bonus_undoctl))
              (command "_.undo" "_auto" "_on") 
          );if 

        );progn then restore undoctl to the status the user had it set to. 
     );if
     (if (not (equal 2 (logand 2 (getvar "undoctl"))))
         (b_restore_sysvars)
     );if
   );progn then restore undo to it's original setting
);if
(setq bonus_undoctl nil)

);defun b_restore_undo


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(Defun C:34 (/ SS CNT LAY LAYLST VAL)

 ;(init_bonus_error 
   (list
     (list "cmdecho" 0
           "expert"  0
     )
     T     ;flag. True means use undo for error clean up.  
   );list  
 ;);init_bonus_error

 (if (not (setq SS (ssget "i")))
   (progn
     (prompt "\nChon doi tuong thuoc lop muon giu lai: ")
     (setq SS (ssget))
   )
 )

 (if SS
   (progn

     (setq CNT 0)

     (while (setq LAY (ssname SS CNT))
       (setq LAY (cdr (assoc 8 (entget LAY))))
       (if (not (member LAY LAYLST))
         (setq LAYLST (cons LAY LAYLST))
       )
       (setq CNT (1+ CNT))
     )

     (if (member (getvar "CLAYER") LAYLST)
       (setq LAY (getvar "CLAYER"))
       (setvar "CLAYER" (setq LAY (last LAYLST)))
     )

     (command "_.-LAYER" "_OFF" "*" "_Y")
     (foreach VAL LAYLST (command "_ON" VAL))
     (command "")

     (if (= (length LAYLST) 1)
       (prompt (strcat "\nLop " (car LAYLST) " duoc giu lai va la lop hien hanh."))
       (prompt (strcat "\n" (itoa (length LAYLST)) " Lop duoc giu lai. "
                       "Lop " LAY " la lop hien hanh."
               )
       )
     )
   )
 )

 (restore_old_error)

 (princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(Defun C:12 ()

 ;(init_bonus_error 
   (list
     (list "cmdecho" 0)
     nil     ;flag. True means use undo for error clean up.  
   );list  
 ;);init_bonus_error

 (Command "_.-LAYER" "_ON" "*" "")
 (prompt "\nTat ca cac lop trong ban ve da duoc mo.")

 (restore_old_error)

 (princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun C:23 ()
 (layproc "off")
 (princ)
)
(defun LAYPROC ( TASK / NOEXIT OPT BLKLST CNT EN PMT ANS LAY NEST BLKLST)


; --------------------- Error initialization ---------------------

 ;(init_bonus_error 
   (list
     (list "cmdecho" 0
           "expert"  0
     )

     nil     ;flag. True means use undo for error clean up.  
   );list  
 ;);init_bonus_error

; -------------------- Variable initialization -------------------

 (setq NOEXIT T)

 (setq OPT (getcfg (strcat "AppData/AC_Bonus/Lay" TASK)))    ; get default option setting
 (if (not (or (null OPT) (= OPT ""))) (setq OPT (atoi OPT)))

 (setq CNT 0)                                                ; cycle counter


 (while NOEXIT

   (initget "Options Undo")
   (if (= TASK "off")
     (setq EN (nentsel "\nOptions/Undo/<Chon doi tuong thuoc lop muom tat>: "))
     (setq EN (nentsel "\nOptions/Undo/<Pick an object on the layer to be FROZEN>: "))
   )

; ------------------------- Set Options --------------------------

   (While (= EN "Options")
     (initget "No Block Entity")
     (cond
       ((= OPT 1)
         (setq PMT "\nBlock level nesting/Entity level nesting/<No nesting>: ")
       )
       ((= OPT 2)
         (setq PMT "\nBlock level nesting/No nesting/<Entity level nesting>: ")
       )
       (T
         (setq PMT "\nEntity level nesting/No nesting/<Block level nesting>: ")
       )
     )
     (setq ANS (getkword PMT))

     (cond
       ((null ANS)
         (if (or (null OPT) (= OPT ""))
           (progn
             (print ANS)
             (setq OPT 3)
             (setcfg (strcat "AppData/AC_Bonus/Lay" TASK) "3")
           )
         )
       )
       ((= ANS "No")
         (setq OPT 1)
         (setcfg (strcat "AppData/AC_Bonus/Lay" TASK) "1")
       )
       ((= ANS "Entity")
         (setq OPT 2)
         (setcfg (strcat "AppData/AC_Bonus/Lay" TASK) "2")
       )
       (T
         (setq OPT 3)
         (setcfg (strcat "AppData/AC_Bonus/Lay" TASK) "3")
       )
     )

     (initget "Options")
     (if (= TASK "off")
       (setq EN (nentsel "\nOptions/Undo/<Chon doi tuong thuoc lop muon tat>: "))
       (setq EN (nentsel "\nOptions/Undo/<Pick an object on the layer to be FROZEN>: "))
     )
   )

; ------------------------- Find Layer ---------------------------

   (if (and EN (not (= EN "Undo")))
     (progn

       (setq BLKLST (last EN))
       (setq NEST (length BLKLST))

       (cond

     ; If the entity is not nested or if the option for entity
     ; level nesting is selected.

         ((or (= OPT 2) (< (length EN) 3))
           (setq LAY (entget (car EN)))
         )

     ; If no nesting is desired

         ((= OPT 1)
           (setq LAY (entget (car (reverse BLKLST))))
         )

     ; All other cases (default)

         (T
           (setq BLKLST (reverse BLKLST))

           (while (and                         ; strip out xrefs
               ( > (length BLKLST) 0)
               (assoc 1 (tblsearch "BLOCK" (cdr (assoc 2 (entget (car BLKLST))))))
                  );and
             (setq BLKLST (cdr BLKLST))
           )
           (if ( > (length BLKLST) 0)          ; if there is a block present
             (setq LAY (entget (car BLKLST)))  ; use block layer
             (setq LAY (entget (car EN)))      ; else use layer of nensel
           )
         )
       )

; ------------------------ Process Layer -------------------------

       (setq LAY (cdr (assoc 8 LAY)))

       (if (= LAY (getvar "CLAYER"))
         (if (= TASK "off")
           (progn
             (prompt (strcat "\nDoi tuong thuoc lop " LAY " (day la lop hien hanh) ban co muon tat khong? Co/<Khong>: "))
             (setq ANS (strcase (getstring)))
             (if (not (or (= ANS "C") (= ANS "CO") (= ANS "c") (= ANS "co")))
               (setq LAY nil)
             )
           )
           (progn
             (prompt (strcat "\nCannot freeze layer " LAY".  It is the CURRENT layer."))
             (setq LAY nil)
           )
         )
         (setq ANS nil)
       )

       (if LAY
         (if (= TASK "off")
           (progn
             (if ANS
               (command "_.-LAYER" "_OFF" LAY "_Yes" "")
               (command "_.-LAYER" "_OFF" LAY "")
             )
             (prompt (strcat "\nLop " LAY " da duoc tat."))
             (setq CNT (1+ CNT))
           )
           (progn
             (command "_.-LAYER" "_FREEZE" LAY "")
             (prompt (strcat "\nLayer " LAY " has been frozen."))
             (setq CNT (1+ CNT))
           )
         )
       )
     )

; -------------- Nothing selected or Undo selected ---------------

     (progn
       (if (= EN "Undo")
         (if (> CNT 0)
           (progn
             (command "_.u")
             (setq CNT (1- CNT))
           )
           (prompt "\nEverything has been undone.")
         )
         (setq NOEXIT nil)
       )
     )
   )
 )

 (restore_old_error)

)           

Thảo luận gốc

Lisp được chia sẻ trong chủ đề lisp chuyển layer. Xem đúng bài viết #165393 để đọc hướng dẫn và trao đổi thêm.

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