!   TITLE:  mass_calcs.mac (MANAGEMENT)
!
!
!   TOOLBAR VERSION:
!
!   OBJECTIVE:
!
!   This sub-macro calculates the mass properties of a selected set of FE
!   models.
!
!
!   COMMAND SYNTAX:
!
!                       (1)         (2)       (3)
!       MASS_CALCS, 'model_name', output, relative_CS
!
!
!   ARGUMENTS:
!
!       (1) 'model_name' = character name of the model (part) to query the
!                          element masses. If left blank, the current selected
!                          set of elements are queried.
!                          [ Model name must be in single quotes ]
!
!       (2) output = output the parameters to a message window:
!
!                   0 (or blank) = DO NOT output to a message window.
!
!                   1 = output to a message window.
!
!       (3) active_CS = the active CS to measure element centroid coordinates
!                       relative to.
!
!                   0 (or blank) = Global cartesian system (default)
!
!           *** NOTE : Best to use a cartesian CS for a more consistent result.
!
!
!   DESCRIPTION:
!
!   The mass/weight is calculated from the existing elements along with the
!   overall ceneter of mass (CM). This allows for a verification of the correct
!   model weights, mass, and CG.
!
!   The CM/CG is measured relative to the input CS (argument 3). (It is
!   recommended to use acartesian system.)
!
!   The elements may be the currently selected set of elements, or the elements
!   associated with a part name may be input in (arguemtn 1).
!
!   The selected element(s) are stored in an array named: ELM_MASS. The array
!   contains 7 pieces of data for each element:
!
!       1.) element number
!       2.) element volume
!       3.) element material density
!       4.) element mass
!       5.) X-coordinate of element centroid (relative to csys = 0)
!       6.) Y-coordinate of element centroid (relative to csys = 0)
!       7.) Z-coordinate of element centroid (relative to csys = 0)
!
!   Each piece of data is stored in each column, with each row an element.
!   The array, ELM_MASS, has the following format:
!
!                                   COLUMNS
!         ROW   |   1    |  2   |    3    |  4   |  5   |   6   |   7   |
!      ------------------------------------------------------------------
!          1    |  E_#1  |  V1  |  dens1  |  M_1 |  Xc1 |  Yc1  |  Zc1  |
!          2    |  E_#2  |  V2  |  dens2  |  M_2 |  Xc2 |  Yc2  |  Zc2  |
!          3    |  E_#3  |  V3  |  dens3  |  M_2 |  Xc3 |  Yc3  |  Xc3  |
!          .    |   .    |  .   |   .     | .    |  .   |   .   |   .   |
!          .    |   .    |  .   |   .     | .    |  .   |   .   |   .   |
!          N    |  E_#N  |  VN  |  densN  |  M_N |  XcN |  YcN  |  ZcN  |
!
!
!   ------ The output parameters from this macro are::
!
!       a.)  m_tot - The total mass/weight of all the selected elements
!
!       b.)  x_cm - X-coordinate of the centroid of the total mass/weight
!
!       c.)  y_cm - Y-coordinate of the centroid of the total mass/weight
!
!       d.)  z_cm - Z-coordinate of the centroid of the total mass/weight
!
!       e.)  ELM_MASS - The array defined above. It is overwritten with
!                       each succesive run of this macro. 
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
out_=1
!
configuration,0
!
multipro,'start',2
    *cset,1,3,modname_,'Model Name (in single quotes)'
    *cset,4,6,actcs__,'Relative CS (for X,Y,Z of C.M.)'
    *cset,61,62,'Calculates the Mass / C.G.',' properties of a part/assembly'
