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

need Lisp - Surface Angle with 4 points

24 REPLIES 24
Reply
Message 1 of 25
gringooo
1541 Views, 24 Replies

need Lisp - Surface Angle with 4 points

Hello all,

 

i am new here and hope for some help...

 

I need very very often to know the angle between two surfaces to write cnc programms...

At the moment i draw allways two lines normal to the line between the two surfaces an use then the angle measuring tool.

Works good, faster then bks and dimensioning, but need also time and allways need to delete the lines after...

 

Is it possible to get a lisp which ask für 1 point on the first surface, 1 point on the other surface and two points on the line between the 2 surfaces? So the lisp know 3 points of each surface and can give out the angle between... Should be allways between 0-180°

So it needs only 4 klicks to get a angle! That would be so great! Smiley Surprised

 

Is there a lisp programmer who can make this possible?

 

Many Thanks!

 

Ingo

 

Tags (1)
24 REPLIES 24
Message 2 of 25
phanaem
in reply to: gringooo

Try this

(defun C:TEST (/ a1 a2 p1 p2)
  (if
    (and
      (setq a1 (getpoint    "\nSelect intersection line.\nSelect first point:"))
      (setq a2 (getpoint a1 "\nSelect second point:"))
      (not (grdraw a1 a2 7 1))
      (setq p1 (getpoint a1 "\nSelect point on first plan:"))
      (setq p2 (getpoint a1 "\nSelect point on second plan:"))
      (not (redraw))
      (princ "\n")
    )
    (* (/ 180.0 pi) (dihedral_angle a1 a2 p1 p2))
  )
)

(defun dihedral_angle (a1 a2 p1 p2)
  (abs
    (apply '-
           (mapcar
             '(lambda (p)
                (angle '(0 0 0) (trans (mapcar '- p a1) 0 (mapcar '- a2 a1)))
              )
             (list p2 p1)
           )
    )
  )
)

 

Message 3 of 25
hmsilva
in reply to: phanaem

Nicely done, phanaem!

 

From the OP, the output angle "Should be allways between 0-180°"  Smiley Wink

 

Henrique

EESignature

Message 4 of 25
Lee_Mac
in reply to: gringooo

Great choice of coordinate system phanaem Smiley Happy

 

Here is another version, without using trans:

 

(defun c:pang ( / p1 p2 p3 p4 )
    (if
        (and
            (setq p1 (getpoint "\nPick 1st point of intersecting line: "))
            (setq p2 (getpoint "\nPick 2nd point of intersecting line: " p1))
            (setq p3 (getpoint "\nPick point on 1st plane: " p1))
            (setq p4 (getpoint "\nPick point on 2nd plane: " p1))
        )
        (princ
            (*  (/ 180.0 pi)
                (rem
                    (+ pi pi
                        (dihedral
                            (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))
                            (v^v (mapcar '- p2 p1) (mapcar '- p4 p1))
                        )
                    )
                    pi
                )
            )
        )
    )
    (princ)
)

(defun dihedral ( n1 n2 )
    (acos
        (/  (apply '+ (mapcar '* n1 n2))
            (* (distance '(0.0 0.0 0.0) n1) (distance '(0.0 0.0 0.0) n2))
        )
    )
)
 
;; ArcCosine  -  Lee Mac
;; Args: -1 <= x <= 1
 
(defun acos ( x )
    (if (<= -1.0 x 1.0)
        (atan (sqrt (- 1.0 (* x x))) x)
    )
)

;; Vector Cross Product  -  Lee Mac
;; Args: u,v - vectors in R^3

(defun v^v ( u v )
    (list
        (- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
        (- (* (car  v) (caddr u)) (* (car  u) (caddr v)))
        (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))
    )
)

(princ)
Message 5 of 25
hmsilva
in reply to: Lee_Mac

Nicely coded, Lee! Smiley Happy

 

Henrique

EESignature

Message 6 of 25
Lee_Mac
in reply to: hmsilva

Thank you Henrique! - though, it's not quite as elegant as phanaem's solution Smiley Happy

Message 7 of 25
hmsilva
in reply to: Lee_Mac


@Lee_Mac wrote:

...

 not quite as elegant as phanaem's solution..


Agree, great solution phanaem's! Smiley Happy

 

Cheers

Henrique

EESignature

Message 8 of 25
gringooo
in reply to: Lee_Mac

Wow! Impresive!

 

@lee after pick the four points i don´t get an angle in the command line?

 

@phanaem

works great!

is it possible to get allways the angle between 0-180° ?

is it possible to get only two decimal place?

 

many thanks!

Message 9 of 25
phanaem
in reply to: hmsilva


@hmsilva wrote:

Nicely done, phanaem!


Thank you Henrique


From the OP, the output angle "Should be allways between 0-180°"  Smiley Wink

 

Henrique




I saw that a second later. This is the correct transformation

