Jump to content

Need a LISP to modify XXX characters and increment them


Recommended Posts

Posted

@Steven P@mhupp
I got done several drawings quickly using the code. Saved a lot of time. Thank you!

I came across a drawing which had 311 series. So I changed the code to replace 202 to 311 all places.

Now I am wondering if we can get the prefix input from user first that will determine what series it is (say 202, 311 or any 3 digit number), and then we take the second input "Specify starting number"

Since the first input is required only first time on the drawing, when the next time I start the command in the same drawing, it automatically knows what the first prefix is.

Is that possible? This will make this code super smart I think.

 

If not, I am happy as is too.

Posted (edited)
On 11/19/2022 at 1:04 AM, shailujp said:

Now I am wondering if we can get the prefix input from user first that will determine what series it is (say 202, 311 or any 3 digit number), and then we take the second input "Specify starting number"

 

So it was something like  50-CHWS-311XXX-C1A instead of 202 ?

Kinda planed for that so now the variable replace uses getstring to search for what you want to replace with the incremental numbering.

 

Example

INCATT3

Starting Number: 15

Text to Replace: XXX

 

50-CHWS-311XXX-C1A would change to 50-CHWS-311015-C1A

*only replaces the first XXX it comes across

50-CXXX-311XXX-C1A would change to 50-C015-311XXX-C1A

 

 

