;; CADLisp Tools - Room Area Label
(vl-load-com)

(defun ct-ra:current-layer-writable-p (/ row flags color)
  (and (setq row (tblsearch "LAYER" (getvar "CLAYER")))
       (setq flags (cdr (assoc 70 row)) color (cdr (assoc 62 row)))
       (= 0 (logand flags 21))
       (>= color 0)))

(defun ct-ra:2d-polyline-p (data / flags)
  (and data
       (or (= (cdr (assoc 0 data)) "LWPOLYLINE")
           (and (= (cdr (assoc 0 data)) "POLYLINE")
                (setq flags (cdr (assoc 70 data)))
                (= 0 (logand flags 88))))))

(defun ct-ra:replace-all (text find replacement / position start)
  (setq start 0)
  (while (setq position (vl-string-search find text start))
    (setq text (strcat (substr text 1 position) replacement
                       (substr text (+ position (strlen find) 1)))
          start (+ position (strlen replacement))))
  text)

(defun ct-ra:escape-mtext (value)
  (setq value (ct-ra:replace-all value "\\" "\\\\")
        value (ct-ra:replace-all value "{" "\\{")
        value (ct-ra:replace-all value "}" "\\}"))
  value)

(defun ct-ra:make-mtext (point value / normal ucs-x)
  (setq normal (trans '(0.0 0.0 1.0) 1 0 T)
        ucs-x (trans '(1.0 0.0 0.0) 1 0 T))
  (entmakex
    (list '(0 . "MTEXT") (cons 8 (getvar "CLAYER"))
          (cons 10 point) (cons 40 (getvar "TEXTSIZE"))
          (cons 1 value) (cons 7 (getvar "TEXTSTYLE"))
          '(71 . 5) (cons 11 ucs-x) (cons 210 normal))))

(defun c:ROOMAREA (/ *error* area closed data ent name obj pick point result undo-started)
  (defun *error* (message)
    (if undo-started (command-s "_.UNDO" "_End"))
    (if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*")))
      (prompt (strcat "\nROOMAREA error: " message)))
    (princ))
  (cond
    ((not (ct-ra:current-layer-writable-p))
      (prompt "\nThe current layer is off, frozen, locked, or xref-dependent; no label created."))
    ((null (setq pick (entsel "\nPick a closed room polyline: ")))
      (prompt "\nNo boundary selected."))
    ((not (ct-ra:2d-polyline-p (setq data (entget (setq ent (car pick))))))
      (prompt "\nThe selected object is not a supported 2D polyline."))
    ((or (vl-catch-all-error-p
           (setq closed (vl-catch-all-apply 'vlax-curve-isClosed (list ent))))
         (not closed))
      (prompt "\nThe selected boundary is open or invalid."))
    ((= (setq name (getstring T "\nRoom name: ")) "")
      (prompt "\nRoom name cannot be blank."))
    ((vl-catch-all-error-p
       (setq obj (vl-catch-all-apply 'vlax-ename->vla-object (list ent))))
      (prompt "\nThe selected boundary cannot be accessed through ActiveX."))
    ((vl-catch-all-error-p
       (setq result (vl-catch-all-apply 'vla-get-Area (list obj))))
      (prompt "\nThe selected boundary has no valid calculable area."))
    ((null (setq point (getpoint "\nLabel insertion point: ")))
      (prompt "\nNo insertion point supplied."))
    (T
      (setq area result point (trans point 1 0))
      (if (= 0 (logand 8 (getvar "UNDOCTL")))
        (progn (command "_.UNDO" "_Begin") (setq undo-started T)))
      (if (ct-ra:make-mtext point
            (strcat (ct-ra:escape-mtext name) "\\PAREA=" (rtos area 2 2)))
        (prompt "\nRoom area label created.")
        (prompt "\nThe room area label could not be created."))
      (if undo-started (progn (command "_.UNDO" "_End") (setq undo-started nil)))))
  (princ))
