;(defun intrsctn()
(defun o ()(exit))
(setq l "NICK K. VAN VLIET, TO M5A 3G5 (416) 921-4653 VERS2.5 July/99
'Used to create ducting intersecting surfaces'  updated 17apr2003")
(defun pr(x)(terpri)(princ x))
(defun prt(x y)(terpri)(princ x)(princ y)(princ " "))

(defun pnts (x y)
 (setq p1(cdr(assoc 10 x)))
 (setq p2(cdr(assoc 11 x)))
 (setq p3(cdr(assoc 10 y)))
 (setq p4(cdr(assoc 11 y)))
 (if (or(eq p1 p3)(eq p2 p3)) (setq p3 p4))
 ;;;ENSURE P3 IS THE INTERSECTION OF OTHER 2 POINTS 'V'
)

(setq a(entget(car(entsel "Pick line 1 of plane 1"))));nea;\
(setq b(entget(car(entsel "Pick line 2 of plane 1"))));nea;\
(pnts a b)(setq p5 p1 p6 p2 p7 p3)            ;;these are p5/p6/p7

(setq a(entget(car(entsel "Pick line 1 of plane 2"))));nea;\
(setq b(entget(car(entsel "Pick line 2 of plane 2"))));nea;\
(pnts a b)                                    ;;these are p1/p2/p3


(setq x1 (car p1) y1 (cadr p1) z1 (caddr p1))  ; -5   5   0   =p1
(setq x2 (car p2) y2 (cadr p2) z2 (caddr p2))  ; -5   5  20   =p2
(setq x3 (car p3) y3 (cadr p3) z3 (caddr p3))  ; 15 -15   0   =p4

(setq x5 (car p5) y5 (cadr p5) z5 (caddr p5))  ;  5   5   0   =p5
(setq x6 (car p6) y6 (cadr p6) z6 (caddr p6))  ;  5   5  20   =p6
(setq x7 (car p7) y7 (cadr p7) z7 (caddr p7))  ;-15 -15   0   =p8

(setq v12 (list (- x2 x1) (- y2 y1) (- z2 z1)))  ;  0   0 20  VECTORS
(setq v13 (list (- x3 x1) (- y3 y1) (- z3 z1)))  ; 20 -20  0
(setq v56 (list (- x6 x5) (- y6 y5) (- z6 z5)))  ;  0   0 20
(setq v57 (list (- x7 x5) (- y7 y5) (- z7 z5)))  ;-20 -20  0

(prt "v12= " v12) ;  0   0  20 unit vectors
(prt "v13= " v13) ; 20 -20   0
(prt "v56= " v56) ;  0   0  20
(prt "v57= " v57) ;-20 -20   0

;;;PG 104/116 UNIT VECTORS OF A PLANE
;;;P8/P9=V12xV13 ->NORMAL TO THE PLANE - CROSSPRODUCT

;;;UxV = U2V3-U3V2 , U3V1-U1V3 , U1V2-U2V1   PG104
;;;V12xV13 = V12.2*V13.3 -V12.3*V13.2 ETC     =>P7

;;;P8 NORMAL VECTOR - CROSSPRODUCT TO PLANE VECTORS V12 V13
(setq x8 (- (* (cadr  v12)(caddr v13)) (* (caddr v12)(cadr v13)))) ;0*0-20*-20 =  400 p8
(setq y8 (- (* (caddr v12)(car v13))   (* (car v12)  (caddr v13))));20*20-0*0  =  400
(setq z8 (- (* (car   v12)(cadr v13))  (* (cadr v12) (car v13))))  ;0*-20-0*20 =    0
(setq p8 (list x8 y8 z8))  (pr "p8 ")(princ p8)    ;0 -400 -400

;;;P9 NORMAL VECTOR - CROSSPRODUCT TO PLANE VECTORS V12 V13
(setq x9 (- (* (cadr  v56)(caddr v57)) (* (caddr v56)(cadr v57)))) ;0*0-20*-20 =  400 p9
(setq y9 (- (* (caddr v56)(car v57))   (* (car v56)  (caddr v57))));20*-20-0*0 = -400
(setq z9 (- (* (car   v56)(cadr v57)) (* (cadr v56) (car v57))))   ;0*-20-0*20 =    0
(setq p9 (list x9 y9 z9))  (pr "p9 ")(princ p9)    ;0 400 -400

;;;PN NORMAL VECTOR - CROSSPRODUCT TO PLANE VECTORS P8 P9 PARALLEL TO INTSCTN LINE
(setq xn (- (* (cadr  p8)(caddr p9)) (* (caddr p8)(cadr p9))))  ;-400*-400-400*-400 =-3200 pn
(setq yn (- (* (caddr p8)(car p9))   (* (car p8)  (caddr p9)))) ;-400*0-0*-400      =    0
(setq zn (- (* (car   p8)(cadr p9))  (* (cadr p8) (car p9))))   ;0*400--400*0       =    0
(setq pn (list xn yn zn))

(pr "pn ")(princ pn)    ;-320000 0 0 parallel to Xaxis & desired intersection line

(setq flagx nil flagy nil flagz nil)

(cond   ;;;is it parallel to the axis?
 ((and (= yn zn)(= yn 0)) (setq flagx 't))   ;xaxis
 ((and (= xn zn)(= xn 0)) (setq flagy 't))   ;yaxis
 ((and (= xn yn)(= xn 0)) (setq flagz 't))   ;zaxis
 (t nil)
)

(setq cos1(/ (+ (* x8 x9)(* y8 y9)(* z8 z9))               ;400*400 + 400*-400 + 0 =0
             (* (expt(+(* x8 x8)(* y8 y8)(* z8 z8)) 0.5)   ;160000 + 160000 + 0
                (expt(+(* x9 x9)(* y9 y9)(* z9 z9)) 0.5))));160000 + 160000 + 0
                                                           ;=0/SQR(320000)^2 =0 ==>> COS 90 = 0
(pr "cos1= ")(princ cos1)

;;;ANGLE=DOT PRODUCT/LENGTHS=U.V/|U||V| PG99/100
;;;U.V = U1V1 + U2V2 + U3V3
;;;|U||V| = LEGTHU*LENGTHV

(setq sin1 (expt (- 1 (expt cos1 2)) 0.5))

(setq ang (* (/ (atan (if (= cos1 0) 100000000  (/ sin1 cos1))) pi) 180))
;;devision by zero

(defun crrctn(x)
 (if (< x 0)(setq x(+ x 360)))
 (if (>= x 180)(setq x(- x 180)))
 (if (or(< x 0)(>= x 180))(crrctn x))
 (setq ang x)
)(crrctn ang)

(pr "ang between the 2 planes= ")(princ ang)

;;;EQUATION OF PLANE 1 => x8(x - x1) + y8(y - y1)+ z8(z - z1) = 0 PG116
;;;EQUATION OF PLANE 2 => x9(x - x5) + y9(y - y5)+ z9(z - z5) = 0 PG116
;;;ELIMINATE X GET Y=>Z & LET Z=-50 ETC.

;;;x8*x + y8*y  z8*z  =  x8*x1 + y8*y1 + z8*z1 TIMES x9 ;eliminate x
;;;x9*x + y9*y  z9*z  =  x9*x5 + y9*y5 + z9*z5 TIMES x8

;;;x9*x8*x + x9*y8*y + x9*z8*z  =  x9*x8*x1 + x9*y8*y1 + x9*z8*z1
;;;x8*x9*x + x8*y9*y + x8*z9*z  =  x8*x9*x5 + x8*y9*y5 + x8*z9*z5
;;;y*(x9*y8 - x8*y9) = -z*(x9*z8 - x8*z9) + x8*x9*(x1-x5) + x9*y8*y1 +
;;; x9*z8*z1 - x8*y9*y5 - x8*z9*z5

;;;AN EQUATION OF y AND z OR PLANE POINT NORMAL

;;;x8*x + y8*y  z8*z  =  x8*x1 + y8*y1 + z8*z1 TIMES y9 ;eliminate y
;;;x9*x + y9*y  z9*z  =  x9*x5 + y9*y5 + z9*z5 TIMES y8

;;;y9*x8*x + y9*y8*y + y9*z8*z  =  y9*x8*x1 + y9*y8*y1 + y9*z8*z1
;;;y8*x9*x + y8*y9*y + y8*z9*z  =  y8*x9*x5 + y8*y9*y5 + y8*z9*z5
;;;x*(y9*x8*x - y8*x9*x) = -z*(y9*z8*z - y8*z9*z) + y9*x8*x1 + y9*y8*y1 +
;;; y9*z8*z1 - y8*x9*x5  - y8*y9*y5 - y8*z9*z5

;;;AN EQUATION OF x AND z OR PLANE POINT NORMAL

;;;x8*x + y8*y  z8*z  =  x8*x1 + y8*y1 + z8*z1  z =  f(x,y)

;;;AN EQUATION OF z = f(x,y) OR PLANE POINT NORMAL

;;;x=(/(  y9*x8*x1 + y9*y8*y1 + y9*z8*z1
;;;           - (y8*x9*x5 + y8*y9*y5 + y8*z9*z5)
;;;           - z*(y9*z8 - y8*z9))
;;;             (y9*x8 - y8*x9))

;;;y=(/(   x9*x8*x1 + x9*y8*y1 + x9*z8*z1
;;;           - (x8*x9*x5 + x8*y9*y5 + x8*z9*z5)
;;;           -z*(x9*z8 - x8*z9))
;;;          (x9*y8 - x8*y9))

;;;z  =  (y8*x1 + y8*y1 + z8*z1 - (x8*x + y8*y  ))/z8

;;;BREAKDOWN EQUATIONS OF X,Y,Z TO GIVE X10,Y10,Z10

;;;THUSLY

(if (= 0 (-(* y9 x8) (* y8 x9)))
 (progn
  (setq flagy 't flagx 't)
  (pr "Line is parallel to Yaxis or Zaxis:
       You must change UCS to Y or Z-axis draw the edges of the 2 planes
       and then FILLET them with FILLETRADIUS = 0 and change its THICKNESS
       to 50")
))

(cond
 ((or flagy flagx)             ;;parallel to Yaxis & Xaxis
  (progn

(command "layer" "t" "7" "m" "7" "c" "7" "7" "s" "7" "")

(setq y10 50 y11 -50)   ;Yaxis

(command "ucs" "y")

   (setq p10 (list (car p1) y10 (caddr p1)))
   (setq p11 (list (car p2) y10 (caddr p2)))

(command "line" p10 p11 "")

(setq e1(entget(car(entlast))))

   (setq p12 (list (car p5) y10 (caddr p5)))
   (setq p13 (list (car p6) y10 (caddr p6)))

(command "line" p12 p13 "")

(setq e2(entget(car(entlast))))

(command "fillet" "r" "0" "" p10 p12 "")

(setq a (list (cadr(assoc 10 e1)) y10 (cadr(assoc 30 e1))))
(setq b (list (cadr(assoc 11 e1)) y10 (cadr(assoc 31 e1))))

(setq c (list (cadr(assoc 10 e2)) y10 (cadr(assoc 30 e2))))
(setq d (list (cadr(assoc 11 e2)) y10 (cadr(assoc 31 e2))))

(cond
 ((= a c) (setq p10 a))
 ((= a d) (setq p10 a))
 ((= b c) (setq p10 b))
 ((= b d) (setq p10 b))
 (t (pr "ERROR: Yaxis fillet no good!")(setq p10 '( 0 y10 0)))
)

(setq p11 (list (car p10) y11 (caddr p10)))

(command "line" p10 p11 "") ;parallel to Yaxis!

;;;;;;;;;;;;   both necessary  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

   (setq x10 50 x11 -50)  ;Xaxis

(command "ucs" "x")

   (setq p10 (list x10 (cadr p1) (caddr p1)))
   (setq p11 (list x10 (cadr p2) (caddr p2)))

(command "line" p10 p11 "")

(setq e1(entget(car(entlast))))

   (setq p12 (list x10 (cadr p5) (caddr p5)))
   (setq p13 (list x10 (cadr p6) (caddr p6)))

(command "line" p12 p13 "")

(setq e2(entget(car(entlast))))

(command "fillet" "r" "0" "" p10 p12 "")

(setq a (list x10 (cadr(assoc 20 e1)) (cadr(assoc 30 e1))))
(setq b (list x10 (cadr(assoc 21 e1)) (cadr(assoc 31 e1))))
(setq c (list x10 (cadr(assoc 20 e2)) (cadr(assoc 30 e2))))
(setq d (list x10 (cadr(assoc 21 e2)) (cadr(assoc 31 e2))))

(cond
 ((= a c) (setq p10 a))
 ((= a d) (setq p10 a))
 ((= b c) (setq p10 b))
 ((= b d) (setq p10 b))
 (t (pr "ERROR: Xaxis fillet no good!")(setq p10 '(x10 0 0)))
)

(setq p11 (list x11 (cadr p10) (caddr p10)))

(command "line" p10 p11 "") ;parallel to Xaxis!

(command "ucs" "z")

 ))

 (t        ;default angled line not parallel to an axis
  (progn
   (setq z10 50 z11 -50)

(pr "z10 ")(princ z10)
(pr "z11 ")(princ z11)

   (setq x10 (/(-(+ (* y9 x8 x1) (* y9 y8 y1) (* y9 z8 z1)) ;;
                 (+ (* y8 x9 x5) (* y8 y9 y5) (* y8 z9 z5))
                 (* z10 (- (* y9 z8) (* y8 z9)))
               )
               (-(* y9 x8) (* y8 x9))
             )
   )
   (setq x11 (/(-(+ (* y9 x8 x1) (* y9 y8 y1) (* y9 z8 z1)) ;;
                 (+ (* y8 x9 x5) (* y8 y9 y5) (* y8 z9 z5))
                 (* z11 (- (* y9 z8) (* y8 z9)))
               )
               (-(* y9 x8) (* y8 x9))
             )
   )
   (setq y10 (/(-(+ (* x9 x8 x1) (* x9 y8 y1) (* x9 z8 z1))
                 (+ (* x8 x9 x5) (* x8 y9 y5) (* x8 z9 z5))
                 (* z10 (- (* x9 z8) (* x8 z9)))
               )
               (-(* x9 y8) (* x8 y9))
             )
   )
   (setq y11 (/(-(+ (* x9 x8 x1) (* x9 y8 y1) (* x9 z8 z1))
                 (+ (* x8 x9 x5) (* x8 y9 y5) (* x8 z9 z5))
                 (* z11 (- (* x9 z8) (* x8 z9)))
               )
               (-(* x9 y8) (* x8 y9))
             )
   )
  )
 )
)

(setq p10 (list x10 y10 z10))    ;;p10=  50,0,0  ok
(setq p11 (list x11 y11 z11))    ;;p11= -50,0,0  ok
                                 ;; intersect is line 50,0,0 to -50,0,0  ok

(pr "From point ")(princ p10)(princ " to ")(princ p11)

(command "layer" "m" "7" "c" "7" "7" "")
(command "line" p10 p11 "")
(command "change" "l" "" "prop" "lt" "phantom" )
;)(intrsctn)
;
1