Jump to content

round up number in autolisp


Qonfire

Recommended Posts

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    5

  • Qonfire

    5

  • irneb

    4

  • ckwilliam

    2

Not sure what mousho wants:

; Round to first even number in the direction of pos. or neg. infinity.
; (RoundToEvenInf  12.1) =>  14
; (RoundToEvenInf -12.1) => -14
(defun RoundToEvenInf (num)
 (if (zerop (rem num 2))
   num
   (* 2 (fix ((if (minusp num) - +) (/ num 2.0) 1)))
 )
)

; Round to first even number in the direction of pos. infinity.
; (RoundToEvenUp  12.1) =>  14
; (RoundToEvenUp -12.1) => -12
(defun RoundToEvenUp (num)
 (if (zerop (rem num 2))
   num
   (* 2 (fix (+ (/ num 2.0) (if (minusp num) 0 1))))
 )
)

Link to comment
Share on other sites

  • 1 year later...
(defun round (number)
 (setq fixn (fix number))
 (if (>= (- number fixn) 0.5)
   (setq rnum (1+ fixn))
   (setq rnum fixn)
 )
)

Edited by SLW210
Code Tags
Link to comment
Share on other sites

  • 9 months later...

Hi, i would like to "round up" to the next .5 in the area dimension routine that we already use. how can i do that and where to integrate the routine.

here's the code that we are using.

 

thank you


(defun c:aaa ( / *error* _SelectIf _ObjectID acdoc acspc d1 d2 fieldformatting msg predicate pt )

    (setq fieldformatting "%lu2%ct4%qf1 PC ") ;; Field Formatting

    (defun *error* ( msg )
        (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
	
    (defun _SelectIf ( msg pred )
        (
            (lambda ( f / e )
                (while
                    (progn (setvar 'ERRNO 0) (setq e (car (entsel msg)))
                        (cond
                            (   (= 7 (getvar 'ERRNO))
                                (princ "\nMissed, try again.")
                            )
                            (   (eq 'ENAME (type e))
                                (if (and f (null (f e)))
                                    (princ "\nInvalid Object.")
                                )
                            )
                        )
                    )
                )
                e
            )
            (eval pred)
        )
    )
		
    (setq acdoc (vla-get-activedocument (vlax-get-acad-object))
          acspc (vlax-get-property acdoc (if (= 1 (getvar 'CVPORT)) 'paperspace 'modelspace))
    )
    (setq _ObjectID
        (eval
            (list 'lambda '( obj )
                (if
                    (and
                        (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
                        (vlax-method-applicable-p (vla-get-utility acdoc) 'getobjectidstring)
                    )
                    (list 'vla-getobjectidstring (vla-get-utility acdoc) 'obj ':vlax-false)
                   '(itoa (vla-get-objectid obj))
                )
            )
        )
    )
			
    (setq predicate
        (function
            (lambda ( x )
                (and
                    (eq (cdr (assoc 0 (setq x (entget x)))) "DIMENSION")
                    (member (boole 4 (+ 128 64 32) (cdr (assoc 70 x))) '(0 1))
                )
            )
        )
    )                     
	
	
    (while
        (and
            (setq d1 (_SelectIf "\nSelect 1st Dimension <Exit>: " predicate))
            (setq d2 (_SelectIf "\nSelect 2nd Dimension <Exit>: " predicate))
            (setq pt (getpoint "\nPoint for Result <Exit>: "))
        )
        (vla-addmtext acspc (vlax-3D-point (trans pt 1 0))) 0.0
            (strcat
                "%<\\AcExpr "
                "%<\\AcObjProp Object(%<\\_ObjId "
                (_ObjectID (vlax-ename->vla-object d1)) ">%).Measurement>% * "
                "%<\\AcObjProp Object(%<\\_ObjId "
                (_ObjectID (vlax-ename->vla-object d2)) ">%).Measurement>% "
                "\\f \"" fieldformatting "\">%"
            )
        )
    )

    (princ)
)
(vl-load-com) (princ)

 

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