;;; ---------------------------------------------------------------------------
;;; DimTattle.lsp
;;; code compiled by YZ August 2026
;;; ---------------------------------------------------------------------------
;;; Pre-issue QA check: finds every dimension whose text has been manually
;;; overridden and recolours it red so it cannot be missed.
;;;
;;; A dimension that has been typed over no longer reports the geometry it
;;; measures. That is the single most dangerous thing that can be left in a
;;; drawing before it goes out, because it looks correct and reads correct
;;; while being a hand-typed lie.
;;;
;;; HOW THE OVERRIDES ARE DETECTED
;;; DXF group 1 on a DIMENSION holds the text override:
;;;
;;;   ""        - no override, the dimension reports its measured value
;;;   "*<>*"    - the measured value is still present (shown by the <> token)
;;;               with a prefix and/or suffix added around it, e.g. "<>mm".
;;;               This is legitimate annotation, not a lie, so it is ignored.
;;;   anything  - the measured value has been replaced outright. THIS is what
;;;   else        gets flagged.
;;;
;;; The selection filter therefore selects dimensions that are NOT empty and
;;; NOT containing "<>".
;;;
;;; The recolour is a real edit and lands in its own undo group, so a single
;;; U immediately afterwards puts the drawing back.
;;;
;;; COMMAND:  DIMTATTLE  - highlight dimensions with overridden text
;;; ---------------------------------------------------------------------------

;; ---------------------------------------------------------------------------
;; c:DIMTATTLE  -  main routine
;; ---------------------------------------------------------------------------
(defun c:DIMTATTLE ( / *error* vars vals sel count )

    (setq vars '("CMDECHO")
          vals (mapcar 'getvar vars)
    )

    (defun DimTattle:Restore ( )
        (mapcar 'setvar vars vals)
        (if (= 8 (logand 8 (getvar "UNDOCTL")))
            (command "_.UNDO" "_End")
            (vl-catch-all-apply '(lambda ( ) (*pop-error-mode*)) '())
        )
        (princ)
    )

    (defun *error* ( msg )
        (DimTattle:Restore)
        (if (and msg (not (wcmatch (strcase msg t) "*break*,*cancel*,*exit*")))
            (princ (strcat "\n** DIMTATTLE error: " msg " **"))
        )
        (princ)
    )

    (setvar "CMDECHO" 0)
    ;; AutoCAD 2015 and later refuse (command) inside an *error* handler
    ;; unless the routine says up front that it will use one. Restore does,
    ;; to close this undo group. The declaring call is absent on older
    ;; releases, so it is wrapped rather than tested for.
    (vl-catch-all-apply '(lambda ( ) (*push-error-using-command*)) '())
    (command "_.UNDO" "_Begin")

    ;; -----------------------------------------------------------------------
    ;; "_X" scans the whole database rather than the current view, so nothing
    ;; off screen is missed. The filter is read as:
    ;;
    ;;    entity is a DIMENSION
    ;;    AND NOT ( group 1 is empty  OR  group 1 contains "<>" )
    ;;
    ;; Note this searches model and paper space alike; a dimension overridden
    ;; on a layout sheet is every bit as dangerous as one in model space.
    ;; -----------------------------------------------------------------------
    (setq sel
        (ssget "_X"
           '(
                (0 . "DIMENSION")
                (-4 . "<NOT")
                    (-4 . "<OR")
                        (1 . "")
                        (1 . "*<>*")
                    (-4 . "OR>")
                (-4 . "NOT>")
            )
        )
    )

    ;; -----------------------------------------------------------------------
    ;; Guard the empty case. The original fed a nil selection straight into
    ;; CHPROP, which dropped the user into an interactive "Select objects:"
    ;; prompt in the middle of what should have been an automatic check.
    ;; -----------------------------------------------------------------------
    (if sel
        (progn
            (setq count (sslength sel))
            (command "_.CHPROP" sel "" "_Color" "1" "")
            (princ (strcat "\n" (itoa count)
                           " overridden dimension"
                           (if (= 1 count) "" "s")
                           " found and coloured red - press U to undo the highlight."
                   )
            )
        )
        (princ "\nNo overridden dimensions found - drawing is clean.")
    )

    (DimTattle:Restore)
    (princ)
)

(princ)
