!   TITLE:  tv_ln_dist_ln.mac (CALCULATION)
!
!
!   TOOLBAR MACRO
!
!   OBJECTIVE:
!
!   This GUI command macro calculates the minimum distance between 2
!   lines.  If the lines intersect, a keypoint is created at the
!   intersection and the coordinates are displayed relative to the
!   active coordinate system.
!
!   ARGUMENTS:
!
!           GUI prompt:     acs = active coordinate system to measure
!                                 intersection corrdinates in.
!
!                           Line numbers to measure distance between:
!
!                                   LN1 and LN2
!
!           NOTE: If the input boxes for line numbers in the dialogue box
!                 are left to their default values of 0, then graphical
!                 picking is enabled. In this case, the user must select
!                 exactly 2 lines.
!
!   DESCRIPTION:
!
!   This macro calculates the minimum (perpendicular) distances         
!   between 2 lines if they are skew or parallel.
!
!   If the lines intersect and are not colinear, the coordinates of the   
!   intersection point are displayed relative to the specified coordinate 
!   system. The displayed coordinates match input coordinate system type. 
!   A keypoint is created at the intersection point. "init_entity_num" is 
!   called after creating the keypoint.                                   
!
!   If the desired coordinate system is the current active coordinate   
!   system, then simply pressing the ENTER key will suffice, since the  
!   default coordinate system value activated by the user prompt is     
!   the current active coordinate system. If the specified coordinate   
!   system is different from the current active coordinate system, the  
!   calculations will be performed and the current active coordinate    
!   system will be restored.                                            
!
!   All output is written to the standard output and to a screen
!   dialog box.
!
!
*get,prkey_,active,0,prkey
/nopr

entity_group

*get,acs1_,active,,csys

multipro,'start',4
    *cset,1,3,csnum_,'COORDINATE SYSTEM #',acs1_
    *cset,4,6,prec_,'Decimal Precision of Output',4
    *cset,7,9,ln_1st,'Enter 1st Line',0
    *cset,10,12,ln_2nd,'Enter 2nd Line',0
    *cset,61,62,'Calculates the minimum distance',' between two lines.'
    *cset,63,64,'If lines cross, the intersection',' coordinates are displayed'
multipro,'end'

! Make sure a resonable display precision was input
*if,prec_,lt,0,then
    prec_=4
*endif

*if,ln_1st*ln_2nd,le,0,then
    /ui,lsel

    get_min_entity
    get_max_entity

    ln_1st=l_min
    ln_2nd=lm

    *get,lcount_,line,,count
*else
    lcount_=lsel(ln_1st)+lsel(ln_2nd)
*endif


*if,lcount_,eq,2,then

    csys,csnum_

    ln_dist_ln,ln_1st,ln_2nd

    *if,err_,eq,1,then

        round_num,xi_,prec_
        xi_=num_

        round_num,yi_,prec_
        yi_=num_

        round_num,zi_,prec_
        zi_=num_

        *get,cstyp_,cdsy,csnum_,attr,kcs

        *if,cstyp_,eq,0,then

*msg,ui,ln_1st,ln_2nd,xi_,yi_,zi_,csnum_
Lines %i and %i intersect at : %/&
%/&
X = %g %/&
Y = %g %/&
Z = %g %/&
%/&
CS = %g  (cartesian coordinates)

        *elseif,cstyp_,eq,1,then

*msg,ui,ln_1st,ln_2nd,xi_,yi_,zi_,csnum_
Lines %i and %i intersect at : %/&
%/&
R = %g %/&
THETA = %g %/&
Z = %g %/&
%/&
CS = %g  (cylindrical coordinates)
        *else

*msg,ui,ln_1st,ln_2nd,xi_,yi_,zi_,csnum_
Lines %i and %i intersect at : %/&
%/&
R = %g %/&
THETA = %g %/&
PHI = %g %/&
%/&
CS = %g  (spherical coordinates)

        *endif

        *set,cstyp_

    *elseif,err_,eq,2,then

        round_num,dist_,prec_
        dist_=num_

*msg,ui,ln_1st,ln_2nd,dist_
Minimum distance between lines %i and %i : %/&
%/&
D = %g

    *else

        round_num,dist_,prec_
        dist_=num_

*msg,ui,ln_1st,ln_2nd,dist_
Lines %i and %i are parallel : %/&
%/&
D = %g

    *endif

    csys,acs1_

    *set,acs1_
    *set,csnum_
    *set,num_

    ! Delete parms created by ln_dist_ln
    *set,xi_,
    *set,yi_,
    *set,zi_,
    *set,dist_
    *set,err_

*elseif,lcount_,gt,2,then
*msg,warn
Too Many lines's Picked. Must Select 2 lines's Only.
*else
*msg,warn
Not Enough lines's Picked. Must Select 2 lines's Only.
*endif

entity_group,1

*set,ln_1st
*set,ln_2nd
*set,prec_
*set,lcount_

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