;; CADLisp Tools - Hole Coordinate Table (WCS)
(defun ct-ht: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-ht: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 . 1) (cons 11 ucs-x) (cons 210 normal))))

(defun c:HOLETABLE (/ *error* center content data ent i point ss undo-started)
  (defun *error* (message)
    (if undo-started (command-s "_.UNDO" "_End"))
    (if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*")))
      (prompt (strcat "\nHOLETABLE error: " message)))
    (princ))
  (cond
    ((not (ct-ht:current-layer-writable-p))
      (prompt "\nThe current layer is off, frozen, locked, or xref-dependent; no table created."))
    (T
      (prompt "\nSelect hole circles: ")
      (if (setq ss (ssget '((0 . "CIRCLE"))))
        (if (setq point (getpoint "\nTable insertion point: "))
          (progn
            (setq i 0 content "HOLE  X  Y")
            (repeat (sslength ss)
              (setq ent (ssname ss i) data (and ent (entget ent)))
              (if (and data (assoc 10 data))
                (progn
                  (setq center (trans (cdr (assoc 10 data)) ent 0)
                        content (strcat content "\\P" (itoa (1+ i)) "  "
                                        (rtos (car center) 2 3) "  "
                                        (rtos (cadr center) 2 3)))))
              (setq i (1+ i)))
            (if (= 0 (logand 8 (getvar "UNDOCTL")))
              (progn (command "_.UNDO" "_Begin") (setq undo-started T)))
            (if (ct-ht:make-mtext (trans point 1 0) content)
              (prompt "\nWCS coordinate table created.")
              (prompt "\nThe coordinate table could not be created."))
            (if undo-started (progn (command "_.UNDO" "_End") (setq undo-started nil))))
          (prompt "\nNo insertion point supplied."))
        (prompt "\nNo circles selected."))))
  (princ))
