Author Topic: Add a character to the end of a piece of text  (Read 1647 times)

0 Members and 1 Guest are viewing this topic.

GrantW

  • Guest
Add a character to the end of a piece of text
« on: August 07, 2012, 08:50:41 PM »
Hey folks,
I'm wondering if there is a quick way to add a single character to a piece of text. The problem I'm having is that I need to show a heap of text rounded to 2 dp and shown to 3dp (I don't know why but thats the clients standard?!?!) eg 23.450, 54.910
The program I use to generate the numbers will spit them out as 23.45 and 54.91 and I need to manually add on a 0 to the end of every number. Is it something that can be done in Autocad or is this a lisp scenario?
Cheers in advance

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Add a character to the end of a piece of text
« Reply #1 on: August 07, 2012, 09:05:32 PM »
You could try something like this:
Welcome to The Swamp  :-)
Code - Auto/Visual Lisp: [Select]
  1. (defun c:addzero (/ e i ss)
  2.   (if (setq ss (ssget '((0 . "text"))))
  3.     (progn (setq i -1)
  4.            (while (setq e (ssname ss (setq i (1+ i))))
  5.              (entmod
  6.                (subst (cons 1 (STRCAT (cdr (assoc 1 (entget e))) "0"))
  7.                       (assoc 1 (entget e))
  8.                       (entget e)
  9.                )
  10.              )
  11.            )
  12.     )
  13.   )
  14.   (princ)
  15. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

GrantW

  • Guest
Re: Add a character to the end of a piece of text
« Reply #2 on: August 08, 2012, 12:45:22 AM »
Thanks ronjonp, I have only logged on a few times but find it a great resource. I have hardly any experience in customisation but hopefully can add something helpful one day!

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Add a character to the end of a piece of text
« Reply #3 on: August 08, 2012, 02:15:58 AM »
Here is a slightly different method that doesn't care how many decimal places are already showing. It will set all selected text to 3 decimal places adding zeros as needed.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:addzero (/ e i ss)
  2.   (setq ss (ssget '((0 . "text")))
  3.         i  -1
  4.   )
  5.   (while (and ss (setq e (ssname ss (setq i (1+ i)))))
  6.     ;; set the numeric precision to 3 decimal places
  7.     (set_precision e 3)
  8.   )
  9.   (princ)
  10. )
  11.  
  12. (defun set_precision (entity precision / elist val)
  13.   ;; Get the entity list and current text value
  14.   (setq elist (entget entity)
  15.         val   (cdr (assoc 1 elist))
  16.   )
  17.   ;; if text is numeric
  18.   (if (/= nil (distof val))
  19.     (entmod
  20.       ;; format it to n decimal places of precision
  21.       (subst (cons 1 (rtos (distof val 2) 2 precision))
  22.              (assoc 1 elist)
  23.              elist
  24.       )
  25.     )
  26.   )
  27. )
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Add a character to the end of a piece of text
« Reply #4 on: August 08, 2012, 08:20:58 AM »
Here is a generic Prefix / Suffix function I wrote a while back:

http://www.cadtutor.net/forum/showthread.php?64606&p=441001&viewfull=1#post441001

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Add a character to the end of a piece of text
« Reply #5 on: August 08, 2012, 08:22:23 AM »
Here is a slightly different method that doesn't care how many decimal places are already showing. It will set all selected text to 3 decimal places adding zeros as needed.

Be careful of DIMZIN  :wink: