Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

lisp available for angle between two lines?

25 REPLIES 25
SOLVED
Reply
Message 1 of 26
barry2104
10490 Views, 25 Replies

lisp available for angle between two lines?

 

i know DIMANGULAR command will let me draw a dimension to display the angle between two lines, but is there a way for my commandbar to show me what the angle is rather than draw a dimension?

I often want to simply know the angle but not display it in the drawing, and it'd be good if there was a command/lisp which showed me in the commandline area, or perhaps a pop up window, similar to what the TLEN lisp does here:

www.turvill.com/t2/free_stuff/tlen.lsp

 

any help appreciated

Running AutoCAD Architecture 2020, in German
Tags (3)
25 REPLIES 25
Message 2 of 26
pbejse
in reply to: barry2104


@barry2104 wrote:

 

i know DIMANGULAR command will let me draw a dimension to display the angle between two lines, but is there a way for my commandbar to show me what the angle is rather than draw a dimension?

I often want to simply know the angle but not display it in the drawing, and it'd be good if there was a command/lisp which showed me in the commandline area, or perhaps a pop up window, similar to what the TLEN lisp does here:

www.turvill.com/t2/free_stuff/tlen.lsp

 

any help appreciated


A quick one

 

(Defun c:test (/ l1 l2 p1 p2 p3)
  (vl-load-com)
  (if (not (member "geomcal.arx" (arx)))
    (arxload "geomcal")
    )
  (setq
    l1 (entsel "\nSelect Object: ")
    l2 (entsel "\nSelect Object: ")
    p1 (vlax-invoke
         (vlax-ename->vla-object (car l1))
         'IntersectWith
         (vlax-ename->vla-object (car l2))
         acExtendBoth
         )
    p2 (vlax-curve-getClosestPointTo
         (vlax-ename->vla-object (car l1))
         (cadr l1)
         )
    p3 (vlax-curve-getClosestPointTo
         (vlax-ename->vla-object (car l2))
         (cadr l2)
         )
    )
  (alert
    (strcat
      "Angle Between two objects is "
      (rtos (c:cal "ang(p1,p2,p3)") 2 2)
      )
    )
  )

 HTH

 

Message 3 of 26
barry2104
in reply to: pbejse

thanks for the response.

call me a n00b but how does one tell from the code what the command is? I copied the text to create a new .lsp file and dragged it into my CAD window, but don't know what to type to test it out!

Smiley Happy

 

Running AutoCAD Architecture 2020, in German
Message 4 of 26
pbejse
in reply to: barry2104


@barry2104 wrote:

thanks for the response.

call me a n00b but how does one tell from the code what the command is? I copied the text to create a new .lsp file and dragged it into my CAD window, but don't know what to type to test it out!

Smiley Happy

 



Save it as whatever name you want wtih a LSP extension. for example

Angd.lsp

Appload

Browse wher you saved the file

Load

 

command : test

 

Currently command name is test,

 

You can however change it to anything you want here:

