;;; ---------------------------------------------------------------------
;;;  SPLITWRD-bgol.lsp
;;;  Command:  SPTX
;;;
;;;  PURPOSE
;;;  Splits a selected multi-word TEXT entity (e.g. "MAIN ROAD") into
;;;  a separate TEXT entity per word, placed at standardized spacing
;;;  along the original text's baseline direction. The original
;;;  combined text is removed after splitting.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   SPTX   at the command line and press Enter.
;;;    3. Select the multi-word text entity to split.
;;;    4. Enter the spacing between resulting words (or accept the
;;;       default, based on the text height).
;;;    5. One new TEXT entity per word is created on layer
;;;       "BGOL-SPTX", laid out along the original baseline direction;
;;;       the original combined text entity is deleted.
;;;
;;;  NOTES
;;;    - Words are split on single spaces; multiple consecutive spaces
;;;      collapse to one gap.
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of text-splitting
;;;      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:SPTX-EnsureLayer (lname / )
  (if (not (tblsearch "LAYER" lname))
    (entmake (list '(0 . "LAYER")
                   '(100 . "AcDbSymbolTableRecord")
                   '(100 . "AcDbLayerTableRecord")
                   (cons 2 lname)
                   '(70 . 0)
                   '(62 . 6)          ; magenta
                   '(6 . "Continuous")))
  )
)

;; Split a string on single spaces, collapsing multiple spaces, returns
;; a list of non-empty words.
(defun BGOL:SPTX-Words (str / words remain pos word)
  (setq words '())
  (setq remain (vl-string-trim " " str))
  (while (> (strlen remain) 0)
    (setq pos (vl-string-search " " remain))
    (if pos
      (progn
        (setq word (substr remain 1 pos))
        (if (> (strlen word) 0) (setq words (append words (list word))))
        (setq remain (vl-string-trim " " (substr remain (+ pos 2))))
      )
      (progn
        (setq words (append words (list remain)))
        (setq remain "")
      )
    )
  )
  words
)

(defun BGOL:SPTX-MakeText (pt txt height rot lname / dxfdata)
  (setq dxfdata (list '(0 . "TEXT")
                       '(100 . "AcDbEntity")
                       (cons 8 lname)
                       '(100 . "AcDbText")
                       (cons 10 pt)
                       (cons 40 height)
                       (cons 1 txt)
                       (cons 50 rot)
                 )
  )
  (entmake dxfdata)
)

(defun C:SPTX ( / ent edata str words basept height rot spacing curpt w n)
  (BGOL:SPTX-EnsureLayer "BGOL-SPTX")
  (setq ent (car (entsel "\nSelect text to split: ")))
  (if ent
    (progn
      (setq edata (entget ent))
      (if (= (cdr (assoc 0 edata)) "TEXT")
        (progn
          (setq str (cdr (assoc 1 edata)))
          (setq basept (cdr (assoc 10 edata)))
          (setq height (cdr (assoc 40 edata)))
          (setq rot (cdr (assoc 50 edata)))
          (if (not rot) (setq rot 0.0))
          (setq words (BGOL:SPTX-Words str))
          (setq n (length words))
          (if (> n 0)
            (progn
              (setq spacing (getreal (strcat "\nWord spacing <" (rtos (* height 1.5) 2 2) ">: ")))
              (if (not spacing) (setq spacing (* height 1.5)))
              (setq curpt basept)
              (foreach w words
                (BGOL:SPTX-MakeText curpt w height rot "BGOL-SPTX")
                (setq curpt (polar curpt rot (+ (* (strlen w) height 0.6) spacing)))
              )
              (entdel ent)
              (princ (strcat "\nSPTX: split into " (itoa n) " word(s) on layer BGOL-SPTX; original text removed."))
            )
            (princ "\nSelected text contains no words to split.")
          )
        )
        (princ "\nSelected entity is not a TEXT entity. Nothing split.")
      )
    )
    (princ "\nNo entity selected.")
  )
  (princ)
)

(princ "\nSPLITWRD-bgol.lsp loaded. Type SPTX to split a multi-word text into one entity per word.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
