INCREMENT A SPECIFIC LETTER WITHIN TEXT

INCREMENT A SPECIFIC LETTER WITHIN TEXT

Anonymous
Not applicable
2,708 Views
19 Replies
Message 1 of 20

INCREMENT A SPECIFIC LETTER WITHIN TEXT

Anonymous
Not applicable

Hi, all 

i am really needing some help here, i have over 100 roads that i need to name daily.  I'm wanting to change the letter in the middle of the text in increments of 1  ( A,B,C--> AA,AB)   without effecting anything else.

I have found many LISP's that can change letters but not one that can do this.

 

what i want is this :

 

STREET A: 16-7.3

STREET B: 16-7.3

STREET C: 16-7.3 

the numbers at the end change, so i would like to ignore those.

 

I guess if we can use a Lisp that looks at anything before ":" and replaces it with (STREET "A" ) and continues to count up. 

 

Another example to clarify things:

eg:

STREET 😧 16-7.3  = STREET A: 16-7.3

STREET 😧 7-6  = STREET B: 7-6

STREET 😧 12.5-8  = STREET C: 12.5-8

 

 

Thank you in advance for the help everyone

 

 

0 Likes
Accepted solutions (1)
2,709 Views
19 Replies
Replies (19)
Message 2 of 20

ВeekeeCZ
Consultant
Consultant

Just quickly, let know if that works.

It only handles TEXTs containing "STREET *: *"

 

(vl-load-com)

(defun c:Streets+ ( / inc-str ss i ed txt cln num suf)
  
  (defun inc-str (string)
    (cond ((= "Z" string)          "AA")
	  ((wcmatch string "@Z")   (strcat (chr (1+ (ascii (substr string 1 1)))) "A"))
	  ((wcmatch string "@ZZ")  (strcat (chr (1+ (ascii (substr string 1 1)))) "AA"))
	  ((= 1 (strlen string))   (chr (1+ (ascii string))))
	  ((= 2 (strlen string))   (strcat (substr string 1 1) (chr (1+ (ascii (substr string 2 1))))))
	  ((princ "\nwarning: unhandled string passed to function inc-str") nil)))
  
  
  (if (setq ss (ssget "_:L" '((0 . "*TEXT") (1 . "STREET *:*"))))
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i))))
	    txt (cdr (assoc 1 ed))
	    cln (vl-string-search ":" txt)
	    num (substr txt 8 (- cln 7))
	    suf (substr txt (1+ cln))
	    txt (strcat "STREET " (inc-str num) suf))
      (entmod (subst (cons 1 txt)
		     (assoc 1 ed)
		     ed))))
  (princ)
  )
Message 3 of 20

Anonymous
Not applicable

Thanks Beekecz,

 

 

 

0 Likes
Message 4 of 20

ВeekeeCZ
Consultant
Consultant

 

 

(vl-load-com)

(defun c:StreetSeries ( / inc-str ss i ed txt cln suf)
  
  (defun inc-str (string)
    (cond ((= "Z" string)          "AA")
	  ((wcmatch string "@Z")   (strcat (chr (1+ (ascii (substr string 1 1)))) "A"))
	  ((= 1 (strlen string))   (chr (1+ (ascii string))))
	  ((= 2 (strlen string))   (strcat (substr string 1 1) (chr (1+ (ascii (substr string 2 1))))))))
  
  (or *strser-num*
      (setq *strser-num* "A"))
  (if (/= "" (setq tmp (getstring (strcat "\nStart with letter <" *strser-num*  ">: "))))
    (setq *strser-num* tmp))

  (if (setq ss (ssget "_:L" '((0 . "*TEXT") (1 . "STREET *:*"))))
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i))))
	    txt (cdr (assoc 1 ed))
	    cln (vl-string-search ":" txt)
	    suf (substr txt (1+ cln))
	    txt (strcat "STREET " *strser-num* suf)
	    *strser-num* (inc-str *strser-num*))
      (entmod (subst (cons 1 txt)
		     (assoc 1 ed)
		     ed))))
  (princ)
  )

 

0 Likes
Message 5 of 20

john.uhden
Mentor
Mentor

Hi, MJ.

