/out,skew,out
/com, skew.mac
!     Takes an existing 3D model and skews the stack
!  arg1 = angle of skew (degrees)
!  arg2 = component name (in single quotes) e.g., 'stator' or 'rotor'
!         This component must at least contain the elements and nodes 
!         for the stack to be skewed.
!
!  Model requirement:
!    Material 1    AIR
!    Axis of rotation is the Z global axis
!    The global polar system is used to skew the model
!
!
!
!
_t_skew=arg1
_ok=0
*if,_t_skew,ne,0,then
 _ok=1
 *if,abs(_t_skew),lt,1,then
   _ok=0
 *endif
*endif
*if,_ok,eq,1,then
_s_skew
/nerr,0,1e4
*get,_dimn,active,,solu,dimn
*if,_dimn,ne,3,then
  /out
  *msg,info
  This macro cannot be used with 2D models-no action
  /out,skew,out,,append
*endif
*if,_dimn,ne,3,:end

_s_stk=arg2
nsel,none
cmsel,,%_s_stk%
*get,ndmx,node,,count
*if,ndmx,le,0,then
  /out
  *msg,info
  There are no nodes/elements for this component-no action
  /out,skew,out,,append
*endif
*if,ndmx,le,0,:end

*get,scpu1,active,,time,cpu
!  load the arrays of z locations of the planes of nodes
*set,_zloc
*dim,_zloc,,ndmx
nsel,none
cm,znod,node
cmsel,,%_s_stk%
esel,u,mat,,1
nsle
_npln=0
csys,1
*get,_rmax,node,,mxloc,x
_scaled=0
*if,_rmax,lt,.1,then
  alls
  csys
  fini
  /prep7
  immed
  nscal,,all,,,1000,1000,1000
  _scaled=1
*endif
cmsel,,%_s_stk%
esel,u,mat,,1
nsle
*do,_npln,1,ndmx
  /gopr
  *get,ndmxc,node,,num,max
  *if,ndmxc,eq,0,exit
  *get,_zmin,node,,mnloc,z
  _zloc(_npln)=_zmin
  nsel,u,loc,z,_zmin-.0001,_zmin+.0001
*enddo
_npln=_npln-1

_delth=arg1
csys,1
fini
/prep7
shpp,off
_stk=_zloc(_npln)-_zloc(1)
nsel,none
cm,cn1,node
*do,_i1,2,_npln
   /gopr
   *msg,info,_i1
   Cycle:  %i
   _ctht=_delth*(_zloc(_i1)-_zloc(1))/_stk
   cmsel,,%_s_stk%
   nsle
   nsel,r,loc,z,_zloc(_i1)-.001,_zloc(_i1)+.001
   ngen,2,,all,,,0,_ctht
   cmsel,a,cn1
   cm,cn1,node
*enddo

/com, move the nodes with larger Zs to _delth 
cmsel,,%_s_stk%
nsle
*get,_zmax,node,,mxloc,z
nsel,r,loc,z,_zloc(_npln)+.001,_zmax+.001
cmsel,u,cn1
*get,_cnt,node,,count
*if,_cnt,gt,0,then
 ngen,2,,all,,,0,_ctht
*endif

*if,_scaled,eq,1,then
  alls
  csys
  fini
  /prep7
  nscal,,all,,,.001,.001,.001
  _001=.001
  *voper,_zloc(1),_zloc(1),mult,_001
  _scaled=0
*endif


*if,skew_stk,eq,'stator',then
 esel,,ename,,36
 *get,_ecnt36,elem,,count
 *if,_ecnt36,gt,0,then
  esel,,mat,,5
  nsle
  csys,1
  *get,_zmin,node,,mnloc,z
  *get,_zmax,node,,mxloc,z
  _stk=_zmax-_zmin
  esel,,ename,,36
  nsle
  cm,cn1,node
  *get,_ncnt36,node,,count
  csys,1
     _sfac=1
  *do,_i1,1,_ncnt36
   /gopr
   *get,_ndmn,node,,num,min
   *get,_cz,node,_ndmn,loc,z
   !_ctht=_delth*(_cz)/(_stk)
   _ctht=_delth*(_cz-_zloc(1)/_sfac)/_stk

   ngen,2,,_ndmn,,,0,_ctht
   nsel,u,,,_ndmn
  *enddo
  nsle
 *endif
*endif


immed,1
*get,scpu2,active,,time,cpu
sdcpu=scpu2-scpu1
:end
_e_skew


*endif
/out