Jump to content

Lisp to calculate versine and insert the text into a drawing


ASH71

Recommended Posts

Hi newbie here, I'm looking for help to create a lisp file that will calculate the versine of an arc then insert the answer as text into a drawing. (I'm useless at explaining).

 

for example if I have an arc 9500mm long with a radius of 300M the versine caculates at 37.5mm (to one decimal point).

 

I want the text to show the following:-

Length 9500

Radius - 300000

Versine = 37.5

Direction of Curvature = UP/DOWN

 

Sorry if I haven't explained it very well - at the moment I use a seperate calculator spreadsheet then edit text in my drawings.

 

Thanks for any advice.:)

Link to comment
Share on other sites

This will get you most of the way there -

(defun c:arcdata ( / ang arc enx ins rad )
   (while
       (progn (setvar 'errno 0) (setq arc (car (entsel "\nSelect arc: ")))
           (cond
               (   (= 7 (getvar 'errno))
                   (princ "\nMissed, try again.")
               )
               (   (null arc) nil)
               (   (/= "ARC" (cdr (assoc 0 (setq enx (entget arc)))))
                   (princ "\nSelected object is not an arc.")
               )
           )
       )
   )
   (if (and (= 'ename (type arc)) (setq ins (getpoint "\nSpecify point for text: ")))
       (progn
           (setq rad (cdr (assoc 40 enx))
                 ang (rem (+ pi pi (- (cdr (assoc 51 enx)) (cdr (assoc 50 enx)))) (+ pi pi))
           )
           (entmake
               (list
                  '(000 . "MTEXT")
                  '(100 . "AcDbEntity")
                  '(100 . "AcDbMText")
                   (cons 010 (trans ins 1 0))
                   (cons 001
                       (strcat
                           "Length: "  (rtos (* ang rad)) "\\P"
                           "Radius: "  (rtos rad)         "\\P"
                           "Versine: " (rtos (* rad (- 1 (cos (/ ang 2.0)))))
                       )
                   )
               )
           )
       )
   )
   (princ)
)

Not sure what you mean by a curvature being 'up' or 'down'.

Link to comment
Share on other sites

Re: Direction of Curvature = UP/DOWN

 

Could we be talking about vertical curves in which case the curve is UP (smile) or DOWN (frown)?

Link to comment
Share on other sites

Cheers for that. I'll try it In the morning

 

As remark says the up down is to indicate the direction of curvature

 

Thanks again!

Link to comment
Share on other sites

Hi Lee Mac, I've ran the routine and although it's not exactly what I was after it is still very useful for me.:)

 

What I was after is for a routine that asks me for the length - which for example I would type 9500. Then ask for the radius - say 300000. then direction of curvature - UP or Down. Then it would calculate the versine and allow me to insert the information into the drawing as follows -:

Length - 9500

Radius - 300000

Versine = 37.5

Direction of Curvature = UP

 

Thanks again for your help.

Link to comment
Share on other sites

(defun c:arcdata ( / crv ins len rad )
   (if
       (and
           (progn
               (initget 6)
               (setq len (getdist "\nSpecify arc length: "))
           )
           (progn
               (initget 6)
               (setq rad (getdist "\nSpecify arc radius: "))
           )
           (progn
               (initget "Up Down")
               (setq crv (cond ((getkword "\nSpecify curvature direction [up/Down] <Up>: ")) ("Up"))
                     ins (getpoint "\nSpecify point for text: ")
               )
           )
       )
       (entmake
           (list
              '(000 . "MTEXT")
              '(100 . "AcDbEntity")
              '(100 . "AcDbMText")
               (cons 010 (trans ins 1 0))
               (cons 001
                   (strcat
                       "Length: "  (rtos len) "\\P"
                       "Radius: "  (rtos rad) "\\P"
                       "Versine: " (rtos (* rad (- 1 (cos (/ len rad 2.0))))) "\\P"
                       "Direction of Curvature: " (strcase crv)
                   )
               )
           )
       )
   )
   (princ)
)

Link to comment
Share on other sites

  • 7 years later...

I know this is from a while ago, Lee Mac made a lisp file for me which I have been able to modify slightly to give an additional line (versine over 7m). Can anyone help / show me how to remove the bottom line if the length of the arc selected is shorter than 7m please? I will insert the code below.

 

(defun c:versine ( / ang arc enx ins rad )
    (while
        (progn (setvar 'errno 0) (setq arc (car (entsel "\nSelect arc: ")))
            (cond
                (   (= 7 (getvar 'errno))
                    (princ "\nMissed, try again.")
                )
                (   (null arc) nil)
                (   (/= "ARC" (cdr (assoc 0 (setq enx (entget arc)))))
                    (princ "\nSelected object is not an arc.")
                )
            )
        )
    )
    (if (and (= 'ename (type arc)) (setq ins (getpoint "\nSpecify point for text: ")))
        (progn
            (setq rad (cdr (assoc 40 enx))
                  ang (rem (+ pi pi (- (cdr (assoc 51 enx)) (cdr (assoc 50 enx)))) (+ pi pi))
            )
            (entmake
                (list
                   '(000 . "MTEXT")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbMText")
                    (cons 010 (trans ins 1 0))
                    (cons 001
                        (strcat
                            "Length: "  (rtos (* ang rad)) "\\P"
                            "Radius: "  (rtos rad)         "\\P"
                            "Versine: " (rtos (* rad (- 1 (cos (/ ang 2.0))))) "\\P"
                            "Versine over 7M: " (rtos (* rad (- 1 (cos (/ 7000 rad 2.0)))))
                        )
                    )
                )
            )
        )
    )
    (princ)
)

Example.pdf

Link to comment
Share on other sites

Keeping with Lee Mac,,, I think you need a sub-function to calculate the arc length and I dug this out:

;;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-me-find-arc-length
(defun arclen ( ent / enx )
    (setq enx (entget ent))
    (*  (cdr (assoc 40 enx))
        (rem (+ pi pi (- (cdr (assoc 51 enx)) (cdr (assoc 50 enx))))
             (+ pi pi)
        )
    )
)

 

in your code to get arc length you'd use this, putting it in before the entmake portion:

(setq ArcLength (arclen arc))

 

After than in the entmake part of your code you can put in an 'if' statement in the strcat bit:

 

(entmake
  (list
    '(000 . "MTEXT")
    '(100 . "AcDbEntity")
    '(100 . "AcDbMText")
    (cons 010 (trans ins 1 0))
    (cons 001
      (strcat
        "Length: "  (rtos (* ang rad)) "\\P"
        "Radius: "  (rtos rad)         "\\P"
        "Versine: " (rtos (* rad (- 1 (cos (/ ang 2.0))))) "\\P"
(if (> ArcLength 7)
        "Versine over 7M: " (rtos (* rad (- 1 (cos (/ 7000 rad 2.0)))))
) ; end if
      ) ; end strcat
    ) ; end cons 001 (create text)
  ) ; end list
) ; end entmake

 

 

Edited by Steven P
Link to comment
Share on other sites

Thanks Steven, I was getting this 'malformed list on input' managed to sort that but now I'm getting 'error syntax error'

 

Any ideas?

 

Ash

Link to comment
Share on other sites

Malformed list on input usually mean a list isn't quite in the right format

error: syntax error is a function isn't quite right

 

Often for both it is a bracket in the wrong place for me

 

try this, a small change in the 'if' statement and added in the arclen function

 

(defun c:versine ( / ang arc enx ins rad )
  ;;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-me-find-arc-length
  (defun arclen ( ent / enx )
    (setq enx (entget ent))
    (*  (cdr (assoc 40 enx))
        (rem (+ pi pi (- (cdr (assoc 51 enx)) (cdr (assoc 50 enx))))
             (+ pi pi)
        ) ; end *
    ) ; end setq
  ) ; end defun


    (while
        (progn (setvar 'errno 0) (setq arc (car (entsel "\nSelect arc: ")))
            (cond
                (   (= 7 (getvar 'errno))
                    (princ "\nMissed, try again.")
                )
                (   (null arc) nil)
                (   (/= "ARC" (cdr (assoc 0 (setq enx (entget arc)))))
                    (princ "\nSelected object is not an arc.")
                )
            ) ; end conds
        ) ; end progn
    ) ; end while
    (if (and (= 'ename (type arc)) (setq ins (getpoint "\nSpecify point for text: ")))
        (progn
          (setq ArcLength (arclen arc))
          (setq rad (cdr (assoc 40 enx))
                ang (rem (+ pi pi (- (cdr (assoc 51 enx)) (cdr (assoc 50 enx)))) (+ pi pi))
          ) ; end setq
          (entmake
            (list
              '(000 . "MTEXT")
              '(100 . "AcDbEntity")
              '(100 . "AcDbMText")
              (cons 010 (trans ins 1 0))
              (cons 001
                (strcat
                  "Length: "  (rtos (* ang rad)) "\\P"
                  "Radius: "  (rtos rad)         "\\P"
                  "Versine: " (rtos (* rad (- 1 (cos (/ ang 2.0))))) "\\P"
                  (if (> ArcLength 7)
                    (strcat "Versine over 7M: " (rtos (* rad (- 1 (cos (/ 7000 rad 2.0)))))) ; versine over 7
                    (strcat "") ; versine under 7
                  ) ; end if
                ) ; end strcat
              ) ; end cons 001 (create text)
            ) ; end list
          ) ; end entmake
        ) ; end progn
    ) ; end if
    (princ)
)

 

  • Like 1
Link to comment
Share on other sites

  • 5 months later...

Hi lads, sorry to drag this one up again. This lisp works great and I don't know if this mod / addition is possible but I have attached a sample of what the lisp does and also a sample of an additional line if it is possible - no worries if it isn't. I've just been adding the additonal direction of curvature manually.

 

Hope this makes sense.

 

Below is the lisp

(defun c:versine ( / ang arc enx ins rad )
  ;;https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-me-find-arc-length
  (defun arclen ( ent / enx )
    (setq enx (entget ent))
    (*  (cdr (assoc 40 enx))
        (rem (+ pi pi (- (cdr (assoc 51 enx)) (cdr (assoc 50 enx))))
             (+ pi pi)
        ) ; end *
    ) ; end setq
  ) ; end defun


    (while
        (progn (setvar 'errno 0) (setq arc (car (entsel "\nSelect arc: ")))
            (cond
                (   (= 7 (getvar 'errno))
                    (princ "\nMissed, try again.")
                )
                (   (null arc) nil)
                (   (/= "ARC" (cdr (assoc 0 (setq enx (entget arc)))))
                    (princ "\nSelected object is not an arc.")
                )
            ) ; end conds
        ) ; end progn
    ) ; end while
    (if (and (= 'ename (type arc)) (setq ins (getpoint "\nSpecify point for text: ")))
        (progn
          (setq ArcLength (arclen arc))
          (setq rad (cdr (assoc 40 enx))
                ang (rem (+ pi pi (- (cdr (assoc 51 enx)) (cdr (assoc 50 enx)))) (+ pi pi))
          ) ; end setq
          (entmake
            (list
              '(000 . "MTEXT")
              '(100 . "AcDbEntity")
              '(100 . "AcDbMText")
              (cons 010 (trans ins 1 0))
              (cons 001
                (strcat
                  "Length: "  (rtos (* ang rad)) "\\P"
                  "Radius: "  (rtos rad)         "\\P"
                  "Versine: " (rtos (* rad (- 1 (cos (/ ang 2.0))))) "\\P"
                  (if (> ArcLength 7000)
                    (strcat "Versine over 7M: " (rtos (* rad (- 1 (cos (/ 7000 rad 2.0)))))) ; versine over 7
                    (strcat "") ; versine under 7
                  ) ; end if
                ) ; end strcat
              ) ; end cons 001 (create text)
            ) ; end list
          ) ; end entmake
        ) ; end progn
    ) ; end if
    (princ)
)
 

Current.jpg

Desired.jpg

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