|

JOINALL: The Ultimate AutoCAD Lisp to Batch Join Lines and Arcs into Polylines

Program Description

JOINALL.lsp is a field-ready AutoCAD Lisp utility built to clean and unify fragmented geometry without manual selection. Instead of relying on the standard JOIN command—which depends on user picking and often misses segments—this routine performs a global scan of the drawing database.

It targets:

  • LINE
  • ARC
  • LWPOLYLINE
  • POLYLINE

The workflow is deliberate:

  1. Run OVERKILL (command-line version) to remove:
    • duplicate segments
    • overlapping geometry
    • zero-length artifacts
  2. Run PEDIT (Multiple → Join) with a user-defined junction tolerance

The result is a clean, lightweight, and continuous polyline network suitable for:

  • CNC toolpaths
  • Hatch boundaries
  • Quantity takeoffs
  • GIS topology cleanup
  • BIM-ready geometry

This is not just a convenience script. It standardizes geometry so downstream tools stop failing.


Download the script here


JOINALL Lisp Code

(defun c:JOINALL ( / ss tol oldcmdecho oldpeditaccept )

  ;; Save current system variables to restore them later
  (setq oldcmdecho (getvar "CMDECHO"))
  (setq oldpeditaccept (getvar "PEDITACCEPT"))

  ;; Local error handling function to restore settings if the user cancels (ESC)
  (defun *error* (msg)
    (setvar "CMDECHO" oldcmdecho)
    (setvar "PEDITACCEPT" oldpeditaccept)
    (if (not (member msg '("Function cancelled" "quit / exit abort")))
      (princ (strcat "\nError: " msg))
    )
    (princ)
  )

  ;; Prompt user for junction tolerance (defaulting to 0.001)
  (setq tol (getreal "\nJunction tolerance <0.001>: "))
  (if (not tol) (setq tol 0.001))

  ;; Configuration: Suppress command noise and auto-accept PEDIT conversion
  (setvar "CMDECHO" 0)
  (setvar "PEDITACCEPT" 1)

  (princ "\nGlobal selection of objects...")

  ;; Select all Lines, Arcs, and existing Polylines (Heavy and Lightweight)
  (setq ss (ssget "X" '((0 . "LINE,ARC,LWPOLYLINE,POLYLINE"))))

  (if ss
    (progn
      ;; Step 1: Clean geometry using OVERKILL
      ;; The hyphen "-" prefix ensures the command-line version runs without dialogs
      (princ "\nCleaning overlapping geometry (OVERKILL)...")
      (command "-OVERKILL" ss "" "")

      ;; Step 2: Join objects using PEDIT
      (princ "\nJoining objects into polylines...")
      ;; _M = Multiple, ss = Selection, _J = Join, tol = Fuzz distance
      (command "_.PEDIT" "_M" ss "" "_J" tol "")

      (princ (strcat "\nSuccess: Objects joined with a tolerance of " (rtos tol 2 4) "."))
    )
    (princ "\nNo valid entities (Lines, Arcs, Polylines) found in drawing.")
  )

  ;; Restore system variables to their original state
  (setvar "CMDECHO" oldcmdecho)
  (setvar "PEDITACCEPT" oldpeditaccept)

  (princ)
)

;; Display loading message
(princ "\nJOINALL loaded. Type JOINALL to run.")
(princ)

Step-by-Step Guide

1. Download and Save

Copy the Lisp code and save it as:

JoinAll.lsp

Store it in a known support path or project folder.


2. Load the Script

In AutoCAD:

APPLOAD
  • Browse to JoinAll.lsp
  • Click Load

For repeated use, add it to the Startup Suite.


3. Execute the Command

JOINALL

Press Enter


4. Set Junction Tolerance

You’ll be prompted:

Junction tolerance <0.001>:
  • Press Enter → uses default 0.001
  • Enter a higher value (e.g., 0.01, 0.1) if gaps exist

Practical rule:

  • mm drawings → 0.001 to 0.01
  • meter-based plans → 0.01 to 0.1
  • imported DWG/DXF → test upward gradually

5. Review Results

The routine will:

  • clean geometry with OVERKILL
  • join entities using PEDIT

Check:

  • command line output
  • polyline continuity (use LIST or PEDIT → Edit Vertex)
  • hatch test (quick validation)

Why Use JOINALL for Drawing Optimization?

Reduce Entity Count

Fragmented drawings often contain thousands of short segments.
JOINALL converts them into continuous polylines, which:

  • reduces DWG file size
  • improves REGEN performance
  • speeds up selection and grip editing

Eliminate Manual Errors

Manual joining fails in real projects:

  • missed segments
  • inconsistent tolerance
  • operator fatigue

This routine removes that variable.


Prepare Geometry for Downstream Workflows

Clean polylines are required for:

  • EXTRUDE
  • PRESSPULL
  • AREA / MASSPROP
  • HATCH
  • BOUNDARY
  • Civil/GIS topology tools

If geometry isn’t continuous, these commands fail or produce bad data.


Handle External Consultant Files

Incoming DWGs are often:

  • exploded
  • duplicated
  • misaligned by small gaps

JOINALL standardizes them in one pass.


Save Time at Scale

Typical cleanup tasks:

  • joining floor plans
  • preparing site boundaries
  • fixing survey imports

What takes 30–60 minutes manually becomes a single command execution.


Technical Notes (What Happens Under the Hood)

  • Uses ssget "X" → scans the entire drawing database
  • Filters entity types explicitly → avoids irrelevant objects
  • Runs -OVERKILL → command-line mode (no dialog interruption)
  • Executes PEDIT M → batch conversion + join
  • Applies fuzz distance (tolerance) → bridges micro gaps

Reference:


Best Practices from Production Use

Control Tolerance Carefully

Too small:

  • geometry won’t join

Too large:

  • unintended joins across gaps

Start conservative and increase only when needed.


Run in a Copy First

On critical drawings:

  • test on a duplicate file
  • verify topology before committing

Combine with Cleanup Workflow

Typical sequence used in production:

  1. AUDIT
  2. PURGE
  3. OVERKILL
  4. JOINALL

Watch Layer Mixing

JOINALL does not isolate by layer.
If different systems overlap (e.g., architecture + structure), consider filtering first.


Common Limitations

  • Does not process:
    • splines
    • ellipses
    • 3D polylines
  • Does not:
    • detect intended design gaps
    • enforce closed polylines automatically
  • Behavior depends on:
    • drawing precision
    • unit scale
    • source file quality

 

FAQ

What does JOINALL do differently from the standard JOIN command?

The built-in JOIN command requires manual selection and often misses segments.
JOINALL runs globally, cleans geometry first, then joins everything in one pass.


Why is OVERKILL included before joining?

Because joining dirty geometry produces bad results.
OVERKILL removes duplicates and overlaps, which prevents broken or redundant polylines.


What tolerance should I use?

Depends on units and source:

  • clean CAD work → 0.001
  • imported files → 0.01 to 0.1
    Increase gradually until joins succeed without merging unrelated geometry.

Can it create closed polylines automatically?

Not explicitly.
If endpoints meet within tolerance, they will join into a closed loop. Otherwise, manual closing may still be required.


Will it affect blocks or annotations?

No. The selection filter limits processing to:

  • LINE
  • ARC
  • POLYLINE types

Can I use it on large drawings?

Yes, but expect processing time depending on entity count.
For very large files:

  • isolate layers
  • run in sections if needed

Does it replace manual cleanup entirely?

For most 2D drafting cleanup tasks, yes.
Edge cases still require inspection, especially for survey or GIS data.


Is it safe for production drawings?

Yes, if used correctly:

  • test tolerance
  • verify results
  • keep a backup