!   TITLE:  tv_ln_dist_kp.mac (CALCULATION)
!
!
!           Implemented graphical picking option.
!
!   TOOLBAR MACRO
!
!   OBJECTIVE:
!
!   This GUI command macro calculates the linear distance between a straight
!   line and a keypoint.
!
!
!   ARGUMENTS:
!
!           1 prompt:   Line & Keypoint numbers to measure distance
!                       between:
!
!                                   LN# and KP#
!
!                       Decimal precision of output
!
!                                   PREC
!
!           NOTE: If the input boxes for the line & keypoint numbers are left
!                 to their default values of 0, then graphical picking is enabled.
!                 In this case, the user must select exactly 1 line and 1 keypoint
!                 only.
!
!
!   DESCRIPTION:
!
!   This macro calculates the perpendicular (minimum) distance between a line
!   and a keypoint. This distance is only valid if the input line is straight.
!
!   The output is to a screen dialog box.
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
entity_group
!
*get,acs1_,active,,csys
!
grphpic_=0
!
multipro,'start',3
    *cset,1,3,ln_1st,'Enter Line #',0
    *cset,4,6,pt_2nd,'Enter KP #',0
    *cset,7,9,prec_,'Enter Decimal Precision',4
    *cset,61,62,'Calculates the normal dist','ance between a line and'
    *cset,63,64,'a keypoint. Enter the line',' and KP numbers.'
multipro,'end'
!
*if,ln_1st,eq,0,then
    *if,pt_2nd,eq,0,then
        /ui,lsel
        /ui,ksel
        get_max_entity
        !
        *get,lcount_,line,,count
        *get,kcount_,kp,,count
        !
        ln_1st=lm
        pt_2nd=km
        !
        grphpic_=1
    *endif
*endif
!
*if,grphpic_,eq,0,then
    ln1_=lsel(ln_1st)
    kp1_=ksel(pt_2nd)
    !
    *if,ln1_,eq,1,then
        *if,kp1_,eq,1,then
            lcount_=1
            kcount_=1
        *else
            lcount_=1
            kcount_=0
        *endif
    *else
        *if,kp1_,eq,1,then
            lcount_=0
            kcount_=1
        *else
            lcount_=0
            kcount_=0
        *endif
    *endif
*endif
!
*if,lcount_,eq,1,then
    *if,kcount_,eq,1,then
        *if,prec_,le,0,then
            prec_=4
        *endif
        !
        tol_=10**(-prec_-1)
        !
        *get,kp11_,line,ln_1st,kp,1
        *get,kp12_,line,ln_1st,kp,2
        !
        *if,grphpic_,eq,1,then
            ksel,a,kp,,kp11_
            ksel,a,kp,,kp12_
        *endif
        !
        cskp,csn+5,0,kp11_,kp12_,pt_2nd
        !
        dy__=abs(ky(pt_2nd))
        !
        *if,abs(dy__),lt,tol_,then
            dy__=0
        *else
            *if,abs(dy__),ge,(10*tol_),then
                round_num,dy__,prec_
            *else
                round_num,dy__,prec_+1
            *endif
            !
            dy__=num_
        *endif
        !
*msg,ui,%ln_1st%,%pt_2nd%,%dy__%
Distance Between ( L %i , KP %i ) = %g
        !
        csdele,csn+5
        csys,acs1_
        !
        *set,dy__,
        *set,kp11_,
        *set,kp12_,
        *set,acs1_,
        *set,tol_,
    *endif
*endif
!
*if,lcount_,gt,1,then
    *if,kcount_,gt,1,then
*msg,warn
Too Many Lines and KP's Picked. Must Select 1 each.
    *elseif,kcount_,lt,1,then
*msg,warn
Too Many Lines, Insufficient KP's Picked. Must Select 1 each.
    *elseif,kcount_,eq,1,then
*msg,warn
Too Many Lines Picked. Must Select 1 Only.
    *endif
*elseif,lcount_,lt,1,then
    *if,kcount_,gt,1,then
*msg,warn
Insufficient Lines and Too Many KP's Picked. Must Select 1 each.
    *elseif,kcount_,lt,1,then
*msg,warn
Insufficient Lines and KP's Picked. Must Select 1 each.
    *elseif,kcount_,eq,1,then
*msg,warn
Insufficient Lines Picked. Must Select 1 Only.
    *endif
*elseif,lcount_,eq,1,then
    *if,kcount_,gt,1,then
*msg,warn
Too Many KP's Picked. Must Select 1 each.
    *elseif,kcount_,lt,1,then
*msg,warn
Insufficient KP's Picked. Must Select 1 each.
    *endif
*endif
!
init_entity_num
!
entity_group,1
!
*set,ln_1st,
*set,pt_2nd,
*set,prec_,
*set,ln1_,
*set,kp1_,
*set,grphpic_,
*set,lcount_,
*set,kcount_,
!
*if,prkey_,eq,1,then
    /go
*endif