Jump to content

Recommended Posts

Posted

I'm looking for a lisp that will allow me to do the exact same thing I do with TCOUNT but for multileaders.  I have a drawing where I have mleaders going to many different lines and each has three lines of text.  The first line has cable id and I want to be able to replace the last two characters of that line.  In TCOUNT I would select each mtext object in order, have the count start  at 1 and increase by 1 (1,1) and then I would have it find and replace the '0' I left at the end of each line with the sequential numbers created by TCOUNT.  This is what I need to be able to do but on all the mleaders I have in this drawing.  Anyone have something like this?

Posted
2 hours ago, rsdonna said:

I'm looking for a lisp that will allow me to do the exact same thing I do with TCOUNT but for multileaders.  I have a drawing where I have mleaders going to many different lines and each has three lines of text.  The first line has cable id and I want to be able to replace the last two characters of that line.  In TCOUNT I would select each mtext object in order, have the count start  at 1 and increase by 1 (1,1) and then I would have it find and replace the '0' I left at the end of each line with the sequential numbers created by TCOUNT.  This is what I need to be able to do but on all the mleaders I have in this drawing.  Anyone have something like this?

@rsdonna, please upload your sample.dwg and the TCOUNT lisp

Posted

I don't have a TCOUNT lisp.  TCOUNT is part of the express tools in AutoCAD.  It's what I use when I'm going to renumber text or mtext.  Unfortunately TCOUNT doesn't work in mleaders so I'm looking for a way to be able to do with mleaders what I do for text and mtext.

Posted

Sure, here is a sample of what I'll have in my drawings.  In my drawings I'll have maybe 50 mleaders like these and I'm looking for a way to be able to pick them in order and then have it replace the -00 at the end with the sequential numbering.  In TCOUNT I can pick what number I want it to start from and what the increment will be and then whether I want to overwrite, find and replace (which is what I need it to do), and also a prefix and a suffix option to place the text.  Thanks for your help

SAMPLE.dwg

Posted (edited)

Give this a try,  also it replaces what ever is the last 2 characters. Call it version 1.

; https://www.cadtutor.net/forum/topic/98622-tcount-for-multileaders/
; change mleader text add number on end.
; By alanH jultu 2025

