Search This Blog

How to Combine Two Polygons into One Single Polyline (Free Routine)


If you work with parcels, floor plans, or site boundaries in AutoCAD, you probably know how tedious it can be to combine two adjacent or overlapping polygons into one single boundary. Usually, you have to trim overlapping lines, explode shapes, and use PEDIT or BOUNDARY to clean things up manually.

Today, I’m sharing a lightweight, free AutoLISP routine (MERGEPOLY) that automates this entire process in seconds. With just one command, you can select two or more touching or overlapping closed polylines and merge them into a single, clean closed polyline.

How the LISP Routine Works

Behind the scenes, this script automates a classic 4-step AutoCAD geometry trick:

  1. Converts your selected closed polylines into Regions.
  2. Uses the UNION command to fuse them into one single shape.
  3. Explodes the resulting region back into individual lines and arcs.
  4. Uses PEDIT to join the outer perimeter back into a single closed polyline.

The AutoLISP Code (MERGEPOLY.lsp)

Click Download .LSP to save the file directly to your computer, or click Copy Code to paste it into Notepad:

MERGEPOLY.lsp
(defun c:MERGEPOLY (/ *error* ss oldEcho oldPedit lastEnt ssExploded)
  ;; Error handler to restore system variables if cancelled
  (defun *error* (msg)
    (if oldEcho (setvar "CMDECHO" oldEcho))
    (if oldPedit (setvar "PEDITACCEPT" oldPedit))
    (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")))
      (princ (strcat "\nError: " msg))
    )
    (princ)
  )

  ;; Save current system variables and suppress command prompts
  (setq oldEcho (getvar "CMDECHO"))
  (setq oldPedit (getvar "PEDITACCEPT"))
  (setvar "CMDECHO" 0)
  (setvar "PEDITACCEPT" 1)

  (princ "\nSelect closed polylines/polygons to combine: ")
  (if (setq ss (ssget '((0 . "LWPOLYLINE,POLYLINE"))))
    (if (> (sslength ss) 1)
      (progn
        ;; 1. Convert selected polylines to Regions
        (command "_.REGION" ss "")
        (setq lastEnt (entlast))
        
        ;; 2. Union the regions together
        (command "_.UNION" "_P" "")
        (setq lastEnt (entlast))

        ;; 3. Explode the combined region into individual lines/arcs
        (command "_.EXPLODE" lastEnt)
        (setq ssExploded (ssget "_P"))
        
        ;; 4. Join segments back into a single closed polyline
        (if ssExploded
          (progn
            (command "_.PEDIT" "_M" ssExploded "" "_J" "0.0" "")
            (princ "\nPolygons successfully combined into one polyline.")
          )
          (princ "\nCould not explode the merged region.")
        )
      )
      (princ "\nPlease select at least two polygons.")
    )
    (princ "\nNothing selected.")
  )

  ;; Restore system variables
  (setvar "CMDECHO" oldEcho)
  (setvar "PEDITACCEPT" oldPedit)
  (princ)
)

How to Load and Run the LISP in AutoCAD

Follow these quick steps to use the routine in your drawing:

  1. Download or Save the File: Click the Download .LSP button above to save MERGEPOLY.lsp directly, or copy the code into Notepad and save it as MERGEPOLY.lsp (with Save as type set to All Files (*.*)).
  2. Load into AutoCAD: Open your AutoCAD drawing, type APPLOAD in the command line, and press Enter.
  3. Select the Script: Browse to where you saved MERGEPOLY.lsp, select it, and click Load (then click Close). (Tip: Add it to your Startup Suite in the APPLOAD window if you want it available in every drawing automatically).
  4. Run the Command: Type MERGEPOLY in the command line and press Enter.
  5. Select Polygons: Click the polygons you want to combine and press Enter. Your shapes are now merged into one!

Troubleshooting & Tips for Best Results

  • Polygons Must Be Closed: If one of your polylines has a tiny gap, AutoCAD won't convert it to a Region. Select your polyline, open the Properties palette (CTRL+1), and verify that Closed is set to Yes.
  • Same Elevation (Z-Value): Both polygons must lie flat on the exact same plane. If the command fails on 3D or imported survey drawings, select your objects and run the FLATTEN command first.
  • Must Touch or Overlap: Make sure the polygons share an edge or overlap. If there is empty space between them, they cannot form a single continuous boundary.

Have a question about this routine or need a custom AutoLISP script for your workflow? Leave a comment below!

No comments:

Post a Comment