!   TITLE:  ar_profile_cut.mac (SM_TOOL)
!
!
!   OBJECTIVE:
!
!   This command macro creates an area profile shape to cut selected volumes.
!
!
!   COMMAND SYNTAX:
!
!                         (1)    (2)    (3)
!        AR_PROFILE_CUT, area, dirovr, distf
!
!
!   ARGUMENTS:
!
!       (1) area = the area whose profile is to be extruded.
!
!       (2) dirovr = the extrusion direction over-ride parameter:
!
!                   0 (or blank) = calculate direction based on volume
!                                  centroid.
!
!                   1 = opposite direction from calculated centroid.
!
!       (3) distf = distance factor. Must be an integer number > 0.
!
!
!   DESCRIPTION:
!
!   The outer perimeter lines of the selected area(s) are extruded to form a
!   hollow closed surface. The surface is extruded through a selected volume,
!   or set of volumes, cutting the volume(s) into two separate regions: volume
!   segments interior to the surface profile, and volume segments exterior to
!   the surface profile.
!
!   If only 1 area, whose profile is used for cutting the volume, is desired,
!   then this area is specified in argment 1. If more than 1 area profile is
!   desired, then those areas must be selected prior to execution of this macro,
!   and argument 1 left blank. (Do not use 'isolate_areas' to select the areas,
!   use the 'asel' command)
!
!   The extrusion direction is calculated by the algorithm using the location
!   of the centroid of the selected volumes. If the automated direction choice
!   is not the desired direction, then simply placing a 1 in the second argument
!   will reverse the direction of extrude.  If the default extrusion distance is
!   not a sufficient distance, then the extrude distance may be increased by a
!   factor specified in argument 3. Otherwise, arguments 2 and 3 are unnecessary,
!   and are routinely left blank.
!
!
!
!
*get,prkey_,active,0,prkey
/nopr

ar_=arg1
dirovr_=arg2
distf_=arg3
!
*if,distf_,eq,0,then
    distf_=1
*else
    distf_=abs(nint(distf_))
*endif
!
*if,dirovr_,eq,0,then
    df_=distf_*10
*elseif,dirovr_,eq,1,then
    df_=-distf_*10
*endif
!
*get,acs1_,active,,csys
!
csys,0
!
cm,aset1_,area
cm,lset1_,line
cm,kset1_,kp
cm,vset1_,volu
!
vsum
*get,vcx_,volu,,cent,x
*get,vcy_,volu,,cent,y
*get,vcz_,volu,,cent,z
k,,vcx_,vcy_,vcz_
!
init_entity_num
kvcent_=km
!
init_entity_num
!
cmsel,s,aset1_
cmsel,s,lset1_
cmsel,s,kset1_
cmsel,s,vset1_
!
*if,ar_,eq,0,then
    entity_array,'a','arprof_','array'
    numars_=ecount
    !
    *do,abc_,1,numars_,1
        asel,s,area,,arprof_(abc_,1)
        lsla,s
        cm,proflns_,line
        *get,plcnt_,line,,count
        ksll,s
        !
        get_max_entity
        kstrt_=km
        get_min_entity
        *get,kcnt_,kp,k_min,nxth
        !
        cskp,csn+5,0,kcnt_,k_min,kstrt_
        !
        k,,kx(km),ky(km),df_*kz(kvcent_)
        get_max_entity
        !
        lstr,kstrt_,km
        get_max_entity
        pthln_=lm
        !
        adrag,proflns_,,,,,,pthln_
        ldele,pthln_,,,1
        init_entity_num
        !
        asel,s,area,,am-(plcnt_-1),am
        !
        *if,abc_,eq,1,then
            cm,newars_,area
        *else
            cmsel,a,newars_
            cm,newars_,area
        *endif
    *enddo
    !
    init_entity_num
    !
    vsba,vset1_,newars_,,delete,delete
    !
    init_entity_num
    !
    lplot
    !
    *set,numars_,
    *set,ecount,
    *set,arprof_(1),
*else
    asel,s,area,,ar_
    lsla,s
    cm,proflns_,line
    *get,plcnt_,line,,count
    ksll,s
    !
    get_max_entity
    kstrt_=km
    get_min_entity
    *get,kcnt_,kp,k_min,nxth
    !
    cskp,csn+5,0,kcnt_,k_min,kstrt_
    !
    k,,kx(km),ky(km),df_*kz(kvcent_)
    get_max_entity
    !
    lstr,kstrt_,km
    get_max_entity
    pthln_=lm
    !
    adrag,proflns_,,,,,,pthln_
    ldele,pthln_,,,1
    init_entity_num
    !
    asel,s,area,,am-(plcnt_-1),am
    cm,newars_,area
    !
    init_entity_num
    !
    vsba,vset1_,newars_,,delete,delete
    !
    init_entity_num
    !
    lplot
*endif
!
kdele,kvcent_
init_entity_num
!
csdele,csn+5
csys,acs1_
!
lplot
!
!
*set,ar_,
*set,dirovr_,
*set,kcnt_,
*set,kstrt_,
*set,df_,
*set,acs1_,
*set,proflns_,
*set,kvcent_,
*set,vcx_,
*set,vcy_,
*set,vcz_,
*set,plcnt_,
*set,pthln_,
*set,abc_,
cmdele,aset1_
cmdele,lset1_
cmdele,kset1_
cmdele,vset1_
cmdele,newars_

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