HungTran Posted October 1 Share Posted October 1 I'm new to AutoLISP and trying to improve my workflow. I'm stuck on this particular problem, and I need help converting an Excel formula into AutoLISP. Here's what I'm trying to do: Get the length A of an object (in mm). Divide A by the Cos of B (B is a decimal number extracted from a text object). Let's call the result C. Round C up to the nearest 0.3, then divide it by 0.3. The final result should be in meters with one decimal place. Also, in dwg file i tried field but can't write rounding function and also can't select object flexibly. LFF.lsp Test.dwg Quote Link to comment Share on other sites More sharing options...
BIGAL Posted October 2 Share Posted October 2 Try this (defun c:LFF ( / obj lengtha txtobj lengthm roundedlength) (defun dtr (a) (* pi (/ a 180.0)) ) (setq obj (car (entsel "\nlengthA: "))) (if obj (progn (setq lengthA (vlax-curve-getDistAtParam obj (vlax-curve-getEndParam obj))) (setq lengtha (* (/ lengtha 12.0) 0.3048)) ;converted to metres (setq txtObj (car (entsel "\nAngleB: "))) (if txtObj (progn (setq angleText (cdr (assoc 1 (entget txtObj)))) (setq angle (dtr (atof angleText))) ;; Text to Number and convert degrees to radians (setq lengthM (/ lengthA (cos angle))) (setq roundedLength (* 0.3 (ceiling (/ lengthM 0.3)))) (princ (strcat "\nResult: " (rtos roundedLength 2 1) " m")) ) ) ) ) (princ) ) Found a new lisp function today "(ceiling" did not know about it. Always learning. Quote Link to comment Share on other sites More sharing options...
HungTran Posted October 2 Author Share Posted October 2 i think "ceiling" not working at autolisp Quote Link to comment Share on other sites More sharing options...
HungTran Posted October 2 Author Share Posted October 2 i tried using leemac roundup code and chat gpt. but the result is not showing while the process is still being executed. can you tell me where is the error and how to fix it i attached CFF.lsp and Test.dwg files to describe the process and desired formula Test.zip Quote Link to comment Share on other sites More sharing options...
BIGAL Posted October 2 Share Posted October 2 I found that using 0.3 the roundup by Lee does not seem to give correct answer, if .3 .6 .9 is desired result. Maybe Lee will comment. Can you confirm that is the values you want, I did a cond at one stage that would return those fractions for any number. I am confused by what your trying to do. Can you fill in the last 2 values please for the attached dwg. Test2.dwg Quote Link to comment Share on other sites More sharing options...
lrm Posted October 3 Share Posted October 3 @HungTran It is not clear what you mean by "...Round C up to the nearest 0.3". If that means, for example, that 5.2 --> 5.3 and 4.45 --> 5.3 then the following will work for you. (defun c:r3 (/ a b c) (setq a (getreal "\nEnter number: ")) (setq b (rem a 1)) (if (> b 0.3) (setq c (+ 1.3 (- a b))) (setq c (+ 0.3 (- a b))) ) (princ c) (princ) ) Quote Link to comment Share on other sites More sharing options...
HungTran Posted October 3 Author Share Posted October 3 2 hours ago, BIGAL said: I found that using 0.3 the roundup by Lee does not seem to give correct answer, if .3 .6 .9 is desired result. Maybe Lee will comment. Can you confirm that is the values you want, I did a cond at one stage that would return those fractions for any number. I am confused by what your trying to do. Can you fill in the last 2 values please for the attached dwg. Test2.dwg 42.27 kB · 0 downloads @BIGAL thank you. i realized the error in the excel structure. not suitable to describe for autocad. i rewrote the order of steps for autocad. hope you understand. i also adjusted the lisp it works but can not print the result. Let see picture and attached NewCode.dwg 1 hour ago, lrm said: @HungTran It is not clear what you mean by "...Round C up to the nearest 0.3". If that means, for example, that 5.2 --> 5.3 and 4.45 --> 5.3 then the following will work for you. (defun c:r3 (/ a b c) (setq a (getreal "\nEnter number: ")) (setq b (rem a 1)) (if (> b 0.3) (setq c (+ 1.3 (- a b))) (setq c (+ 0.3 (- a b))) ) (princ c) (princ) ) @Irm I mean the nearest multiple of 0.3 e.g. 1.325 to 1.5. That number divisible by 0.3 results in an integer NewCode.dwg Quote Link to comment Share on other sites More sharing options...
BIGAL Posted October 3 Share Posted October 3 Test this idea for rounding see if it is what you want. (setq pre (+ (fix (/ lengthc 0.3)) 1)) (setq roundedlength (* pre 0.3)) Quote Link to comment Share on other sites More sharing options...
Tsuky Posted October 3 Share Posted October 3 This? (defun c:FOO ( / A B C) (defun round_sup (x w / n) (setq n (+ (fix x) (/ (fix (* (- x (fix x)) (/ 1.0 w))) (/ 1.0 w)))) (cond ((zerop (- x n)) (float n) ) (T (+ n (* (/ x (abs x)) w)) ) ) ) (princ "\nSelect LINE or POLYLINE") (while (not (setq ss (ssget "_+.:E:S" '((0 . "LWPOLYLINE,LINE")))))) (setq A (vlax-curve-getDistAtParam (ssname ss 0) (vlax-curve-getEndParam (ssname ss 0)))) (princ "\nSelect TEXT or MTEXT") (while (not (setq ss (ssget "_+.:E:S" '((0 . "*TEXT")))))) (setq B (atof (cdr (assoc 1 (entget (ssname ss 0)))))) (setq C (/ A (cos (* pi (/ B 180.0))))) (round_sup (* C 0.001) 0.5) ) Quote Link to comment Share on other sites More sharing options...
lrm Posted October 3 Share Posted October 3 @HungTran What should happen if the lenght divided by 0.3 is an interger? E.g., should 1.5 stay at 1.5 or round up to 1.8? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.