! ******************************
! $Revision: 145002 $
! $Date: 2009-11-13 11:20:27 -0500 (Fri, 13 Nov 2009) $
! ******************************
! macro MTmrchkz ()

! *** primary function: checks for mirror topology on the plus-minus z (xy plane)
! ***
! *** Notice - This file contains ANSYS Confidential information ***

!  input arguments: none

!  output arguments:
!     _symmz     (int,sc)        1=topologically symmetric;  0=not symmetric

/nopr
*if,_DEBUG,gt,2,then
   /gopr
*endif

*get,_ucsys,active,,csys !to allow restore of prev csys
   
!   dsys,4 !for the *vget,kp locations !doesn't work
dsys,0  !for now
csys,4 !for ksel loc

allsel,below,area  !gets the keypoints
cm,_khold,kp
   
!check for z-z symmetry
_symmz=1
   
   *get,_num,kp,0,count
   !be careful of the location tolerance (zmin-zmax) on the command!
   ksel,s,loc,z,0,10000
   seltol,1e-2   ! use looser tolerance (default is 1e-6) since VSUM is inaccurate
   ksel,u,loc,z,0
   seltol,0
   *get,_nump,kp,0,count
   cm,_plusz,kp
   ksel,s,loc,z,0,-10000
   seltol,1e-2   ! use looser tolerance (default is 1e-6) since VSUM is inaccurate
   ksel,u,loc,z,0
   seltol,0
   *get,_numn,kp,0,count
   cm,_negz,kp
   
   !first check equal number on both sides
   *if,_nump,ne,_numn,then
      _symmz=0
   *endif

   ! check if all on the plane
   *if,_nump,eq,0,then
      _symmz=0
   *elseif,_numn,eq,0,then
      _symmz=0
   *endif
    
   !second check by keypoint mirror coordinates

   !!!!!! will do the dsys logic ourselves with *vfun,,global,,newcsys
   !!!!!!!!! we know we have the same number of kp's on +/- Z of the workplane now
   !! just check by comparing the sums of the xyz location columns
   !! the other way (based on cprad.mac) is probably going to be too slow
   *if,_symmz,eq,1,then
      *del,_kplocs
      *del,_xmask
      allsel,below,area
      *get,_highkp,kp,,num,max
      *dim,_kplocs,array,_highkp,3
      *dim,_xmask,array,_highkp
      
      !bug in dsys command to do the transformation ourselves
      csys,0  !!!!!!!NEED THIS!!!
      *vget,_kplocs(1,1),kp,,loc,x 
      *vget,_kplocs(1,2),kp,,loc,y
      *vget,_kplocs(1,3),kp,,loc,z
         
      *vfun,_kplocs(1),local,_kplocs(1),4 !put them in workplane coordinates
  
      !now we have keypoint coordinates in the workplane coordinate system
      
      cmsel,s,_plusz 
      *vfill,_xmask(1),ramp,0     
      *vget,_xmask(1),kp,,ksel
      
      *vmask,_xmask(1)
      *vscfun,_xsum_p,sum,_kplocs(1,1)
      *vmask,_xmask(1)
      *vscfun,_ysum_p,sum,_kplocs(1,2)
      *vmask,_xmask(1)
      *vscfun,_zsum_p,sum,_kplocs(1,3)
      
      cmsel,s,_negz      
      *vget,_xmask(1),kp,,ksel
      
      *vmask,_xmask(1)
      *vscfun,_xsum_n,sum,_kplocs(1,1)
      *vmask,_xmask(1)
      *vscfun,_ysum_n,sum,_kplocs(1,2)
      *vmask,_xmask(1)
      *vscfun,_zsum_n,sum,_kplocs(1,3)
      
      !need a bigger tolerance on the compare operation (1e-10 is default) 
!      *if,_xsum_p,ne,_xsum_n,then
!         _symmz=0
!      *elseif,_ysum_p,ne,_ysum_n,then
!         _symmz=0
!      *elseif,_zsum_p,ne,-(_zsum_p),then
!         _symmz=0
!      *endif
      
      !   so do it like this:

      _chktol=0.05  !tolerance on the check !should be based on model size
 
      *if,_xsum_p,gt,(_xsum_n+_chktol),then
         _symmz=0
      *endif      
      *if,_xsum_p,lt,(_xsum_n-_chktol),then
         _symmz=0
      *endif

      *if,_ysum_p,gt,(_ysum_n+_chktol),then
         _symmz=0
      *endif
      *if,_ysum_p,lt,(_ysum_n-_chktol),then
         _symmz=0
      *endif

      *if,_zsum_p,gt,(-_zsum_n+_chktol),then
         _symmz=0
      *endif
      *if,_zsum_p,lt,(-_zsum_n-_chktol),then
         _symmz=0
      *endif
      
   *endif

   
   !third check by topology of areas
      !not implemented yet
      
   !end of checks
   !_symmz either equals 0(no z-z symm) or 1(has z-z symm) 
  
cmsel,s,_khold
cmdel,_plusz
cmdel,_negz

_num=
_nump=
_numn=

_rattol=
_xsum_p=
_xsum_n=
_ysum_p=
_ysum_n=
_zsum_p=
_zsum_n=

csys,_ucsys
_ucsys=

!don't get rid of _symmz, that's what we are setting

dsys,0
!