(Defun c:test <------- c:Angd or whatever

 

 BTW: change the alert part of the routine to this

 

(alert
    (strcat
      "Angle Between two objects is \n "
      (rtos (c:cal "ang(p1,p2,p3)") 2 2)
      " and "
      (rtos (c:cal "ang(p1,p3,p2)") 2 2)
      )
   
    )

 

Message 5 of 26
barry2104
in reply to: pbejse

doesn't seem to be working properly...  I updated the alert part but after entering the command ("TEST"), an empty dialog box appears with an OK or CLOSE option. I click ok and then proceed to select the two lines and then press enter. The command bar then simply states:
Ne Zahl....:

 

note, I'm working in Germany/in German but CAD is installed in English, so I'm not sure what Ne Zahl is supposed to mean, but either way there is no numerical value in the command bar.

 more tweaking needed?

Running AutoCAD Architecture 2020, in German
Message 6 of 26
Lee_Mac
in reply to: barry2104

A little verbose, but try something like this:

 

(defun c:lang ( / _SelectIf e1 e2 f in l1 l2 s1 s2 )

    (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 f (lambda ( x ) (eq "LINE" (cdr (assoc 0 (entget x))))))
    (if
        (and
            (setq l1 (_SelectIf "\nSelect 1st Line: " 'f))
            (setq l2 (_SelectIf "\nSelect 2st Line: " 'f))
        )
        (progn
            (setq l1 (entget l1)
                  l2 (entget l2)
                  s1 (cdr (assoc 10 l1))
                  s2 (cdr (assoc 10 l2))
                  e1 (cdr (assoc 11 l1))
                  e2 (cdr (assoc 11 l2))
                  in (inters s1 e1 s2 e2 nil)
            )
            (if (null in)
                (princ "\nLines are Parallel.")
                (progn
                    (if (< (distance in e1) (distance in s1))
                        (setq e1 s1)
                    )
                    (if (< (distance in e2) (distance in s2))
                        (setq e2 s2)
                    )
                    (princ (strcat "\nAngle: " (angtos (LM:GetInsideAngle e1 in e2))))
                )
            )
        )
    )
    (princ)
)

;; Get Inside Angle  -  Lee Mac
;; Returns the smaller angle subtended by three points with vertex at p2

(defun LM:GetInsideAngle ( p1 p2 p3 )
    (   (lambda ( a ) (min a (- (+ pi pi) a)))
        (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi))
    )
)

 

Message 7 of 26
barry2104
in reply to: Lee_Mac

very nice LeeMac, thanks for your input

only suggestion to perfect it would be the potential to go to X (3?) decimal places and or show/convert it to you in radians in the one command bar output.

 

also this won't currently work if one or both of the lines are POLYLINES... is that difficult to integrate into the code?

Running AutoCAD Architecture 2020, in German
Message 8 of 26
pbejse
in reply to: barry2104


@barry2104 wrote:

doesn't seem to be working properly...  I updated the alert part but after entering the command ("TEST"), an empty dialog box appears with an OK or CLOSE option. I click ok and then proceed to select the two lines and then press enter. The command bar then simply states:
Ne Zahl....:

 



Im sorry but i have no idea at all Smiley Very Happy

 

Message 9 of 26
Lee_Mac
in reply to: barry2104


@barry2104 wrote:

very nice LeeMac, thanks for your input

only suggestion to perfect it would be the potential to go to X (3?) decimal places and or show/convert it to you in radians in the one command bar output.


 

You're welcome.

 

The precision of the Angular measurement is controlled by the setting of your AUPREC System Variable, however, you can override this precision in the code (and furthermore displaying a radian result alongside the decimal result) by changing:

 

(princ (strcat "\nAngle: " (angtos (LM:GetInsideAngle e1 in e2))))

 

To:

 

(setq ang (LM:GetInsideAngle e1 in e2))
(princ (strcat "\nAngle:-\nDegrees: " (angtos ang 0 3) "\tRadians: " (angtos ang 3 3)))

 

Study the VLIDE AutoLISP Documentation on the angtos function to learn more. This short tutorial will help you access this documentation.

 


@barry2104 wrote:

also this won't currently work if one or both of the lines are POLYLINES... is that difficult to integrate into the code?


The current code will indeed only work for Line entities. To include other linear entities, such as Polylines with straight segments / XLines etc. a different approach must be used.

Message 10 of 26
stevesfr
in reply to: Lee_Mac

Lee, I am confused.  When the angle is obviously between zero and 180 why does program report the answer as 90 degrees less than correct?  i.e.  11 degrees instead of 101 degrees ?  What am I missing ?

Steve

Message 11 of 26
Lee_Mac
in reply to: stevesfr


@stevesfr wrote:

Lee, I am confused.  When the angle is obviously between zero and 180 why does program report the answer as 90 degrees less than correct?  i.e.  11 degrees instead of 101 degrees ?  What am I missing ?

Steve


In a few quick tests I can't seem to replicate this Steve - are you using a UCS other than World? (I haven't tested the routine for UCS compatibility).

 

 

Message 12 of 26
stevesfr
in reply to: Lee_Mac

Lee; my dumb, program always reports smallest of the two angles which equal 180. all is OK, thx.

Steve

Message 13 of 26
pbejse
in reply to: barry2104


@barry2104 wrote:

doesn't seem to be working properly...  I updated the alert part but after entering the command ("TEST"), an empty dialog box appears with an OK or CLOSE option. I click ok and then proceed to select the two lines and then press enter. The command bar then simply states:
Ne Zahl....:

 

note, I'm working in Germany/in German but CAD is installed in English, so I'm not sure what Ne Zahl is supposed to mean, but either way there is no numerical value in the command bar.

 more tweaking needed?


Rw-write:

 

(Defun  c:AngD (/ l1 l2 p1 p2 p3 verts Anglist x y npt)
  (vl-load-com)
  (setq
    l1    (entsel "\nSelect Object: ")
    l2    (entsel "\nSelect Object: ")
    verts (list
            (vlax-curve-getClosestPointTo
              (vlax-ename->vla-object (car l1))
              (cadr l1)
              )
            (vlax-invoke
              (vlax-ename->vla-object (car l1))
              'IntersectWith
              (vlax-ename->vla-object (car l2))
              acExtendNone
              )
            (vlax-curve-getClosestPointTo
              (vlax-ename->vla-object (car l2))
              (cadr l2)
              )
            )
    )
  (repeat (1- (length verts))
    (setq
      x   (car verts)
      y   (cadr verts)
      npt (list (car x) (cadr y))
      )
    (setq
      AngList
       (append
         Anglist
         (list (atan (/ (distance npt x) (distance npt y))))
         )
      verts
       (cdr verts)
      )
    )
  (setq sss AngList)
  (mapcar
    '(lambda (n m) (setq ang (/ (* (+ n m) 180.0) pi)))
    Anglist
    (cdr Anglist)
    )
  (princ (strcat  "\nAngles:\n" (rtos ang 2 3) " and " (rtos  (- 180 ang) 2 3 )))
  (princ)
  )

 

(Defun c:test (/ l1 l2 p1 p2 p3)
  (vl-load-com)
  (if (not (member "geomcal.arx" (arx)))
    (arxload "geomcal")
    )
  (setq
    l1 (entsel "\nSelect Object: ")
    l2 (entsel "\nSelect Object: ")
    p1 (vlax-invoke
         (vlax-ename->vla-object (car l1))
         'IntersectWith
         (vlax-ename->vla-object (car l2))
         acExtendNone
         )
    p2 (vlax-curve-getClosestPointTo
         (vlax-ename->vla-object (car l1))
         (cadr l1)
         )
    p3 (vlax-curve-getClosestPointTo
         (vlax-ename->vla-object (car l2))
         (cadr l2)
         )
    )
  (alert
    (strcat
      "Angle Between two objects is  "
  
      (rtos
        (min (setq tl (c:cal "ang(p1,p2,p3)")) (abs (- 360 tl)))  2 2)
     
      )
    
    )
  )

 

 

Message 14 of 26
barry2104
in reply to: pbejse

the second batch of code you wrote above seems to work perfectly - for both lines AND polylines, and for any angle, to two decimal places.

 

thanks a lot guys

Running AutoCAD Architecture 2020, in German
Message 15 of 26
pbejse
in reply to: barry2104


@barry2104 wrote:

the second batch of code you wrote above seems to work perfectly - for both lines AND polylines, and for any angle, to two decimal places.

 

thanks a lot guys


glad it worked for you..

 

Cheers barry

 


 

Message 16 of 26
stevesfr
in reply to: pbejse

What to revise to obtain degrees, minutes, seconds instead of decimal degrees ?

TIA

Message 17 of 26
Lee_Mac
in reply to: stevesfr


@stevesfr wrote:

What to revise to obtain degrees, minutes, seconds instead of decimal degrees ?

TIA


On mine, change:

 

(princ (strcat "\nAngle: " (angtos (LM:GetInsideAngle e1 in e2))))

 

to:

 

(princ (strcat "\nAngle: " (angtos (LM:GetInsideAngle e1 in e2) 1)))

 

The precision will be reflected by your AUPREC System Variable; to force a precision, (say 3.d.p.), use:

 

(princ (strcat "\nAngle: " (angtos (LM:GetInsideAngle e1 in e2) 1 3)))

 

Here is the reference for angtos:

 

(angtos  [units]  [precision])

Units:
0 -- Degrees
1 -- Degrees/minutes/seconds
2 -- Grads
3 -- Radians
4 -- Surveyor's units

Precision:
Integer specifying number of d.p. to display.

 

Message 18 of 26
stevesfr
in reply to: Lee_Mac

Thanks Lee, perfect.

Message 19 of 26
Kent1Cooper
in reply to: barry2104


@barry2104 wrote:

 

i know DIMANGULAR command will let me draw a dimension to display the angle between two lines, but is there a way for my commandbar to show me what the angle is rather than draw a dimension?

I often want to simply know the angle but not display it in the drawing, and it'd be good if there was a command/lisp which showed me in the commandline area. ....


Seemed like a good thing to have, so I made the attached based on my Bisector routine here:

http://cadtips.cadalyst.com/linear-objects/linear-entity-bisector

[The "title" there is a little misleading -- I would have titled it something like "Bisect Angle/Spacing between straight things."]

 

ReportAngle.lsp reports the included angle in the direction between the picked locations on two straight entities of virtually any type -- Line, line segment of any variety of Polyline, extension line of a Dimension, dimension line of a linear Dimension, part of a Hatch pattern, straight-format Leader, Ray, Xline, Mline, Wipeout, Trace, Viewport, Image, Tolerance box, 2D Solid, 3DFace, straight edge of 3D Solid or Region, AND any of those things nested within an Xref or Block.

 

Note that it reports under the current settings for angle mode and precision, but you can add arguments in two (angtos) functions to force a particular format, if the current settings might not always be what you want.

 

You could probably add some other entity types I don't have a new-enough version to have [e.g. Tables?], if you'd ever want to know angles related to them.

 

[One category of thing I haven't figured out how to exclude from valid selections, if anyone has any ideas how to identify one -- a portion of a 3D Solid or Region edge/boundary that comes from a Spline or splined Polyline.  Such a thing that comes from a Circle, Arc, Polyline arc segment or Ellipse can be excluded by the check on whether Osnap Center returns anything, but Splines are another matter.]

Kent Cooper, AIA
Message 20 of 26
stevesfr
in reply to: Kent1Cooper

Kent, thank you so much ! ! !

Steve

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report