;; ===================================================================== ;; DecreaseTextValue.lsp - subtract a value from numbers in texts ;; (e.g. recalculating spot levels) ;; Command: DECREASETEXTVALUE ;; AutoCAD / GstarCAD (AutoLISP). Description and installation: ;; https://fixcontrol.co/tools/autocad-lisp-scripts ;; ;; WHAT IT DOES: ;; 1. Asks for the value to subtract from the numbers (Enter = 0.1). ;; A negative number (e.g. -0.25) INCREASES the values. ;; 2. You select texts (TEXT, MTEXT) containing numbers, ;; e.g. levels "+12,30" or "11.85". ;; 3. The value is subtracted from each number and the text is ;; replaced. Texts that are not a number are skipped (listed on ;; the command line). ;; Result: decimal comma, the same number of decimal places as the ;; original (or as the entered value, if it has more), a leading "+" ;; is kept for results >= 0. ;; Both comma and dot are accepted. Undo with U. ;; ;; OPTIONS: no settings in the file - the value is entered at run time. ;; ===================================================================== ;; number of digits after the dot in a string (defun DTV-decimals (s / p n) (setq n 0) (if (setq p (vl-string-search "." s)) (progn (setq p (+ p 2)) (while (and (<= p (strlen s)) (wcmatch (substr s p 1) "#")) (setq n (1+ n) p (1+ p)) ) ) ) n ) (defun c:DecreaseTextValue (/ *error* ss i ent txtVal numVal decrement decPrec prec newVal entData plus oldDimzin cnt) (vl-load-com) (defun *error* (msg) (if oldDimzin (setvar "DIMZIN" oldDimzin)) (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")) (princ (strcat "\nError: " msg)) ) (princ) ) ;; rtos without trimming zeros (DIMZIN = 0), restored at the end (setq oldDimzin (getvar "DIMZIN")) (setvar "DIMZIN" 0) (setq decrement (getreal "\nValue to subtract (default 0.1): ")) (if (not decrement) (setq decrement 0.1) ) (setq decPrec (DTV-decimals (vl-string-right-trim "." (vl-string-right-trim "0" (rtos (abs decrement) 2 6))))) (princ "\nSelect texts to modify: ") (setq ss (ssget '((0 . "TEXT,MTEXT")))) (if ss (progn (setq cnt 0) (repeat (setq i (sslength ss)) (setq ent (ssname ss (setq i (1- i)))) (setq txtVal (cdr (assoc 1 (entget ent)))) ;; no \P, no spaces, comma -> dot (setq txtVal (vl-string-subst "" "\\P" txtVal)) (setq txtVal (vl-string-trim " " txtVal)) (setq txtVal (vl-string-subst "." "," txtVal)) (setq plus (= (substr txtVal 1 1) "+")) (if (numberp (setq numVal (vl-catch-all-apply 'read (list txtVal)))) (progn (setq prec (max (DTV-decimals txtVal) decPrec)) (setq newVal (rtos (- numVal decrement) 2 prec)) (setq newVal (vl-string-subst "," "." newVal)) (if (and plus (>= (- numVal decrement) 0)) (setq newVal (strcat "+" newVal)) ) (setq entData (entget ent)) (setq entData (subst (cons 1 newVal) (assoc 1 entData) entData)) (entmod entData) (setq cnt (1+ cnt)) ) (princ (strcat "\nCannot convert: " txtVal)) ) ) (princ (strcat "\nModified " (itoa cnt) " text objects.")) ) (princ "\nNo text objects selected.") ) (setvar "DIMZIN" oldDimzin) (princ) ) (princ "\nDECREASETEXTVALUE loaded. Type DECREASETEXTVALUE to run.") (princ)