;;; ---------------------------------------------------------------------
;;;  CIRTXT-bgol.lsp
;;;  Command:  CTXR
;;;
;;;  PURPOSE
;;;  Draws a circle sized and centered to enclose each selected TEXT
;;;  entity, for presentation-style highlighting of important values
;;;  (elevation callouts, benchmark points, etc.). Circle size adapts
;;;  automatically to each text's actual bounding box, so short and
;;;  long text values each get a properly-fitted circle.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   CTXR   at the command line and press Enter.
;;;    3. Select the TEXT entities to highlight, press Enter to finish.
;;;    4. A circle is created around each selected text, sized to its
;;;       bounding box diagonal (with a small padding factor), on layer
;;;       "BGOL-CTXR". The original text is left untouched.
;;;
;;;  NOTES
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of circle-around-text
;;;      highlighting utilities common in CAD 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)

(setq *bgol-ctxr-pad* 1.15)  ; radius padding multiplier

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

(defun BGOL:CTXR-MakeCircle (center radius lname / )
  (entmake (list '(0 . "CIRCLE")
                 (cons 8 lname)
                 (cons 10 center)
                 (cons 40 radius)))
)

(defun C:CTXR ( / ss n i ent obj minpt maxpt radius center lname)
  (setq lname "BGOL-CTXR")
  (BGOL:CTXR-EnsureLayer lname)
  (princ "\nSelect TEXT entities to circle: ")
  (setq ss (ssget '((0 . "TEXT"))))
  (if ss
    (progn
      (setq n 0)
      (setq i 0)
      (repeat (sslength ss)
        (setq ent (ssname ss i))
        (setq obj (vlax-ename->vla-object ent))
        (vla-getboundingbox obj 'minpt 'maxpt)
        (setq minpt (vlax-safearray->list minpt))
        (setq maxpt (vlax-safearray->list maxpt))
        (setq radius (* (/ (distance minpt maxpt) 2.0) *bgol-ctxr-pad*))
        (setq center (list (/ (+ (car minpt) (car maxpt)) 2.0)
                            (/ (+ (cadr minpt) (cadr maxpt)) 2.0)
                            (/ (+ (caddr minpt) (caddr maxpt)) 2.0)))
        (BGOL:CTXR-MakeCircle center radius lname)
        (setq n (1+ n))
        (setq i (1+ i))
      )
      (princ (strcat "\nCTXR: " (itoa n) " circle(s) drawn around selected text on layer " lname "."))
    )
    (princ "\nNo text selected.")
  )
  (princ)
)

(princ "\nCIRTXT-bgol.lsp loaded. Type CTXR to draw a fitted circle around selected text.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
