;;; ---------------------------------------------------------------------
;;;  SOUNDING-bgol.lsp
;;;  Command:  SND
;;;
;;;  PURPOSE
;;;  Converts plain decimal elevation/depth text (e.g. "82.64") into
;;;  traditional bathymetric sounding notation: the integer part shown
;;;  large, with the decimal digits shown as a smaller under-script
;;;  numeral beside it — the standard nautical/hydrographic chart
;;;  convention for water-depth soundings.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   SND   at the command line and press Enter.
;;;    3. Select one or more TEXT entities holding plain decimal depth
;;;       values.
;;;    4. For each, an integer-part TEXT (larger, blue) and a decimal
;;;       under-script TEXT (smaller, yellow, offset down-right) are
;;;       created at/near the original position, on layer "BGOL-SND".
;;;
;;;  NOTES
;;;    - The original text entities are left untouched.
;;;    - Decimal digits are right-padded to a 4-digit sounding tag
;;;      (e.g. .64 -> "6400"), matching common chart-sounding style.
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of bathymetric sounding
;;;      notation utilities common in hydrographic 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:SND-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:SND-Text (pt txt ht colr lname / )
  (entmake (list '(0 . "TEXT")
                 '(100 . "AcDbEntity")
                 (cons 8 lname)
                 (cons 62 colr)
                 '(100 . "AcDbText")
                 (cons 10 pt)
                 (cons 40 ht)
                 (cons 1 txt)))
)

(defun BGOL:SND-DecTag (txt / dotpos intpart decpart)
  ;; Splits "82.64" into ("82" . "6400") style sounding tag
  (setq dotpos (vl-string-search "." txt))
  (if dotpos
    (progn
      (setq intpart (substr txt 1 dotpos))
      (setq decpart (substr txt (+ dotpos 2)))
      (while (< (strlen decpart) 4)
        (setq decpart (strcat decpart "0"))
      )
      (cons intpart decpart)
    )
    (cons txt "0000")
  )
)

(defun C:SND ( / ss n i ent edata pt txt tag ht)
  (BGOL:SND-EnsureLayer "BGOL-SND")
  (setq ss (ssget '((0 . "TEXT"))))
  (if ss
    (progn
      (setq n (sslength ss))
      (setq i 0)
      (while (< i n)
        (setq ent (ssname ss i))
        (setq edata (entget ent))
        (setq pt (cdr (assoc 10 edata)))
        (setq txt (cdr (assoc 1 edata)))
        (setq ht (cdr (assoc 40 edata)))
        (if (not ht) (setq ht 2.5))
        (if (and pt txt)
          (progn
            (setq tag (BGOL:SND-DecTag txt))
            (BGOL:SND-Text pt (car tag) ht 5 "BGOL-SND")
            (BGOL:SND-Text (list (+ (car pt) (* ht 0.9))
                                  (- (cadr pt) (* ht 0.15))
                                  (caddr pt))
                            (cdr tag) (* ht 0.55) 2 "BGOL-SND")
          )
        )
        (setq i (1+ i))
      )
      (princ (strcat "\nSND: " (itoa n) " elevation label(s) converted to sounding notation."))
    )
    (princ "\nNo text entities selected.")
  )
  (princ)
)

(princ "\nSOUNDING-bgol.lsp loaded. Type SND to convert decimal elevations to sounding notation.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
