Jump to content

Can someone edit this code?


ktbjx

Recommended Posts

(defun c:roundingz (/ o s)
 (if (setq s (ssget ":L" '((0 . "*text"))))
   (foreach a (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
     (setq o (vlax-ename->vla-object a))
     
     (vla-put-textstring o (rtos (lm:roundto (atof (vla-get-textstring o)) 2) 2 1))
)
 )
 (princ)
)
(defun lm:roundto (n p) (lm:roundm n (expt 10.0 (- p))))
(defun lm:roundm (n m) (* m (atoi (rtos (/ n (float m)) 2 0))))

is it possible that numbers below 0.05 will retain the number,
higher than that, will be changed into 1 decimal point??
because for example, 0.04 will give me 0.0... i need it to be the same 0.04

Link to comment
Share on other sites

54 minutes ago, ktbjx said:
55 minutes ago, ktbjx said:

(vla-put-textstring o (rtos (lm:roundto (atof (vla-get-textstring o)) 2) 2 1))

 

(vla-put-textstring o (rtos (lm:roundto (atof (vla-get-textstring o)) 2) 2 2))
Link to comment
Share on other sites

1 hour ago, 1958 said:

(vla-put-textstring o (rtos (lm:roundto (atof (vla-get-textstring o)) 2) 2 2))

this makes the number into 2 decimals.

the one i posted makes it into 1 decimal...

 

so i was thinking if condition? but IDK how

something like:

if the value is <= 0.05

(vla-put-textstring o (rtos (lm:roundto (atof (vla-get-textstring o)) 2) 2 2))

else

(vla-put-textstring o (rtos (lm:roundto (atof (vla-get-textstring o)) 2) 2 1)) 

 

Link to comment
Share on other sites

(defun c:roundingz (/ o s aa)
 (if (setq s (ssget ":L" '((0 . "*text"))))
  (foreach a (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
   (setq o  (vlax-ename->vla-object a)
         aa (lm:roundto (atof (vla-get-textstring o)) 2)
   )
   (if (<= aa 0.05)
    (vla-put-textstring o (rtos aa 2 1))
    (vla-put-textstring o (rtos aa 2 2))
   )
  )
 )
 (princ)
)
(defun lm:roundto (n p) (lm:roundm n (expt 10.0 (- p))))
(defun lm:roundm (n m) (* m (atoi (rtos (/ n (float m)) 2 0))))
Link to comment
Share on other sites

32 minutes ago, 1958 said:

(if (<= aa 0.05)
    (vla-put-textstring o (rtos aa 2 1))
    (vla-put-textstring o (rtos aa 2 2))
   )

 

 

(vla-put-textstring o (rtos aa 2 (if (<= aa 0.05) 1 2)))

 

  • Like 1
Link to comment
Share on other sites

Note that rtos will round to the given precision, hence the code can become:

(defun c:roundingz ( / i s v x )
    (if (setq s (ssget "_:L" (cons '(0 . "*TEXT") (kg:SsgetFilterReal))))
        (repeat (setq i (sslength s))
            (setq i (1- i)
                  x (entget (ssname s i))
                  v (assoc 1 x)
            )
            (entmod (subst (cons 1 (rtos (atof (cdr v)) 2 (if (<= (atof (cdr v)) 0.05) 2 1))) v x))
        )
    )
    (princ)
)
(defun kg:SsgetFilterReal ()   ; Courtesy of Roy
   '(
        (1 . "~*[~-.0-9]*")    ; only minus signs, decimal points and numbers allowed
        (1 . "*`.*")           ; there must be one decimal point
        (1 . "~*`.*`.*")       ; only one decimal point allowed
        (-4 . "<OR")
           (1 . "~*-*")        ; there is no minus sign
           (-4 . "<AND")
               (1 . "-*")      ; the minus sign must be the first character
               (1 . "~*-*-*")  ; only one minus sign allowed
           (-4 . "AND>")
        (-4 . "OR>")
    )
)

(princ)

My thanks to @Roy_043 for the useful ssget filter list function.

Edited by Lee Mac
Link to comment
Share on other sites

10 hours ago, 1958 said:

(defun c:roundingz (/ o s aa)
 (if (setq s (ssget ":L" '((0 . "*text"))))
  (foreach a (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
   (setq o  (vlax-ename->vla-object a)
         aa (lm:roundto (atof (vla-get-textstring o)) 2)
   )
   (if (<= aa 0.05)
    (vla-put-textstring o (rtos aa 2 1))
    (vla-put-textstring o (rtos aa 2 2))
   )
  )
 )
 (princ)
)
(defun lm:roundto (n p) (lm:roundm n (expt 10.0 (- p))))
(defun lm:roundm (n m) (* m (atoi (rtos (/ n (float m)) 2 0))))

 

thank you sir! now i know how it should look like using if statement...

 

 

 

10 hours ago, Tharwat said:

 

 


(vla-put-textstring o (rtos aa 2 (if (<= aa 0.05) 1 2)))

 

wow, you just made it even shorter! i will try this sir! thanks!

 

 

 

10 hours ago, Lee Mac said:

Note that rtos will round to the given precision, hence the code can become:


(defun c:roundingz ( / i s v x )
    (if (setq s (ssget "_:L" (cons '(0 . "*TEXT") (kg:SsgetFilterReal))))
        (repeat (setq i (sslength s))
            (setq i (1- i)
                  x (entget (ssname s i))
                  v (assoc 1 x)
            )
            (entmod (subst (cons 1 (rtos (atof (cdr v)) 2 (if (<= (atof (cdr v)) 0.05) 2 1))) v x))
        )
    )
    (princ)
)
(defun kg:SsgetFilterReal ()   ; Courtesy of Roy
   '(
        (1 . "~*[~-.0-9]*")    ; only minus signs, decimal points and numbers allowed
        (1 . "*`.*")           ; there must be one decimal point
        (1 . "~*`.*`.*")       ; only one decimal point allowed
        (-4 . "<OR")
           (1 . "~*-*")        ; there is no minus sign
           (-4 . "<AND")
               (1 . "-*")      ; the minus sign must be the first character
               (1 . "~*-*-*")  ; only one minus sign allowed
           (-4 . "AND>")
        (-4 . "OR>")
    )
)

(princ)

My thanks to @Roy_043 for the useful ssget filter list function.

 

okay, this sir makes my life even easier! for filtering the strings out! i like this! its very useful.

I dont have to manually check the text to be a number,

ican just select them all and the routine will do the work!

 

 

Link to comment
Share on other sites

13 hours ago, ktbjx said:

Okay, this sir makes my life even easier! for filtering the strings out! i like this! its very useful.

I dont have to manually check the text to be a number,

ican just select them all and the routine will do the work!

 

 

You're welcome! 🍺

Link to comment
Share on other sites

7 minutes ago, Lee Mac said:

 

I think you have the then/else values switched 😉

I guess you are right although and as you can see, I just shortened the codes for the user 1958 and did not read OP's codes to check them out. 

Thanks for pointing that out. :)

Link to comment
Share on other sites

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...