Jump to content

Help Converting Excel Formula to AutoLISP for Object Length Calculation


Recommended Posts

Posted

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

Posted

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.

Posted

i think "ceiling" not working at autolisp96FAD45D-DE17-456B-9C57-541ABB7D5A20.png.5a0d756fe5b07946352ae2299f35d926.png

Posted

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

Posted

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.

image.png.ade6dad921b5fb2cc2caaef8bba06978.png

Test2.dwg

Posted

@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)
)

 

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

image.png.ade6dad921b5fb2cc2caaef8bba06978.png

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.dwg53B4E3A5-3B4B-4428-8BA9-ADFFB13FF9AC.thumb.png.b63ced30c836b72fd0e0bbc7d86929d9.png

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

Posted

Test this idea for rounding see if it is what you want.

 

(setq pre (+ (fix (/ lengthc 0.3)) 1))
(setq roundedlength (* pre 0.3))

 

Posted

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

 

Posted

@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?

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