Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help - How to put consecutive letters

24 REPLIES 24
SOLVED
Reply
Message 1 of 25
arperezinf
1461 Views, 24 Replies

Help - How to put consecutive letters

Hi everyone

 

I'm new to the forum and autolisp programming, I need your help.

 

I have the following lsp code, which when I run it, create a code in this consecutive way:

 

PPP_001
PPP_002
PPP_003
PPP_n times.

 

I would like that instead of putting consecutive numbers, put letters for example like this:

 

PPP_A,
PPP_B ...
PPP_Z

 

And when you finish the alphabet, increase one more letter up to n times, like this for example.

 

PPP_AA,
PPP_BB ...
PPP_n times

 

This is the code

 

(defun c:NCAEM (/ #EntityCount GroupEntity Entity NSec TNSEC HUB valor) 
  (setq #EntityCount 0)
  (setq NSec 0)
  (setq HUB "PPP")
  (setq NSec (getint "\nEnter starting number: [001] "))

  (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 190))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )

    (SETQ TNSEC (itoa Nsec))
    (IF (= (strlen TNSEC) 2) (setq TNSEC (strcat "0" tnsec)))
    (IF (= (strlen TNSEC) 1) (setq TNSEC (strcat "00" tnsec)))

    (setq valor (strcat HUB "-" TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )

  (ALERT "Process completed satisfactorily!")
)

 

 

Where I need the letters to be is where it says "TNSEC"

 

(setq valor (strcat HUB "-" TNSEC))

 

Could you tell me how it can be done, that I have to change to put letters.

 

Thank you very much for reading my message and for your responses.
Greetings.

Labels (1)
24 REPLIES 24
Message 2 of 25
Sea-Haven
in reply to: arperezinf

Up to Z is straight forward paste (chr 65) to command line so (chr (+ x 1)) will give B C D etc. 

 

I have used this often but not past 90. Its not a problem the issue I see is when you get into BB close dwg get a coffee and continue. It may be a case of last is 2 characters so (66 67) is next. (65 66 67) ABC

 

I know Gile has ABC = 731 alpha2number

Message 3 of 25
pbejse
in reply to: arperezinf


@arperezinf wrote:

Where I need the letters to be is where it says "TNSEC"

(setq valor (strcat HUB "-" TNSEC))

Could you tell me how it can be done, that I have to change to put letters.

An old gem from Owen Wengerd

(defun c:NCAEM (/ Str+ #EntityCount GroupEntity Entity NSec TNSEC HUB valor)
;;;	Owen Wengerd http://www.manusoft.com/		;;;
;;;	Additional cond for INTEGER pBe			;;;
  (defun Str+ (source / prefix rchar)
    (If (eq (type (read source)) 'INT)
      (itoa (1+ (atoi source)))
      (cond
        ((= source "") "A")
        ((= (setq prefix (substr source
                                 1
                                 (1- (strlen source))
                         )
                  rchar  (substr source (strlen source))
            )
            "Z"
         )
         (strcat (Str+ prefix) "A")
        )
        ((strcat prefix (chr (1+ (ascii rchar)))))
      )
    )
  )  
  (setq #EntityCount 0)
  (setq NSec 0)
  (setq HUB "PPP")
  (setq TNSec (strcase (getstring "\nEnter starting letter: ")))

  (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 190))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )
    (setq valor (strcat HUB "-" TNSEC))
    (setq TNSEC (Str+ TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )

  (ALERT "Process completed satisfactorily!")
)

 HTH

Message 4 of 25
CADaSchtroumpf
in reply to: arperezinf

My contribution

(defun inc_txt (Txt / Boucle Decalage Val_Txt)
	(setq
		Boucle 1
		Val_txt ""
	)
	(while (<= Boucle (strlen Txt))
		(setq Ascii_Txt (vl-string-elt Txt (- (strlen Txt) Boucle)))
		(if	(not Decalage)
			(setq Ascii_Txt (1+ Ascii_Txt))
		)
		(if	(or (= Ascii_Txt 58) (= Ascii_Txt 91) (= Ascii_Txt 123))
			(setq
				Ascii_Txt
					(cond
						((= Ascii_Txt 58) 48)
						((= Ascii_Txt 91) 65)
						((= Ascii_Txt 123) 97)
					)
				Decalage nil
			)
			(setq Decalage T)
		)
		(setq Val_Txt (strcat (chr Ascii_Txt) Val_Txt))
		(setq Boucle (1+ Boucle))
	)
	(if (not Decalage)
		(setq Val_Txt
			(strcat
				(cond
					((< Ascii_Txt 58) "0")
					((< Ascii_Txt 91) "A")
					((< Ascii_Txt 123) "a")
				)
				Val_Txt
			)
		)
	)
	Val_Txt
)
(defun c:demo ( / string n)
	(setq string (getstring "\nStart incrementation from?: "))
	(initget 7)
	(setq n (getint "\nNumber or repeatition: "))
	(textscr)
	(print string)
	(repeat (1- n)
		(print (setq string (inc_txt string)))
	)
	(prin1)
)
Message 5 of 25
john.uhden
in reply to: arperezinf

I think we did this several years ago and @Anonymous came up with the function to increment alpha numbers in the same way Excel handles its columns.  I don't know how to search for it, but hopefully Doug will remember and chime in.

John F. Uhden

Message 6 of 25
Sea-Haven
in reply to: john.uhden

If you look at these 2 functions then can take a seed string and add 1 to it

 

 

; Alpha2Number - Converts Alpha string into Number
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Str$ = String to convert
; Syntax example: (Alpha2Number "ABC") = 731
;-------------------------------------------------------------------------------
(defun Alpha2Number (Str$ / Num#)
  (if (= 0 (setq Num# (strlen Str$)))
    0
    (+ (* (- (ascii (strcase (substr Str$ 1 1))) 64) (expt 26 (1- Num#)))
       (Alpha2Number (substr Str$ 2))
    )
  )
)
;-------------------------------------------------------------------------------
; Number2Alpha - Converts Number into Alpha string
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Num# = Number to convert
; Syntax example: (Number2Alpha 731) = "ABC"
;-------------------------------------------------------------------------------
(defun Number2Alpha (Num# / Val#)
  (if (< Num# 27)
    (chr (+ 64 Num#))
    (if (= 0 (setq Val# (rem Num# 26)))
      (strcat (Number2Alpha (1- (/ Num# 26))) "Z")
      (strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
    )
  )
);defun Number2Alpha

 

 

(Alpha2Number "ABC") = 731 

add 1

(Number2Alpha 732) = "ABD"
When hit Z need to reset the start value as it will be "ABZA" = 19605 

 

 

 

Message 7 of 25
arperezinf
in reply to: arperezinf

Hello 👋 @Sea-Haven @pbejse @CADaSchtroumpf @john.uhden 

Sorry. ☹️
The solution does not work correctly, we tried and saw that it worked, but in the end it does not put the letters A, B, C ... AA, AB, AC ... BB, BC, BD.

Message 8 of 25
arperezinf
in reply to: CADaSchtroumpf

This code does what I need, but I don't know how to integrate it into my code.
Message 9 of 25
arperezinf
in reply to: Sea-Haven

I'm trying to test these two functions, but can't get it to work for me.
How can I integrate it into my lisp code?
Message 10 of 25
arperezinf
in reply to: pbejse

The function is fine, but I need it to put the letters A, B, C and when it gets to Z, change to AA, to AZ and then change to BB to BZ, so on.
Message 11 of 25
john.uhden
in reply to: arperezinf

Maybe this is it...

  ;; By Doug Broad (2-10-17)
    ;; Sorry @john.uhden.  Been busy with a Statics and Strengths class today.
    ;; Here is one that works with a base 25, using 0 as A and 25 as Z.
    ;; Convert base 10 to letters
    ;; 0 = a to 25 = z for each digit.
    ;; Works for 0 to (- (expt 26 7) 1) integer maximum
    ;; JFU did Cooperial compaction and adjusted n by one
    (defun ntos (n)
      (if (< (setq n (1- n)) 26)
        (chr (+ 65 n))
        (strcat (ntos (/ n 26))(chr (+ 65 (rem n 26))))
      )
  )

John F. Uhden

Message 12 of 25
arperezinf
in reply to: john.uhden

Hello @john.uhden.
Thank you for your comment.
Could you tell me how I can insert your function into my lisp routine to make it work for me?
Remember I don't know anything about lisp programming.
Thank you.
Greetings.
Message 13 of 25
john.uhden
in reply to: arperezinf

I hope I interpreted your code correctly.

Try this:

 

(defun c:NCAEM (/ ntos #EntityCount GroupEntity Entity NSec TNSEC HUB valor) 
  (defun ntos (n) ;; by  2/10/17 and compacted by 
    (if (< (setq n (1- n)) 26)
      (chr (+ 65 n))
      (strcat (ntos (/ n 26))(chr (+ 65 (rem n 26))))
    )
  )
  (setq #EntityCount 0)
  (setq NSec 0)
  (setq HUB "PPP")
  (initget 6)
  (or
    (setq NSec (getint "\nEnter starting number <1>: "))
    (setq NSec 1)
  )

  (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 190))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )

    (SETQ TNSEC (ntos NSec))
    (setq valor (strcat HUB "-" TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )

  (ALERT "Process completed satisfactorily!")
)

 

John F. Uhden

Message 14 of 25
arperezinf
in reply to: arperezinf

Hello @john.uhden 

Sorry for the delay in answering.
Your code works perfectly!
Thank you!

Message 15 of 25
john.uhden
in reply to: arperezinf

@arperezinf 

YAY!  It was your code.  Just needed a little touch up.

John F. Uhden

Message 16 of 25
arperezinf
in reply to: arperezinf

Hello @john.uhden 

 

Sorry if I don't open another new question, but I have a query about my same problem.

 

The lisp routine works perfect!

 

I would like to be able to select a block and for this to be the first one by which you start numbering the routine alphabetically.

 

Will you be able to do what I say?

 

Thank you very much for reading my message.
Greetings.

Message 17 of 25
john.uhden
in reply to: arperezinf

@arperezinf 

Good job staying in the same thread.

Yes, I think I see what you want...

You select one (1) candidate block and we give it number 1.

Then it selects all the rest and continues numbering at 2.

Right?

Actually, it will still ask what number to start with (default = 1), but you could enter a different number if you wanted.

John F. Uhden

Message 18 of 25
arperezinf
in reply to: arperezinf

Hello @john.uhden 

Yeah that's right.

 

I would like to select a block, so that it is the first one (1) and then the others are numbered, just like the lisp routine that you did.

 

Yes, I saw that I can start with any number and that is great! Because sometimes I need to start with another number.

 

Your lisp routine is excellent for what I need!😀👏👍🙏

 

Thank you very much for reading my message.
Greetings.

Message 19 of 25
john.uhden
in reply to: arperezinf

Hello, @arperezinf 

I was just going to fix it up for you, but I noticed something I hadn't noticed before...

(cons 0 "SPR????")

I have never heard of an entity of that type.  Somehow I was under the impression that you were filtering for a block name.  Do you really have entities like that?  What version/vertical of AutoCAD are you using?

Should I just trust your code that you have "SPR<whatever>" entities and proceed?

I mean, if they exist then it's all the same to me.  If the rest of your code worked, then maybe why should I even be asking?

Alrighty then.  I'm not sure about this but give it a try...

(defun c:NCAEM (/ ntos link #EntityCount GroupEntity Entity NSec TNSEC HUB) 
  (defun ntos (n) ;; by @dbroad 2/10/17 and compacted by @john.uhden
    (if (< (setq n (1- n)) 26)
      (chr (+ 65 n))
      (strcat (ntos (/ n 26))(chr (+ 65 (rem n 26))))
    )
  )
  (defun link (Entity N / valor TNSEC)
    (IF (getlinksipre Entity 98) 
      ()
      (progn
	    (setq NSec N)
        (SETQ TNSEC (ntos NSec))
        (setq valor (strcat HUB "-" TNSEC))
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (1+ NSec))
      )
    )
	T
  )
  (setq #EntityCount 0)
  (setq HUB "PPP")
  (and
    (setq Entity (car (entsel "\nSelect first SPRSyMBOL: ")))
	(or
	  (and
        (= (cdr (assoc 0 (entget Entity))) "SPRSYMBOL")
		(= (cdr (assoc 70 (entget Entity))) 190)
	  )
	  (prompt "\nEntity selected is not a qualified SPRSYMBOL.")
	)
    (not (initget 6))
    (or
      (setq NSec (getint "\nEnter starting number <1>: "))
      (setq NSec 1)
    )
    (link Entity NSec)
    (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 190))))
	(setq GroupEntity (ssdel Entity GroupEntity))

    (while (setq Entity (ssname GroupEntity #EntityCount))
      (setq #EntityCount (1+ #EntityCount))
      (link Entity NSec)
	)
  )

  (ALERT "Process completed satisfactorily!")
)

John F. Uhden

Message 20 of 25
arperezinf
in reply to: arperezinf

Hello @john.uhden 👋


Sorry for the delay in responding, I was trying to adapt the lisp routine to your modification.

 

Thank you very much for fixing the routine code for me! 👏🙏😲

 

Your modification works perfect!😁👏🙏

 

Now I have a problem and it is my fault for not having said it before. The runtina lisp that I publish is not complete, I only published a part, do not imagine that they were going to modify it completely as you did. I'm so sorry!

 

I am using Autodesk AutoCAD Map 2021.

 

All the lisp code that I am going to put below this paragraph is only for one block and this block has an ID equal to "190" assigned and it is found within a group of blocks with the name "SPRSYMBOL".

 

 

(setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 190))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )

    (SETQ TNSEC (ntos NSec))

    (setq valor (strcat HUB "_" TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )

 

 

That's why that name "SPRSYMBOL" seems strange to you.

 

Now, with the modification that you made, it only works for ID 190 and I need it to work for the other IDs of blocks, which I did not publish before, because that part of the code that I published above is repeated for each block and the only thing that changes are the IDs of the blocks that are these numbers 65, 190, 200, 207 and 451.

 

If we go to the code before fixing it, the lisp routine would look like this:

 

 

(defun c:NCAEM (/ ntos #EntityCount GroupEntity Entity NSec TNSEC HUB valor) 
  (defun ntos (n) ;; by @dbroad 2/10/17 and compacted by @john.uhden https://forums.autodesk.com/
    (if (< (setq n (1- n)) 26)
      (chr (+ 65 n))
      (strcat (ntos (/ n 26))(chr (+ 65 (rem n 26))))
    )
  )
  (setq #EntityCount 0)
  (setq NSec 0)
  (setq HUB "PPP")
  (initget 6)
  (or
    (setq NSec (getint "\nEnter starting number <1>: "))
    (setq NSec 1)
  )

  (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 65))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )

    (SETQ TNSEC (ntos NSec))
    (setq valor (strcat HUB "-" TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )
  
  (setq #EntityCount 0)
  (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 190))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )

    (SETQ TNSEC (ntos NSec))
    (setq valor (strcat HUB "-" TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )

(setq #EntityCount 0)
  (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 200))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )

    (SETQ TNSEC (ntos NSec))
    (setq valor (strcat HUB "-" TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )

  (setq #EntityCount 0)
  (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 207))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )

    (SETQ TNSEC (ntos NSec))
    (setq valor (strcat HUB "-" TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )

  (setq #EntityCount 0)
  (setq GroupEntity (ssget "x" (list (cons 0 "SPRSYMBOL") (cons 70 451))))

  (while (ssname GroupEntity #EntityCount) 

    (setq Entity       (ssname GroupEntity #EntityCount)
          #EntityCount (+ #EntityCount 1)
    )

    (SETQ TNSEC (ntos NSec))
    (setq valor (strcat HUB "-" TNSEC))
    (IF (getlinksipre Entity 98) 
      ()
      (progn 
        (addlinksipre Entity 98 Valor)
        (chsprcolor Entity 1)
        (setq NSec (+ NSec 1))
      )
    )
  )
  (ALERT "Process completed satisfactorily!")
)

 

 

As you can see, the previous code fragment is repeated again and only the ID numbers of the blocks change.

 

So with your code fix, I can't put the IDs of the blocks, I modified my routine but I don't know where to put the IDs of the blocks.

 

Sorry for my mistake for not posting all the code for the lisp routine first.

 

Have a nice day and a good weekend if you can't answer me before.

 

Again I apologize for my mistake and for making it work double.

 

Thank you very much for reading my message.

Greetings.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report