Simple Incrementing of numbers

Simple Incrementing of numbers

Anonymous
Not applicable
5,668 Views
13 Replies
Message 1 of 14

Simple Incrementing of numbers

Anonymous
Not applicable

I've been reading and trying to get an autolisp for the past two days that just does a simple increment of a selected group by a user prompted amount. I've tried several lisps I found and many were just way too in depth or just didn't help.

 

I just want to run the lisp, grab a selection of numbers I need to change, be prompted by how much do I want to change them and then it changes them for me.

 

I tried to write something simple but I clearly haven't read enough of the autolisp guides to try and write one myself because it was a flop.

 

(defun c:Increment ()
(setq inc (ssget "Select Objects for Incrementing"))
(setq num (getint "Enter amount to Increment:"))
(+ inc num)
)

 

That was the code I came up with trying to accomplish my goal but it was a failure. Can someone point me in the right direction or to an existing lisp I might have missed?

 

Thanks

-Cory

0 Likes
Accepted solutions (1)
5,669 Views
13 Replies
Replies (13)
Message 2 of 14

Satoews
Advocate
Advocate

Try this

 

(defun c:Inc ()
(prompt "Select Objects for incrementing.")
(setq inc (ssget))
(setq num (getint "Enter amount to Increment:"))
(+ (SSLENGTH inc) num)
)
Shawn T
0 Likes
Message 3 of 14

Anonymous
Not applicable

This is actually completing a process unfortunately it's not working as intended. This just returns a value equal to the number of objects selected and the number I inputed.

 

I'm trying to make this add or subtracted the inputted number from the numbers I have selected. I appreciate the attempt though!

0 Likes
Message 4 of 14

Satoews
Advocate
Advocate
(defun c:Inc ()
(prompt "Select Objects for incrementing.")
  (setq inc (ssget))
  (setq num (getint "Enter amount to Increment:"))
  (setq fin1 (- (SSLENGTH inc) num))
  (setq fin2 (+ (SSLENGTH inc) num))
  (prompt (strcat "\nTotal Increment is for addition is "(rtos fin2)" "))
  (prompt (strcat "\nTotal Increment is for subtraction is "(rtos fin1)" "))
  (princ)
)

this will give you both the addition and subtraction 😃 give me a bit to clear it up some.

Shawn T
0 Likes
Message 5 of 14

Anonymous
Not applicable

It's sort of still doing the same thing as before

 

Ex: I run INC

 

I select 8 numbers: 25, 26, 27, 28, 29, 30, 31, 32

 

I enter amount to increment: 8

 

I expect those numbers to become 33, 34, 35, 36, 37, 38, 39, 40, however, I instead get data spat into the command list saying:

 

Total Increment is for addition is 16.0000

Total Increment is for subtraction is 0.0000

 

It's purely taking the object count of 8 and adding 8 to it to get 16 and subtracting 8 from it to get 0. The data I'm trying to modify is just standard text but purely a number. How can I manipulate it so that this will actually adjust the selected numbers to the new numbers?

 

Again, I really appreciate the effort you've put in trying to sort this out, Thanks!

0 Likes
Message 6 of 14

hmsilva
Mentor
Mentor
Accepted solution

Perhaps something like this:

 

