Jump to content

This program is not running I don't know what are the problems pls fix it for me


Recommended Posts

Posted (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 by SLW210
Added Code Tags!
Posted

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?

Posted

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)
)

 

Posted (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 by mhupp
  • Like 1
Posted (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 by rlx
  • Like 3
Posted

@mhupp we all do it a typo. Forgot the C:.

 

(defun C:ITN () (c:IncrementTextNumber))

 

  • Like 1
Posted
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.

Posted

@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.

Posted
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.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...