!   TITLE:  ct_dist_calcs.mac (CALCULATION)
!
!
!           Implemented grphical picking option.
!
!   TOOLBAR MACRO
!
!   OBJECTIVE:
!
!   This GUI command macro calculates the linear distances between 2 keypoints.
!
!
!   ARGUMENTS:
!
!           1st prompt:     acs = active coordinate system to measure axial
!                                 distance in.
!
!                           Keypoint numbers to measure distance between:
!
!                                   KP1 and KP2
!
!           NOTE: If the input boxes for keypoint 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 key-
!                 points only.
!
!   DESCRIPTION:
!
!   This macro calculates the linear (cartesian) distances between 2 keypoints.
!   These distances are measured in all 3 axial directions: Dx, Dy and Dz, and the
!   absolute linear distance between them, Dabs. The distances are calculated rel-
!   ative to a desired user-specified coordinate system.
!
!   If the specified coordinate system is not cartesian, then the distance calcu-
!   lations are measured along the oriented axial directions of that coordinate
!   system as if it were cartesian. Therefore, a cartesian coordinate system need
!   not be created or specified as long as the axial directions of the specified
!   coordinate system are oriented as desired.
!
!   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 to a screen dialog box.
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
entity_group
!
*get,acs1_,active,,csys
!
grphpic_=0
!
multipro,'start',4
    *cset,1,3,csnum_,'COORDINATE SYSTEM #',acs1_
    *cset,4,6,prec_,'Decimal Precision of Output',4
    *cset,7,9,kp_1st,'Enter 1st Keypoint',0
    *cset,10,12,kp_2nd,'Enter 2nd Keypoint',0
    *cset,61,62,'Calculates the X,Y,Z and abs','olute (linear) distance'
    *cset,63,64,'between 2 keypoints. Enter',' the keypoint numbers.'
multipro,'end'
!
*if,_button,eq,0,then
    *if,kp_1st,eq,0,then
        *if,kp_2nd,eq,0,then
            /ui,ksel
            get_min_entity
            get_max_entity
            !
            *get,kcount_,kp,,count
            !
            kp_1st=k_min
            kp_2nd=km
            !
            grphpic_=1
        *endif
    *endif
    !
    *if,grphpic_,eq,0,then
        kp1_=ksel(kp_1st)
        kp2_=ksel(kp_2nd)
        !
        *if,kp1_,eq,1,then
            *if,kp2_,eq,1,then
                kcount_=2
            *else
                kcount_=1
            *endif
        *else
            *if,kp2_,eq,1,then
                kcount_=1
            *else
                kcount_=0
            *endif
        *endif
    *endif
    !
    *if,kcount_,eq,2,then
        csys,csnum_
        !
        *get,csnumtyp,cdsy,csnum_,attr,kcs
        !
        *if,csnumtyp,ne,0,then
            clocal,csn+1,0,0,0,0,0,0,0
            csnum_=csn+1
        *endif
        !
        *if,kp_1st,ne,0,then
            *if,kp_2nd,ne,0,then
                *if,prec_,le,0,then
                    prec_=4
                *endif
                !
                tol_=10**(-prec_-1)
                !
                dx__=abs(kx(kp_1st)-kx(kp_2nd))
                !
                *if,abs(dx__),lt,tol_,then
                    dx__=0
                *else
                    *if,abs(dx__),ge,(10*tol_),then
                        round_num,dx__,prec_
                    *else
                        round_num,dx__,prec_+1
                    *endif
                    !
                    dx__=num_
                *endif
                !
                dy__=abs(ky(kp_1st)-ky(kp_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
                !
                dz__=abs(kz(kp_1st)-kz(kp_2nd))
                !
                *if,abs(dz__),lt,tol_,then
                    dz__=0
                *else
                    *if,abs(dz__),ge,(10*tol_),then
                        round_num,dz__,prec_
                    *else
                        round_num,dz__,prec_+1
                    *endif
                    !
                    dz__=num_
                *endif
                !
                dabs__=distkp(kp_1st,kp_2nd)
                !
                *if,abs(dabs__),lt,tol_,then
                    dabs__=0
                *else
                    *if,abs(dabs__),ge,(10*tol_),then
                        round_num,dabs__,prec_
                    *else
                        round_num,dabs__,prec_+1
                    *endif
                    !
                    dabs__=num_
                *endif
                !
    *msg,ui,%kp_1st%,%kp_2nd%,%dx__%,%dy__%,%dz__%,%dabs__%
    Distance Between KP #'s ( %i , %i ) : %/&
    %/&
    Dx = %g %/&
    Dy = %g %/&
    Dz = %g %/&
    %/&
    Dabs = %g
                !
                *if,csnumtyp,ne,0,then
                    csdele,csnum_
                *endif
                !
                csys,acs1_
                !
                *set,acs1_,
                *set,csnum_,
                *set,csnumtyp,
                *set,dx__,
                *set,dy__,
                *set,dz__,
            *set,dabs__,
                *set,tol_,
            *endif
        *endif
    *elseif,kcount_,gt,2,then
    *msg,warn
    Too Many KP's Picked. Must Select 2 KP's Only.
    *elseif,kcount_,lt,2,then
    *msg,warn
    Not Enough KP's Picked. Must Select 2 KP's Only.
    *endif
*endif
!
entity_group,1
!
*set,grphpic_,
*set,kp1_,
*set,kp2_,
*set,kp_1st,
*set,kp_2nd,
*set,prec_,
*set,kcount_,
!
*if,prkey_,eq,1,then
    /go
*endif