!   TITLE:  profile_coords.mac (CALCULATION)
!
!
!   OBJECTIVE:
!
!   This macro creates an output file of keypoint coordinates of a selected
!   set of lines.
!
!
!   COMMAND SYNTAX:
!
!                        (1)   (2)    (3)   (4)
!        PROFILE_COORDS, cs, strt_kp, prec, prm
!
!
!   ARGUMENTS:
!
!       (1) cs = the coordinate system number to measure KP coordinates relative
!                to. If desired C.S. is the active C.S., then simply input:
!
!                                       cs = 'a'
!
!       (2) strt_kp = the 1st keypoint in the line profile to begin tabulation
!                     of coordinates.
!
!       (3) prec = the desired decimal precision of the coordinates. Must be
!                  an integer value of 1 through 8.  If left blank, defaults
!                  to 3.
!
!       (4) prm = create keypoint structured file or parameter structured file.
!
!                   0 (or blank) = keypoint structured file
!                   1 = parameter structured file
!
!
!   DESCRIPTION:
!
!   Each line in the selected set must be connected in a continuous chain. The
!   line set may be open-ended or may form a closed loop.  However, the line
!   set cannot have any branching keypoints.
!
!   The coordinate system may be any type.  Whatever type the C.S., the X, Y
!   and Z components correspond to that ype of coordinate (e.g., X,Y,Z are
!   X,Y,Z in cartesian; X,Y,Z are R, THETA, Z in cylindrical; etc.) If the
!   active C.S. is desired, then the 1st argument is the character scalar 'a'.
!
!   The coordinates are full-precision real numbers.  A desired decimal may
!   be specified for argument 3, 'prec', and must be an integer value of 1
!   through 8. If this argument is left blank, the precision defaults to 3
!   decimal places of accuracy.
!
!   The output file has the following name:     pf_coord.mac
!
!   This file has 2 options for output:
!
!           1.) keypoint (numbered) structure file. This sets the numerical
!               value of the coordinates in the keypoint command, allowing
!               for cut-and-paste useage.
!
!           2.) parameter structured file. This creates the parameters for
!               the dimensions of each point in a profile, and the keypoint
!               commands in parameter form.
!
!
!
!
*get,prkey_,active,0,prkey
/nopr

csnum_=arg1
skp_=arg2
prec_=arg3
prm_=arg4
!
*get,acs1_,active,,csys
!
*get,ptyp_,parm,csnum_,type
!
*if,ptyp_,eq,3,then
    *if,csnum_,eq,'a',then
        csnum_=acs1_
    *else
        csnum_=0
    *endif
*endif
!
*if,prec_,eq,0,then
    prec_=3
*endif
!
cm,lnset_,line
*get,numlns_,line,,count
ksll,s
*get,kpcnt_,kp,,count
!
tol_=10**(-(prec_+1))
!
csys,csnum_
!
!
/output,pf_coord,mac
!
*msg,info

/output
*if,prm_,eq,0,then
    /output,pf_coord,mac,,append
*msg,info,%csnum_%
Keypoint Coordinates Measured From:  C.S. = %i
    /output
*elseif,prm_,eq,1,then
    /output,pf_coord,mac,,append
*msg,info,%csnum_%
Keypoint Coordinate Parameters Measured From:  C.S. = %i
    /output
*endif
!
/output,pf_coord,mac,,append
!
*msg,info

*msg,info
KP#     X       Y       Z
!
*msg,info

/output
!
*set,kp_1st_,
!
*do,aaa_,1,numlns_,1
    ksel,s,kp,,skp_
    !
    dx__=kx(skp_)
    !
    *if,abs(dx__),lt,tol_,then
        dx__=0
    *else
        round_num,dx__,prec_
        dx__=num_
    *endif
    !
    dy__=ky(skp_)
    !
    *if,abs(dy__),lt,tol_,then
        dy__=0
    *else
        round_num,dy__,prec_
        dy__=num_
    *endif
    !
    dz__=kz(skp_)
    !
    *if,abs(dz__),lt,tol_,then
        dz__=0
    *else
        round_num,dz__,prec_
        dz__=num_
    *endif
    !
    *if,skp_,ne,kp_1st_,then
        *if,prm_,eq,0,then
            /output,pf_coord,mac,,append