I think we did this before with Doug Broad's help, that is advancing the alpha characters the same way as Excel does columns.  I really didn't study your routine (yes, shame on me), but I think the OP is pointing out that with each change the prospective candidate must watch out for something that may have just changed (as part of the routine).  That could slow things down a tad, but it's not as though a pot hole has turned into a sink hole.

Do you want me to look it up in my junk pile of soft-garbage?  It's a good thing that hard drives don't start stinking after years of composting code.

Wait a second... what's that I smell?

John F. Uhden

0 Likes
Message 6 of 20

ВeekeeCZ
Consultant
Consultant

Hey John, thanks for the offer. I did a quick research before and now with your info again... do you mean THIS thread?

I looks like it only converts integer 2 letters...

Although I'm quite happy with the simple one I have, I'll happily take the better one.

 

0 Likes
Message 7 of 20

john.uhden
Mentor
Mentor
You found the right thread. I think my post #15 includes the improvement
on what Doug had provided.
Then again, I also think that the premise of alien ancestors is credible.

John F. Uhden

0 Likes
Message 8 of 20

Anonymous
Not applicable

That's great beekeecz,

 

so you know why it labels them in the reverse order of selection tho? 

 

 

0 Likes
Message 9 of 20

ВeekeeCZ
Consultant
Consultant
Accepted solution

Sure I know, I've wrote it that way. It's about the way how you process the loop. HERE you have many examples how you can do that. I used the "reverse repeat" method (2a) before, now method no. 2. Reverse method is a bit shorter, I would say more straight-forward, potentially less faulty. And the order usually doesn't matter... 

 

(vl-load-com)

