!   TITLE:  v_sel-attr.mac (CALCULATION)
!
!
!   OBJECTIVE:
!
!   This macro stores a set of selected volumes, OR selects a set of stored
!   volumes by their attributes.
!
!
!   COMMAND SYNTAX:
!
!                           (1)        (2)
!           V_SEL-ATTR, store/select, vgroup
!
!
!   ARGUMENTS:
!
!       (1) store/select = store or retrieval action:
!
!               0 (or blank) = store selected volumes
!
!               1 = retrieve (select) the previous stored volumes
!
!       (2) vgroup = volume group to store/select:
!
!               1 (or blank) = 1st group of selected volumes
!
!               2 = 2nd group of selected volumes
!
!
!   DESCRIPTION:
!
!   A predetermined set of volumes which are subject to the boolean operation,
!   VGLUE (see ANSYS help), alters the volume numbers, and the areas and or
!   lines comprising those volumes, while the volumes themselves are otherwise
!   unaltered (i.e., they contain the same space, volume and surface area).
!
!   Since the volumes numbers have changed, and some of the areas comprising
!   those volumes have been altered, there does not exist any predictable
!   method of reselecting those volumes once the VGLUE operation has been
!   executed.
!
!   The selection of all volumes stored allows for one to select the volumes
!   based on their volumes and/or centroidal locations, instead of their
!   volume numbers.
!
!   Therefore, after a VGLUE operation, those volumes that have been saved
!   prior to the glue operation may be reselected after the operation.
!
!   The prodedure for use of this macro is as follows (for the 1st group
!   of selected volumes):
!
!       1.)  Select any of, or all of, the volumes, that will be glued
!            together prior to the VGLUE operation, that are desired to be
!            recalled or reselected after the operation.
!
!       2.)  Execute this command macro with 0 (or blank) in the argument:
!
!                               v_sel-attr,0,1
!
!            This will store those selected volumes that are desired for
!            reselection after the glue operation by their volume
!            attributes.
!
!       3.)  Perform the glue operation of the selected volumes (execute
!            the VGLUE operation).
!
!       4.)  To reselect or recall the volumes stored in step 2, execute
!            this macro with a 1 in the argument:
!
!                               v_sel-attr,1,1
!
!   The desired volumes (group 1) are selected and available for further
!   operations as desired.
!
!   If there is a 2nd group of volumes to be selected after the glue operation,
!   then simply executing the above steps with the newly selected volumes,
!   changing the 2nd argument to 2, indicating the store/select operation of
!   those selected volumes.
!   (e.g., in step 2 above, the command would be:  v_sel-attr,0,2)
!
!
!
*get,prkey_,active,0,prkey
/nopr
!
savretr_=arg1
groupn_=arg2
!
*if,groupn_,eq,0,then
    groupn_=1
*endif
!
*get,activcs_,active,,csys
!
*if,savretr_,eq,0,then
    vol_cg
    !
    *if,groupn_,eq,1,then
        *set,vstore1_(1),
        arry_transfer,'vcg_data','vstore1_'
    *elseif,groupn_,eq,2,then
        *set,vstore2_(1),
        arry_transfer,'vcg_data','vstore2_'
    *endif
    !
    *set,vcg_data(1),
