Incremental Numbering Lisp

Incremental Numbering Lisp

kylecanario
Explorer Explorer
2,609 Views
8 Replies
Message 1 of 9

Incremental Numbering Lisp

kylecanario
Explorer
Explorer

Hello,

 

Can anyone help me figure out how to write a specific section of my code? I am still new to lisp and I need some help figuring out the proper way to write the end of my lisp. I wanted to make an incremental lisp that creates MTEXT and it goes up in increments of 1 from whatever starting integer that I input. This command is for numbering lots for plan sets. I wanted to try making this instead of using TCount because I dont like using that command. This is what I have currently:

 

(defun c:LotNumbers ( / StartingLotNumber Lot LotText NextlotNumber)
 
  (setq StartingLotNumber (getint "\nStarting Lot Number :"))
  
(while 
  
  (setq Lot (getpoint "\nSelect Lot to Place Number :"))
 
(if Lot
 
(setq LotText (entmake (list (cons 0 "MTEXT")
                                                  (cons 100 "AcDbEntity")
                                                  (cons 100 "AcDbMText")
                                                 (cons 8 "H-LOT$")
                                                 (cons 10 Lot)
                                                 (cons 40 5)
                                                 (cons 7 "ROMANS")
                                                 (cons 1 (rtos StartingLotNumber 2 1))
                                                 (cons 71 5)
                                                 );end of list
 
                                          )) ;end of entmake and setq functions
 
  (entmod (list (cons 10 Lot) LotText))
   
  (setq StartingLotNumber (1+ StartingLotNumber))
 
  ) ;end of if function
 
) ;end of while function
 
  (princ)
 
  )
 
 
--------------------------------------------------------------------------------------------------------------------------
 
I am under the impression that the code stops working at the "(setq StartingLotNumber (1+ StartingLotNumber))" line. It is adding 1 to the integer and not being converted into a string when entmake is used again for the next MTEXT entity. Can anyone help me write that out properly?
0 Likes
Accepted solutions (1)
2,610 Views
8 Replies
Replies (8)
Message 2 of 9

paullimapa
Mentor
Mentor

see edits:

; LotNumbers incremental lisp that creates MTEXT 
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/incremental-numbering-lisp/m-p/12458772#M459316
(defun c:LotNumbers ( / StartingLotNumber Lot LotText NextlotNumber)
  ; setup a default lot # to start off
  (if(not **StartingLotNumber**)(setq **StartingLotNumber** 100))
 ;  (setq StartingLotNumber (getint "\nStarting Lot Number :"))
 ; make sure # is entered before continuing
 (while(not StartingLotNumber)
   (setq StartingLotNumber (getint (strcat "\nStarting Lot Number <" (itoa **StartingLotNumber**) ">: " ))) ; prompt with default
   (if(not StartingLotNumber)(setq StartingLotNumber **StartingLotNumber**)) ; set to default if enter was pressed
   (setq **StartingLotNumber** StartingLotNumber) ; reset default
 )

 (while 
  (setq Lot (getpoint "\nSelect Lot to Place Number :"))
  (if Lot
   (progn ; need to add
    (setq LotText (entmake (list (cons 0 "MTEXT")
                                                  (cons 100 "AcDbEntity")
                                                  (cons 100 "AcDbMText")
                                                 (cons 8 "H-LOT$")
                                                 (cons 10 Lot)
                                                 (cons 40 5)
                                                 (cons 7 "ROMANS")
;                                                 (cons 1 (rtos StartingLotNumber 2 1)) ; only if you want a decimal
                                                 (cons 1 (itoa StartingLotNumber)) ; convert integer to ascii
                                                 (cons 71 5)
                                                 );end of list
 
                                          )) ;end of entmake and setq functions
 
  ; (entmod (list (cons 10 Lot) LotText)) ; don't need this
    (setq StartingLotNumber (1+ StartingLotNumber))
   ) ; end progn
  ) ;end of if function
 ) ;end of while function
  (setq **StartingLotNumber** StartingLotNumber) ; reset default
  (princ)
) ; defun

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 9

Sea-Haven
Mentor
Mentor

Just a suggestions , if **StartingLotNumber** is nil then ssget the mtext and look for highest number and add 1, if ss is nil then set to say 1.

Message 4 of 9

paullimapa
Mentor
Mentor

yes very good suggestion since  **StartingLotNumber** will be nil every time the same drawing is reopeoned.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 9

smallƑish
Advocate
Advocate

Will <this> work for you?

0 Likes
Message 6 of 9

kylecanario
Explorer
Explorer

