;;; ---------------------------------------------------------------------
;;;  FT2M-bgol.lsp
;;;  Command:  FM2
;;;
;;;  PURPOSE
;;;  Converts TEXT/MTEXT entities written in feet-inch notation
;;;  ( e.g.  90' 20"  ->  90 feet 20 inches ) into a new metre value,
;;;  placed at the same insertion point as the original text.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   FM2   at the command line and press Enter.
;;;    3. Select one or more TEXT/MTEXT entities formatted as  ft' in"
;;;    4. Press Enter to finish selection.
;;;    5. A new text object showing the converted metre value is created
;;;       next to each selected entity, on layer "BGOL-M2FT".
;;;
;;;  NOTES
;;;    - Expected input format:  <feet>' <inches>"   e.g.  1523' 6"
;;;    - Conversion constant used: 1 foot = 0.3048 metre
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of feet-inch-to-metre text
;;;      conversion utilities that are 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:FT2M-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")))
  )
)

;; Parse a string like  1523' 6"  into a real value in decimal feet
(defun BGOL:FT2M-ParseFeetInches (str / p1 p2 ftpart inpart feet inches)
  (setq p1 (vl-string-search "'" str))
  (setq p2 (vl-string-search "\"" str))
  (if (and p1 p2 (< p1 p2))
    (progn
      (setq ftpart (substr str 1 p1))
      (setq inpart (substr str (+ p1 2) (- p2 p1 1)))
      (setq feet   (atof (vl-string-trim " " ftpart)))
      (setq inches (atof (vl-string-trim " " inpart)))
      (+ feet (/ inches 12.0))
    )
    nil ; not a recognized ft'-in" pattern
  )
)

(defun C:FM2 ( / ss n ent edata str decft meters newstr ip lyr)
  (BGOL:FT2M-EnsureLayer "BGOL-M2FT")
  (setq ss (ssget '((0 . "TEXT,MTEXT"))))
  (if ss
    (progn
      (setq n 0)
      (repeat (sslength ss)
        (setq ent   (ssname ss n))
        (setq edata (entget ent))
        (setq str   (cdr (assoc 1 edata)))
        (setq decft (BGOL:FT2M-ParseFeetInches str))
        (if decft
          (progn
            (setq meters (* decft 0.3048))
            (setq newstr (strcat (rtos meters 2 3) " m"))
            (setq ip (cdr (assoc 10 edata)))
            (entmake (list '(0 . "TEXT")
                            '(100 . "AcDbEntity")
                            (cons 8 "BGOL-M2FT")
                            '(100 . "AcDbText")
                            (cons 10 (list (+ (car ip) 0.0) (+ (cadr ip) 0.0) (caddr ip)))
                            (cons 40 (cond ((cdr (assoc 40 edata))) (t 2.5)))
                            (cons 1 newstr)
                            (cons 50 (cond ((cdr (assoc 50 edata))) (t 0.0)))
                      )
            )
          )
          (princ (strcat "\nSkipped (not ft'-in\" format): " str))
        )
        (setq n (1+ n))
      )
      (princ (strcat "\n" (itoa n) " text entities processed by FM2."))
    )
    (princ "\nNo TEXT/MTEXT entities selected.")
  )
  (princ)
)

(princ "\nFT2M-bgol.lsp loaded. Type FM2 to convert feet-inch text to metres.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
