PDA

View Full Version : Rounding text to nearest 5 - values inside text



Arterius
2016-08-30, 11:56 AM
Dear users,

do you have any routine that can round text to the nearest values?
I have a lots of texts in autocad (not mtext, if importatnt), there are values inside text (only values, no additional text or symbols), I need to round all of them to the nearest 5 (eg. 122 -> 120 or 123->125).
Do you have any idea?

Thanks in advance!

Tom Beauford
2016-08-30, 01:33 PM
This should help: http://www.cadtutor.net/forum/showthread.php?50398-Lisp-routine-that-will-round-decimal-places

Arterius
2016-09-01, 08:37 AM
not realy, these codes erase unnecessary zeros from the end of number (eg. 123.00 -> 123 or 21.40 -> 21.4), did not round up or down... :(
but thanks for replay!

Bruno.Valsecchi
2016-09-01, 01:02 PM
With this?



(defun round_number (xr n / )
(* (fix (atof (rtos (/ xr (float n)) 2 0))) n)
)


Model use:
(round_number 122 5) ->120
(round_number 123 5) ->125

or (round_number 123.123 0.005) -> 123.125

Arterius
2016-09-01, 01:12 PM
thanks Bruno, but I have no idea how to use it :) I am poor in routines :(
can you help and provide full code? please...
How to change in code rounding method, eg. I would like to round up to nearest 5 or 0.5...?
thanks in advance!

Tom Beauford
2016-09-01, 02:05 PM
not realy, these codes erase unnecessary zeros from the end of number (eg. 123.00 -> 123 or 21.40 -> 21.4), did not round up or down... :(
but thanks for replay!

C3PO rounds to two decimal places, just change the 2 on the line 21 to 0 and it should do as you want.
For 2dp change (rtos item 2 2) to (rtos item 2 0)

Bruno.Valsecchi
2016-09-01, 10:07 PM
Try this


(defun round_number (xr n / )
(* (fix (atof (rtos (/ xr (float n)) 2 0))) n)
)
(defun c:round_text ( / js dxf_cod mod_sel n round lremov ename)
(princ "\nSelect an model object text for filtering: ")
(while
(null
(setq js
(ssget "_+.:E:S"
(list
'(0 . "*TEXT")
(cons 67 (if (eq (getvar "CVPORT") 1) 1 0))
(cons 410 (if (eq (getvar "CVPORT") 1) (getvar "CTAB") "Model"))
)
)
)
)
(princ "\nIsn't an object available for this fonction!")
)
(vl-load-com)
(setq dxf_cod (entget (ssname js 0)))
(foreach m (foreach n dxf_cod (if (not (member (car n) '(0 67 410 8 6 62 48 420 70))) (setq lremov (cons (car n) lremov))))
(setq dxf_cod (vl-remove (assoc m dxf_cod) dxf_cod))
)
(initget "Single All Manual")
(if (eq (setq mod_sel (getkword "\nSelection filtered by, [Single/All/Manual]<Manual>: ")) "Single")
(setq n -1)
(if (eq mod_sel "All")
(setq js (ssget "_X" dxf_cod) n -1)
(setq js (ssget dxf_cod) n -1)
)
)
(initget 1)
(setq round (getreal "\nRound up to near at?: "))
(repeat (sslength js)
(setq ename (vlax-ename->vla-object (ssname js (setq n (1+ n)))))
(if (vlax-property-available-p ename 'TextString)
(cond
((eq (type (setq value (read (vlax-get ename 'TextString)))) 'REAL)
(vlax-put ename 'TextString (rtos (round_number value round) 2 2))
)
((eq (type value) 'INT)
(vlax-put ename 'TextString (rtos (round_number value round) 2 0))
)
)
)
)
(prin1)
)

Arterius
2016-09-02, 12:57 PM
Thanks a lot for routine!
Works perfect.