Best AutoLISP to Draw a Centerline Between Two Lines (Anti-X Error Script)
Program Description
The CenterlineDraw (CLD) AutoLISP routine is a fast, production-ready tool that automatically creates a precise centerline between two selected lines. It solves a common drafting issue: incorrect centerlines caused by reversed or inconsistent line directions.
Unlike basic scripts that can generate a crossed “X” result, this routine evaluates both possible endpoint pairings and selects the correct one using 2D distance comparison logic. The result is a clean, accurate centerline whether the source lines are parallel, angled, reversed, or imported from unreliable geometry.
Another practical advantage: this routine creates standard LINE entities, not associative objects. That means:
- no dependency on newer AutoCAD object types
- no unexpected behavior when modifying geometry
- full compatibility with legacy workflows and downstream software
How to Use the CLD Command (Step-by-Step)
1. Save the File
Paste the AutoLISP code into a text editor and save it as:
CenterlineDraw.lsp
Make sure the extension is .lsp
or download the script here.
2. Load into AutoCAD
Type:
APPLOAD
Then press Enter.
Official reference: https://help.autodesk.com/view/ACD/2024/ENU/?guid=GUID-APPLOAD
3. Load the Script
Select CenterlineDraw.lsp and click Load
4. Run the Command
Type:
CLD
Press Enter.
5. Select Two Lines
- Select the first line
- Select the second line
If you press ESC, AutoCAD cancels cleanly (no script error).
6. Result
The routine:
- reads endpoints using DXF group codes (10 / 11)
- evaluates correct pairing using distance comparison
- calculates midpoints using mapcar
- draws a centerline on the current layer
Why This Routine Works Reliably in Real Drawings
Eliminates the “X” Centerline Error
Most scripts fail when lines are drawn in opposite directions. This routine compares both pairing configurations and selects the correct one using total distance:
- Start–Start / End–End
- Start–End / End–Start
This guarantees correct orientation.
Ignores Bad Z Values (Real-World Fix)
Imported drawings often contain incorrect elevations. This routine uses a 2D distance function, ignoring Z values during pairing.
This prevents:
- twisted centerlines
- incorrect midpoint selection
- geometry distortion
Lightweight (No ActiveX / COM)
The routine uses:
- entget
- DXF codes
- native AutoLISP functions
No vlax / COM calls.
Benefits:
- faster execution
- fewer compatibility issues
- stable across AutoCAD versions
Reference: https://help.autodesk.com/view/ACD/2024/ENU/?guid=GUID-DXF
Clean Geometry Output (Key Advantage)
AutoCAD’s native CENTERLINE command (introduced in 2017) creates associative centerline objects.
This routine does not.
It creates a simple LINE entity, which means:
- no associativity
- no automatic updates
- no hidden constraints
This is often preferred in:
- fabrication drawings
- exports to other CAD systems
- legacy environments
Fast Midpoint Calculation
Midpoints are computed using mapcar, keeping the code:
- simple
- readable
- efficient
Corrected AutoLISP Code (Stable Version)
This version avoids forced exits and lets AutoCAD handle cancellation cleanly.
“`lisp id=”c9d2k1″ (defun dist2d (a b) (distance (list (car a) (cadr a)) (list (car b) (cadr b))) )
(defun midpoint (a b) (mapcar ‘(lambda (x y) (/ (+ x y) 2.0)) a b) )
(defun c:CLD (/ ent1 ent2 data1 data2 pt1 pt2 pt3 pt4 dA dB midPt1 midPt2)
;; Select first line (while (not ent1) (setq ent1 (car (entsel “\nSelect first line (or press ESC to cancel): “))) (if (and ent1 (/= “LINE” (cdr (assoc 0 (entget ent1))))) (progn (princ “\nNot a line.”) (setq ent1 nil)) ) )
;; Select second line (while (not ent2) (setq ent2 (car (entsel “\nSelect second line (or press ESC to cancel): “))) (if (and ent2 (/= “LINE” (cdr (assoc 0 (entget ent2))))) (progn (princ “\nNot a line.”) (setq ent2 nil)) ) )
;; Cache data (setq data1 (entget ent1) data2 (entget ent2))
;; Extract points (setq pt1 (cdr (assoc 10 data1)) pt2 (cdr (assoc 11 data1)) pt3 (cdr (assoc 10 data2)) pt4 (cdr (assoc 11 data2)))
;; Compare pairing options (setq dA (+ (dist2d pt1 pt3) (dist2d pt2 pt4))) (setq dB (+ (dist2d pt1 pt4) (dist2d pt2 pt3)))
(if (< dA dB) (setq midPt1 (midpoint pt1 pt3) midPt2 (midpoint pt2 pt4)) (setq midPt1 (midpoint pt1 pt4) midPt2 (midpoint pt2 pt3)) )
;; Create centerline (entmake (list ‘(0 . “LINE”) (cons 8 (getvar “CLAYER”)) (cons 10 midPt1) (cons 11 midPt2) ) )
(princ “\nCenterline drawn.”) (princ) ) “`
Practical Use Cases
- Mechanical drafting: shafts, symmetry axes
- Architecture: wall centerlines, layout alignment
- Civil drawings: offsets, alignments
- Steel detailing: beam center tracking
Works especially well with:
- imported DWG files
- inconsistent geometry
- reversed line directions
Limitations
- Only supports LINE entities
- Does not extend beyond endpoints
- No automatic linetype assignment
These choices keep execution fast and predictable.
FAQ
Does it support polylines?
No. Only LINE objects are supported. You would need to extract segments or convert geometry.
Why not use AutoCAD CENTERLINE?
AutoCAD’s CENTERLINE command creates associative objects that:
- update automatically
- depend on source geometry
This routine:
- creates a simple LINE
- avoids associativity
- works better for exports and legacy workflows
What happens if lines are not parallel?
The routine still works. It creates a centerline between corresponding midpoints, not a geometric bisector.
What if my drawing has wrong elevations?
No issue. The routine uses 2D distance checks, so Z values are ignored during pairing.
Why use entmake instead of LINE command?
Because entmake:
- avoids OSNAP interference
- runs faster
- creates consistent geometry
Can this be extended?
Yes. Common upgrades:
- polyline support
- CENTER linetype assignment
- automatic layer creation
- centerline extension
This routine removes a repetitive manual step and prevents small geometric mistakes that typically go unnoticed until late in a project.

