Find and Replace predefined text with today date and month

Find and Replace predefined text with today date and month

karthikeyanskm
Contributor Contributor
321 Views
1 Reply
Message 1 of 2

Find and Replace predefined text with today date and month

karthikeyanskm
Contributor
Contributor

Wanted to create lisp for

Predefined text/mtext/attribute value - ££ which shall be replaced with today's date

Predefined text/mtext/attribute value - ## which shall be replaced with today's month

 

But chatgpt open ai code is not working for it.

i had created this lisp with a openAI

(defun c:SUB ()
(setq old_text "££")
(setq new_text (rtos (getvar "DATE") 2 100))
(setq ss (ssget "X" '((0 . "TEXT")(1 . old_text))))
(if ss
(progn
(setq len (sslength ss))
(repeat len
(setq ent (ssname ss (setq len (1- len))))
(entmod (subst (cons 1 new_text) (assoc 1 (entget ent)) (entget ent)))
)
(princ (strcat "Text '" old_text "' replaced with today's date."))
)
(princ (strcat "Text '" old_text "' not found."))
)
(princ)
(setq old_text "££")
(setq new_text (rtos (getvar "DATE") 2 100))
(setq ss (ssget "X" '((0 . "TEXT")(1 . old_text))))
(if ss
(progn
(setq len (sslength ss))
(repeat len
(setq ent (ssname ss (setq len (1- len))))
(entmod (subst (cons 1 new_text) (assoc 1 (entget ent)) (entget ent)))
)
(princ (strcat "Text '" old_text "' replaced with today's date."))
)
(princ (strcat "Text '" old_text "' not found."))
)
(princ)

(setq old_text "##")
(setq today (getvar "DATE"))
(setq new_text (rtos (car today) 2 100))
(setq ss (ssget "X" '((0 . "TEXT")(1 . old_text))))
(if ss
(progn
(setq len (sslength ss))
(repeat len
(setq ent (ssname ss (setq len (1- len))))
(entmod (subst (cons 1 new_text) (assoc 1 (entget ent)) (entget ent)))
)
(princ (strcat "Text '" old_text "' replaced with today's month."))
)
(princ (strcat "Text '" old_text "' not found."))
)
(princ)
)

0 Likes
322 Views
1 Reply
Reply (1)
Message 2 of 2

Kent1Cooper
Consultant
Consultant

Don't use AI to generate AutoLisp code yet -- it's woefully incapable, though presumably it will get better in time.

 

So many things....

 

You want the CDATE System Variable, to get YYYYMMDD format, not DATE which gives the Julian day number [useful for comparing dates, but not for knowing what today is].  The fact that it asks for 100 decimal places is laughable.

 

This:

(setq ss (ssget "X" '((0 . "TEXT") (1 . old_text))))

is wrong for putting a variable requiring evaluation into a "quoted" list that cannot contain such things.  It should be:

(setq ss (ssget "X" (list '(0 . "TEXT") (cons 1 old_text))))

Read about the (list) and (quote) functions in the AutoLisp Reference.

 

This:

(cons 1 new_text)

within the (subst) functions should be using only portions of what's in that variable:

(cons 1 (substr new_text 7 2))

for the day, and

(cons 1 (substr new_text 5 2))

for the month.

 

Try those correctios and see whether it might work [I haven't tried it].  I did not go through analyzing parentheses, etc., and there may be other errors I didn't notice yet.

Kent Cooper, AIA
0 Likes