CXATTVIS圖塊與屬性版本 1.0.0已在 AutoCAD 中測試
批次顯示或隱藏圖塊屬性
一步讓多個圖塊的屬性可見或不可見,可以針對一個標籤,也可以針對全部。
如何使用
- 輸入 CXATTVIS,選擇圖塊。
- 選擇 Show 或 Hide。
- 輸入標籤;全部屬性輸入 *。
請注意
- 屬性的值不會被改動,只是改變是否顯示。
在 AutoCAD 中載入
- 把檔案存在任意位置。
- 輸入 APPLOAD,選擇檔案,按「載入」。
- 輸入指令名稱:CXATTVIS。
想每次啟動都可用,在同一個對話方塊中加入「啟動套件」,或用選用的載入器一次載入整個資料夾。 關於選用的載入器
已在 AutoCAD(完整版)中測試。其他能執行 AutoLISP 的軟體也許可用,但未經測試。
程式碼
純文字,沒有任何隱藏內容。複製後存成名為 cxattvis.lsp 的檔案,或直接下載。
;;; ============================================================================
;;; CXATTVIS - Show or hide block attributes
;;; Version 1.0.0
;;; CADXChange LISP tools - https://cadxchange.net/resources/lisp/cxattvis
;;; Tested in: AutoCAD (full version). Other programs that run AutoLISP may work but are not tested.
;;; Copyright (c) CADXChange. Free for personal and commercial use; do not resell or
;;; republish it as a collection. Terms: https://cadxchange.net/legal/lisp-terms
;;; ============================================================================
;; Show or hide attributes of the block references in NAMES. TAG is a tag name or "*" for every attribute.
;; SHOW = T shows, nil hides. Returns how many attributes were changed.
(defun cx:attvis-run (names tag show / n flags)
(setq n 0)
(foreach e names
(foreach a (cx:attribs e)
(if (or (= tag "*") (= (strcase (cx:dxf 2 (entget a))) (strcase tag)))
(progn
(setq flags (cx:dxf 70 (entget a)))
(cx:upd a 70 (if show (boole 1 flags -2) (boole 7 flags 1)))
(setq n (1+ n)))))
(entupd e))
n)
(defun c:CXATTVIS ( / *error* cx:sv ss names tags tag act n)
(setq *error* cx:err)
(if (setq ss (cx:pick "\nSelect blocks with attributes: " '((0 . "INSERT") (66 . 1))))
(progn
(setq names (cx:ss->list ss)
tags (cx:tags names))
(princ (strcat "\nAttributes in the selection: " (cx:join tags ", ")))
(setq act (cx:ask-kw "Attributes" "Show Hide" "Hide")
tag (cx:ask-str "Attribute tag (* for all)" "*"))
(cx:start '(("CMDECHO" . 0)))
(setq n (cx:attvis-run names tag (= act "Show")))
(princ (strcat "\n" (itoa n) " attribute(s) " (if (= act "Show") "shown" "hidden") "."))
(cx:end)))
(princ))
;;; ---- Shared helpers (used by the command above; identical in every CADXChange tool) ----
(defun cx:dxf (code ed)
(cdr (assoc code ed)))
;; The ATTRIB entities of a block reference, in order.
(defun cx:attribs (ins / e out)
(setq e (entnext ins))
(while (and e (= (cx:dxf 0 (entget e)) "ATTRIB"))
(setq out (cons e out) e (entnext e)))
(reverse out))
;; Change one group of an entity: (cx:upd ename 8 "LAYER1"); the group is added when missing.
(defun cx:upd (ename code value / ed old)
(setq ed (entget ename) old (assoc code ed))
(entmod (if old (subst (cons code value) old ed) (append ed (list (cons code value)))))
(entupd ename))
;; Close the undo step and put the system variables back.
(defun cx:end ()
(foreach v cx:sv (setvar (car v) (cdr v)))
(command "_.UNDO" "_End")
(setq cx:sv nil)
(princ))
;; Error handler: restores everything, stays quiet on Esc.
(defun cx:err (msg)
(if (> (getvar "CMDACTIVE") 0) (command))
(cx:end)
(if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
(princ (strcat "\nError: " msg)))
(princ))
;; Use the objects already selected, or ask the user to select; FILTER is an ssget filter list or nil.
(defun cx:pick (msg filter / ss)
(setq ss (if filter (ssget "_I" filter) (ssget "_I")))
(if (null ss)
(progn
(princ msg)
(setq ss (if filter (ssget filter) (ssget)))))
ss)
;; Selection set to a list of entity names.
(defun cx:ss->list (ss / i out)
(setq i 0)
(if ss
(repeat (sslength ss)
(setq out (cons (ssname ss i) out)
i (1+ i))))
(reverse out))
;; The attribute tags found in a list of block references, each once, in order of appearance.
(defun cx:tags (names / out)
(foreach e names
(foreach a (cx:attribs e)
(if (not (member (strcase (cx:dxf 2 (entget a))) (mapcar 'strcase out)))
(setq out (append out (list (cx:dxf 2 (entget a))))))))
out)
(defun cx:join (lst delim / out)
(setq out "")
(foreach s lst
(setq out (if (= out "") s (strcat out delim s))))
(if lst out ""))
;; Replace every OLD in STR by NEW (case sensitive).
(defun cx:replace (str old new / n out pos)
(setq n (strlen old) out "" pos 1)
(if (= n 0)
str
(progn
(while (<= pos (1+ (- (strlen str) n)))
(if (= (substr str pos n) old)
(setq out (strcat out new) pos (+ pos n))
(setq out (strcat out (substr str pos 1)) pos (1+ pos))))
(strcat out (substr str pos)))))
;; Ask for one of the keywords in BITS ("Yes No"), default DEF; returns the keyword.
(defun cx:ask-kw (msg bits def / v)
(initget 0 bits)
(setq v (getkword (strcat "\n" msg " [" (cx:replace bits " " "/") "] <" def ">: ")))
(if v v def))
;; Ask for a string (spaces allowed) with a default; "-" means an empty string.
(defun cx:ask-str (msg def / v)
(setq v (getstring T (strcat "\n" msg " <" def ">: ")))
(cond ((= v "") def) ((= v "-") "") (T v)))
;; Save the system variables in VARS ((name . value) ...), set them, and open one undo step.
;; The caller declares cx:sv as a local so the saved values live exactly as long as the command.
(defun cx:start (vars)
(setq cx:sv (mapcar (function (lambda (v) (cons (car v) (getvar (car v))))) vars))
(command "_.UNDO" "_Begin")
(foreach v vars (setvar (car v) (cdr v)))
(princ))
(princ "\nCXATTVIS loaded. Type CXATTVIS to start.")
(princ)
版本紀錄
版本 1.0.0 · 2026年10月27日
首個版本。