!   TITLE:  tv_mark_cg.mac (CALCULATION)
!
!
!   TOOLBAR VERSION
!
!   OBJECTIVE:
!
!   This macro creates a keypoint at, and marks the position of, the center
!   of gravity (CG) of a volume or set of volumes and outputs the coordinates
!   and total volume to a dialogue box, and a set of parameters.
!
!
!   ARGUMENTS:
!
!       (1) c_or_d = create or delete CG marker/keypoint parameter:
!
!               0 (or blank) = create CG marker/keypoint
!
!               1 = delete CG marker/keypoint
!
!       (2) rcs = relative coordinate system
!
!
!   DESCRIPTION:
!
!   The selected volume(s) CG is visually marked with a set of axes. A keypoint
!   is placed at the CG location for information/measurement query. The key-
!   point number parameter is called 'cgkp_'.
!
!       OUTPUT @ DIALOGUE BOX:
!
!           C.S.= ... --- X_cg = ... , Y_cg = ... , Z_cg = ... --- V = ...
!
!       OUTPUT PARAMETERS:
!
!               XCG%RCS% = X (or R) coordinate of CG
!               YCG%RCS% = Y (or THETA) coordinate of CG
!               ZCG%RCS% = Z (or PHI) coordinate of CG
!               VCG = total volume of selected (CG) volumes
!
!           where %RCS% is simply the integer number of the relative C.S.
!
!
!
!
*get,prkey_,active,0,prkey
/nopr

*get,acs1_,active,,csys
!
multipro,'start',3
    *cset,1,3,cord_,'Create(0) or Delete(1) C.G.',0
    *cset,4,6,rcs_,'C.S. to mark C.G. relative to',acs1_
    *cset,7,9,prec_,'Enter Decimal Precision',4
    *cset,61,62,'Creates a C.G. keypoint and',' marker relative to a'
    *cset,63,64,'specified coordinate system.'
multipro,'end'
!
cm,vset1_,volu
!
init_entity_num
cmsel,s,vset1_
!
*if,cord_,eq,0,then
    *get,cgkptyp_,parm,cgkp_,type
    !
    *if,cgkptyp_,ne,-1,then
        ksel,s,kp,,cgkp_
        lslk,s,0
        ldele,all,,,1
        kdele,cgkp_
        !
        get_max_entity
        !
        *set,cgkp_,
    *endif
    !
    *if,prec_,le,0,then
        prec_=4
    *endif
    !
    tol_=10**(-prec_-1)
    !
    csys,0
    vsum
    !
    *get,v_,volu,,volu
    !
    *get,vcgx_,volu,,cent,x
    *get,vcgy_,volu,,cent,y
    *get,vcgz_,volu,,cent,z
    !
    k,,vcgx_,vcgy_,vcgz_
    !
    get_max_entity
    !
    cgkp_=km
    !
    csys,rcs_
    !
    *get,racs_,active,,csys
    *get,racstyp_,cdsy,racs_,attr,kcs
    !
    *if,racstyp_,ne,0,then
        clocal,csn+5,0,0,0,0,0,0,0
    *endif
    !
    k,,kx(cgkp_)+1,ky(cgkp_),kz(cgkp_)
    k,,kx(cgkp_),ky(cgkp_),kz(cgkp_)-1
    k,,kx(cgkp_)-1,ky(cgkp_),kz(cgkp_)
    k,,kx(cgkp_),ky(cgkp_),kz(cgkp_)+1
    k,,kx(cgkp_),ky(cgkp_)+1,kz(cgkp_)
    k,,kx(cgkp_),ky(cgkp_)-1,kz(cgkp_)
    !
    get_max_entity
    !
    *do,yz_,1,6,1
        lstr,cgkp_,km-(6-yz_)
    *enddo
    !
    get_max_entity
    !
    csys,rcs_
    !
    xcg%rcs_%=kx(cgkp_)
    ycg%rcs_%=ky(cgkp_)
    zcg%rcs_%=kz(cgkp_)
    !
    x_=kx(cgkp_)
    !
    *if,abs(x_),lt,tol_,then
        x_=0
    *else
        *if,abs(x_),ge,(10*tol_),then
            round_num,x_,prec_
        *else
            round_num,x_,prec_+1
        *endif
        !
        x_=num_
    *endif
    !
    y_=ky(cgkp_)
    !
    *if,abs(y_),lt,tol_,then
        y_=0
    *else
        *if,abs(y_),ge,(10*tol_),then
            round_num,y_,prec_
        *else
            round_num,y_,prec_+1
        *endif
        !
        y_=num_
    *endif
    !
    z_=kz(cgkp_)
    !
    *if,abs(z_),lt,tol_,then
        z_=0
    *else
        *if,abs(z_),ge,(10*tol_),then
            round_num,z_,prec_
        *else
            round_num,z_,prec_+1
        *endif
        !
        z_=num_
    *endif
    !
    *if,v_,lt,tol_,then
        v_=0
    *else
        *if,v_,ge,(10*tol_),then
            round_num,v_,prec_
        *else
            round_num,v_,prec_+1
        *endif
        !
        v_=num_
        vcg=num_
    *endif
    !
    *if,racstyp_,eq,0,then
*msg,ui,%rcs_%,%x_%,%y_%,%z_%,%v_%
C.S.= %i --- X_cg = %g , Y_cg = %g , Z_cg = %g --- V = %g
    *else
        *if,racstyp_,eq,1,then
*msg,ui,%rcs_%,%x_%,%y_%,%z_%,%v_%
C.S.= %i --- R_cg = %g , THETA_cg = %g , Z_cg = %g --- V = %g
        *elseif,racstyp_,eq,2,then
*msg,ui,%rcs_%,%x_%,%y_%,%z_%,%v_%
C.S.= %i --- R_cg = %g , THETA_cg = %g , PHI_cg = %g --- V = %g
        *endif
    *endif
    !
    *if,racstyp_,ne,0,then
        csdele,csn+5
    *endif
    !
    *set,x_,
    *set,y_,
    *set,z_,
    *set,tol_,
    *set,num_,
    *set,racs_,
    *set,racstyp_,
    *set,yz_,
    *set,vcgx_,
    *set,vcgy_,
    *set,vcgz_,
    *set,cgkptyp_,
    *set,v_,
*elseif,cord_,eq,1,then
    ksel,s,kp,,cgkp_
    lslk,s,0
    ldele,all,,,1
    kdele,cgkp_
    !
    *set,cgkp_,
*endif
!
csys,acs1_
init_entity_num
isolate_lines
cmsel,s,vset1_
aslv,s
lsla,a
!
lplot
!
*set,cord_,
*set,rcs_,
*set,acs1_,
*set,prec_,
cmdele,vset1_

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