(defun c:demo (/ ent i newval num ss val)
    (if (and (princ "\nSelect Objects for Incrementing:")
             (setq ss (ssget "_:L" '((0 . "TEXT") (1 . "#*"))))
             (setq num (getint "\nEnter amount to Increment:"))
        )
        (repeat (setq i (sslength ss))
            (setq ent    (entget (ssname ss (setq i (1- i))))
                  val    (atoi (cdr (assoc 1 ent)))
                  newval (itoa (+ val num))
            )
            (entmod (subst (cons 1 newval) (assoc 1 ent) ent))
        )
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 7 of 14

Satoews
Advocate
Advocate

This is mtext your selecting? kk that makes more sense. Do you just want the readout for the additions or subtractions? Or do you want the mtext to change to the new value? 

Shawn T
0 Likes
Message 8 of 14

Anonymous
Not applicable

Program below allows user to select numerical TEXT or MTEXT, ask for an offset (+ or -) and then replace with offset value

 

(defun c:txtofst()
   (if (= ost nil)
     (setq ost (getint "\nEnter desired offset: "))
     (setq tos (getint (strcat "\nEnter desired offset <" (rtos ost) ">: ")))
   )
   (if (/= tos nil)(setq ost tos))
   (setq pt1 (getpoint "\nSelect window corner to increment numbers: "))
   (setq pt2 (getcorner pt1 "\nSelect opposite corner: "))
   (setq ss (ssget "W" pt1 pt2 '((0 . "TEXT"))))
   (setq ssm (ssget "W" pt1 pt2 '((0 . "MTEXT"))))


   (if (/= ss nil)
      (progn
         (setq sslen (sslength ss))
         (setq index 0)
         (while (/= index sslen)
            (setq elist (entget (ssname ss index)))
            (setq old_t (assoc 1 elist))
            (setq new_t (atoi (cdr (assoc 1 elist))))
            (setq new_t (itoa (+ new_t ost)))
            (setq new_t (cons 1 new_t))
            (setq elist (subst new_t old_t elist))
            (entmod elist)
            (setq index (+ index 1))
         )
     )
   )

   (if (/= ssm nil)
      (progn
         (setq sslen (sslength ssm))
         (setq index 0)
         (while (/= index sslen)
            (setq elist (entget (ssname ssm index)))
            (setq old_t (assoc 1 elist))
            (setq new_t (atoi (cdr (assoc 1 elist))))
            (setq new_t (itoa (+ new_t ost)))
            (setq new_t (cons 1 new_t))
            (setq elist (subst new_t old_t elist))
            (entmod elist)
            (setq index (+ index 1))
         )
     )
   )
)

Message 9 of 14

Satoews
Advocate
Advocate

Please use hmsilvas as the solution but i made a little edit to the code if it suits you for mtext.

 

(setq ss (ssget "_:L" '((0 . "TEXT,MTEXT") (1 . "#*")))) 

 

Shawn T
Message 10 of 14

Satoews
Advocate
Advocate
   (setq ss (ssget "W" pt1 pt2 '((0 . "TEXT")(1 . "#*")))
   (setq ssm (ssget "W" pt1 pt2 '((0 . "MTEXT")(1 . "#*")))

Also if your going to use igabriel's code I'd impliment some of silvas code as well.

 

PS: Finally if both of these solutions would fill your needs you can have more than one solution to a post. After you have adde one person to a solution, go back to the post and add the second or third. Took me a while to figure that out =P

Shawn T
Message 11 of 14

Anonymous
Not applicable

YES, that worked perfectly! I tried it both ways. Setting a value of 8 all numbers increased by 8 and setting a value of -8 decreased all numbers by 8.

 

Thank you so much this will save me a ton of time!

 

- Cory

0 Likes
Message 12 of 14

hmsilva
Mentor
Mentor

You're welcome, Cory!
Glad I could help

Henrique

EESignature

0 Likes
Message 13 of 14

3dwannab
Advocate
Advocate

This will work for negative values and allows the user to choose the padding they want. Also, some undo handling in there too.

 

;; ABOUT
;; A program to increment existing numbers by either positive or negative values.
;;
;; THREAD
;; Based on thread here: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/simple-incrementing-of-numbers/m-p/6026521#M338754
;;
;; 3DWANNAB MODS & FIXES ON 2022.07.06
;; - Added undo / error handling.
;; - Will only select and change TEXT or MTEXT that has numbers and negative numbers only.
;; - Will work with existing negative values.
;;
;; TO DO: N/A

(defun c:Num_Offset (/ *error* acDoc padding ss num i ent valOld valNew) 

  (defun *error* (errmsg) 
    (and acDoc (vla-EndUndoMark acDoc))
    (and errmsg 
         (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
         (princ (strcat "\n<< Error: " errmsg " >>\n"))
    )
  )

  (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))

  (setq padding (atof (cond ((getenv "MyNumOffsetPaddingValue")) ("1")))) ;; Get saved offset value from registry or default to 1

  (if 
    (and (princ "\nSelect Objects for Incrementing : ") 
         (setq ss (ssget "_:L" '((0 . "*TEXT") (1 . "#*,-#*")))) ;; This will only replace strings that have numbers in them, I've added minus numbers here
         (setq num (getint "\nEnter amount to Increment / Decrement (- or +) : "))
         (setq padding (cond ((getint (strcat "\nEnter padding amount <" (vl-princ-to-string (fix padding)) ">: "))) ((fix padding))))
    )

    (repeat (setq i (sslength ss)) 
      (setq ent (entget (ssname ss (setq i (1- i)))))
      (setq valOld (cdr (assoc 1 ent)))
      (setq valNew (AT:NumFix (itoa (+ (atoi valOld) num)) padding))

      (setq ent (subst (cons 1 valNew) (assoc 1 ent) ent))
      (entmod ent)
    ) ;; End repeat
  ) ;; End if

  (setenv "MyNumOffsetPaddingValue" (vl-princ-to-string padding)) ;; Write our default offset value to registry

  (*error* nil)
  (princ)
)

;; -----------------------------------------------------------------------
;; ----------------------=={ Functions START }==--------------------------

(defun AT:NumFix (numInput lenNew / lenInput minus result) 
  (vl-load-com)
  ; Fix number string with leading zeros
  ; numInput - Number string to fix
  ; lenNew - Number of characters for final string
  ; Alan J. Thompson, 10.29.09. https://www.cadtutor.net/forum/topic/14276-alanjts-misc-useful-lisp-subroutines/?do=findComment&comment=151562
  ; 3dwannab 2022.07.09. Added minus values support
  (setq minus (vl-string-search "-" numInput))
  (setq numInput (vl-string-left-trim "0" numInput)) ;; Trims existing padding
  (setq lenInput (strlen numInput))
  ;; If a minus is found set the lenInput minus 1 and trim the minus. This will get added back later on
  (if minus 
    (progn 
      (setq lenInput (abs (- 1 (strlen numInput))))
      (setq numInput (vl-string-left-trim "-" numInput))
    )
  )
  (setq result (vl-princ-to-string numInput))
  (while (< (strlen result) lenNew) 
    (setq result (strcat "0" result))
  ) ;_ while
  ;; Output the result depending on whether there's a minus or not
  (if minus 
    (strcat "-" result)
    result
  )
)

(princ)

;; -----------------------------------------------------------------------
;; ---------------------=={ Functions END }==-- --------------------------

 

 

 

 

Message 14 of 14

vladimir_michl
Advisor
Advisor

Try NumInText. See https://www.cadforum.cz/en/adding-multiplying-and-rounding-numbers-inside-dwg-drawing-texts-tip11289

 

You may need to set processing integers first:

(setq _numintextDoInts T)

 

Vladimir Michl, www.arkance-systems.cz - www.cadforum.cz

 

0 Likes