Search This Blog

Count & Label Overlapping Objects in AutoCAD


Duplicate and overlapping geometry is one of the most common headaches in CAD drafting. Whether you have stacked blocks, duplicate survey points, or multiple line endpoints landing on the exact same coordinate, invisible overlaps can inflate material takeoffs, break CNC toolpaths, and bloat file sizes.

While AutoCAD’s built-in OVERKILL command deletes duplicates automatically, sometimes you need to audit and inspect where overlaps are happening—and how many objects are stacked there—before removing anything.

In this post, you’ll get a free, ready-to-use AutoLISP routine (COUNTOVERLAP) that scans your selection, counts overlapping coordinates, and places a clean, color-coded text label showing the exact overlap count at each spot.


Key Features of the COUNTOVERLAP Routine

  • Multi-Object Support: Detects overlaps across Points, Blocks (INSERT), Circles, Arcs, Lines (start and end points), and Polylines (LWPOLYLINE) vertices.
  • Cross-Type Counting: If a line endpoint, a block insertion point, and a polyline vertex all meet at the exact same coordinate, the label displays 3.
  • Clutter-Free Output: Locations with only 1 object are ignored—labels (2, 3, 4, etc.) only appear where two or more items overlap.
  • Dedicated Layer & Color: Automatically creates a layer named OVERLAP_COUNTS in Red (Color 1) so you can easily isolate, review, or delete all generated numbers in one click using LAYDEL or QSELECT.
  • Adjustable Tolerance: Built-in 0.01 coordinate tolerance catches points that are nearly touching.

The AutoLISP Code

Copy the entire code block below and paste it into a plain text editor like Notepad:

CountOverlap.lsp (AutoLISP)
(defun c:COUNTOVERLAP (/ ss tol lay col ht i ent etype pts pt found item counts)
  (setq tol 0.01)             ; Coordinate tolerance for overlapping locations
  (setq lay "OVERLAP_COUNTS") ; Target layer name for generated labels
  (setq col 1)                ; AutoCAD Color Index (1 = Red, 2 = Yellow, 3 = Green, 4 = Cyan, 5 = Blue, 6 = Magenta)

  (prompt "\nSelect objects (Points, Blocks, Circles, Arcs, Lines, Polylines): ")
  (if (setq ss (ssget '((0 . "POINT,INSERT,CIRCLE,ARC,LINE,LWPOLYLINE"))))
    (progn
      (initget 6)
      (setq ht (getdist (strcat "\nSpecify text height <" (rtos (getvar "TEXTSIZE") 2 2) ">: ")))
      (if (null ht) (setq ht (getvar "TEXTSIZE")))
      
      ;; Create the target layer if it does not already exist
      (if (not (tblsearch "LAYER" lay))
        (entmake
          (list
            '(0 . "LAYER")
            '(100 . "AcDbSymbolTableRecord")
            '(100 . "AcDbLayerTableRecord")
            (cons 2 lay)
            '(70 . 0)
            (cons 62 col)
          )
        )
      )

      (setq counts '() i 0)
      ;; Extract relevant points/vertices from each selected object
      (while (< i (sslength ss))
        (setq ent (entget (ssname ss i))
              etype (cdr (assoc 0 ent))
              pts '())
        
        (cond
          ((member etype '("POINT" "INSERT" "CIRCLE" "ARC"))
           (setq pts (list (cdr (assoc 10 ent))))
          )
          ((= etype "LINE")
           (setq pts (list (cdr (assoc 10 ent)) (cdr (assoc 11 ent))))
          )
          ((= etype "LWPOLYLINE")
           (let ((elev (cond ((cdr (assoc 38 ent))) (0.0))))
             (foreach dxf ent
               (if (= (car dxf) 10)
                 (setq pts (cons (list (cadr dxf) (caddr dxf) elev) pts))
               )
             )
           )
          )
        )
        
        ;; Group extracted coordinates within tolerance
        (foreach pt pts
          (setq found nil)
          (foreach item counts
            (if (equal pt (car item) tol)
              (setq counts (subst (cons (car item) (1+ (cdr item))) item counts)
                    found T)
            )
          )
          (if (not found)
            (setq counts (cons (cons pt 1) counts))
          )
        )
        (setq i (1+ i))
      )
      
      ;; Create styled text labels on the dedicated layer
      (foreach item counts
        (if (> (cdr item) 1)
          (entmake
            (list
              '(0 . "TEXT")
              (cons 8 lay)
              (cons 62 col)
              (cons 10 (mapcar '+ (car item) (list (* ht 0.5) (* ht 0.5) 0.0)))
              (cons 40 ht)
              (cons 1 (itoa (cdr item)))
            )
          )
        )
      )
      (prompt (strcat "\nOverlap counts created on layer '" lay "'."))
    )
    (prompt "\nNo supported objects selected.")
  )
  (princ)
)

How to Load and Use This LISP in AutoCAD

  1. Save the Script: Open Notepad, paste the code above, and click File > Save As. Name the file CountOverlap.lsp and change Save as type to All Files (*.*).
  2. Load into AutoCAD: Open your drawing, type APPLOAD (or AP) in the command line, and press Enter. Browse to CountOverlap.lsp, select it, and click Load.
  3. Run the Command: Type COUNTOVERLAP in the command line and press Enter.
  4. Select Geometry: Window-select the area or entire drawing you want to check and press Enter.
  5. Set Text Height: Press Enter to accept your current default text height, or type a custom height value. Red count labels will immediately appear next to every overlapping point.

Easy Customization Tips

You can tweak the first three variables at the top of the code to match your company’s CAD standards:

No comments:

Post a Comment