!   TITLE:  finalize_model_data.mac (MANAGEMENT)
!
!
!       Added argument 1 (purge) function.
!
!   OBJECTIVE:
!
!   This sub-macro finalizes the data storage of part macros.
!
!
!   COMMAND SYNTAX:
!
!                              (1)
!        FINALIZE_MODEL_DATA, purge
!
!
!   ARGUMENTS:
!
!       (1) purge = boolean used to determine entities to be purged:
!
!               0 = delete all lower solid model entities:
!
!                   a.) If volumes exist, delete all extraneous areas,
!                       lines and KP's.
!
!                   b.) If no volumes exist, but only areas, delete all
!                       extraneous lines and KP's.
!
!               1 = delete all extraneous lines and KP's
!
!               2 = delete all extraneous KP's
!
!
!   DESCRIPTION:
!
!   This sub-macro is used to finalize storage of:
!
!       1.) solid model entities into groups by name
!
!       2.) model names in an array called 'MODEL'
!
!       3.) model parameters in arrays, and delete these parameters from
!           the database for use in other modles
!
!       4.) purge excess solid model entities that may result from model
!           creation
!
!   This aids in selecting parts or subassemblies, and tracking of parts by
!   their names and order of creation.
!
!   This macro is executed at the end of all part macros when used in an
!   assembly (ASSEMBLE = 1).
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
*if,assemble,eq,1,then
    *if,subassm_,eq,1,then      ! subassm_ is defined in mod_assem_prep.mac
        init_entity_num
    *else
    *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
    !
    *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
    !
    lplot
    !
    *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
    !
    !
    *set,namhold_,
    *set,nummat_,
    *set,abc_,
    *set,arry_nam,
    *set,mnz_,
    *set,_parchk1,
    *set,_parchk2,
    *set,_parchk3,
*endif
!
*if,prkey_,eq,1,then
    /go
*endif