/out,b_mach2d,out
/com,b_mach2d.mac
!
!  builds the complete model-see steps below
!
!  arg1 = name of parameter file (*.des)
!  arg2 = 0, build the winding
!       NE 0, do not build the winding
!
/com,(1)  remove the existing model & check for error condition
/nerr,0,1e5
fini
/cle,all
_arg1=arg1
*get,_targ1,parm,_arg1,type
*if,_targ1,ne,3,then
  /out
  /nerr
  *msg,error
  Parameter file name was input correctly. It needs to be a character&
  parameter or enclosed in single quotes  as 'mach2'.  No action
  /out,b_mach2d,out,,append
  _error=1
*endif
*if,_error,eq,1,:end

/com, (2) load the new parameter file
/inp,%arg1%,des            !  the parameter file

/nerr,0,1e5
_m_error=0
/com, (3) Build the new rotor
*if,rotor_id,eq,'pmrotor',then
 pm_rotor,rref,use_53,nrpgen,use_53      !  build the permanent magnet rotor
*elseif,rotor_id,eq,'slotrot'
 slotrot,,rref,0,nrpgen,use_53
*else
 /out
 /nerr
 *msg,error
 ROTOR_ID does not match existing rotor templates-stator was not built
 _m_error=1
*endif
/out,b_mach2d,out,,append
/nerr,0,1e5

/com, (4) Build the new stator
*if,stat_id,eq,'slotsta',then
 slotsta,0,stref,use_53,nspgen,use_53    !  build the slotted stator
*elseif,stat_id,eq,'pmstat'
 pm_stat,use_53,stref,nspgen,use_53
*else
 /out
 /nerr
 *msg,error
 STATOR_ID does not match existing stator templates-rotor was not built
 _m_error=1
*endif
/out,b_mach2d,out,,append

/nerr,0,1e5
*if,_m_error,ne,0,:end

/com, (6)  Apply the periodic conditions
/nopr
permach,1                  !  applies periodic conditions to model sides
/out,b_mach2d,out,,append


/com,  (7) Connect the two models
_mg1=1
mvrotor,,1
/out,b_mach2d,out,,append


/com, (8)  Build the winding
*if,arg2,eq,0,then
 *get,_targ1,parm,w_file,type
 *if,_targ1,eq,3,then
  b_wndsc,w_file  !  builds the winding
 *endif
*endif

abbr,,mabbr,mac           !   restores the ToolBar


:end
_mg1=0
/out