(defun c:wow1 ( / num str newstr obj  len)
(setq num (getint "\nEnter start number "))
(while (setq ent (entsel "\nPick Mleader - Enter to stop. "))
 (setq obj (vlax-ename->vla-object (car ent)))
 (setq str (vlax-get obj 'textstring))
 (setq len (strlen str))
 (if (< num 10)
 (setq newstr (strcat (substr str 1 (- len 2)) "0" (rtos num 2 0)))
 (setq newstr (strcat (substr str 1 (- len 2)) (rtos num 2 0)))
 )
 (vlax-put obj 'textstring newstr)
 (setq num (1+ num))
)
(princ)
)

 

Edited by BIGAL
Posted
13 hours ago, BIGAL said:

Give this a try,  also it replaces what ever is the last 2 characters. Call it version 1.

; https://www.cadtutor.net/forum/topic/98622-tcount-for-multileaders/
; change mleader text add number on end.
; By alanH jultu 2025

(defun c:wow1 ( / num str newstr obj  len)
(setq num (getint "\nEnter start number "))
(while (setq ent (entsel "\nPick Mleader - Enter to stop. "))
 (setq obj (vlax-ename->vla-object (car ent)))
 (setq str (vlax-get obj 'textstring))
 (setq len (strlen str))
 (if (< num 10)
 (setq newstr (strcat (substr str 1 (- len 2)) "0" (rtos num 2 0)))
 (setq newstr (strcat (substr str 1 (- len 2)) (rtos num 2 0)))
 )
 (vlax-put obj 'textstring newstr)
 (setq num (1+ num))
)
(princ)
)

 

This is great.  Is there a way to have it still do this if there are multiple lines of text in the mleader?

I still have the wire name at the top line always but sometimes add 1 or 2 additional lines with other info about that wire.

 

Thanks for your help.

2025-07-27 09_11_25-Autodesk AutoCAD 2026 - [SAMPLE.dwg].png

SAMPLE.dwg

Posted
1 hour ago, SLW210 said:

Did you try the codes in the thread I posted?

I did.  I had found it before and it is great just doesn't do exactly what i need.  I can use it to add the numbering at the end but if I need to renumber then it won't work the way I need it to.

Thank you for this.

Posted (edited)

I guess look for newline then work backwards two characters. Try this.

 

(defun c:wow ( / num str str1 str2 newstr obj  len )
(setq num (getint "\nEnetr start number "))
(while (setq ent (entsel "\nPick Mleader - Enter to stop. "))
 (setq obj (vlax-ename->vla-object (car ent)))
 (setq str (vlax-get obj 'textstring))
 (setq pos (vl-string-position 92 str))
 (if (= pos nil)
  (progn
   (setq len (strlen str))
   (if (< num 10)
   (setq newstr (strcat (substr str 1 (- len 2)) "0" (rtos num 2 0)))
   (setq newstr (strcat (substr str 1 (- len 2)) (rtos num 2 0)))
  )
  )
  (progn
   (if (< num 10)
    (setq newstr (strcat "0" (rtos num 2 0)))
    (setq newstr (rtos num 2 0))
   )
   (setq str1 (strcat (substr str 1 (- pos 2)) newstr))
   (setq str2 (substr str (+ pos 1)))
   (setq newstr (strcat str1 str2))
  )
 )
 (vlax-put obj 'textstring newstr)
 (setq num (1+ num))
)
(princ)
)

 

 

Edited by BIGAL
Posted
20 hours ago, rsdonna said:

I did.  I had found it before and it is great just doesn't do exactly what i need.  I can use it to add the numbering at the end but if I need to renumber then it won't work the way I need it to.

Thank you for this.

I linked several LISPs in that thread, which ones did you try?

Posted
1 hour ago, SLW210 said:

I linked several LISPs in that thread, which ones did you try?

I tried CopyText and MTMTMLT.  I found CopyText great as it is something that I will end up using quite a bit since I have to copy text to multiple mleaders sometimes, but neither of the ones I tried did what I was looking for unfortunately.  MTMTMLT only adds the sequential numbering at the end and doesn't let me replace what I have in the text as a place holder.  What I need replaced is always at the end of the first line and since I have multiple lines of text sometimes it doesn't work with the numbering being added at the end of all the text

Posted

There was a link to a LISP at Autodesk forums as well.

 

I have a few versions of my LISP as mentioned in that thread, I'll think about updating and altering one if I get time.

 

I need a clearly explained before and after .dwg, just go through what you do in TCOUNT, better yet show it with TCOUNT on MText as well, even with your drawings as MText, I followed the steps you gave in your first post and do not get the results you ask for.

Posted

You got lucky, stopped at work waiting on more information.

 

I lightly tested this on your drawings.

 

;;; Sequential Numbering added to MLeader, Mtext and Text with Find and Replace.                  *|
;;;                                                                                               *|
;;; https://www.cadtutor.net/forum/topic/26420-tcount-in-a-multileader/#comment-648519            *|
;;;                                                                                               *|
;;; With help from: https://forums.augi.com/showthread.php?117315-Modify-text-in-multileader      *|
;;;                                                                                               *|
;;; By SLW210 (Steve Wilson)                                                                      *|
;;;                                                                                               *|
;;; No guarantees use at your own risk                                                            *|
;;;                                                                                               *|
;;; Original 07/29/2025                                                                           *|
;;;                                                                                               *|
;;;                                                                                               *|
;;; Thanks most to AlanJT, lpseifert and irneb (Where have y'all gone?)                           *|
;;;                                                                                               *|
;;;************************************************************************************************|
;;;************************************************************************************************|
;;; When asked for FIND/REPLACE, leave blank to skip.                                             *|
;;;                                                                                               *|
;;; For MTEXT/MLEADER, enter All to apply the change to all lines, or specify a line number.      *|
;;;                                                                                               *|
;;;************************************************************************************************|
;;;************************************************************************************************|

(vl-load-com)

(defun c:FnRSeq () (c:FindReplaceLineSeq)) ; Shortcut command

(defun c:FindReplaceLineSeq (/		ss	   ent
			     en_obj	current_text
			     entType	findStr	   replaceBase
			     lineNum	lineNumStr lineList
			     newLines	idx	   replNum
			     newVal	i
			    )

  ;; Prompt for find text
  (setq findStr (getstring T "\nEnter FIND text: "))
  (if (= findStr "")
    (progn (princ "\nFIND string cannot be empty.") (exit))
  )

  ;; Starting replacement number
  (setq	replaceBase
	 (getstring T
		    "\nEnter starting REPLACE number (e.g. 01): "
	 )
  )
  (if (not (numberp (read replaceBase)))
    (progn (princ "\nInvalid number. Exiting.") (exit))
  )

  ;; Prompt for line number
  (initget 1 "All")
  (setq	lineNumStr
	 (getstring
	   "\nEnter line number for MTEXT/Multileader (or type 'All'): "
	 )
  )
  (setq	lineNum	(if (wcmatch (strcase lineNumStr) "ALL")
		  nil
		  (atoi lineNumStr)
		)
  )

  ;; Get selection set
  (setq ss (ssget '((0 . "TEXT,MTEXT,MULTILEADER"))))
  (if (not ss)
    (progn (princ "\nNo valid text entities selected.") (exit))
  )

  ;; Initialize replacement counter
  (setq replNum (atoi replaceBase))
  (setq idx 0)

  ;; Process each selected entity
  (repeat (sslength ss)
    (setq ent (ssname ss idx))
    (setq en_obj (vlax-ename->vla-object ent))
    (setq entType (cdr (assoc 0 (entget ent))))
    (setq current_text (vla-get-TextString en_obj))
    (setq newVal
	   (strcat
	     (substr "0000000000"
		     1
		     (- (strlen replaceBase) (strlen (itoa replNum)))
	     )
	     (itoa replNum)
	   )
    )

    ;; Process by entity type
    (cond
      ;; Plain TEXT
      ((= entType "TEXT")
       (vla-put-TextString
	 en_obj
	 (vl-string-subst newVal findStr current_text)
       )
      )

      ;; MTEXT or MULTILEADER
      ((or (= entType "MTEXT") (= entType "MULTILEADER"))
       (setq lineList (split-text-lines current_text))
       (setq i -1)
       (setq newLines
	      (mapcar
		(function
		  (lambda (line)
		    (setq i (1+ i))
		    (if	(or (null lineNum) (= i (1- lineNum)))
		      (vl-string-subst newVal findStr line)
		      line
		    )
		  )
		)
		lineList
	      )
       )
       (vla-put-TextString en_obj (join-lines newLines "\\P"))
      )
    )

    ;; Increment counter and index
    (setq replNum (1+ replNum))
    (setq idx (1+ idx))
  )

  (princ "\nSequential find & replace completed.")
  (princ)
)

;;;*************************|
;;; Functions               |
;;;*************************|

(defun split-text-lines	(txt)
  (vl-remove-if 'null (parse txt "\\P"))
)

(defun parse (str delim / result pos start)
  (setq	start 1
	result '()
  )
  (while (setq pos (vl-string-search delim str (1- start)))
    (setq result (cons (substr str start (- pos start -1)) result))
    (setq start (+ pos (strlen delim) 1))
  )
  (reverse (cons (substr str start) result))
)

(defun join-lines (lst sep)
  (if (null lst)
    ""
    (apply
      'strcat
      (cons (car lst)
	    (mapcar (function (lambda (s) (strcat sep s))) (cdr lst))
      )
    )
  )
)

 

This LISP just does a Find and Replace Sequentially no Prefix/Suffix.

Posted
1 hour ago, SLW210 said:

You got lucky, stopped at work waiting on more information.

 

I lightly tested this on your drawings.

 

;;; Sequential Numbering added to MLeader, Mtext and Text as Prefix, Suffix or Find and Replace.  *|
;;;                                                                                               *|
;;; https://www.cadtutor.net/forum/topic/98622-tcount-for-multileaders/#findComment-675446        *|
;;;                                                                                               *|
;;; With help from: https://forums.augi.com/showthread.php?117315-Modify-text-in-multileader      *|
;;;                                                                                               *|
;;; By SLW210 (Steve Wilson)                                                                      *|
;;;                                                                                               *|
;;; No guarantees use at your own risk                                                            *|
;;;                                                                                               *|
;;; Original 07/28/2025                                                                           *|
;;;                                                                                               *|
;;;                                                                                               *|
;;; Thanks most to AlanJT, lpseifert and irneb (Where have y'all gone?)                           *|
;;;                                                                                               *|
;**************************************************************************************************|
;**************************************************************************************************|
;;; When asked for FIND/REPLACE, leave blank to skip.                                             *|
;;;                                                                                               *|
;;; For MTEXT/MLEADER, enter All to apply the change to all lines, or specify a line number.      *|
;;;                                                                                               *|
;**************************************************************************************************|
;**************************************************************************************************|

(vl-load-com)

(defun c:TMTMS () (c:TxtMTxtMldrSeq))	; shortcut

(defun c:TxtMTxtMldrSeq	(/	    ss	       ent	  en_obj
			 current_text	       new_text	  num
			 prefix	    suffix     numType	  i
			 entType    findStr    replaceStr lineNum
			 lineNumStr lineList   newLines
			)

  ;; Prompt for prefix and suffix
  (setq prefix (getstring "\nEnter prefix text (or leave blank): "))
  (setq suffix (getstring "\nEnter suffix text (or leave blank): "))

  ;; Prompt for numbering type
  (setq	numType
	 (strcase
	   (getstring
	     "\nEnter 'P' for Prefix, 'S' for Suffix, or 'N' for No modification: "
	   )
	 )
  )
  (if (not (member numType '("P" "S" "N")))
    (progn (princ "\nInvalid choice. Exiting.") (exit))
  )

  ;; Find and Replace
  (setq	findStr
	 (getstring T "\nEnter FIND text (or press Enter to skip): ")
  )
  (setq	replaceStr
	 (if (/= findStr "")
	   (getstring T "\nEnter REPLACE text: ")
	   ""
	 )
  )

  ;; Line number
  (initget 1 "All")
  (setq	lineNumStr
	 (getstring
	   "\nEnter line number for MTEXT/Multileader (or type 'All'): "
	 )
  )
  (setq	lineNum	(if (wcmatch (strcase lineNumStr) "ALL")
		  nil
		  (atoi lineNumStr)
		)
  )

  ;; Get selection set
  (setq ss (ssget '((0 . "TEXT,MTEXT,MULTILEADER"))))
  (if (not ss)
    (progn (princ "\nNo valid text entities found.") (exit))
  )

  ;; Loop through entities
  (setq i 0)
  (repeat (sslength ss)
    (setq ent (ssname ss i))
    (setq en_obj (vlax-ename->vla-object ent))
    (setq entType (cdr (assoc 0 (entget ent))))
    (setq current_text (vla-get-TextString en_obj))
    (setq num (itoa (1+ i)))

    ;; Find & replace
    (if	(and (/= findStr "") (/= replaceStr ""))
      (setq current_text
	     (vl-string-subst replaceStr findStr current_text)
      )
    )

    (cond
      ;; TEXT
      ((= entType "TEXT")
       (setq new_text
	      (cond
		((= numType "P") (strcat prefix num " " current_text))
		((= numType "S") (strcat current_text " " suffix num))
		((= numType "N") current_text)
	      )
       )
       (vla-put-TextString en_obj new_text)
      )

      ;; MTEXT or MULTILEADER
      ((or (= entType "MTEXT") (= entType "MULTILEADER"))
       (setq lineList (split-text-lines current_text))

       (setq newLines
	      (if (and lineNum (> lineNum 0) (<= lineNum (length lineList)))
		;; Modify specific line
		(mapcar
		  '(lambda (line idx)
		     (if (= idx (1- lineNum))
		       (cond
			 ((= numType "P") (strcat prefix num " " line))
			 ((= numType "S") (strcat line " " suffix num))
			 ((= numType "N") line)
		       )
		       line
		     )
		   )
		  lineList
		  (number-seq 0 (1- (length lineList)))
		)
		;; Modify all lines
		(mapcar
		  '(lambda (line)
		     (cond
		       ((= numType "P") (strcat prefix num " " line))
		       ((= numType "S") (strcat line " " suffix num))
		       ((= numType "N") line)
		     )
		   )
		  lineList
		)
	      )
       )

       ;; Combine lines
       (setq new_text
	      (apply 'strcat
		     (mapcar '(lambda (l) (strcat l "\\P")) newLines)
	      )
       )
       (vla-put-TextString en_obj new_text)
      )
    )

    (setq i (1+ i))
  )

  (princ "\nModifications completed.")
  (princ)
)

;; Split text into lines (MTEXT line delimiter = \P)
(defun split-text-lines	(txt)
  (vl-remove-if 'null (parse txt "\\P"))
)

;; Parse string by delimiter
(defun parse (str delim / result pos start)
  (setq start 1)
  (while (setq pos (vl-string-search delim str (1- start)))
    (setq result (cons (substr str start (- pos start)) result))
    (setq start (+ pos (strlen delim) 1))
  )
  (reverse (cons (substr str start) result))
)

;; Create list of numbers from start to end
(defun number-seq (start end)
  (if (> start end)
    nil
    (cons start (number-seq (1+ start) end))
  )
)

 

I left the options from the other LISP, due to time, if you want or I get more time, I might remove the other options.

I gave this a try and I don't know if maybe I'm doing something wrong but it gave me something weird.

It changed the end of the 1st line but it just gave me 0's instead of renumbering and then on the two middle mleaders it removed the "D" at the end of strand.

 

Thanks for your help with this, really appreciate it!

2025-07-28 12_51_17-Autodesk AutoCAD 2026 - [SAMPLE.dwg].png

2025-07-28 12_52_15-Autodesk AutoCAD 2026 - [SAMPLE.dwg].png

Posted (edited)

Did you try what I posted ? Worked for me.

 

image.png.33388243a2e9f0d8ae6c551064562895.png

Edited by BIGAL
Posted

You still haven't provided the before and after drawing or the steps you want.

19 hours ago, SLW210 said:

There was a link to a LISP at Autodesk forums as well.

 

I have a few versions of my LISP as mentioned in that thread, I'll think about updating and altering one if I get time.

 

I need a clearly explained before and after .dwg, just go through what you do in TCOUNT, better yet show it with TCOUNT on MText as well, even with your drawings as MText, I followed the steps you gave in your first post and do not get the results you ask for.

 

Posted

I placed a new LISP in my earlier post to just do a Sequential Find and Replace, see if that works for you.

 

I am working on a bigger LISP with dialog box input and select the options, but that's going to be a while due to my workload.

Posted
12 hours ago, BIGAL said:

Did you try what I posted ? Worked for me.

 

image.png.33388243a2e9f0d8ae6c551064562895.png

 

Works for me here as well. 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...