;;; ---------------------------------------------------------------------
;;;  CLDBGN-bgol.lsp
;;;  Command:  CLB
;;;
;;;  PURPOSE
;;;  Draws a cloud-shaped callout outline (revision-cloud style) through
;;;  a series of user-picked points, using bulge-arc polyline segments of
;;;  a user-specified radius so the "bumps" are uniform and consistent.
;;;  Handy for marking up or calling attention to important content
;;;  (tables, notes, key dimensions) in a presentation drawing.
;;;
;;;  USAGE
;;;    1. Load this file (APPLOAD or drag-drop onto the drawing window).
;;;    2. Type   CLB   at the command line and press Enter.
;;;    3. Enter the arc radius to use for the cloud's bumps.
;;;    4. Pick points outlining the area to enclose; press Enter when done.
;;;    5. A single closed cloud-shaped LWPOLYLINE (bulge arcs) is created
;;;       on layer "BGOL-CLB".
;;;
;;;  NOTES
;;;    - Minimum of 3 points required to form a closed cloud.
;;;    - This is an original, independently written implementation.
;;;      It is INSPIRED by the general idea of cloud/callout drawing
;;;      utilities common in CAD markup 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:CLB-EnsureLayer (lname / )
  (if (not (tblsearch "LAYER" lname))
    (entmake (list '(0 . "LAYER")
                   '(100 . "AcDbSymbolTableRecord")
                   '(100 . "AcDbLayerTableRecord")
                   (cons 2 lname)
                   '(70 . 0)
                   '(62 . 3)          ; green
                   '(6 . "Continuous")))
  )
)

;; Compute bulge factor for a chord between two points, bumping outward,
;; using the requested arc radius. bulge = tan(included_angle / 4).
(defun BGOL:CLB-Bulge (p1 p2 radius / chord halfchord ratio incAng)
  (setq chord (distance p1 p2))
  (setq halfchord (/ chord 2.0))
  (if (> radius halfchord)
    (progn
      (setq ratio (/ halfchord radius))
      (setq incAng (* 2.0 (asin ratio)))
      (tan (/ incAng 4.0))
    )
    ;; radius too small for this chord: fall back to a modest bulge
    (tan (/ (/ pi 2.0) 4.0))
  )
)

(defun C:CLB ( / radius ptlist pt dxfdata i n p1 p2 bulge lname)
  (setq lname "BGOL-CLB")
  (BGOL:CLB-EnsureLayer lname)
  (setq radius (getreal "\nSpecify cloud arc radius: "))
  (if (and radius (> radius 0))
    (progn
      (setq ptlist '())
      (while (setq pt (getpoint
                          (if ptlist
                            (strcat "\nPick next point (Enter to finish): ")
                            "\nPick first point: ")))
        (setq ptlist (append ptlist (list pt)))
      )
      (setq n (length ptlist))
      (if (>= n 3)
        (progn
          (setq dxfdata (list '(0 . "LWPOLYLINE")
                               '(100 . "AcDbEntity")
                               (cons 8 lname)
                               '(100 . "AcDbPolyline")
                               (cons 90 n)
                               '(70 . 1)))  ; closed
          (setq i 0)
          (while (< i n)
            (setq p1 (nth i ptlist))
            (setq p2 (nth (rem (1+ i) n) ptlist))
            (setq bulge (BGOL:CLB-Bulge p1 p2 radius))
            (setq dxfdata (append dxfdata (list (cons 10 p1) (cons 42 bulge))))
            (setq i (1+ i))
          )
          (entmake dxfdata)
          (princ (strcat "\nCloud created with " (itoa n) " points on layer " lname "."))
        )
        (princ "\nAt least 3 points are required to build a cloud. Nothing created.")
      )
    )
    (princ "\nInvalid or no radius entered — command cancelled.")
  )
  (princ)
)

(princ "\nCLDBGN-bgol.lsp loaded. Type CLB to draw a cloud/callout outline.")
(princ "\n  -- Free & open-source, courtesy of BGol Community (https://bgol.in/) --")
(princ)
