Author Topic: Select similar blocks which have different names  (Read 8176 times)

0 Members and 1 Guest are viewing this topic.

3dwannab

  • Newt
  • Posts: 39
Re: Select similar blocks which have different names
« Reply #15 on: March 30, 2022, 05:27:34 PM »
Here's one that uses the built-in SELECTSIMILAR command switching the SELECTSIMILARMODE variable to 128 which is for name only.

Credit to Lee for some code in there.

Code: [Select]
;;
;; Select Similar Blocks by name
;; Updated by 3dwannab on 2022.03.30.
;;
;; I updated this on the 2022.03.30 to use the SELECTSIMILAR command by setting
;; the SELECTSIMILARMODE to 128 which is to select similar by name.
;; Along with a filter for the selection of blocks, after this it's pretty simple.
;;
;; Initial code was here by Lee Mac: http://www.theswamp.org/index.php?topic=49667.msg548516#msg548516
;;

(defun c:QSBlocks_Similar ( / *error* acDoc def lst ss1 ss2 var_cmdecho var_osmode var_selectsimilarmode )

  (princ "Filter select all similar Blocks by name :\n")

  (defun *error* (errmsg)
    (and acDoc (vla-EndUndoMark acDoc))
    (and errmsg
     (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
     (princ (strcat "\n<< Error: " errmsg " >>\n"))
     )
    (setvar 'cmdecho var_cmdecho)
    (setvar 'osmode var_osmode)
    (setvar 'selectsimilarmode var_selectsimilarmode)
    )

  (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))

  (setq var_cmdecho (getvar 'cmdecho))
  (setq var_osmode (getvar 'osmode))
  (setq var_selectsimilarmode (getvar 'selectsimilarmode))
  (setvar 'cmdecho 0)
  (setvar 'osmode 0)

  ; Code by Lee Mac http://www.cadtutor.net/forum/showthread.php?92638-Simple-fix-%28LISP-noob%29-Syntax-problem&p=633824&viewfull=1#post633824
  ;; Iterate over the block table and compile a list of xref blocks to exclude
  (while (setq def (tblnext "block" (not def)))
   (if (= 4 (logand 4 (cdr (assoc 70 def))))
     (setq lst (vl-list* "," (cdr (assoc 2 def)) lst))
     )
   )

  ;; Attempt to retrieve a selection of blocks (but not xrefs)
  (setq ss1 (ssget (cons '(0 . "INSERT") (if lst (vl-list* '(-4 . "<NOT") (cons 2 (apply 'strcat (cdr lst))) '((-4 . "NOT>")))))))

  ;; Set selectsimilarmode to use the name of an object.
  (setvar 'selectsimilarmode 128)

  ;; If ss1 one is valid then do this
  (if ss1
    (progn
      (vl-cmdf "_.selectsimilar" ss1 "")
      (setq ss2 (ssget)) ;; Create a new selection set for to zoom and reselect as the zoom objects will do this
      (command "_.zoom" "_O" ss2 "")
      (sssetfirst nil ss2)
      (princ (strcat "\n: ------------------------------\n\t\t<<< "(itoa (sslength ss2)) (if (> (sslength ss2) 1) " <<< INSERTS objects" " <<< INSERT object") " selected\n: ------------------------------\n"))
      )
    (princ "\n: ------------------------------\n\t\t*** Nothing Selected ***\n: ------------------------------\n")
    )

  (*error* nil) (princ)

  )

« Last Edit: March 30, 2022, 05:35:38 PM by 3dwannab »