SR211 Posted August 26 Posted August 26 (edited) (defun c:IncrementTextNumber () (setq ent (car (entsel "\nSelect the text to increment: "))) ; Select the text entity (if ent (progn (setq entData (entget ent)) ; Get the entity data (setq textString (cdr (assoc 1 entData))) ; Extract the text string ;; Find the position of the last numeric part (setq i (strlen textString)) (while (and (> i 0) (not (numberp (atoi (substr textString i 1))))) (setq i (1- i)) ) ;; Find the start of the numeric part (setq numEnd i) (while (and (> i 0) (numberp (atoi (substr textString i 1))))) (setq i (1- i)) ) (setq numStart (+ i 1)) ;; Extract and increment the number (setq numericPart (substr textString numStart (- numEnd numStart -1))) (setq numberValue (atoi numericPart)) (setq newNumberValue (+ numberValue 1)) ;; Keep leading zeros (setq newNumericPart (rtos newNumberValue 2 0)) (while (< (strlen newNumericPart) (strlen numericPart)) (setq newNumericPart (strcat "0" newNumericPart)) ) ;; Replace the old number with the new number (setq newTextString (strcat (substr textString 1 (1- numStart)) newNumericPart)) ;; Update the text in the drawing (entmod (subst (cons 1 newTextString) (assoc 1 entData) entData)) (entupd ent) (princ "\nText updated successfully.") ) (princ "\nNo text selected.") ) (princ) ) Edited August 26 by SLW210 Added Code Tags! Quote
SLW210 Posted August 26 Posted August 26 What does it show on the commandline? Did you run it in the VLIDE or equivalent? What exactly should it do? Can you post a sample drawing, before and after? Lots of similar LISPs around, where did you get this? Quote
Steven P Posted August 26 Posted August 26 To many ')' in the second while line I think - this will increase numbers: I'd rename the LISP IncrementTextNumber is far too many letters to type (mine is 'qt+' or 'qt-' for + or - 1) (defun c:IncrementTextNumber () (setq ent (car (entsel "\nSelect the text to increment: "))) ; Select the text entity (if ent (progn (setq entData (entget ent)) ; Get the entity data (setq textString (cdr (assoc 1 entData))) ; Extract the text string ;; Find the position of the last numeric part (setq i (strlen textString)) (while (and (> i 0) (not (numberp (atoi (substr textString i 1))))) (setq i (1- i)) ) ;; Find the start of the numeric part (setq numEnd i) (while (and (> i 0) (numberp (atoi (substr textString i 1)))) ;) (setq i (1- i)) ) (setq numStart (+ i 1)) ;; Extract and increment the number (setq numericPart (substr textString numStart (- numEnd numStart -1))) (setq numberValue (atoi numericPart)) (setq newNumberValue (+ numberValue 1)) ;; Keep leading zeros (setq newNumericPart (rtos newNumberValue 2 0)) (while (< (strlen newNumericPart) (strlen numericPart)) (setq newNumericPart (strcat "0" newNumericPart)) ) ;; Replace the old number with the new number (setq newTextString (strcat (substr textString 1 (1- numStart)) newNumericPart)) ;; Update the text in the drawing (entmod (subst (cons 1 newTextString) (assoc 1 entData) entData)) (entupd ent) (princ "\nText updated successfully.") ) ; end progn (princ "\nNo text selected.") ) ; end if (princ) ) Quote
mhupp Posted August 26 Posted August 26 (edited) On 8/26/2024 at 8:09 AM, Steven P said: ...I'd rename the LISP IncrementTextNumber is far too many letters to type (mine is 'qt+' or 'qt-' for + or - 1) You could also make sub abbreviated Lisps that calling the longer one. autocad does this with lots of commands like "circle" or just C for example. (defun C:ITN () (C:IncrementTextNumber)) (defun c:IncrementTextNumber () (setq ent (car (entsel "\nSelect the text to increment: "))) ; Select the text entity .... Edited August 27 by mhupp 1 Quote
rlx Posted August 26 Posted August 26 (edited) you could rewrite your while loops to : (while (and (> i 0) (not (wcmatch (substr textstring i 1) "#"))) (setq i (1- i))) (while (and (> i 0) (wcmatch (substr textstring i 1) "#")) (setq i (1- i))) this may fix some or all of your problems , it depends on the format of your texts. If your text ends with some letters you still have to append those too. for example (I disabled entsel and replaced it with a fix text) (defun c:IncrementTextNumber ( / ent entData textString i numEnd numStart numericPart numberValue newNumberValue newNumericPart newTextString ) ;(setq ent (car (entsel "\nSelect the text to increment: "))) ; Select the text entity (setq ent t) (if ent (progn ;(setq entData (entget ent)) ; Get the entity data ;(setq textString (cdr (assoc 1 entData))) ; Extract the text string ;;; *** testing (setq textString "123abc456def789ghi") ;; Find the position of the last numeric part (setq i (strlen textString)) ;(while (and (> i 0) (not (numberp (atoi (substr textString i 1))))) (setq i (1- i)) ) (while (and (> i 0) (not (wcmatch (substr textstring i 1) "#"))) (setq i (1- i))) ;; Find the start of the numeric part (setq numEnd i) ;(while (and (> i 0) (numberp (atoi (substr textString i 1)))) (setq i (1- i)) ) (while (and (> i 0) (wcmatch (substr textstring i 1) "#")) (setq i (1- i))) (setq numStart (+ i 1)) ;; Extract and increment the number (setq numericPart (substr textString numStart (- numEnd numStart -1))) (setq numberValue (atoi numericPart)) (setq newNumberValue (+ numberValue 1)) ;; Keep leading zeros (setq newNumericPart (rtos newNumberValue 2 0)) (while (< (strlen newNumericPart) (strlen numericPart)) (setq newNumericPart (strcat "0" newNumericPart)) ) ;; Replace the old number with the new number (setq newTextString (strcat (substr textString 1 (1- numStart)) newNumericPart)) (if (< numEnd (strlen textstring)) (setq newTextString (strcat newTextString (substr textString (1+ numEnd))))) ;| ;; Update the text in the drawing (entmod (subst (cons 1 newTextString) (assoc 1 entData) entData)) (entupd ent) |; (alert (strcat "Org. text : " (vl-princ-to-string textString) "\nNew text : " (vl-princ-to-string newTextString))) (princ "\nText updated successfully.") ) (princ "\nNo text selected.") ) (princ) ) (c:IncrementTextNumber) Edited August 26 by rlx 3 Quote
BIGAL Posted August 26 Posted August 26 @mhupp we all do it a typo. Forgot the C:. (defun C:ITN () (c:IncrementTextNumber)) 1 Quote
mhupp Posted August 27 Posted August 27 22 hours ago, BIGAL said: @mhupp we all do it a typo. Forgot the C:. Yep been programing mostly in VBA as of late. haven't used lisp in almost 2 years now. so its all slipping away. Quote
BIGAL Posted August 27 Posted August 27 @mhupp Don't want to steal post but don't forget Autodesk said about 5 years ago was dropping support for VBA. Who knows if changed their mind. Quote
mhupp Posted August 27 Posted August 27 9 minutes ago, BIGAL said: @mhupp Don't want to steal post but don't forget Autodesk said about 5 years ago was dropping support for VBA. Who knows if changed their mind. That very well might be but use it for Excel and Solidworks with my new job. I deal alot with Multi level BOMS in a system that is quite old and can't really handle it without help. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.