*elseif,savretr_,eq,1,then
    *if,groupn_,eq,1,then
        *get,parmtyp_,parm,vstore1_,type
        !
        *if,parmtyp_,eq,1,then
            vol_cg
            *get,nvols1_,parm,vcg_data,dim,x
            *get,nvstor1_,parm,vstore1_,dim,x
            !
            *set,v_attr_1(1),
            !
            *dim,v_attr_1,array,nvols1_+2,nvstor1_+2,4
            !
            arry_transfer,'vcg_data','v_attr_1',,,1,2,,,3,1      ! V #/volume
            arry_transpose,'vstore1_','v_attr_1',,,1,2,,,1,3
            !
            arry_transpose,'vstore1_','v_attr_1',,,3,,,,2,3,2     ! X cg
            arry_transpose,'vstore1_','v_attr_1',,,4,,,,2,3,3     ! Y cg
            arry_transpose,'vstore1_','v_attr_1',,,5,,,,2,3,4     ! Z cg
            !
            arry_transfer,'vcg_data','v_attr_1',,,3,,,,3,2,2     ! X cg
            arry_transfer,'vcg_data','v_attr_1',,,4,,,,3,2,3     ! Y cg
            arry_transfer,'vcg_data','v_attr_1',,,5,,,,3,2,4     ! Z cg
            !
            *do,j__,3,nvstor1_+2,1
                *do,i__,3,nvols1_+2,1
                    v_attr_1(i__,j__,1)=(abs(v_attr_1(i__,2,1)-v_attr_1(2,j__,1))/v_attr_1(2,j__,1))*100
                *enddo
            *enddo
            !
            *do,j__,3,nvstor1_+2,1
                *do,i__,3,nvols1_+2,1
                    deltax1_=v_attr_1(i__,2,2)-v_attr_1(2,j__,2)
                    deltay1_=v_attr_1(i__,2,3)-v_attr_1(2,j__,3)
                    deltaz1_=v_attr_1(i__,2,4)-v_attr_1(2,j__,4)
                    !
                    v_attr_1(i__,j__,2)=sqrt((deltax1_**2)+(deltay1_**2)+(deltaz1_**2))
               *enddo
            *enddo
            !
           *do,j__,3,nvstor1_+2,1
                *dim,vquery1_,array,nvols1_,3,1
                !
                arry_transfer,'v_attr_1','vquery1_',3,nvols1_+2,1,1,1,1,1,1,1
                arry_transfer,'v_attr_1','vquery1_',3,nvols1_+2,j__,j__,1,1,1,2,1
                arry_transfer,'v_attr_1','vquery1_',3,nvols1_+2,j__,j__,2,2,1,3,1
                !
                arry_search_lt,'vquery1_',1,,,2,2,1,1
                !
                *if,hits,eq,1,then
                    *if,j__,eq,3,then
                        r__=hit(1,2,1)
                        vsel,s,volu,,vquery1_(r__,1,1)
                    *else
                        r__=hit(1,2,1)
                        vsel,a,volu,,vquery1_(r__,1,1)
                    *endif
                *elseif,hits,gt,1,then
                    *dim,vqsub1_,array,hits,2,1
                    !
                    *do,k__,1,hits,1
                        rquery1_=hit(k__,2,1)
                        vqsub1_(k__,1,1)=vquery1_(rquery1_,1,1)
                        vqsub1_(k__,2,1)=vquery1_(rquery1_,3,1)
                    *enddo
                    !
                    arry_min_val,'vqsub1_',,,2,2,1,1
                    !
                    *if,j__,eq,3,then
                        vsel,s,volu,,vqsub1_(min_ir,1,1)
                    *else
                        vsel,a,volu,,vqsub1_(min_ir,1,1)
                    *endif
                *endif
                !
                *set,vquery1_(1),
                *set,vqsub1_(1),
            *enddo
        *endif
    *elseif,groupn_,eq,2,then
        *get,parmtyp_,parm,vstore2_,type
        !
        *if,parmtyp_,eq,1,then
            vol_cg
            *get,nvols2_,parm,vcg_data,dim,x
            *get,nvstor2_,parm,vstore2_,dim,x
            !
            *set,v_attr_2(1),
            !
            *dim,v_attr_2,array,nvols2_+2,nvstor2_+2,4
            !
            arry_transfer,'vcg_data','v_attr_2',,,1,2,,,3,1      ! V #/volume
            arry_transpose,'vstore2_','v_attr_2',,,1,2,,,1,3
            !
            arry_transpose,'vstore2_','v_attr_2',,,3,,,,2,3,2     ! X cg
            arry_transpose,'vstore2_','v_attr_2',,,4,,,,2,3,3     ! Y cg
            arry_transpose,'vstore2_','v_attr_2',,,5,,,,2,3,4     ! Z cg
            !
            arry_transfer,'vcg_data','v_attr_2',,,3,,,,3,2,2     ! X cg
            arry_transfer,'vcg_data','v_attr_2',,,4,,,,3,2,3     ! Y cg
            arry_transfer,'vcg_data','v_attr_2',,,5,,,,3,2,4     ! Z cg
            !
            *do,j__,3,nvstor2_+2,1
                *do,i__,3,nvols2_+2,1
                    v_attr_2(i__,j__,1)=(abs(v_attr_2(i__,2,1)-v_attr_2(2,j__,1))/v_attr_2(2,j__,1))*100
                *enddo
            *enddo
            !
            *do,j__,3,nvstor2_+2,1
                *do,i__,3,nvols2_+2,1
                    deltax2_=v_attr_2(i__,2,2)-v_attr_2(2,j__,2)
                    deltay2_=v_attr_2(i__,2,3)-v_attr_2(2,j__,3)
                    deltaz2_=v_attr_2(i__,2,4)-v_attr_2(2,j__,4)
                    !
                    v_attr_2(i__,j__,2)=sqrt((deltax2_**2)+(deltay2_**2)+(deltaz2_**2))
               *enddo
            *enddo
            !
           *do,j__,3,nvstor2_+2,1
                *dim,vquery2_,array,nvols2_,3,1
                !
                arry_transfer,'v_attr_2','vquery2_',3,nvols2_+2,1,1,1,1,1,1,1
                arry_transfer,'v_attr_2','vquery2_',3,nvols2_+2,j__,j__,1,1,1,2,1
                arry_transfer,'v_attr_2','vquery2_',3,nvols2_+2,j__,j__,2,2,1,3,1
                !
                arry_search_lt,'vquery2_',1,,,2,2,1,1
                !
                *if,hits,eq,1,then
                    *if,j__,eq,3,then
                        r__=hit(1,2,1)
                        vsel,s,volu,,vquery2_(r__,1,1)
                    *else
                        r__=hit(1,2,1)
                        vsel,a,volu,,vquery2_(r__,1,1)
                    *endif
                *elseif,hits,gt,1,then
                    *dim,vqsub2_,array,hits,2,1
                    !
                    *do,k__,1,hits,1
                        rquery2_=hit(k__,2,1)
                        vqsub2_(k__,1,1)=vquery2_(rquery2_,1,1)
                        vqsub2_(k__,2,1)=vquery2_(rquery2_,3,1)
                    *enddo
                    !
                    arry_min_val,'vqsub2_',,,2,2,1,1
                    !
                    *if,j__,eq,3,then
                        vsel,s,volu,,vqsub2_(min_ir,1,1)
                    *else
                        vsel,a,volu,,vqsub2_(min_ir,1,1)
                    *endif
                *endif
                !
                *set,vquery2_(1),
                *set,vqsub2_(1),
            *enddo
        *endif
    *endif
*endif
!
csys,activcs_
!
vplot
!
*set,v_attr_1(1),
*set,v_attr_2(1),
*set,vcg_data(1),
*set,deltax1_,
*set,deltay1_,
*set,deltaz1_,
*set,deltax2_,
*set,deltay2_,
*set,deltaz2_,
*set,hit(1),
*set,hits,
*set,min_val,
*set,min_ir,
*set,min_ic,
*set,min_ip,
*set,savretr_,
*set,activcs_,
*set,groupn_,
!
*if,prkey_,eq,1,then
    /go
*endif