Author Topic: Divide a TEXT with specific number & replace original TEXT with divided result.  (Read 568 times)

0 Members and 1 Guest are viewing this topic.

JohnCamper

  • Mosquito
  • Posts: 9
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 ?

zak26

  • Mosquito
  • Posts: 7
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)
)

JohnCamper

  • Mosquito
  • Posts: 9
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.

zak26

  • Mosquito
  • Posts: 7
Have you tried the lisp?

JohnCamper

  • Mosquito
  • Posts: 9
Yes sir, I did ! It asks for the text to divide and replace.
Pic for reference


zak26

  • Mosquito
  • Posts: 7
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)
)

JohnCamper

  • Mosquito
  • Posts: 9
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 .

Dave M

  • Newt
  • Posts: 195
Here's something else you can try as well.
Civil 3D 2018 - Microstation SS4 - Windows 10 - Dropbox

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9624
Welcome to TheSwamp, zak26.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

zak26

  • Mosquito
  • Posts: 7
Thanks John Kaul

BIGAL

  • Swamp Rat
  • Posts: 711
  • 30 + years of using Autocad
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"))))
A man who never made a mistake never made anything

zak26

  • Mosquito
  • Posts: 7
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?

BIGAL

  • Swamp Rat
  • Posts: 711
  • 30 + years of using Autocad
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.

« Last Edit: May 17, 2021, 03:14:09 AM by BIGAL »
A man who never made a mistake never made anything

zak26

  • Mosquito
  • Posts: 7
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.

BIGAL

  • Swamp Rat
  • Posts: 711
  • 30 + years of using Autocad
Did you look at the doc file ? Its a fraction of what I have 40 years at it.
A man who never made a mistake never made anything