!   TITLE:  finalize_model.mac (MANAGEMENT)
!
!
!   Modified code to assign material attributes to the areas or volumes.
!
!   This sub-macro is used to finalize storage of solid model entities
!   into groups by name, and store all model names in an array called 'MODEL'.
!   This aids in selecting parts or subassemblies, and tracking their names.
!   It is called at the end of all part macros to store pertinent model data.
!
!
*get,prkey_,active,0,prkey
/nopr
!
*if,subassm_,eq,1,then      ! subassm_ is defined in mod_assem_prep.mac
    init_entity_num
*else
    purge_model
*endif
!
nummrg,kp
!
init_entity_num
!
*get,vcount,volu,,count         ! Selected volume count
!
*get,_parchk1,parm,name,type    ! Model name type: 0=scalar, 1=array,
!                               ! 2=table, 3=character scalar,
!                               ! 4=character array, -1=undefined Parameter.
!
*if,_parchk1,eq,-1,then
    name='part1_'               ! Default if part name undefined
*endif
!
    *get,vlow_,volu,,num,min
    !
    *do,abc_,1,vcount,1
        *if,abc_,eq,1,then
            v__=vlow_
        *endif
        !
        vsel,s,volu,,v__
        eslv,s
        *get,ecount_,elem,,count
        *get,vmat_,volu,v__,attr,mat
        !
        *if,ecount_,eq,0,then
            *if,attrmat_,ne,0,then
                *if,vmat_,eq,0,then
                    vatt,attrmat_
                *endif
            *endif
        *endif
        !
        init_entity_num
        *get,v__,volu,v__,nxth
    *enddo
*else
    *get,acount,area,,count
    *get,alow_,area,,num,min
    !
    *do,abc_,1,acount,1
        *if,abc_,eq,1,then
            a__=alow_
        *endif
        !
        asel,s,area,,a__
        esla,s
        *get,ecount_,elem,,count
        *get,amat_,area,a__,attr,mat
        !
        *if,ecount_,eq,0,then
            *if,attrmat_,ne,0,then
                *if,amat_,eq,0,then
                    aatt,attrmat_
                *endif
            *endif
        *endif
        !
        init_entity_num
        *get,a__,area,a__,nxth
    *enddo
*endif
!
*if,vcount,gt,0,then
    !
    cm,name,volu
    !
    !
    *if,modl_num,eq,1,then
        model(modl_num,1)=name
    *else
        *do,mnz_,1,modl_num-1,1
            mdlbuff(mnz_,1)=model(mnz_,1)
        *enddo
        !
        mdlbuff(modl_num,1)=name
        !
        *set,model(1),
        *dim,model,char,modl_num
        !
        *do,mnz_,1,modl_num,1
            model(mnz_,1)=mdlbuff(mnz_,1)
        *enddo
        !
        *set,mdlbuff(1),
        *dim,mdlbuff,char,modl_num+1
    *endif
    !
    !
*else
    !
    cm,name,area
    !
    !
    *if,modl_num,eq,1,then
        model(modl_num,1)=name
    *else
        *do,mnz_,1,modl_num-1,1
            mdlbuff(mnz_,1)=model(mnz_,1)
        *enddo
        !
        mdlbuff(modl_num,1)=name
        !
        *set,model(1),
        *dim,model,char,modl_num
        !
        *do,mnz_,1,modl_num,1
            model(mnz_,1)=mdlbuff(mnz_,1)
        *enddo
        !
        *set,mdlbuff(1),
        *dim,mdlbuff,char,modl_num+1
    *endif
    !
    !
*endif
!
allsel,all
!
*set,mnz_,
*set,vcount,
!
*get,_parchk2,parm,cs_arry,type
*get,_parchk3,parm,arry_nam,type
!
*if,_parchk2,eq,4,then
    *do,mnz_,1,csn-11,1         ! uninitializes c.s. parameter names
        *set,%cs_arry(mnz_,1)%,
    *enddo
*endif
!
*if,_parchk3,ne,-1,then
    _indxoff=4              ! parameter array index offset (lines 5&6, PAR_ARRAY)
    par_array,arry_nam      ! stores model parameters in an array
*endif
!
*if,_parchk2,eq,4,then
    *do,mnz_,1,csn-11,1         ! reinitializes c.s. parameter names
        *set,%cs_arry(mnz_,1)%,cs_arryv(mnz_,1)
    *enddo
*endif
!
*get,nummat_,parm,mat_name,dim,y
!
*if,nummat_,gt,0,then       ! reinitializes material number parameters
    *do,abc_,1,nummat_,1
        mat_prop(1,abc_)=abc_
        namhold_=mat_name(1,abc_)
        %namhold_%=abc_
    *enddo
*endif
!
lplot
!
*set,namhold_,
*set,nummat_,
*set,abc_,
*set,arry_nam,
*set,mnz_,
*set,_parchk1,
*set,_parchk2,
*set,_parchk3,
*set,attrmat_,
*set,v__,
*set,a__,
*set,vlow_,
*set,alow_,
*set,ecount_,
*set,vcount,
*set,acount,
*set,amat_,
*set,vmat_,
!
*if,prkey_,eq,1,then
    /go
*endif