;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; TCOUNT  TEXT COUNT
;
; added #TCOUNTseparator for no-space counters (CAD Studio, 2018 - www.cadstudio.cz)
;
(defun c:tcount ( / ss_sort ss start inc startinc flag flt n)
 
(acet-error-init
 (list (list "cmdecho" 0
             "highlight" (getvar "highlight")
             "limcheck" 0
       );list
       T
 );list
);acet-error-init
 
(setq flt '((-4 . "<OR")
             (0 . "TEXT")
             (0 . "MTEXT")
             (0 . "ATTDEF")
            (-4 . "OR>")
           )
);setq
 
(if (and (setq ss (ssget flt));setq
         (setq ss (car (acet-ss-filter (list ss
                                             '(("LAYERUNLOCKED") ;Dis-allow locked layers, non-current-space
                                               ("CURRENTUCS")    ;and dis-allow objects not in current ucs
                                              )
                                             T
                                       );list
                       );acet-ss-filter
                  );car
         );setq
    );and
    (progn
     (if (not #bns_tcount_sort)
         (setq #bns_tcount_sort "Select-order")
     );if
 
     (initget "X Y Select-order")
     (setq ss_sort (getkword (acet-str-format
                               "\nSort selected objects by [X/Y/Select-order] <%1>: "
                               #bns_tcount_sort
                             );acet-str-format
                   );getkword
     );setq
     (if (or (not ss_sort)
             (equal ss_sort "")
         );or
         (setq ss_sort #bns_tcount_sort);setq then
         (setq #bns_tcount_sort ss_sort);setq else
     );if
     (if (not #bns_tcount_start)
         (setq #bns_tcount_start 1)
     );if
     (if (not #bns_tcount_inc)
         (setq #bns_tcount_inc 1)
     );if
     (while (not flag)
      (setq flag T)
      (setq startinc (strcat (itoa (fix #bns_tcount_start)) ","
                             (itoa (fix #bns_tcount_inc))
                     );strcat
            startinc (getstring (acet-str-format
                                 "\nSpecify starting number and increment (Start,increment) <%1>: "
                                 startinc
                                );acet-str-format
                     );getstring
      );setq
      (if (equal startinc "")
          (setq startinc (strcat (itoa (fix #bns_tcount_start)) ","
                                 (itoa (fix #bns_tcount_inc))
                         );strcat
          );setq
      );if
      (setq startinc (acet-str-to-list "," startinc))
      (if (or (not (car startinc))
              (not (numberp (read (car startinc))))
              (and (cadr startinc)
                   (not (numberp (read (cadr startinc))))
              );and
          );or
          (progn
           (princ "\nInvalid input.")
           (setq flag nil)
          );progn then
      );if
      (setq start (atoi (car startinc)));setq
      (if (cadr startinc)
          (setq inc (atoi (cadr startinc)));setq then
          (setq inc 1);setq else
      );if
      (if start
          (setq #bns_tcount_start start)
      );if
      (if (or (not inc)
              (equal inc 0)
          );or
          (setq inc #bns_tcount_inc);setq then
      );if
      (if inc
          (setq #bns_tcount_inc inc)
      );if
     );while
     (bns_get_tcountmode_cmd)
     (setq n (bns_tcount
                 ss                  ;the selection set
                 ss_sort             ;the sort type
                 start               ;the start number
                 inc                 ;the increment
                 #bns_tcount_mode    ;placement mode =overwrite/prefix/suffix/find&replace
                 #bns_tcount_replace ;search string for Find&replace option.
             )
     );setq
     (princ (acet-str-format "\n%1 objects modified." (itoa n)))
     (if ss
         (command "_.select" ss "")
     );if
    );progn then
);if
 
(acet-error-restore)
);defun c:tcount

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
(defun bns_tcount ( ss ss_sort start inc mode str / a na e1 e2 n lst b j )
 
(if (not (equal "Select-order" ss_sort))
    (progn
     (cond
      ((equal "X" ss_sort)
       (setq ss (bns_esort ss
                       '((e1) (car (cdr (assoc 10 e1)))) ;quoted function for x
                )
       );setq
      );cond #1
      ((equal "Y" ss_sort)
       (setq ss (bns_esort ss
                       '((e1) (* -1.0 (cadr (cdr (assoc 10 e1))))) ;quoted function for y
                )
       );setq
      );cond #2
      ((equal "Z" ss_sort)
       (setq ss (bns_esort ss
                       '((e1) (caddr (cdr (assoc 10 e1))))
                )
       );setq
      );cond #3
     );cond close
    );progn then
);if
 
(setq j 0)
(setq n 0);setq
(repeat (sslength ss)
 
(setq na (ssname ss n)
      e1 (entget na)
      e2 e1
);setq
(cond
 ((equal "TEXT" (cdr (assoc 0 e1)))
     (setq a (itoa start))
     (setq a (place_tcount (cdr (assoc 1 e1)) ;the original
                           a                  ;the number
                           mode
                           str               ;Search string to use if
             )                               ;mode=Find&replace
     );setq
     (entmod (subst (cons 1 a)
                    (assoc 1 e1)
                    e1
             );subst
     );entmod
 );cond #1
 ((equal "MTEXT" (cdr (assoc 0 e1)))
   ;first build one big string from the 3 and 1 group codes
   (setq a "")
   (foreach x e1 
    (if (= (car x) 3) 
        (setq a (strcat a (cdr x)))
    )
   );foreach
   ;then split it apart using \\p  (paragraph) as a delimiter
   (setq   a (strcat a (cdr (assoc 1 e1)))
         lst (acet-str-to-list "\\P" a)
           b ""
   );setq
   ;then make the number additions and build one big string again.
   (foreach x lst
     (setq a (itoa start))
     (setq a (place_tcount x                 ;the original
                           a                 ;the number
                           mode
                           str               ;Search string to use if
             )                               ;mode=Find&replace
           b (strcat b "\\P" a)
     );setq
     (setq start (+ start inc));setq
   );foreach
   (setq start (- start inc));setq
   (setq   b (substr b 3)
         lst nil
   );setq 
   ;the rebuild the e-list without the 3 and 1 group codes.
   (foreach x e1
    (if (and (/= (car x) 3)
             (/= (car x) 1)
        );and
        (setq lst (append lst (list x)))
    )
   );foreach
   ;split the new big string apart and place in the elist as 3 and 1 group codes.
   (setq e1 lst)
   (while (> (strlen b) 250)
    (setq e1 (append e1 
                     (list (cons 3
                                 (substr b 1 250)
                           )
                     )
             );append
           b (substr b 251)
    );setq
   );while
   (setq e1 (append e1 (list (cons 1 b))));setq
   (entmod e1)
 );cond #2
 ((equal "ATTDEF" (cdr (assoc 0 e1)))
     (setq a (itoa start))
     (setq a (place_tcount (cdr (assoc 2 e1)) ;the original
                           a                  ;the number
                           mode
                           str               ;Search string to use if
             )                               ;mode=Find&replace
     );setq
     (setq a (acet-str-replace " " "" a)) ;spaces are not allowed in attdefs so remove them
     (entmod (subst (cons 2 a) 
                    (assoc 2 e1)
                    e1
             );subst
     );entmod
 );cond #3
);cond close
 
(if (not (equal e2 (entget (cdr (assoc -1 e2)))))
    (setq j (+ j 1))
)
 
(setq n (+ n 1));setq
(setq start (+ start inc));setq
);repeat
 
j
);defun bns_tcount
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(if (not #TCOUNTseparator)(setq #TCOUNTseparator " ")) ; ADDED parameter, change to "" for no-space counters

(defun place_tcount (org num mode str / )
 
(cond
 ((or (not mode)
      (equal mode "Overwrite")
  );or
  (setq str num);setq
 );cond #1
 ((equal mode "Prefix")
  (setq str (strcat num #TCOUNTseparator org));setq
 );cond #2
 ((equal mode "Suffix")
  (setq str (strcat org #TCOUNTseparator num));setq
 );cond #3
 ((equal mode "Find&replace")
  (setq str (acet-str-replace str num org));setq
 );cond #4
);cond close
 
str
);defun place_tcount
 