Thank you so much for marking up my code very quickly. I will not be able to try this markup until this upcoming Wednesday. I will reply to this thread as soon as I test it and I will let you know how it works for me. From what I can gather I did not set up my while loop right and it was not resetting properly? I also was not using itoa to convert my integers into a string so they can go up in increments? I'm still new to lisp and working with strings and loops is new territory for me.

 

 

Happy Holidays!

0 Likes
Message 7 of 9

paullimapa
Mentor
Mentor
Accepted solution

you are very welcome...actually it was your (if Lot function. Since there are multiple statements that follow when this tests true, then you'll need to use (progn to group them together...just keep looking at different examples and you'll be able to pick coding up very quickly. Here's a revised version which takes into account what @Sea-Haven suggested which is to first do a search for any existing MTEXT on the specified layer (in this case it's "H-LOT$") for LotNumbers and if found use this to increment to the next #:

; LotNumbers incremental lisp that creates MTEXT on specific layer
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/incremental-numbering-lisp/m-p/12458772#M459316
(defun c:LotNumbers ( / do_get_num Lot LotText LotLayer NextlotNumber num StartingLotNumber)
 (setq LotLayer "H-LOT$") ; set LotLayer name
 ; do_get_num function returns highest number value given layer to search for mtext 
 ; Argument:
 ; lyr = layername
 ; Returns
 ; nil if no #s found in all MText content
 ; highest number found in all MText content
 (defun do_get_num (lyr / txtcur txtnew ss)
  (if(setq ss(ssget"_X" (list '(0 . "MTEXT") (cons 8 lyr)))) ; select all MTEXT on LotLayer
   (progn ; when found
    (setq txtcur 1) ; start off with number as 1
    (foreach itm (mapcar 'cadr (ssnamex ss)) ; cycle through each entity item in selection set
     (if(not(zerop(setq txtnew(atoi(getpropertyvalue itm "TEXT"))))) ; if text string can be converted to a number
      (if(> txtnew txtcur)(setq txtcur txtnew)) ; then compare and retain higher # as current
     )
    ) ; foreach
    (if (= txtcur 1)(setq txtcur nil)) ; if no change then nil
   ) ; progn
  ) ; if
  txtcur
 ) ; defun do_get_num
 ; setup a default lot # to start off
 (if(not **StartingLotNumber**)
    (if(setq num(do_get_num LotLayer))
     (setq **StartingLotNumber** (1+ num)) ; found last number increment 1 & set as default number
     (setq **StartingLotNumber** 100) ; else set default number as 100
    )
 )
 ;  (setq StartingLotNumber (getint "\nStarting Lot Number :"))
 ; make sure # is entered before continuing
 (while(not StartingLotNumber)
   (setq StartingLotNumber (getint (strcat "\nStarting Lot Number <" (itoa **StartingLotNumber**) ">: " ))) ; prompt with default
   (if(not StartingLotNumber)(setq StartingLotNumber **StartingLotNumber**)) ; set to default if enter was pressed
   (setq **StartingLotNumber** StartingLotNumber) ; reset default
 )
 (while 
  (setq Lot (getpoint "\nSelect Lot to Place Number :"))
  (if Lot
   (progn ; need to add
    (setq LotText (entmake (list (cons 0 "MTEXT")
                                                 (cons 100 "AcDbEntity")
                                                 (cons 100 "AcDbMText")
                                                 (cons 8 LotLayer)
                                                 (cons 10 Lot)
                                                 (cons 40 5)
                                                 (cons 7 "ROMANS")
;                                                 (cons 1 (rtos StartingLotNumber 2 1)) ; only if you want a decimal
                                                 (cons 1 (itoa StartingLotNumber)) ; convert integer to ascii
                                                 (cons 71 5)
                                                 );end of list
 
                                          )) ;end of entmake and setq functions
 
  ; (entmod (list (cons 10 Lot) LotText)) ; don't need this
    (setq StartingLotNumber (1+ StartingLotNumber))
   ) ; end progn
  ) ;end of if function
 ) ;end of while function
  (setq **StartingLotNumber** StartingLotNumber) ; reset default
  (princ)
) ; defun

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 9

kylecanario
Explorer
Explorer

Hello Paul

 

I tried this revised version of the incremental numbering lisp and it is working so well. Thank you for taking the time to making my code work and for adding @Sea-Haven 's suggestion. Even though I was missing a lot of lines of code to make this functional; I still learned a lot from this experience and I have a long way to go!

 

Thank you again and happy holidays

0 Likes
Message 9 of 9

paullimapa
Mentor
Mentor

You are very welcome and we’re all here to assist if you have further questions….cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos