Jump to content

Lisp lệnh 3 1 2 trong chủ đề “Lisp hay bị lỗi, tự động undo n lần ???”



Sửa rồi như sau:

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 65364_3_1_2.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 3 1 2 để chạy lisp.

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

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(Defun C:3 (/ SS CNT LAY LAYLST VAL)
 (list  (list "cmdecho" 0 "expert" 0 )  T) ;flag. True means use undo for error clean up. 
 (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:1 ()

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

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

(restore_old_error)

(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun C:2 ()
 (layproc "off")
 (princ)
)

(defun LAYPROC (TASK / NOEXIT OPT BLKLST CNT EN PMT ANS LAY NEST BLKLST)
 (list (list "cmdecho" 0 "expert" 0 )  nil);flag. True means use undo for error clean up. 

				; -------------------- 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/: "))
     (setq EN (nentsel "\nOptions/Undo/: "))
   )

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

   (While (= EN "Options")
     (initget "No Block Entity")
     (cond
((= OPT 1)
 (setq PMT "\nBlock level nesting/Entity level nesting/: ")
)
((= OPT 2)
 (setq PMT "\nBlock level nesting/No nesting/: ")
)
(T
 (setq PMT "\nEntity level nesting/No 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/: "))
(setq EN (nentsel "\nOptions/Undo/: "))
     )
   )

; ------------------------- 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/: "
	)
      )
      (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 hay bị lỗi, tự động undo n lần ???. Xem đúng bài viết #65364 để đọc hướng dẫn và trao đổi thêm.

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