!   TITLE:  ln_dist_ln.mac (CALCULATION)
!
!               Changed flag "isect_kp" to "del_kp" and modified logic
!               to keep the intersection KP by default.  Added calc
!               for distance between parallel lines.
!
!   OBJECTIVE:
!
!   Compute the minimum distance between two 3 dimensional lines.  If
!   the lines cross, compute the coordinates of the intersection point 
!   relative to the active coordinate system.
!
!   COMMAND SYNTAX:
!
!                     (1)  (2)  (3)  (4)
!         ln_dist_ln, KP1, KP2, KP3, KP4
!
!       or
!
!         ln_dist_ln, L1,  L2
!
!   ARGUMENTS:
!
!       (1) KP1 = Keypoint number for line 1,
!                 or the line number for line 1
!
!       (2) KP2 = Other keypoint number for line 1,
!                 or the line number for line 2
!
!       (3) KP3 = Keypoint number for line 2
!
!       (4) KP4 = Other keypoint number for line 2
!
!   FLAGS:
!
!       del_kp =    If this parameter has a numeric value of 1 prior to
!                   calling this macro, a keypoint will not be created
!                   at the intersection of the two lines.
!                   This parameter is unset at the end of this macro.
!                   If a KP is created, its number is assigned to "kpi_".
!
!   DESCRIPTION:
!
!   Computes the minimum (or perpendicular) distance between two lines.
!
!   The distance result is returned in the parameter "dist_".
!
!   If the distance is zero, the lines are intersecting and the location
!   of the intersection is returned in parameters "xi_", "yi_" and "zi_"
!   The coordinates will be cartesian, polar or spherical depending on
!   active coordinate system type.
!
!   If 4 arguments are supplied, they are assumed to be key points,
!   otherwise line number input is assumed.
!
!   The parameter "err_" is set according to the relationship between
!   the two lines as follows:
!
!       0 = Lines are parallel
!       1 = Lines intersect
!       2 = Lines are skew
!
!
*get,count_,cmd,0,nargs     ! count_ = # of args + 1

*get,prkey_,active,0,prkey
/nopr

*if,count_,eq,5,then

    kp1_=arg1
    kp2_=arg2
    kp3_=arg3
    kp4_=arg4

*else

    l1_=arg1
    l2_=arg2

    *get,kp1_,line,l1_,kp,1
    *get,kp2_,line,l1_,kp,2
    *get,kp3_,line,l2_,kp,1
    *get,kp4_,line,l2_,kp,2

*endif

*get,csys_,active,0,csys            ! Store the current CS #

csys,0                              ! Perform calcs in cart. GCS

! Write the input file for ln_dist_ln.c
*cfopen,ln_dist_ln,out

*do,i_,1,4

*vwrite,kx(kp%i_%_),ky(kp%i_%_),kz(kp%i_%_)
(3E30.18)

*enddo

*cfclos

/gopr

/sys,ln_dist_ln

/nopr

parres,change,ln_dist_ln,in

*if,err_,eq,1,then

    ! --- LINES INTERSECT --------------------------------------------

    *get,kpi_,kp,0,num,max
    kpi_=kpi_+1

    k,kpi_,xi_,yi_,zi_

    csys,csys_

    xi_=kx(kpi_)
    yi_=ky(kpi_)
    zi_=kz(kpi_)

*msg,info,csys_
Active CS = %g

*msg,info,err_,dist_,xi_,yi_,zi_
ERR_ = %g,  DIST_ = %g%/&
XI_  = %g%/&
YI_  = %g%/&
ZI_  = %g

*msg,info,kpi_
KPI_ = %g

    *if,del_kp,eq,1,then

        kdel,kpi_
        *set,kpi_
        *set,del_kp
    *else
        init_entity_num
    *endif

*elseif,err_,eq,2,then

    ! --- SKEW LINES -------------------------------------------------

*msg,info,csys_
Active CS = %g

*msg,info,err_,dist_
ERR_ = %g,  DIST_ = %g

*else

    ! --- PARALLEL LINES ---------------------------------------------

    cskp,csn+11,0,kp1_,kp2_,kp3_

    dist_=ky(kp3_)

    csdele,csn+11

*msg,info,csys_
Active CS = %g

*msg,info,err_,dist_
ERR_ = %g,  DIST_ = %g

*endif

!
!

csys,csys_

*set,count_
*set,i_
*set,kp1_
*set,kp2_
*set,kp3_
*set,kp4_
*set,l1_
*set,l2_
*set,csys_

*if,prkey_,eq,1,then
    /go
*endif