CADLISPMASTER
Stop wasting time on repetitive AutoCAD tasks. CAD LISP Master provides free, efficient AutoLISP codes and clear tutorials designed to automate your workflow and speed up your drafting.

Drawing-Wide Block Replacement in AutoCAD: Swap All Matching Instances

Replace all matching block instances drawing-wide in AutoCAD using CBW.lsp. Free AutoLISP script for global block swapping while preserving properties
Replace All Matching Blocks Instantly Drawing-Wide

Updating block symbols across an entire drawing file can turn into a massive headache during late-stage design revisions. Whether you are replacing an outdated sprinkler head symbol, updating a door block, or swapping out electrical outlets across a multi-story floor plan, finding every single instance scattered across different layers and viewports is time-consuming and prone to human error.

The CBW.lsp (Change Block World) utility performs a global database replacement in two simple picks. Rather than hunting through the Block Replace tool or typing complex block names, you select your new master block, pick one instance of the old block, and the script automatically swaps every matching block across the entire drawing database.


What Makes CBW.lsp Different?

  • Full Database Search: Uses a global filter (ssget "X") to instantly scan Model Space and Paper Space viewports, ensuring no hidden or isolated block instances are missed.

  • Property Preservation: Replaces the block definition while leaving original coordinates, rotation angles, layer assignments, and scale values perfectly intact.

  • No Name Typing Needed: Picks objects visually on screen, bypassing drop-down lists and exact block name spelling checks.


Step-by-Step Execution Guide

  1. Load CBW.lsp in AutoCAD using the APPLOAD command.

  2. Type CBW in the command prompt and hit Enter.

  3. Select the master block (the new block symbol you want to apply).

  4. Select one instance of the target block (the old block you want to swap out).

  5. The script scans the drawing database and replaces all matching instances instantly, confirming the total replaced count in your command history.


AutoLISP Code (CBW.lsp)

Copy the code below and save it as CBW.lsp inside your AutoCAD support folder:

(defun c:CBW ( / *error* acDoc oldOsnap masterBlk masterName targetBlk targetName ss i )
  (vl-load-com)
  (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (vla-StartUndoMark acDoc)
  (setq oldOsnap (getvar "OSMODE"))
  (defun *error* (msg)
    (if oldOsnap (setvar "OSMODE" oldOsnap))
    (vla-EndUndoMark acDoc)
    (princ "\nCAD Lisp [www.daolpro.com]")
    (princ)
  )
  (if (and (setq masterBlk (car (entsel "\nSelect the master block (new): ")))
               (= (cdr (assoc 0 (entget masterBlk))) "INSERT"))
    (progn
      (setq masterName (vla-get-name (vlax-ename->vla-object masterBlk)))
      (if (and (setq targetBlk (car (entsel "\nSelect the target block (old): ")))
                   (= (cdr (assoc 0 (entget targetBlk))) "INSERT"))
        (progn
          (setq targetName (vla-get-name (vlax-ename->vla-object targetBlk)))
          (if (/= masterName targetName)
            (if (setq ss (ssget "X" (list '(0 . "INSERT") (cons 2 targetName))))
              (progn
                (setq i 0)
                (repeat (sslength ss)
                  (vla-put-name (vlax-ename->vla-object (ssname ss i)) masterName)
                  (setq i (1+ i))
                )
                (princ (strcat "\nSuccessfully replaced " (itoa (sslength ss)) " block(s)."))
              )
              (princ "\nNo matching target blocks found.")
            )
            (princ "\nMaster and target blocks are the same.")
          )
        )
        (princ "\nInvalid selection. Target must be a block.")
      )
    )
    (princ "\nInvalid selection. Master must be a block.")
  )
  (setvar "OSMODE" oldOsnap)
  (vla-EndUndoMark acDoc)
  (princ "\nCAD Lisp [www.daolpro.com]")
  (princ)
)

Revision Workflow Tip

Before running global block replacements across large team project files, thaw and unlock all relevant layers so the script can access and update every block reference cleanly.

Post a Comment