Search This Blog

Convert Strip Hatches into Open Single-Line Polylines with Global Width


When working with architectural, civil, or MEP drawings received from external consultants or exported from PDFs, you often encounter walls, roads, or thick linework represented as solid hatches rather than polylines.

While hatches look fine visually, they bloat file sizes, complicate length takeoffs, and make editing tedious. Manually tracing the centerline of each hatch and matching its thickness using Global Width can take hours.

In this post, we share a custom AutoLISP routine—HATCH2SINGLEPL—that automates this entire workflow in seconds.


What Does HATCH2SINGLEPL Do?

Unlike standard boundary-extraction tools that leave you with closed loops or overlapping double lines, HATCH2SINGLEPL analyzes the geometry of uniform-width strip hatches and converts them into clean, open single-line polylines.

Key Features:
  • Automatic Centerline Detection: Finds the exact centerline of both straight rectangular hatches and multi-segment paths (such as L-shaped or U-shaped walls).
  • Exact Global Width Matching: Calculates the thickness of the original hatch and applies that exact value to the polyline's ConstantWidth property.
  • No Overlapping Segments: Outputs a true open polyline (Closed = No) with zero duplicate vertices.
  • Layer Preservation: Automatically places the new polyline on the same layer as the original hatch.
  • Batch Processing: Select and convert dozens or hundreds of hatches in a single command.

The AutoLISP Code (HATCH2SINGLEPL.lsp)

Click the Copy Code button below and save the script as a .lsp file:

HATCH2SINGLEPL.lsp
(defun c:HATCH2SINGLEPL (/ ss i ent bndEnt plObj coords pts n p1 p2 p3 p4 mid1 mid2 wid newPl offEnt offCoords uniquePts area perim halfLen)
  (vl-load-com)
  (princ "\nSelect strip hatches to convert to open single-line polylines: ")
  (if (setq ss (ssget '((0 . "HATCH"))))
    (progn
      (vla-StartUndoMark (vla-get-ActiveDocument (vlax-get-acad-object)))
      (setq i 0)
      (while (< i (sslength ss))
        (setq ent (ssname ss i))
        
        ;; Extract outer boundary as LWPolyline
        (command "_.-HATCHEDIT" ent "_B" "_P" "_Y")
        (setq bndEnt (entlast))
        
        (if (and bndEnt (= (cdr (assoc 0 (entget bndEnt))) "LWPOLYLINE"))
          (progn
            (setq plObj (vlax-ename->vla-object bndEnt))
            ;; Extract 2D vertex list
            (setq coords (vlax-get plObj 'Coordinates))
            (setq pts nil)
            (while coords
              (setq pts (cons (list (car coords) (cadr coords)) pts))
              (setq coords (cddr coords))
            )
            (setq pts (reverse pts))
            
            ;; Remove duplicate closing vertex if present
            (if (equal (car pts) (last pts) 1e-6)
              (setq pts (reverse (cdr (reverse pts))))
            )
            
            (setq n (length pts))
            
            ;; CASE 1: Simple 4-sided Rectangular Hatch (Exact Open Single Line)
            (if (= n 4)
              (progn
                (setq p1 (nth 0 pts)
                      p2 (nth 1 pts)
                      p3 (nth 2 pts)
                      p4 (nth 3 pts))
                ;; Compare adjacent edge lengths to find the width (shorter side)
                (if (< (distance p1 p2) (distance p2 p3))
                  (progn
                    (setq wid (distance p1 p2))
                    (setq mid1 (mapcar '(lambda (a b) (/ (+ a b) 2.0)) p1 p2))
                    (setq mid2 (mapcar '(lambda (a b) (/ (+ a b) 2.0)) p3 p4))
                  )
                  (progn
                    (setq wid (distance p2 p3))
                    (setq mid1 (mapcar '(lambda (a b) (/ (+ a b) 2.0)) p2 p3))
                    (setq mid2 (mapcar '(lambda (a b) (/ (+ a b) 2.0)) p4 p1))
                  )
                )
                
                ;; Draw clean 2-point open polyline
                (command "_.PLINE" "_non" mid1 "_non" mid2 "")
                (setq newPl (entlast))
                (vla-put-ConstantWidth (vlax-ename->vla-object newPl) wid)
                (vla-put-Layer (vlax-ename->vla-object newPl) (vla-get-Layer plObj))
                
                ;; Delete boundary and original hatch
                (entdel bndEnt)
                (entdel ent)
              )
              
              ;; CASE 2: Multi-segment Strip Hatch (L-shape, U-shape, etc.)
              (progn
                (setq area (vla-get-Area plObj))
                (setq perim (vla-get-Length plObj))
                (if (>= (- (* perim perim) (* 16.0 area)) 0.0)
                  (progn
                    (setq wid (/ (- perim (sqrt (- (* perim perim) (* 16.0 area)))) 4.0))
                    ;; Offset inward by slightly less than half-width (49.9%) to prevent self-intersection collapse
                    (vla-Offset plObj (- (* wid 0.499)))
                    (setq offEnt (entlast))
                    (if (and offEnt (not (eq offEnt bndEnt)))
                      (progn
                        (setq offCoords (vlax-get (vlax-ename->vla-object offEnt) 'Coordinates))
                        (setq uniquePts nil)
                        (while offCoords
                          (setq uniquePts (cons (list (car offCoords) (cadr offCoords)) uniquePts))
                          (setq offCoords (cddr offCoords))
                        )
                        (setq uniquePts (reverse uniquePts))
                        ;; Take the first half of vertices to form an open single path
                        (setq halfLen (1+ (/ (length uniquePts) 2)))
                        (command "_.PLINE")
                        (repeat halfLen
                          (command "_non" (car uniquePts))
                          (setq uniquePts (cdr uniquePts))
                        )
                        (command "")
                        (setq newPl (entlast))
                        (vla-put-ConstantWidth (vlax-ename->vla-object newPl) wid)
                        (vla-put-Layer (vlax-ename->vla-object newPl) (vla-get-Layer plObj))
                        
                        ;; Clean up temporary objects
                        (entdel offEnt)
                        (entdel bndEnt)
                        (entdel ent)
                      )
                      (progn
                        (entdel bndEnt)
                        (princ "\nSkipped complex hatch: could not generate open centerline.")
                      )
                    )
                  )
                  (progn
                    (entdel bndEnt)
                    (princ "\nSkipped hatch: non-uniform width.")
                  )
                )
              )
            )
          )
        )
        (setq i (1+ i))
      )
      (vla-EndUndoMark (vla-get-ActiveDocument (vlax-get-acad-object)))
      (princ "\nConversion to open single polylines complete.")
    )
    (princ "\nNo hatches selected.")
  )
  (princ)
)

How to Load and Run the LISP in AutoCAD

No comments:

Post a Comment