*msg,info,skp_,%dx__%,%dy__%,%dz__%
%i ---  k,,%g, %g, %g
            /output
        *elseif,prm_,eq,1,then
            /output,pf_coord,mac,,append
*msg,info,skp_
Keypoint # %i
*msg,info,%dx__%
Dx0 =%g
*msg,info,%dy__%
Dy0 =%g
*msg,info,%dz__%
Dz0 =%g
*msg,info
!
            /output
        *endif
    *endif
    !
    ln_skip=mod(aaa_,5)
    !
    *if,ln_skip,eq,0,then
        *if,aaa_,ne,1,then
            /output,pf_coord,mac,,append
*msg,info
!
            /output
        *endif
    *endif
    !
    cmsel,s,lnset_
    lslk,r,0
    !
    *if,aaa_,eq,1,then
        kp_1st_=skp_
        get_max_entity
        prfln_=lm
    *else
        lsel,u,line,,prfln_
        get_max_entity
        prfln_=lm
    *endif
    !
    *get,kp11_,line,prfln_,kp,1
    *get,kp12_,line,prfln_,kp,2
    !
    *if,skp_,eq,kp11_,then
        juncpt_=kp12_
    *else
        juncpt_=kp11_
    *endif
    !
    skp_=juncpt_
    !
    *set,juncpt_
    !
    *if,aaa_,eq,numlns_,then
        *if,skp_,ne,kp_1st_,then
            !
            dx__=kx(skp_)
            !
            *if,abs(dx__),lt,tol_,then
                dx__=0
            *else
                round_num,dx__,prec_
                dx__=num_
            *endif
            !
            dy__=ky(skp_)
            !
            *if,abs(dy__),lt,tol_,then
                dy__=0
            *else
                round_num,dy__,prec_
                dy__=num_
            *endif
            !
            dz__=kz(skp_)
            !
            *if,abs(dz__),lt,tol_,then
                dz__=0
            *else
                round_num,dz__,prec_
                dz__=num_
            *endif
            !
            *if,prm_,eq,0,then
                /output,pf_coord,mac,,append
*msg,info,skp_,%dx__%,%dy__%,%dz__%
%i ---  k,,%g, %g, %g
                /output
            *elseif,prm_,eq,1,then
                /output,pf_coord,mac,,append
*msg,info,skp_
Keypoint # %i
*msg,info,%dx__%
Dx0 =%g
*msg,info,%dy__%
Dy0 =%g
*msg,info,%dz__%
Dz0 =%g
*msg,info
!
                /output
            *endif
        *endif
    *endif
*enddo
!
init_entity_num
!
*if,prm_,eq,1,then
    /output,pf_coord,mac,,append
*msg,info
!
    /output
    !
    *do,aaa_,1,kpcnt_,1
        /output,pf_coord,mac,,append
*msg,info
k,,Dx0 ,Dy0 ,Dz0
        /output
        !
        ln_skip=mod(aaa_,5)
        !
        *if,ln_skip,eq,0,then
            *if,aaa_,ne,1,then
                /output,pf_coord,mac,,append
*msg,info
!
                /output
            *endif
        *endif
        !
    *enddo
*endif
!
cmsel,s,lnset_
!
csys,acs1_
!
lplot
!
!
!
*set,csnum_,
*set,acs1_,
*set,skp_,
*set,prfln_,
*set,juncpt_,
*set,aaa_,
*set,kp11_,
*set,kp12_,
*set,kp_1st_,
*set,dx__,
*set,dy__,
*set,dz__,
*set,ptyp_,
*set,num_,
*set,tol_,
*set,prec_,
*set,numlns_,
*set,prm_,
*set,ln_skip,
*set,kpcnt_,
cmdele,lnset_

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