;;; ---------------------------------------------------------------------
;;;  ENDCHORD-bgol.lsp
;;;  Command:  ECHD
;;;
;;;  PURPOSE
;;;  Creates a brand-new two-point polyline (a "chord") by connecting
;;;  only the first and last vertex of each selected polyline,
;;;  discarding all intermediate vertices. A fast, extreme
;;;  simplification for turning a noisy multi-vertex polyline into a
;;;  single straight reference segment.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   ECHD   at the command line and press Enter.
;;;    3. Select one or more polylines.
;;;    4. For each selected polyline, a new straight two-point polyline
;;;       joining its start and end point is created on layer
;;;       "BGOL-ECHD". The original polylines are left untouched.
;;;
;;;  NOTES
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of endpoint-chord
;;;      simplification utilities common in CAD/survey drafting
;;;      workflows, but the code, command name and file name here are
;;;      new and are NOT copied from, nor identical to, any specific
;;;      third-party product.
;;;
;;;  ---------------------------------------------------------------------
;;;  Developed and shared for the CAD community by:  BGol Community
;;;  Community website:                              https://bgol.in/
;;;  License:  Open-source & free to use, modify, and share for all.
;;;            Provided "as is", without warranty of any kind.
;;;  ---------------------------------------------------------------------

(vl-load-com)

(defun BGOL:ECHD-EnsureLayer (lname / )
  (if (not (tblsearch "LAYER" lname))
    (entmake (list '(0 . "LAYER")
                   '(100 . "AcDbSymbolTableRecord")
                   '(100 . "AcDbLayerTableRecord")
                   (cons 2 lname)
                   '(70 . 0)
                   '(62 . 1)          ; red
                   '(6 . "Continuous")))
  )
)

(defun BGOL:ECHD-MakeChord (p1 p2 lname / dxfdata)
  (setq dxfdata (list '(0 . "LWPOLYLINE")
                       '(100 . "AcDbEntity")
                       (cons 8 lname)
                       '(100 . "AcDbPolyline")
                       (cons 90 2)
                       '(70 . 0)
                       (cons 10 (list (car p1) (cadr p1)))
                       (cons 10 (list (car p2) (cadr p2)))
                 )
  )
  (entmakex dxfdata)
)

(defun C:ECHD ( / ss n i ename obj p1 p2 made)
  (BGOL:ECHD-EnsureLayer "BGOL-ECHD")
  (princ "\nSelect polyline(s) to simplify to a start-end chord: ")
  (setq ss (ssget '((0 . "LWPOLYLINE"))))
  (if ss
    (progn
      (setq n (sslength ss))
      (setq made 0)
      (setq i 0)
      (while (< i n)
        (setq ename (ssname ss i))
        (setq obj (vlax-ename->vla-object ename))
        (setq p1 (vlax-curve-getStartPoint obj))
        (setq p2 (vlax-curve-getEndPoint obj))
        (if (and p1 p2 (> (distance p1 p2) 1e-8))
          (progn
            (BGOL:ECHD-MakeChord p1 p2 "BGOL-ECHD")
            (setq made (1+ made))
          )
        )
        (setq i (1+ i))
      )
      (princ (strcat "\nECHD: " (itoa made) " chord polyline(s) created (layer BGOL-ECHD)."))
    )
    (princ "\nNo polylines selected.")
  )
  (princ)
)

(princ "\nENDCHORD-bgol.lsp loaded. Type ECHD to collapse polylines to start-end chords.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
