/out,b_mach3d,out
/com,b_mach3d.mac
/com, b_mach3d.mac   11/02/01  set use_53=0
!  builds the complete model, saves database to "mname".db
!  coil is on stator side
!
!  arg1 = name of parameter file (*.des), defaults to mach3d.des
!  arg2 = 0, build the winding
!       NE 0, do not build the winding
!
_err=0
*if,arg5,eq,1,then
 *ask,desnam,Parameter file name,0
 *get,_ty,parm,desnam,type
 *if,_ty,ne,3,then
   _err=1
   /nerr
   *msg,error
   The file name must be Character data. If the name is entered&
   directly, it must be enclosed in single quotes. A parameter&
   which has character data can also be entered. No action
 *endif
 arg1=desnam
*endif
*if,_err,eq,1,:end


/com,(1)  remove the existing model
/nerr,0,1e5
fini
/cle,all
abbr,,mabbr3d,mac

_arg1=arg1
*get,_targ1,parm,_arg1,type
*if,_targ1,ne,3,then
  arg1='mach3d'
*endif



*set,_cpu
*dim,_cpu,,7


/com, (2) load the new parameter file
/inp,%arg1%,des            !  the parameter file
*get,_cpu(1),active,,time,wall

use_53=0
/com, (3) Build the new rotor
*if,rotor_id,eq,'pmrotor',then
 pm_rotor,rref,,nrpgen,,r_mesh      !  build the permanent magnet rotor
*else
 /nerr
 *msg,error
 The rotor ID being specified is not available or is specified incorrectly
 /out,b_mach3d,out,,append
 /nerr,0,1e5
*endif
/out,b_mach3d,out,,append
*get,_cpu(2),active,,time,wall


/com, (4) Build the new stator
*if,stat_id,eq,'slotsta',then
 slotsta,0,stref,,nspgen,,,s_mesh    !  build the slotted stator
*elseif,stat_id,eq,'rslotsta',then
 rslotsta,0,stref,,nspgen,,,s_mesh    !  build the rectangular slotted stator
*else
 /nerr
 *msg,error
 The stator ID being specified is not available or is specified incorrectly
 /out,b_mach3d,out,,append
 /nerr,0,1e5
*endif
/out,b_mach3d,out,,append
*get,_cpu(3),active,,time,wall
save,m_2d,db


/com, (5)  Extrude the 2D model into a 3D model
mach3d,r_stk,m_stk,s_stk,n_div3d,skew_ang  ! BUILDS 3D model
                                                 ! parameter file has stack lengths
*get,_cpu(4),active,,time,wall                                        !  
/out,b_mach3d,out,,append


/com, (6)  Apply the periodic conditions if not a full machine
*if,nrpgen,lt,nrp,then
_mg1=1
 pmach3d,1                  !  applies periodic conditions to model sides
_mg1=0
*endif
/out,b_mach3d,out,,append
*get,_cpu(5),active,,time,wall
!save

/com, (7)  Build the winding
*get,_twfile,parm,w_file,type
*if,_twfile,ne,3,then
  arg2=1
*else
  _optwnd=0
  *if,skew_ang,gt,0,then
    *get,_tskew,parm,skew_stk,type
    *if,_tskew,eq,3,then
      *if,skew_stk,eq,'stator',then
         _optwnd=1
      *endif
    *endif
  *endif
*endif

*if,arg2,eq,0,then
 /nerr,0,1e5
  gen_arc=1
  !*if,_optwnd,eq,1,then
  !         1        2       3       4      5       6       7      8  9 10   11
  !b_wndsc,w_file,'s_iron',.0001,   coil_w,coil_h,180/nsp,maxslotr,nsp, ,  ,skew_ang        !     !builds the winding
  !*else
  *set,_in_wnd
  *dim,_in_wnd,,12
  _in_wnd(1)=ax_clear,coil_w,coil_h,180/nsp,maxslotr,nsp
  *set,_in_wndc
  *dim,_in_wndc,char,2
  _in_wndc(1)=w_file,'s_iron'
  !b_wndsc,w_file,'s_iron',ax_clear,coil_w,coil_h,180/nsp,maxslotr,nsp !  builds the winding
  _n_prom=1
  b_wndsc
  _n_prom=0
  !*endif
*endif

/out,b_mach3d,out,,append
*get,_cpu(6),active,,time,wall
_mg1=1
fmagbc,'s_iron'
_mg1=
save,%arg1%,db

/out,b_mach3d,out,,append

/com, (8) skew the model
*if,skew_ang,gt,0,then
 skew,skew_ang,skew_stk
*endif
/out,b_mach3d,out,,append
*get,_cpu(7),active,,time,wall
fitplot                    !  resizes the plot

alls
/pnum,mat,1
/num,1
/view,1,1,1,1
/auto
eplo

save,%mname%,db            !  saves model to "MNAME".db

*if,arg3,gt,0,then
  _mg1=1
  mvrot3d,arg3,1
  _mg1=
  mvpsol
  fini
  /post1
  tor_3d

*endif

*stat,_cpu
:end
/out