multipro,'end'
!
! Parameter type: 0=scalar, 1=array, 2=table, 3=character scalar,
!                 4=character array, -1=undefined
!
*if,_button,eq,0,then
    *get,argtyp_,parm,modname_,type
    !
    *if,argtyp_,eq,3,then
        select,modname_
    *else
        modname_='Assembly'
    *endif
    !
    *set,elm_mass(1),
    !
    ! USER  <97>  User-defined system (default).
    ! SI  <97>  International system (SI or MKS; m, kg, s, K).
    ! CGS  <97>  CGS system (cm, g, s, °C).
    ! MPA  <97>  MPA system (mm, Mg, s, °C).
    ! BFT  <97>  British system using feet (ft, slug, s, °F).
    ! BIN  <97>  British system using inches (in, lbf*s2/in, s, °F).
    !
    ! Units specified by /UNITS command:
    ! 0 = USER, 1 = SI, 2 = CGS, 3 = BFT, 4 = BIN, 6 = MPA
    !
    *get,units,active,,units
    !
    *if,units,eq,0,then
        g_sys=g_user
    *elseif,units,eq,1,then
        g_sys=g_si
    *elseif,units,eq,2,then
        g_sys=g_cgs
    *elseif,units,eq,3,then
        g_sys=g_bft
    *elseif,units,eq,4,then
        g_sys=g_bin
    *elseif,units,eq,6,then
        g_sys=g_mpa
    *endif
    !
    !                          (1)     (2)   (3)  (4)   (5)    (6)   (7)
    !           ENTITY_ARRAY, entity, aname, typ, dir, #rows, #col, #pln
    !
    !
    entity_array,'e','elm_mass','array',,,7
    elmcnt_=ecount
    !
    csys,actcs__
    !
    *do,klm__,1,elmcnt_,1
        *get,elm_mass(klm__,2),elem,elm_mass(klm__,1),volu
        *get,nmat__,elem,elm_mass(klm__,1),attr,mat
        *get,rho__,dens,nmat__,temp,0
        !
        elm_mass(klm__,3)=rho__*g_sys
        elm_mass(klm__,4)=elm_mass(klm__,2)*elm_mass(klm__,3)
        !
        *get,elm_mass(klm__,5),elem,elm_mass(klm__,1),cent,x
        *get,elm_mass(klm__,6),elem,elm_mass(klm__,1),cent,y
        *get,elm_mass(klm__,7),elem,elm_mass(klm__,1),cent,z
    *enddo
    !
    m_tot=elm_mass(1,4)
    xcm_tot=(elm_mass(1,4))*(elm_mass(1,5))
    ycm_tot=(elm_mass(1,4))*(elm_mass(1,6))
    zcm_tot=(elm_mass(1,4))*(elm_mass(1,7))
    !
    *do,klm__,2,elmcnt_,1
        m_tot=m_tot+elm_mass(klm__,4)
        xcm_tot=xcm_tot+((elm_mass(klm__,4))*(elm_mass(klm__,5)))
        ycm_tot=ycm_tot+((elm_mass(klm__,4))*(elm_mass(klm__,6)))
        zcm_tot=zcm_tot+((elm_mass(klm__,4))*(elm_mass(klm__,7)))
    *enddo
    !
    x_cm=xcm_tot/m_tot
    !
    y_cm=ycm_tot/m_tot
    !
    z_cm=zcm_tot/m_tot
    !
    *if,out_,eq,1,then
        *msg,ui,modname_,%m_tot%,%actcs__%,%x_cm%,%y_cm%,%z_cm%
        Part Name = %12S %/&
        %/&
        Total Mass = %g %/&
        %/&
        Center-of-Mass coordinates relative to:  CSYS = %i %/&
        %/&
        X_cm = %g %/&
        Y_cm = %g %/&
        Z_cm = %g %/&
        %/&
    *endif
*endif
!
configuration,1
!
eplot
!
*set,elmcnt_,
*set,ecount,
*set,nmat__,
*set,rho__,
*set,units,
*set,modname_,
*set,argtyp_,
*set,out_,
*set,actcs__,
*set,_button,
!
*if,prkey_,eq,1,then
    /go
*endif