TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnCamper on May 13, 2021, 12:24:06 PM

Title: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: JohnCamper on May 13, 2021, 12:24:06 PM
I have thousands of Texts on CAD.I made a mistake and forgot to divide the Texts with specific number(i.e. 3.2 to be precise).
For eg: I have 50m in CAD as text and i want to divide it with 3.2 and replace that 50m with ≈16m (rounded up).
I searched the whole internet & the forums for the lisp or any other solution but i could not find anything that I could work upon as a reference or a foundation. Could anyone help me with it ?
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: zak26 on May 13, 2021, 08:03:15 PM
Well you can use qselect, select the text, and in contents put the string you are looking for and then in properties replace the contents for what you want.
Or also use this code.
Code: [Select]
;;; Divides and replaces a text
;;; By Isaac A. 20210513
;;; http://www.theswamp.org/index.php?action=post;topic=56742.0;last_msg=604663
(defun c:d&r (/ div i ss srch srch1 str)
   (setq srch (getstring T "\nType the text to divide and replace: ")
         div  (getreal "\nType the divisor: ")
         srch1 (atof srch)
         rplc (strcat (rtos (/ srch1 div) 2 0) "m")
   )
   (if (setq ss (ssget "_X" (list (cons 0 "*TEXT") (cons 1 (strcat "*" srch "*"))
 (cons 410 (getvar 'ctab)))))
      (progn
         (setq i -1)
         (repeat (sslength ss)
            (setq i (1+ i))
            (setq enam (entget (ssname ss i)))
            (entmod (subst (cons 1 rplc) (cons 1 srch) enam))
         )
      )
   )
   (princ)
)
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: JohnCamper on May 13, 2021, 08:28:19 PM
Thanks for the reply !
Your solution partly works Sir, bcoz it works as search & replace. But in this case it is tedious to search the individual texts and replace as there are thousands of texts.So how can it me made such that it :-Divides all the Selected texts with divisor at once.
ie 1st step : To select all the required texts (manually).
   2nd step: Ask for a divisor and replace all the selected texts with their respective divided results.
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: zak26 on May 13, 2021, 08:37:10 PM
Have you tried the lisp?
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: JohnCamper on May 13, 2021, 08:42:16 PM
Yes sir, I did ! It asks for the text to divide and replace.
Pic for reference

Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: zak26 on May 13, 2021, 09:04:30 PM
Ok, so I missunderstood before and This is what you want.
Code: [Select]
;;; Divides texts by 3.2 and replaces the texts
;;; By Isaac A. 20210513
;;; http://www.theswamp.org/index.php?action=post;topic=56742.0;last_msg=604663
(defun c:d&r (/ div enam i rplc ss srch srch1 str)
   (if (setq ss (ssget "_X" (list (cons 0 "*TEXT") (cons 1 (strcat "*" "m"))
 (cons 410 (getvar 'ctab)))))
      (progn
         (setq i -1)
         (repeat (sslength ss)
            (setq i (1+ i))
            (setq enam (entget (ssname ss i))
                  srch (cdr (assoc 1 enam))
                  srch1 (atof srch)
                  rplc (strcat (rtos (/ srch1 3.2) 2 0) "m")
            )
            (entmod (subst (cons 1 rplc) (cons 1 srch) enam))
         )
      )
   )
   (princ)
)
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: JohnCamper on May 13, 2021, 09:13:06 PM
Wow !! Exactly what i wanted. Thank you so much, sir !  Saved me a ton of time.:smitten: If there was a selection option of texts on the code of which i wanted to divide, it would be even better .
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: Dave M on May 14, 2021, 12:11:26 PM
Here's something else you can try as well.
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: JohnK on May 14, 2021, 12:13:09 PM
Welcome to TheSwamp, zak26.
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: zak26 on May 14, 2021, 03:38:42 PM
Thanks John Kaul
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: BIGAL on May 15, 2021, 08:21:57 PM
It may be worthwhile adding an option pick a text and get the layer name as the ssget "X" gets all text also maybe a (410 . "Model"), these can be added pretty easy into the ssget. Can also use variables extmin extmax to pick window so no need for user to pick an area.

(if (setq ss (ssget "_X" (list (cons 0 "*TEXT") (cons 1 "*m")(cons 8 lay)(cons 410 "Model"))))
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: zak26 on May 15, 2021, 10:57:18 PM
Valid points BigAl, but the user wanted everything in just a click or a command call, and I don't know if there were many or just one layer, but thanks, is something to consider.
P.S. I didn't know that I could use "*m" instead of (strcat "*" "m") There still too much that I have to learn. And also I'd like you to give me some opinions on my programs but those are to sell, so can I send you emails?
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: BIGAL on May 17, 2021, 03:10:21 AM
The single click would be pick a text hence get layer.

Perhaps describe what your other lisps are, there is so much free stuff out there and selling is almost impossible.

Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: zak26 on May 17, 2021, 10:32:59 AM
Well maybe you're right, and besides I'm not discovering the wheel, but I'll keep posting and asking for advice and solutions for my programs when I can't find myself a solution. Thank you.
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: BIGAL on May 17, 2021, 09:03:57 PM
Did you look at the doc file ? Its a fraction of what I have 40 years at it.
Title: Re: Divide a TEXT with specific number & replace original TEXT with divided result.
Post by: zak26 on May 18, 2021, 01:02:02 AM
Didn't until now that you posted the comment, I don't get used to Theswamp way of showing posts, that doesn't show the pics and the attachment's if you're not logged.

It's impressive your work, and I'm glad and want to thank you for sharing your knowledge with us. You have helped me a lot, (more in CadTutor) but thanks.