- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I would love some assistance with a Lisp routine that I used AI to create for me that is not working 100% and don't seem to be getting anywhere with AI now after many revisions.
Summary of what i'm trying to achieve.
A Lisp routine that prompts to select multiple single-line text objects and replaces the period symbols (.) with | (vertical bar). But only those that appear as decimals in numeric values.
e.g. Int. Walls 0.15, 0.35
to
Int. Walls 0|15, 0|35
Unfortunately, the below code will only result in processing 1 line of text and won't process the other lines of text when selecting multiple. It otherwise works fine.
Can this be modified for this to work?
Thanks in advance!
(defun is-digit (ch)
"Return T if character CH is a numeric digit (0-9)."
(if (and (>= (ascii ch) (ascii "0"))
(<= (ascii ch) (ascii "9")))
T
nil
)
)
(defun replace-decimal-period (str)
"Return a new string built from STR where a period that is both preceded and followed
by a digit (i.e. a decimal point) is replaced with a '|'."
(setq newStr "") ; accumulator for new string
(setq len (strlen str)) ; note: in AutoLISP, string indexes are 1-based.
(setq i 1)
(while (<= i len)
(setq char (substr str i 1))
(if (and (equal char ".")
(> i 1) ; make sure we have a previous character
(< i len) ; and a character following the period
(is-digit (substr str (- i 1) 1)) ; previous char is a digit
(is-digit (substr str (+ i 1) 1)) ; next char is a digit
)
(setq newStr (strcat newStr "|")) ; Add the pipe symbol
;; Skip adding the period by not adding `char`
)
(if (not (and (equal char ".")
(> i 1)
(< i len)
(is-digit (substr str (- i 1) 1))
(is-digit (substr str (+ i 1) 1)))
)
(setq newStr (strcat newStr char)) ; Add the character if it's not a period to replace
)
(setq i (1+ i))
)
newStr
)
(defun c:ReplaceDecimal ( / ss count i obj txt newTxt)
"Prompt to select TEXT objects. Then, for each, replace a decimal point ('.')
that is between two digits with a '|'. Works on both AcDbText and AcDbMText."
(setq ss (ssget '((0 . "TEXT,MTEXT"))))
(if ss
(progn
(setq count (sslength ss))
(setq i 0)
(while (< i count)
(setq obj (vlax-ename->vla-object (ssname ss i)))
(cond
((= (vla-get-objectname obj) "AcDbText")
(setq txt (vla-get-TextString obj))
(setq newTxt (replace-decimal-period txt))
(if (/= txt newTxt)
(vla-put-TextString obj newTxt)
)
)
((= (vla-get-objectname obj) "AcDbMText")
(setq txt (vla-get-contents obj))
(setq newTxt (replace-decimal-period txt))
(if (/= txt newTxt)
(vla-put-contents obj newTxt)
)
)
)
(setq i (1+ i))
)
(princ "\nFinished processing: decimal points replaced where applicable.")
)
(princ "\nNo text objects selected.")
)
(princ) ; exit quietly
)
Solved! Go to Solution.