(defun c:INCATT3 ( / num tag pre ss1 ) 
  (vl-load-com)
  (if (and
        (setq i (getint "\nStarting Number: "))
        (setq replace (getstring "\nText to Replace: "))
        (setq l (1+ (strlen replace)))
      )
    (progn      
      (setq num (AT:NumFix (itoa i) (strlen replace)))  ;convert inter to sring before selection
      (while (setq ss (ssget "_+.:E:S:L" '((0 . "INSERT") (66 . 1))))
        (if (and (setq tagstring (getpropertyvalue (ssname ss 0) "LINE")) (setq n (vl-string-search replace tagstring))) ;if block has ATTRIBUTE line and contains
          (progn                                                                                                         ;the string "202XXX" update to new string 
            (setpropertyvalue (ssname ss 0) "LINE" (strcat (substr tagstring 1 n) num (substr tagstring (+ n l))))                 
            (setq i (1+ i))
            (setq num (AT:NumFix (itoa i) (strlen replace))) 
          )
          (princ (strcat "\"LINE\" Attribute not found or doesn't contain \"" replace "\""))
        )        
      )
    )
  )
  (princ)
)
(defun AT:NumFix (s n)
 ;; Fix number string with leading zeros
 ;; s - Number string to fix
 ;; n - Number of characters for final string
 ;; Alan J. Thompson, 10.29.09
 ;; (AT:NumFix i 3) i= 1 = 001
  (if (< (strlen s) n)
    (AT:NumFix (strcat "0" s) n)
    s
  )
)

 

Edited by mhupp
updated code
Posted (edited)
1 hour ago, mhupp said:

 

So it was something like  50-CHWS-311XXX-C1A instead of 202 ?

Kinda planed for that so now the variable replace uses getstring to search for what you want to replace with the incremental numbering.

 

Example

INCATT3

Starting Number: 15

Text to Replace: XXX

 

50-CHWS-311XXX-C1A would change to 50-CHWS-311015-C1A

*only replaces the first XXX it comes across

50-CXXX-311XXX-C1A would change to 50-C015-311XXX-C1A

 

 

(defun c:INCATT3 ( / num tag pre ss1 ) 
  (vl-load-com)
  (if (and
        (setq i (getint "\nStarting Number: "))
        (setq replace (getstring "\nText to Replace: "))
        (setq l (1+ (strlen replace)))
      )
    (progn      
      (setq num (AT:NumFix (itoa i) 3)) ;convert inter to sring before selection
      (while (setq ss (ssget "_+.:E:S:L" '((0 . "INSERT") (66 . 1))))
        (if (and (setq tagstring (getpropertyvalue (ssname ss 0) "LINE")) (setq n (vl-string-search replace tagstring))) ;if block has ATTRIBUTE line and contains
          (progn                                                                                                         ;the string "202XXX" update to new string 
            (setpropertyvalue (ssname ss 0) "LINE" (strcat (substr tagstring 1 n) num (substr tagstring (+ n l))))                 
            (setq i (1+ i))
            (setq num (AT:NumFix (itoa i) 3))
          )
          (princ (strcat "\"LINE\" Attribute not found or doesn't contain \"" replace "\""))
        )        
      )
    )
  )
  (princ)
)
(defun AT:NumFix (s n)
 ;; Fix number string with leading zeros
 ;; s - Number string to fix
 ;; n - Number of characters for final string
 ;; Alan J. Thompson, 10.29.09
 ;; (AT:NumFix i 3) i= 1 = 001
  (if (< (strlen s) n)
    (AT:NumFix (strcat "0" s) n)
    s
  )
)

 

That worked wonderfully.
I came across only one drawing (seemed like an exception though) which has only one X (eg. 200-DD-11021200X-C1A). So I copied the code and changed all "3" to "1" and "001" to "1". It worked for that case as well.
 

(defun c:INCATTM2 ( / num tag pre ss1 ) 
  (vl-load-com)
  (if (and
        (setq i (getint "\nStarting Number: "))
        (setq replace (getstring "\nText to Replace: "))
        (setq l (1+ (strlen replace)))
      )
    (progn      
      (setq num (AT:NumFix (itoa i) 1)) ;convert inter to sring before selection
      (while (setq ss (ssget "_+.:E:S:L" '((0 . "INSERT") (66 . 1))))
        (if (and (setq tagstring (getpropertyvalue (ssname ss 0) "LINE")) (setq n (vl-string-search replace tagstring))) ;if block has ATTRIBUTE line and contains
          (progn                                                                                                         ;the string "202XXX" update to new string 
            (setpropertyvalue (ssname ss 0) "LINE" (strcat (substr tagstring 1 n) num (substr tagstring (+ n l))))                 
            (setq i (1+ i))
            (setq num (AT:NumFix (itoa i) 1))
          )
          (princ (strcat "\"LINE\" Attribute not found or doesn't contain \"" replace "\""))
        )        
      )
    )
  )
  (princ)
)
(defun AT:NumFix (s n)
 ;; Fix number string with leading zeros
 ;; s - Number string to fix
 ;; n - Number of characters for final string
 ;; Alan J. Thompson, 10.29.09
 ;; (AT:NumFix i 1) i= 1 = 1
  (if (< (strlen s) n)
    (AT:NumFix (strcat "0" s) n)
    s
  )
)

 

I guess I can copy and create for 2 digit as well similarly if needed.

I want to check one last thing. Can this code ask for same length input (say two XX) and replace same length output (01) and so on?
X      1

XX   01

XXX 001

XXXX 0001

 

Edited by shailujp
Posted (edited)
54 minutes ago, shailujp said:

I guess I can copy and create for 2 digit as well similarly if needed.

I want to check one last thing. Can this code ask for same length input (say two XX) and replace same length output (01) and so on?
X      1

XX   01

XXX 001

XXXX 0001

 

updadte  this line of code in both places.

(setq num (AT:NumFix (itoa i) 3) 
(setq num (AT:NumFix (itoa i) (strlen replace)))

 

Edited by mhupp
Posted
5 hours ago, mhupp said:

 

updadte  this line of code in both places.

(setq num (AT:NumFix (itoa i) 3) 
(setq num (AT:NumFix (itoa i) (strlen replace)))

 

mhupp, you are AWESOME! That worked perfectly well. Thank you so much for your help on this. Much appreciated!

  • 2 weeks later...
Posted (edited)

hello,

 

About the last comment..       (updadte  this line of code in both places.)

Where can I update? 

 

(setq num (AT:NumFix (itoa i) 3)

(setq num (AT:NumFix (itoa i) (strlen replace)))

Edited by cadmaster1004
Posted
1 hour ago, cadmaster1004 said:

hello,

 

About the last comment..       (updadte  this line of code in both places.)

Where can I update?

 

last code I posted is updated.

  • Like 1
Posted

Your answer was there!
Thank you very much.

  • Like 1

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