!   TITLE:  max_kp_dist.mac (MANAGEMENT)
!
!
!   OBJECTIVE:
!
!   This sub-macro determines the keypoint with the maximum distance from a
!   specified coordinate system (CS).
!
!
!   COMMAND SYNTAX:
!
!                     (1)     (2)
!        MAX_KP_DIST, acs, comp_dir
!
!
!   ARGUMENTS:
!
!       (1) acs = the active C.S. This coordinate system may be any type.
!
!       (2) comp_dir = the axis component direction of the ACS in which to
!                      measure the distance:
!
!               a.) Positive (+) axis direction:
!
!                   '+x' = component distance in +X direction
!
!                   '+y' = component distance in +Y direction
!
!                   '+z' = component distance in +Z direction
!
!
!               b.) Negative (-) axis direction:
!
!                   '-x' = component distance in -X direction
!
!                   '-y' = component distance in -Y direction
!
!                   '-z' = component distance in -Z direction
!
!
!               c.) Absolute magnitude (+ and -) axis directions:
!
!                   'x' = component distance in +/- X direction
!
!                   'y' = component distance in +/- Y direction
!
!                   'z' = component distance in +/- Z direction
!
!
!               d.) Absolute magnitude (all directions):
!
!                   'r' (or blank) = maximum distance
!
!
!   DESCRIPTION:
!
!   This macro determines the maximum distance of any model entity from a
!   user specified coordinate system. If the model coordinate system is
!   specified, then the maximum distance of the model's entities is deter-
!   mined from it. The pertinent output from this macro are:
!
!       1.) totkpd_ = the array containing all selected KP's of a model
!                     and their absolute distance. This array is output
!                     when COMP_DIR is blank or 'r'.
!
!                                   --- OR ---
!
!           kpd_ =  the array containing the KP's of a model and their distance
!                   as measured in the user defined coordinate direction.  This
!                   array is output when COMP_DIR is a specified coordinate
!                   direction ( i.e., +x, +y, +z, -x, -y, -z, x, y or z ).
!
!           These arrays have dimension Nkp x 2, where Nkp is the number of KP
!           entities, and 2 is the number of columns. The 1st column is the KP
!           numbers, and the 2nd column is the distance of that KP in its
!           respective user-defined coordinate direction.
!
!       2.) max_dist = maximum value of distances found in column 2 of arrays
!
!       3.) kp_maxd = the KP number at the maximum distance
!
!       4.) max_indx = the row index containing 'kp_maxd' and 'max_dist'
!                      within the arrays.
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
acs_=arg1
compdir_=arg2
!
cm,kpset_,kp                        ! store KP's in component group
!
!
!
!
*get,arr1typ_,parm,totkpd_,type
*get,arr2typ_,parm,kpd_,type
!
*if,arr1typ_,eq,1,then
    *set,totkpd_(1),
*endif
!
*if,arr2typ_,eq,1,then
    *set,kpd_(1),
*endif
!
!
!
!
*get,comptyp_,parm,compdir_,type
!
*if,comptyp_,ne,3,then
    compdir_='r'
*endif
!
!
!
!
*get,actcs_,active,,csys
!
csys,acs_                       ! initiate relative coordinate system
!
*get,acstyp_,cdsy,acs_,attr,kcs
!
*if,acstyp_,ne,0,then
    create_cs,0,,0,0,0,0,0,0,0
*endif
!
!
!
!
entity_array,'k','totkpd_','array',1,,2,1   ! create KP/distance array
kpcnt_=ecount                               ! of all KP's
!
*do,ijk_,1,kpcnt_,1                     ! store KP distances in array
    kx_=kx(totkpd_(ijk_,1))
    ky_=ky(totkpd_(ijk_,1))
    kz_=kz(totkpd_(ijk_,1))
    !
    kdmax_=sqrt((kx_**2)+(ky_**2)+(kz_**2))
    !
    totkpd_(ijk_,2)=kdmax_
*enddo
!
max_value,'totkpd_',kpcnt_,2,1,1,1,2,1
max_dist=max_val
kp_maxd=totkpd_(max_indx,1)             ! KP number @ maximum distance
!
!
!
!
*if,compdir_,eq,'+x',then
    cmsel,s,kpset_
    ksel,r,loc,x,0,1.1*max_dist
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1  ! create KP/distance array
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=kx(kpd_(ijk_,1))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*elseif,compdir_,eq,'+y',then
    cmsel,s,kpset_
    ksel,r,loc,y,0,1.1*max_dist
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=ky(kpd_(ijk_,1))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*elseif,compdir_,eq,'+z',then
    cmsel,s,kpset_
    ksel,r,loc,z,0,1.1*max_dist
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=kz(kpd_(ijk_,1))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*elseif,compdir_,eq,'-x',then
    cmsel,s,kpset_
    ksel,r,loc,x,-1.1*max_dist,0
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=abs(kx(kpd_(ijk_,1)))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*elseif,compdir_,eq,'-y',then
    cmsel,s,kpset_
    ksel,r,loc,y,-1.1*max_dist,0
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=abs(ky(kpd_(ijk_,1)))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*elseif,compdir_,eq,'-z',then
    cmsel,s,kpset_
    ksel,r,loc,z,-1.1*max_dist,0
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=abs(kz(kpd_(ijk_,1)))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*elseif,compdir_,eq,'x',then
    cmsel,s,kpset_
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=abs(kx(kpd_(ijk_,1)))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*elseif,compdir_,eq,'y',then
    cmsel,s,kpset_
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=abs(ky(kpd_(ijk_,1)))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*elseif,compdir_,eq,'z',then
    cmsel,s,kpset_
    *get,kpcnt_,kp,,count
    !
    *if,kpcnt_,ge,1,then
        entity_array,'k','kpd_','array',1,,2,1
        kpcnt_=ecount
        !
        *do,ijk_,1,kpcnt_,1
            kdmax_=abs(kz(kpd_(ijk_,1)))
            kpd_(ijk_,2)=kdmax_
        *enddo
    *endif
*endif
!
*if,compdir_,ne,'r',then
    *if,kpcnt_,ge,1,then
        *set,totkpd_(1),
        !
        max_value,'kpd_',kpcnt_,2,1,1,1,2,1
        max_dist=max_val
        kp_maxd=kpd_(max_indx,1)
    *else
        max_dist=0
        kp_maxd=0
    *endif
*endif
!
!
!
!
cmsel,s,kpset_      ! restore the original selected set
csys,actcs_         ! restore original C.S.
!
*set,acs_,
*set,actcs_,
*set,compdir_,
*set,ecount,
*set,max_val,
*set,arr1typ_,
*set,arr2typ_,
*set,kdmax_,
*set,kpcnt_,
*set,kx_,
*set,ky_,
*set,kz_,
cmdele,kpset_

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