(defun c:StreetSeries ( / inc-str ss i ed txt cln suf)
  
  (defun inc-str (string)
    (cond ((= "Z" string)          "AA")
	  ((wcmatch string "@Z")   (strcat (chr (1+ (ascii (substr string 1 1)))) "A"))
	  ((= 1 (strlen string))   (chr (1+ (ascii string))))
	  ((= 2 (strlen string))   (strcat (substr string 1 1) (chr (1+ (ascii (substr string 2 1))))))))
  
  (or *strser-num*
      (setq *strser-num* "A"))
  (if (/= "" (setq tmp (getstring (strcat "\nStart with letter <" *strser-num*  ">: "))))
    (setq *strser-num* tmp))

  (if (setq i 0
            ss (ssget "_:L" '((0 . "*TEXT") (1 . "STREET *:*"))))
    (repeat (sslength ss)
      (setq ed (entget (ssname ss i))
	    txt (cdr (assoc 1 ed))
	    cln (vl-string-search ":" txt)
	    suf (substr txt (1+ cln))
	    txt (strcat "STREET " *strser-num* suf)
	    *strser-num* (inc-str *strser-num*)
            i (1+ i))
      (entmod (subst (cons 1 txt)
		     (assoc 1 ed)
		     ed))))
  (princ)
  )

@john.uhden No, didn't find it. Never mind 🙂

0 Likes
Message 10 of 20

Anonymous
Not applicable

THANK YOU... 

 

#YOURALEGEND!!!

0 Likes
Message 11 of 20

Scottu2
Advocate
Advocate

raghibboctor,

 I like the concept of the routine.  It can be useful for similar sequencing situations.  Nice one BeekeeCZ.

Just to clarify post 8.   Coding wise, if the text is selected by using a window then the entities are added to the selection set from the last one created to the earliest.

You have to select the text column in the sequence to force the selection set of text like a sorted y-coordinate.

0 Likes
Message 12 of 20

john.uhden
Mentor
Mentor
It would be easy to add code to sort the selection in descending Y order.

John F. Uhden

0 Likes
Message 13 of 20

Scottu2
Advocate
Advocate

John.uhden,

 

How would the code work to re-order the selection set elements by y-axis?

Would it just move things within the selection set, create a new selection set or a new list?

 

0 Likes
Message 14 of 20

john.uhden
Mentor
Mentor

I would convert the selection set into a list of ENAMEs and then sort the list in descending Y order...

(defun @SortY (ss / i e lst)
  (repeat (setq i (sslength ss))
     (setq e (ssname ss (setq i (1- i)))
             lst (cons e lst)
     )
  )
  (vl-sort lst '(lambda (a b)(> (caddr (assoc 10 (entget a)))(caddr (assoc 10 (entget b))))))
)

Of course the selection set has to all have a DXF code 10.

John F. Uhden

0 Likes
Message 15 of 20

Scottu2
Advocate
Advocate
John.Uhden,

I added the routine and highlighted the changes in blue.
The lambda notation is a bit tricky to understand.
I'm not at my CAD station to verify the code.
My apologies to BeekeeCZ in advance for the errors.


(vl-load-com)
(defun @SortY (ss / i e lst)
(repeat (setq i (sslength ss))
(setq e (ssname ss (setq i (1- i)))
lst (cons e lst)
)
)
(vl-sort lst '(lambda (a b)(> (caddr (assoc 10 (entget a)))(caddr (assoc 10 (entget b))))))
)
(defun c:StreetSeries ( / inc-str ss i ed txt cln suf enList) (defun inc-str (string) (cond ((= "Z" string) "AA") ((wcmatch string "@Z") (strcat (chr (1+ (ascii (substr string 1 1)))) "A")) ((= 1 (strlen string)) (chr (1+ (ascii string)))) ((= 2 (strlen string)) (strcat (substr string 1 1) (chr (1+ (ascii (substr string 2 1)))))))) (or *strser-num* (setq *strser-num* "A")) (if (/= "" (setq tmp (getstring (strcat "\nStart with letter <" *strser-num* ">: ")))) (setq *strser-num* tmp)) (if (setq i 0 ss (ssget "_:L" '((0 . "*TEXT") (1 . "STREET *:*"))))
(progn
(setq enList (@Sorty ss))
(repeat (length enList) (setq ed (nth i enList)) txt (cdr (assoc 1 ed)) cln (vl-string-search ":" txt) suf (substr txt (1+ cln)) txt (strcat "STREET " *strser-num* suf) *strser-num* (inc-str *strser-num*) i (1+ i)) (entmod (subst (cons 1 txt) (assoc 1 ed) ed));entmod
);repeat
);progn
);if (princ) )
Message 16 of 20

ВeekeeCZ
Consultant
Consultant

@john.uhden wrote:

Hi, MJ.

... advancing the alpha characters the same way as Excel does columns...


  


@ВeekeeCZ wrote:

@john.uhden No, didn't find it. Never mind 🙂


  

I was about the time to stretch my mind just little bit...

 

(defun :ExcelLetterNumbering1+ (s / u)
  (setq s (vl-list->string (reverse (mapcar '(lambda (c)
					       (- (setq c (+ c (if u 0 1)))
						  (if (setq u (< c 91)) 0 26)))
					    (reverse (vl-string->list s))))))
  (strcat (if u "" "A") s))

Last line little bothers me, but can't figure out any better.

0 Likes
Message 17 of 20

john.uhden
Mentor
Mentor
I am thoroughly impressed (as usual) so long as you provide capital letters.

Command: (:excelletternumbering1+ "aab")
"AHHI"

Ah, Hi to you too. Or is that some kind of Mongol war cry?

John F. Uhden

0 Likes
Message 18 of 20

ВeekeeCZ
Consultant
Consultant

Hmm, but don't tell anyone. Hidden weakness which would never be fixed.

 

Anyway, I guess it was nice exercise... Now something more useful:

(defun ston (s / l)
    (if (= 1 (setq l (strlen s)))
      (- (ascii s) 64)
      (+ (* 26 (ston (substr s 1 (1- l)))) (ston (substr s l)))))

(defun ntos (n)
      (if (< (setq n (1- n)) 26)
        (chr (+ 65 n))
        (strcat (ntos (/ n 26))(chr (+ 65 (rem n 26))))))
_$ (ntos (1+ (ston "AZ")))
"BA"
_$ (ntos (1+ (ston "ZAZ")))
"ZBA"

This time just adjusted Lee's base converter, HERE

0 Likes
Message 19 of 20

Scottu2
Advocate
Advocate

I had a change to test the routine and make corrections today.

I like that sort routine John, thanks.

 

0 Likes
Message 20 of 20

john.uhden
Mentor
Mentor

I think that sorting is one of the most powerful/useful features that came along with Visual Lisp (nee Vital Lisp).

God bless Peter Petrov!  I hope Autodesk paid him well.

John F. Uhden

0 Likes