t arguments: none

!  output arguments:
!   _SUCCESS            (int,sc)      =0 no success, =1 success


/nopr
_SUCCESS=1
fini
/prep7

immed,off                       ! Turn off graphical updates
cm,_facetk,kp
cm,_facetl,line
cm,_faceta,area
cm,_facetv,volu

! --- clear existing mesh stuff
*get,_nne,elem,,count
*if,_nne,ne,0,then
   aclear,all
   vclear,all
   edel,all
   ndel,all
   numstr,elem,1
   numstr,node,1
*endif
lesize,all,,,,,,,,0             ! Clear size issues

*get,_typn,etyp,,num,max
*get,_typa,active,,type
_maxa=arinqr(0,14)
*del,_attr
*dim,_attr,array,_maxa,3
_typn=_typn+1
et,_typn,mesh200                ! Create element type one as dummy shells(200)
keyopt,_typn,1,4                ! Set up MESH200 as 3 noded triangles
keyopt,_typn,2,1                ! Turn off shape checking
type,_typn
smrt,off

! --- get areas unattached to volumes
vsla                            ! Get volumes attached to the selected areas
*get,_nnv,volu,,count           ! Find out how many volumes there are
allsel,below,volu               ! Select everything attached to volumes
*get,_nnva,area,,count
asel,inve                       ! Select all areas not attached to a volume
cmsel,r,_faceta
cm,_iareas,area                 ! Make component out of independent areas
*get,_nna,area,,count           ! Find out how many independent areas there are
cmsel,s,_facetk
cmsel,s,_facetl
cmsel,s,_faceta
cmsel,s,_facetv

! --- mesh areas attached to volumes
*if,_nnv,ne,0,then              ! If number of volumes is not zero, then...
   _nv=-1
   *do,_i,1,_nnv                ! Loop on volumes
      _nv = vlnext(_nv)
      vsel,s,,,_nv,,,1          ! Select the volume and everything below
      cmsel,r,_faceta           !   Now select only the picked areas from that
      *get,_nva,area,,count     ! Find out how many areas are on the volume
      *if,_nva,gt,0,then
         _na = arnext(-1)          ! Get the first area
         *do,_j,1,_nva             ! Loop on areas
            esel,none
            asel,s,,,_na,,,1       ! Select the area and everyting below
            *get,_attr(_na,1),area,_na,attr,mat
            *get,_attr(_na,2),area,_na,attr,real
            *get,_attr(_na,3),area,_na,attr,type
            aatt,,,_typn
            amesh,_na              ! Mesh the area
            *if,_STATUS,gt,1,then
               _SUCCESS=0
            *endif
            emodif,all,mat,_i
            emodif,all,real,_na
            aslv,s                ! Reselect the areas
            cmsel,r,_faceta
            _na = arnext(_na)     ! Get the next area
         *enddo
      *endif
      cmsel,s,_facetv             ! Reselect the original volume set
      *abcheck,30*(_i/_nnv)
   *enddo
*endif


! --- mesh the unattached (independent) areas
!
! Loop through all independent areas and make components out of all areas that
! are connected.  Each of these components of areas is in turn given a unique
! material number, and all areas are given a real property equal to their
! number.
!
_nas=0                          ! Intitialize area subcomponent counter to 0
*if,_nna,ne,0,then              ! If there are independent areas, then...
   _na=-1                       ! reset area number pointer 
   *do,_i,1,_nna                ! Loop on over all independent areas
      cmsel,s,_iareas           ! Select component of independent areas
      _na=arnext(_na)           ! Get next area in in selected set
      *if,_na,eq,0,exit         ! If area number equals zero, exit
      asel,s,area,,_na,,,1      ! Select area number
      *get,_acount1,area,,count ! initialize subcomponent area number
      *do,_j,1,_nna             ! Loop over independent areas
         asll                   ! Select all areas attached to selected lines
         cmsel,r,_faceta
         *get,_acount2,area,,count ! Get number of areas within new selected set
         *if,_acount2,eq,_acount1,exit   ! If number of areas in this new set is
                                         !   equal to counter, then...
         allsel,below,area       ! Select all entities below selected areas
      *enddo
      _nas=_nas+1         ! increment area subcomponent counter
      cm,aset%_nas%,area  ! Make component out of this new set
      _na=arnext(-1)      ! Reset area number to first area within set
      *do,_k,1,_acount2   ! Loop on areas within set
         asel,s,area,,_na,,,1     ! Select area which is being pointed to
                                  !  real number to area number
         esel,none
         *get,_attr(_na,1),area,_na,attr,mat
         *get,_attr(_na,2),area,_na,attr,real
         *get,_attr(_na,3),area,_na,attr,type
         aatt,,,_typn
         amesh,_na        ! mesh the selected area
         *if,_STATUS,gt,1,then
            _SUCCESS=0
         *endif
         emodif,all,mat,_nnv+_nas
         emodif,all,real,_na
         cmsel,s,aset%_nas%       ! Reselect area set
         _na=arnext(_na)          ! Increment area pointer
      *enddo
      asel,inve
      cmsel,r,_iareas
      cm,_iareas,area            ! Select component of all independent areas
      *get,_ttcount,area,,count
      _ttcount=_nna-_ttcount
      *abcheck,35*(_ttcount/_nna)
   *enddo
*endif

cmsel,s,_facetk,kp
cmsel,s,_facetl,line
cmsel,s,_faceta,area
cmsel,s,_facetv,volu
esla
nsle

*if,_nas,gt,0,then
   *do,_i,1,_nas
     cmdele,aset%_i%
   *enddo
*endif

cmdele,_facetk
cmdele,_facetl
cmdele,_faceta
cmdele,_facetv

type,_typa

_acount1=
_acount2=
_atyp=
_maxa=
_amat=
_arel=
_na=
_nv=
_NIAREAS=_nas
_nas=
_nna=
_nne=
_nnv=
_nva=
_nnva=
_nta=
_typa=
_ttcount=
_i=
_j=
_k=

smrt,on
