Jump to content

How to round numbers to 0 or 5


dimitarrr
 Share

Recommended Posts



(defun c:DDD ( / a_lst b_lst obj len pt idx mtxt)
(setq a_lst (list "PIPE" "PP 20*2.3" "PP 25*2.5" "PP 32*3.0" "PP 40*3.7" "PP 50*4.6" "PP 63*5.8" "1 1/2" "2" "3" "4" "5"))
(setq b_lst (list "PP 20*2.3" "PP 25*2.5" "PP 32*3.0" "PP 40*3.7" "PP 50*4.6" "1 1#4" "1 1#2" "2" "3" "4" "5"))
(setq obj (vlax-ename->vla-object (car (entsel "\nSelect Line : ")))
      len (rtos (vlax-get-property obj 'length) 2 0)
)
(setq pt (mapcar '(lambda (x y) (/ (+ x y) 2.0)) (vlax-get obj 'startpoint) (vlax-get obj 'endpoint)))
(setq idx (ah:butts 1 "v"  a_lst)) 
(cond ( (setq mtxt (strcat "L=" len "\\P" (nth idx b_lst)  )) (vl-position idx '(0 1 2 3)) )
      ( (setq mtxt (strcat  (substr "L=" len "\\P" (nth idx b_lst) 1 1) (substr (nth idx b_lst) 3)  )) (vl-position idx '(5 6)) )
      (t (setq mtxt (strcat "L=" len "\\P" (nth idx b_lst) )))
)

(command "_mleader" pt pause mtxt)
)
 

 

I need help. I want to round numbers at the end. For example if length of line is 173 and its nearest to 175 the result to be 175. If length of line is 178 the result to be 180.

 

Link to comment
Share on other sites

(setq num (getreal))
(if (< (rem num 5) 2.5) (setq num05 (- num (rem num 5))) (setq num05 (+ (- num (rem num 5)) 5)))

Link to comment
Share on other sites

This should do it.

 

(setq obj (vlax-ename->vla-object (car (entsel "\nSelect Line : ")))
      len (rtos (LM:roundm (vlax-get-property obj 'length) 5) 2 0)
)

;; Round Multiple  -  Lee Mac
;; Rounds 'n' to the nearest multiple of 'm'
(defun LM:roundm ( n m )
    (* m (fix ((if (minusp n) - +) (/ n (float m)) 0.5)))
)

 

--edit

I'm getting nil for pt if your calculating midpoint try this.

(setq pt (mapcar '/ (mapcar '+ (vlax-get obj 'startpoint) (vlax-get obj 'endpoint)) '(2 2)))

 

 

Edited by mhupp
  • Like 3
Link to comment
Share on other sites

(vl-load-com)
(defun AH:Butts (AHdef verhor butlst / fo fname x  k )
(setq fo (open (setq fname (vl-filename-mktemp "" "" ".dcl")) "w"))
(write-line  "AHbutts : dialog  {" fo)
(write-line  (strcat "  label =" (chr 34) (nth 0 butlst) (chr 34) " ;" )fo)
(write-line "   : row   {" fo)
(if (=  (strcase verhor) "V")
(progn
(write-line "   : boxed_radio_column    {" fo)
(write-line  (strcat " width = " (rtos (+ (strlen (nth 0 butlst)) 10) 2 0) " ;")  fo)           ; increase 10 if label does not appear
)
(write-line "   : boxed_radio_row       {" fo)
)
(setq x 1)
(repeat (- (length butlst) 1) 
(write-line "   : radio_button  {" fo)
(write-line  (strcat "key = "  (chr 34) "Rb" (rtos x 2 0)  (chr 34) ";") fo)
(write-line  (strcat "label = " (chr 34) (nth x  butlst) (chr 34) ";") fo)
(write-line "   }" fo)
(write-line "spacer_1 ;" fo)
(setq x (+ x 1))
)
(write-line "   }" fo)
(write-line "   }" fo)
(write-line "spacer_1 ;" fo)
(write-line "   ok_only;" fo)
(write-line "   }" fo)
(close fo)
(setq dcl_id (load_dialog fname))
(if (not (new_dialog "AHbutts" dcl_id) )
(exit)
)
(setq x 1)
(repeat (- (length butlst) 1)
(setq k (strcat "Rb" (rtos x 2 0)))
(action_tile k  (strcat "(setq but "  (rtos x 2 0) ")" "(done_dialog)"))
(if (= ahdef x)(set_tile k "1"))
(setq x (+ x 1))
)
(action_tile "accept" (strcat "(setq but "  (rtos ahdef 2 0) ")" "(done_dialog)"))
(start_dialog)
(unload_dialog dcl_id)
(vl-file-delete fname)
(1- but)
)
 
 
(defun c:DDD ( / a_lst b_lst obj len pt idx mtxt)
(setq a_lst (list "PIPE" "PP 20*2.3" "PP 25*2.5"))
(setq b_lst (list "PP 20*2.3" "PP 25*2.5"))
(setq obj (vlax-ename->vla-object (car (entsel "\nSelect Line : ")))
      len (rtos (vlax-get-property obj 'length) 2 0)
)
(setq pt (mapcar '(lambda (x y) (/ (+ x y) 2.0)) (vlax-get obj 'startpoint) (vlax-get obj 'endpoint)))
(setq idx (ah:butts 1 "v"  a_lst)) 
(cond ( (setq mtxt (strcat "L=" len "\\P" (nth idx b_lst)  )) (vl-position idx '(0 1 2 3)) )
      ( (setq mtxt (strcat  (substr "L=" len "\\P" (nth idx b_lst) 1 1) (substr (nth idx b_lst) 3)  )) (vl-position idx '(5 6)) )
      (t (setq mtxt (strcat "L=" len "\\P" (nth idx b_lst) )))
)
(command "_mleader" pt pause mtxt)
)
 

 

Here I want L=125, not 124. I tried your suggestions but didn't work.

Test.jpg

Link to comment
Share on other sites

;;----------------------------------------------------------------------;;
;; CREATE MLEADER W/ OPTIONS SUBFUNCTIONS AH:Butts & LM:ROUNDM
(defun c:DDD (/ a_lst b_lst obj len pt idx mtxt)
  (vl-load-com)
  (setq a_lst (list "PIPE" "PP 20*2.3" "PP 25*2.5"))
  (setq b_lst (list "PP 20*2.3" "PP 25*2.5"))
  (setq obj (vlax-ename->vla-object (car (entsel "\nSelect Line : ")))
      len (rtos (LM:roundm (vlax-get-property obj 'length) 5) 2 0)
  )
  (setq pt (mapcar '/ (mapcar '+ (vlax-get obj 'startpoint) (vlax-get obj 'endpoint)) '(2 2)))
  (setq idx (ah:butts 1 "v" a_lst))
  (cond
    ((setq mtxt (strcat "L=" len "\\P" (nth idx b_lst))) (vl-position idx '(0 1 2 3)))
    ((setq mtxt (strcat (substr "L=" len "\\P" (nth idx b_lst) 1 1) (substr (nth idx b_lst) 3))) (vl-position idx '(5 6)))
    (t (setq mtxt (strcat "L=" len "\\P" (nth idx b_lst))))
  )
  (command "_mleader" pt pause mtxt)
)
;;----------------------------------------------------------------------;;
;; Multi AHbutton Dialog box for a single choice repalcment of initget
;; By Alan H Feb 2019
(defun AH:Butts (AHdef verhor butlst / fo fname x k)
  (setq fo (open (setq fname (vl-filename-mktemp "" "" ".dcl")) "w"))
  (write-line "AHbutts : dialog  {" fo)
  (write-line (strcat "  label =" (chr 34) (nth 0 butlst) (chr 34) " ;") fo)
  (write-line "   : row   {" fo)
  (if (= (strcase verhor) "V")
    (progn
      (write-line "   : boxed_radio_column    {" fo)
      (write-line (strcat " width = " (rtos (+ (strlen (nth 0 butlst)) 10) 2 0) " ;") fo)  ; increase 10 if label does not appear
    )
    (write-line "   : boxed_radio_row       {" fo)
  )
  (setq x 1)
  (repeat (- (length butlst) 1)
    (write-line "   : radio_button  {" fo)
    (write-line (strcat "key = " (chr 34) "Rb" (rtos x 2 0) (chr 34) ";") fo)
    (write-line (strcat "label = " (chr 34) (nth x butlst) (chr 34) ";") fo)
    (write-line "   }" fo)
    (write-line "spacer_1 ;" fo)
    (setq x (+ x 1))
  )
  (write-line "   }" fo)
  (write-line "   }" fo)
  (write-line "spacer_1 ;" fo)
  (write-line "   ok_only;" fo)
  (write-line "   }" fo)
  (close fo)
  (setq dcl_id (load_dialog fname))
  (if (not (new_dialog "AHbutts" dcl_id))
    (exit)
  )
  (setq x 1)
  (repeat (- (length butlst) 1)
    (setq k (strcat "Rb" (rtos x 2 0)))
    (action_tile k (strcat "(setq but " (rtos x 2 0) ")" "(done_dialog)"))
    (if (= ahdef x) (set_tile k "1"))
    (setq x (+ x 1))
  )
  (action_tile "accept" (strcat "(setq but " (rtos ahdef 2 0) ")" "(done_dialog)"))
  (start_dialog)
  (unload_dialog dcl_id)
  (vl-file-delete fname)
  (1- but)
)
;; Round Multiple  -  Lee Mac
;; Rounds 'n' to the nearest multiple of 'm'
(defun LM:roundm ( n m )
    (* m (fix ((if (minusp n) - +) (/ n (float m)) 0.5)))
)

 

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

dimitarr please put a comment in about using my Multi radio buttons code, its a recognition of where you got code from. See Mhupp code.

 

; Multi radio Buttons by Alanh Consulting. 

 

There is more Multi Toggles, Multi getvals, Muti Radios 2col.

 

You can avoid putting multi  in your code altogether, just make sure its saved in a support path directory.

 

(if (not AH:Butts)(load "Multi radio buttons.lsp")) 			; loads the program if not loaded already

 

  • Agree 1
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
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.

 Share

×
×
  • Create New...