;;; ---------------------------------------------------------------------
;;;  XSSMOOTH-bgol.lsp
;;;  Command:  XSM
;;;
;;;  PURPOSE
;;;  Smooths a cross-section ground/natural-surface line while staying
;;;  close to the true surveyed values. Resamples the selected line at a
;;;  user-specified interval, then applies a light 3-point moving-average
;;;  smoothing pass to the elevation (Y) component only, avoiding the
;;;  large-deviation problem of a full spline conversion. Other drawing
;;;  elements (e.g., a design/canal profile) are left completely
;;;  untouched.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   XSM   at the command line and press Enter.
;;;    3. Select the ground/natural-surface cross-section line.
;;;    4. Enter the vertex insertion interval.
;;;    5. A new, smoothed LWPOLYLINE is created on layer "BGOL-XSM",
;;;       closely tracking the original data. The source line is left
;;;       untouched.
;;;
;;;  NOTES
;;;    - Smoothing is a simple 3-point moving average over the sampled Y
;;;      values, not a spline fit, to keep deviation from real survey
;;;      data small.
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of cross-section smoothing
;;;      utilities common in survey/civil 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:XSM-EnsureLayer (lname / )
  (if (not (tblsearch "LAYER" lname))
    (entmake (list '(0 . "LAYER")
                   '(100 . "AcDbSymbolTableRecord")
                   '(100 . "AcDbLayerTableRecord")
                   (cons 2 lname)
                   '(70 . 0)
                   '(62 . 5)          ; blue
                   '(6 . "Continuous")))
  )
)

(defun BGOL:XSM-MakePolyline (pointlist lname / dxfdata pt)
  (setq dxfdata (list '(0 . "LWPOLYLINE")
                       '(100 . "AcDbEntity")
                       (cons 8 lname)
                       '(100 . "AcDbPolyline")
                       (cons 90 (length pointlist))
                       '(70 . 0)
                 )
  )
  (foreach pt pointlist
    (setq dxfdata (append dxfdata (list (cons 10 pt))))
  )
  (entmake dxfdata)
)

;; 3-point moving average smoothing of the Y component; X unchanged
(defun BGOL:XSM-Smooth (pts / n i out p0 p1 p2 avgY)
  (setq n (length pts))
  (setq out '())
  (setq i 0)
  (while (< i n)
    (cond
      ((or (= i 0) (= i (1- n)))
       (setq out (append out (list (nth i pts))))
      )
      (t
       (setq p0 (nth (1- i) pts))
       (setq p1 (nth i pts))
       (setq p2 (nth (1+ i) pts))
       (setq avgY (/ (+ (cadr p0) (cadr p1) (cadr p2)) 3.0))
       (setq out (append out (list (list (car p1) avgY))))
      )
    )
    (setq i (1+ i))
  )
  out
)

(defun C:XSM ( / ent obj step len pts dist pt smoothed)
  (BGOL:XSM-EnsureLayer "BGOL-XSM")
  (setq ent (car (entsel "\nSelect ground/natural-surface cross-section line to smooth: ")))
  (if ent
    (progn
      (setq step (getdist "\nVertex insertion interval: "))
      (if (not step) (setq step 5.0))
      (setq obj (vlax-ename->vla-object ent))
      (setq len (vlax-curve-getDistAtParam obj (vlax-curve-getEndParam obj)))
      (setq pts '())
      (setq dist 0.0)
      (while (< dist len)
        (setq pt (vlax-curve-getPointAtDist obj dist))
        (setq pts (append pts (list (list (car pt) (cadr pt)))))
        (setq dist (+ dist step))
      )
      (setq pt (vlax-curve-getEndPoint obj))
      (setq pts (append pts (list (list (car pt) (cadr pt)))))
      (setq smoothed (BGOL:XSM-Smooth pts))
      (BGOL:XSM-MakePolyline smoothed "BGOL-XSM")
      (princ (strcat "\nXSM: smoothed ground line rebuilt with " (itoa (length smoothed)) " point(s) (BGOL-XSM). Original left untouched."))
    )
    (princ "\nNo polyline selected.")
  )
  (princ)
)

(princ "\nXSSMOOTH-bgol.lsp loaded. Type XSM to smooth a cross-section ground line.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
