CXBLKSELBlocchi e attributiVersione 1.0.0Provato in AutoCAD
Selezionare blocchi per nome o per valore di attributo
Seleziona tutti i blocchi con un certo nome, oppure solo quelli il cui attributo ha un certo valore, per poterli spostare, copiare o modificare insieme.
Come si usa
- Digita CXBLKSEL e clicca un blocco, oppure scegli Name e scrivi il nome. I caratteri jolly funzionano.
- Se vuoi restringere, usa TAG=valore, per esempio SIZE=DN50.
- I blocchi corrispondenti vengono selezionati, pronti per il comando successivo.
Da sapere
- La ricerca avviene nella scheda Modello o Layout corrente.
Caricarlo in AutoCAD
- Salva il file dove preferisci.
- Digita APPLOAD, scegli il file e premi Carica.
- Digita il nome del comando: CXBLKSEL.
Per averlo a ogni avvio, aggiungilo alla Raccolta di avvio nella stessa finestra, oppure carica un'intera cartella in una volta con il caricatore facoltativo. Il caricatore facoltativo
Provato in AutoCAD (versione completa). Altri programmi che eseguono AutoLISP potrebbero funzionare, ma non sono stati provati.
Il codice
Testo semplice, niente di nascosto. Copialo in un file chiamato cxblksel.lsp oppure scaricalo.
;;; ============================================================================
;;; CXBLKSEL - Select all blocks of one name or attribute value
;;; Version 1.0.0
;;; CADXChange LISP tools - https://cadxchange.net/resources/lisp/cxblksel
;;; 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
;;; ============================================================================
;; Every block reference in the current layout whose block name matches NAME (wildcards allowed).
;; With TAG and VALUE, only those whose attribute TAG matches VALUE (wildcards allowed, any case).
;; Returns a selection set, or nil when nothing matches.
(defun cx:blksel-run (name tag value / ss out)
(setq ss (ssget "X" (list '(0 . "INSERT") (cons 2 name) (cons 410 (getvar "CTAB")))))
(if (and ss tag)
(progn
(setq out (ssadd))
(foreach e (cx:ss->list ss)
(if (and (cx:att-get e tag) (wcmatch (strcase (cx:att-get e tag)) (strcase value)))
(ssadd e out)))
(setq ss (if (> (sslength out) 0) out nil))))
ss)
(defun c:CXBLKSEL ( / *error* cx:sv r name filter p tag value ss)
(setq *error* cx:err)
(initget "Name")
(setq r (entsel "\nClick a block, or [Name]: "))
(cond
((= r "Name") (setq name (cx:ask-str "Block name (wildcards allowed)" "-")))
((and r (= (cx:dxf 0 (entget (car r))) "INSERT")) (setq name (cx:dxf 2 (entget (car r)))))
(r (princ "\nThat is not a block.")))
(if (and name (/= name ""))
(progn
(setq filter (cx:ask-str "Only blocks where an attribute has a value, as TAG=value (- for all)" "-"))
(if (and (/= filter "-") (/= filter "") (setq p (cx:find filter "=" 1)))
(setq tag (cx:trim (substr filter 1 (1- p)))
value (cx:trim (substr filter (1+ p)))))
(if (setq ss (cx:blksel-run name tag value))
(progn
(sssetfirst nil ss)
(princ (strcat "\n" (itoa (sslength ss)) " block(s) selected.")))
(princ "\nNo matching blocks in this layout."))))
(princ))
;;; ---- Shared helpers (used by the command above; identical in every CADXChange tool) ----
;; 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))
(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))
;; The ATTRIB of a block reference with the tag TAG (any case), or nil.
(defun cx:att-find (ins tag / r)
(foreach a (cx:attribs ins)
(if (and (not r) (= (strcase (cx:dxf 2 (entget a))) (strcase tag))) (setq r a)))
r)
(defun cx:att-get (ins tag / a)
(if (setq a (cx:att-find ins tag)) (cx:dxf 1 (entget a))))
;; 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))
;; 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)))
;; 1-based position of SUB in STR at or after START, or nil.
(defun cx:find (str sub start / n i r)
(setq n (strlen sub) i start r nil)
(if (> n 0)
(while (and (not r) (<= i (1+ (- (strlen str) n))))
(if (= (substr str i n) sub) (setq r i))
(setq i (1+ i))))
r)
;; Remove spaces and tabs at both ends.
(defun cx:trim (s / a b)
(setq a 1 b (strlen s))
(while (and (<= a b) (member (substr s a 1) '(" " "\t"))) (setq a (1+ a)))
(while (and (>= b a) (member (substr s b 1) '(" " "\t"))) (setq b (1- b)))
(substr s a (max 0 (1+ (- b a)))))
(princ "\nCXBLKSEL loaded. Type CXBLKSEL to start.")
(princ)
Cronologia delle versioni
Versione 1.0.0 · 27 ottobre 2026
Prima versione.
Altri comandi: Blocchi e attributi
CXATTFILLCompilare un attributo in molti blocchiCXATTNUMNumerare in sequenza un attributo di bloccoCXATTCOPYCopiare i valori degli attributi da un blocco ad altriCXATTVISMostrare o nascondere gli attributi dei blocchiCXBLKCOUNTContare i blocchi per nomeCXBLKREPLSostituire blocchi con un altro bloccoCXBLKRENRinominare i blocchi in bloccoCXBLKXPScomporre i blocchi conservando il testo degli attributiCXBLKATInserire un blocco in molti punti in una volta