(defun dihedral_angle (a1 a2 p1 p2)
  (rem
    (+ pi
       (abs
         (apply '-
                (mapcar
                  '(lambda (p)
                     (angle '(0 0 0) (trans (mapcar '- p a1) 0 (mapcar '- a2 a1)))
                   )
                  (list p1 p2)
                )
         )
       )
    )
    pi
  )
)

 

Message 10 of 25
Lee_Mac
in reply to: gringooo


@gringooo wrote:

@lee after pick the four points i don´t get an angle in the command line?


Are you sure? (as the program performs fine for me) - if you don't get an angle, what do you get?

Message 11 of 25
hmsilva
in reply to: phanaem


@phanaem wrote:

Thank you Henrique

...


 

You're welcome, phanaem!

A very clean code, great thinking!

 

Henrique

EESignature

Message 12 of 25
gringooo
in reply to: Lee_Mac

after pick all four points i get nothing back...

command abort

 

i try it with all variation of picking the 4 points

 

Message 13 of 25
Lee_Mac
in reply to: gringooo

It looks as though only three points have been specified, as the fourth prompt is not shown - here are my results, as an example:

 

Pick 1st point of intersecting line:
Pick 2nd point of intersecting line:
Pick point on 1st plane:
Pick point on 2nd plane: 30.0 <-- angle

 

Lee

Message 14 of 25
hmsilva
in reply to: Lee_Mac

Perhaps changing the System variable CLIPROMPTUPDATE to 1

Henrique

EESignature

Message 15 of 25
gringooo
in reply to: Lee_Mac

im sorry Lee!

works great after i reload my drawing! Smiley LOL

 

seems i get allways the right angle!! Gents, your are great!

 

@phanaem

your solution give me not allways the wished angle back. have a look at the dwg example...

i need the 128,78°

but with your fist solution i get the 231,22°

with your second solution i get the 51,22°

 

Allways the angle between the two surfaces and between 0-180°

So i don´t have to start a calculator after picking the four points...

 

thanks!

 

 

Message 16 of 25
Lee_Mac
in reply to: gringooo


@gringooo wrote:

im sorry Lee!

works great after i reload my drawing! Smiley LOL


 

No problem!

 


@gringooo wrote:

@phanaem

your solution give me not allways the wished angle back. have a look at the dwg example...

i need the 128,78°

but with your fist solution i get the 231,22°

with your second solution i get the 51,22°


 

Try this modification of phanaem's code (I hope you don't mind phanaem):

 

(defun c:test ( / a1 a2 nv p1 p2 x1 x2 x3 )
    (if
        (and
            (setq a1 (getpoint "Select 1st point of intersection line:"))
            (setq a2 (getpoint "Select 2nd point of intersection line:" a1))
            (setq p1 (getpoint "\nSelect point on 1st plane: " a1))
            (setq p2 (getpoint "\nSelect point on 2nd plane: " a1))
        )
        (progn
            (setq nv (mapcar '- a2 a1)
                  x1 (angle '(0.0 0.0) (trans (mapcar '- p1 a1) 0 nv))
                  x2 (angle '(0.0 0.0) (trans (mapcar '- p2 a1) 0 nv))
                  x3 (rem (+ pi pi (- x2 x1)) (+ pi pi))
            )
            (print (* (/ 180.0 pi) (min (- (+ pi pi) x3) x3)))
        )
    )
    (princ)
)

 

Message 17 of 25
gringooo
in reply to: Lee_Mac

wow! works also perfect now!

which of the two cool code should i use now? Smiley Happy

 

i know, not much important, but allways two decimals would be nice?

Message 18 of 25
Lee_Mac
in reply to: gringooo


@gringooo wrote:

wow! works also perfect now!

which of the two cool code should i use now? Smiley Happy

 

i know, not much important, but allways two decimals would be nice?


Excellent - all credit to phanaem of course Smiley Happy

 

For 2 d.p.:

 

(defun c:test ( / a1 a2 nv p1 p2 x1 x2 x3 )
    (if
        (and
            (setq a1 (getpoint "Select 1st point of intersection line:"))
            (setq a2 (getpoint "Select 2nd point of intersection line:" a1))
            (setq p1 (getpoint "\nSelect point on 1st plane: " a1))
            (setq p2 (getpoint "\nSelect point on 2nd plane: " a1))
        )
        (progn
            (setq nv (mapcar '- a2 a1)
                  x1 (angle '(0.0 0.0) (trans (mapcar '- p1 a1) 0 nv))
                  x2 (angle '(0.0 0.0) (trans (mapcar '- p2 a1) 0 nv))
                  x3 (rem (+ pi pi (- x2 x1)) (+ pi pi))
            )
            (princ (strcat "\nAngle: " (angtos (min (- (+ pi pi) x3) x3) 0 2)))
        )
    )
    (princ)
)
Message 19 of 25
phanaem
in reply to: Lee_Mac

Of course I do not mind Lee.
On the contrary, thanks for fixing my mistake.

 

Message 20 of 25
gringooo
in reply to: gringooo

is it possible to get the answer automaticly into the clipboard?

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

Post to forums  

Autodesk Design & Make Report

”Boost