Numbering In Sequence

Numbering In Sequence

Tanookh
Contributor Contributor
873 Views
5 Replies
Message 1 of 6

Numbering In Sequence

Tanookh
Contributor
Contributor

Hello,

I have an existing text and I want to add numbering to it in a sequential manner,

from left to right and then bottom to top.

If any expert could provide me a Lisp code for this,

I would greatly appreciate it.

I have uploaded a file for reference.

Thanks in advance. 

0 Likes
Accepted solutions (1)
874 Views
5 Replies
Replies (5)
Message 2 of 6

pbejse
Mentor
Mentor

Is the format always B#?

Why do you have those tiny text below the beam size? Will those be included on the sequencing?

 

0 Likes
Message 3 of 6

Tanookh
Contributor
Contributor

Thanks for reply  @pbejse


Format will be changed as per requirements, and yes, it includes numbering in sequence.


I tried this with the help of ChatGPT, but it's not working properly.

Can you please review this?

 

(defun c:TY ()
  (setq ents (ssget '((0 . "TEXT"))))
  (if ents
    (progn
      (setq count (1- (sslength ents)))
      (setq startNumber (getint "\nEnter the starting number: "))
      (setq number startNumber)
      (setq prefix (getstring "\nEnter the label prefix: "))

      ;; Get the current scale factor
      (setq currentScale (getvar 'textsize))

      (repeat (sslength ents)
        (setq ent (ssname ents count))
        (setq entData (entget ent))
        (setq insertionPoint (cdr (assoc 10 entData)))
        (setq textHeight (cdr (assoc 40 entData)))

        ;; Calculate text rotation angle
        (setq textRotation (cdr (assoc 50 entData)))

        ;; Wrap the text rotation angle within the range of -180 to 180 degrees
        (setq textRotation (rem textRotation 360.0))
        (if (< textRotation -180.0) (setq textRotation (+ textRotation 360.0)))

        (setq text (entmake (list '(0 . "TEXT") (cons 10 insertionPoint) (cons 40 textHeight) (cons 1 (strcat prefix (itoa number))) '(7 . "Standard") '(8 . "0") (cons 50 textRotation))))
        (entmod text)

        (setq count (1- count))
        (setq number (1+ number))
      )

      (princ "\nNumbered labels updated.")
    )
    (princ "\nNo text selected.")
  )
)

(princ "\nTY command loaded. Type 'TY' to update labels.")

 

0 Likes
Message 4 of 6

Kent1Cooper
Consultant
Consultant

@Tanookh wrote:

... I want to add numbering to it in a sequential manner,

from left to right and then bottom to top.

.... 


Correct me if I'm misunderstanding, but the image and drawing show the numbering being first from top to bottom with the 0° set and then left to right with the 90° ones.  Can you clarify?

 

One three-part glaring error:  1)  If the green and magenta Text objects already exist at rotations you want to remain, there's no need to check the rotation and correct it to the more readable direction [if that's what the rotation check is supposed to be for] -- just use the rotation of the existing ones for the new ones.  2)  Even if it were necessary to do that, it shouldn't check the relationship to 180° [and entity data would never store such a thing as less than negative 180°, nor for that matter as a negative value at all], but rather whether the rotation is greater than 90° but not greater than 270°.  3)  It's written as though rotation numerical values in entity data are in degrees, but they're in radians.

 

You have two B19's -- is that intended?

 

There's no accounting in the code for right-justification of the new ones [see the 72 & 73 entries in entity data], nor of moving the insertion point off.  Are the super-tiny ones the result of the code you posted?  Is there some way of "writing to" ChatGPT and describing what it got wrong?

 

It would need to separate the selection into a set of things at 0 [same in ° and radians] rotation and another set at 90° [i.e. (/ pi 2) radians] rotation.  Then it would need to sort the 0 set by insertion-point Y coordinate, and do its thing with those, then sort the 90° set by insertion-point X coordinate, and do its thing with those.

Kent Cooper, AIA
0 Likes
Message 5 of 6

Kent1Cooper
Consultant
Consultant
Accepted solution

It sounded interesting to work out, so I came up with this, which worked for me in your sample drawing:

 

(defun C:LTYX ; = Label Text by Y top-to-bottom then X left-to-right
  (/ ss ssr0 n txt listY listX inorder pre int tdata)
  (if (setq ss (ssget '((0 . "TEXT") (8 . "BEAM_SIZES_LAYOUT") (72 . 0) (73 . 0))))
    (progn ; then
      (setq ssr0 (ssget "_P" '((50 . 0.0)))); those at zero rotation
      (repeat (setq n (sslength ssr0))
        (setq
          txt (ssname ssr0 (setq n (1- n)))
          listY (cons (list txt (caddr (assoc 10 (entget txt)))) listY)
        ); setq
        (ssdel txt ss); remove from overall set
      ); repeat
      (repeat (setq n (sslength ss)); the 90° ones remaining
        (setq
          txt (ssname ss (setq n (1- n)))
          listX (cons (list txt (cadr (assoc 10 (entget txt)))) listX)
        ); setq
      ); repeat
      (setq
        inorder ; list: 0° top-down, then 90° left-right
        (append
          (mapcar 'car (vl-sort listY '(lambda (a b) (> (cadr a) (cadr b)))))
          (mapcar 'car (vl-sort listX '(lambda (a b) (< (cadr a) (cadr b)))))
        ); append
        pre (getstring "\nLabel prefix: ")
        int (getint "\nStarting number: ")
      ); setq
      (setvar 'clayer "0")
      (foreach txt inorder
        (setq tdata (entget txt))
        (command "_.text"
          "_style" (cdr (assoc 7 tdata))
          "_right" "_non" (polar (cdr (assoc 10 tdata)) (cdr (assoc 50 tdata)) -100); insertion
          (cdr (assoc 40 tdata)); height
          (cvunit (cdr (assoc 50 tdata)) "radian" "degree"); rotation
          (strcat pre (itoa int)); text content
        ); command
        (setq int (1+ int))
      ); foreach
    ); progn
    (prompt "\nNo qualifying Text selected.")
  ); if
  (prin1)
)

 

It accepts only Text on that particular Layer, and only left-justified, so that it can place the label Text appropriately in relation.

 

It could have *error* handling to first save and afterwards reset the current Layer and Text properties if you want, and have Undo begin/end wrapping, and remember what number you were up to and offer the next one as default on subsequent use if that's relevant, etc.

 

If you select any not at 0° or 90°, they should "work," but their sorting order will be in among, and numbered in order by the X coordinate of their insertion points along with, any 90° ones.

Kent Cooper, AIA
0 Likes
Message 6 of 6

Tanookh
Contributor
Contributor

@Kent1Cooper Wow.👌
Its working for me.


Thank you so much for your help.🙂

0 Likes