
! script name: amo.d/amoexecute

!A
!A NAME:
!A
!A 	EXECUTE AMO
!A
!A DESCRIPTION:
!A
!A 	This is a CUSTOM script that will create Automated Manufacturing
!A	Output (AMO) for a specific post processor.  The script is specific
!A	to one post processor.  A command_file is automatically created for
!A	this post and then executed.  The command_file can be
!A	edited and, rerun using TurboPost. NOTE: DO NOT rerun the command_file
!A	using AMO.  The changes that were made will be overwritten.
!A
!B INSTRUCTIONS:
!B
!B	The areas in here are broken into SUBroutines.  There are three
!B	main areas that are the most customizable.
!B	  1) SUB Toolch - This is where the output for a tool change is 
!B	                  created. The startup is also output in this section.
!B	  2) SUB Drilling - This area is designed to output a certain
!B	                    canned_cycle for a certain tool type (i.e. REAMER)
!B	  3) SUB Milling - This area outputs milling data. It will split
!B	associated data into: Round Milling, Plunging, Surfaces, Path
!B	Groups and, Contours.
!B	  4) SUB Threading - This area is designed to output a call to
!B	             helical_mill_cut, the threading cycle in Tpost.
!B
!B	There are comments throughout the script to better explain
!B	specific areas.
!B
!# see also - amo.d/execute.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: execute amo ")

! Standard Variables are in /Amo_vars1/ and /Amo_vars2/
! Custom Variables are in /Amo_defaults/ /Amo_defaults2/

COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$[128],Last_tool$[256]
COM /Amo_defaults/ Zfeed,Ret_3d
COM /Amo_defaults2/ Lencomp$[64],Diacomp$[64]

DIM Answer$[256],Destination$[256],Dir$[1024]
DIM Data_name$[128],Dname$[128],Dstr$[128]
DIM Base_name$[128],Next_tool$[256]

ENTITY Last_point	! Holds the last known position for Optimization
ENTITY NextTool		! Entity To hold the next TOOL in the tasks.
Start_work= CurrentWorkplane
WsEventData= 12345
WsAmoData= 23051

! SWITCHES: These are convenience switches. This will aide in the customization

Use_tdata=0	! 0= Start at 1 and increment tool numbers
		! 1= Use CNC tool-pocket number: from the tool
Do_nxt=0	! 0= No Next_tool and 1= Use Next_tool (Pre-Staging)
One_pkt=0	! Used to determine if a Path Group that has one contour
		! should be cut using mcut_path_grp or contour_cut.
		! 0= Use mcut_path_grp 1= Use contour_cut
Do_safe=0	! For canned cycles where a position to the first location
		! is done before the length offset is applied.
		! 0= No safe start and 1= Pre-position for length offset
One_zone=0      ! Switch. Used for an execute that uses workzones.
                ! ONLY affects the "Many Tasks -> One Zone" scenario.
                ! 0= Output rotary_zero function after every tool.
                !    This is useful so that rotary info is output after a tool breaks
                ! 1= Rotary_zero is output ONLY after the first tool in a zone.
                !    For example; if an E number is output only once.

! This is an area for switch and setting common values to cut down on 
! customization time.  The strings aren't true calcs. They are what is to
! be output to the command file. If these fields are calculated by means other
! than a calculation, then the line must be changed directly in SUB Toolch
! or SUB Milling. Defaults can be added to the COM /Amo_defaults2/.

Lencomp$= "Tool_num"  ! Length offset calc.
Diacomp$= "Tool_num+30"  ! Diameter comp calc.

! DEFAULT SECTION: Defaults can be added and placed in the COM /Amo_defaults/
! line. The COM should be duplicated in the SUB's where it is used.

DeActivateUndo
Units= CurrentUnits
IF Units= WsMetric THEN
	Zfeed= 50	! The mmpm Z feedrate default in SUB Toolch (Endmills)
	Ret_3d= 125	! The Metric Retract_z default in SUB Milling
ELSE
	Zfeed= 2.35	! The ipm Z feedrate default in SUB Toolch (Endmills)
	Ret_3d= 5	! The English Retract_z default in SUB Milling
END IF
Last_tool$=""

! Setup area after script re-entry.

Edata= FindUserData("EVENT INFO",WsGeneralString)
IF ValidGeometry(Edata) THEN
	Start_num= GetUserValue(Edata,"Start number:")
	S_up= GetUserValue(Edata,"Startup:")
	In_post= GetUserValue(Edata,"In post:")
	Stat= DeleteEntity(Edata)
ELSE
	In_post= 0
	Start_num= 1
	S_up= 1
END IF
No_tool=0

Oper_grp= FindNamedGrp("AMO OPERATIONS")	! Task list
IF NOT ValidGeometry(Oper_grp) THEN Leave

CALL Read_material

! If you always want a certain process then hard code All_face to the
! value you want. DON'T use the synergy variable "amo direction" below.
! The 0.0 at the end is a default if the synergy variable "amo direction"
! does not exist (i.e. the process button wasn't pressed).
! VALUES: 0= Many->One / 1= One->Many
All_face=NamedVarVal("amo direction",WsToolVars,0.0)


! WORK ZONES: This section will find all the active work zones.
! Note: Components is the internal name for work zones.

Num_comps= 1
Do_components= 0
CompList= FindUserData("AMO WORK ZONES",WsAmoData)
IF ValidGeometry(Complist) THEN
	Num_comps= GetUserValue(Complist,"Number components:")
	IF Num_comps THEN
		ALLOCATE Comp(Num_comps)
		FOR K= 1 TO Num_comps
			IF CheckUserField(Complist,"Component "&VAL$(K)&":",0) THEN
				Do_components= 1
				Comp(K)=GetUserValue(Complist,"Component "&VAL$(K)&":")
			ELSE
				Comp(K)= -1
			END IF
		NEXT K
	END IF
END IF
StdWorkplane

Number= GroupCount(Oper_grp)

! This is the main section. This area does not really need to be customized.
! it calls the SUBs to do its tasks.

Components_done= 0
Last_comp= 0
FOR Big_pass= 1 TO Num_comps^(All_face AND Do_components) ! Once or Many 
First_tool= 1
Has_events= 0
FOR K= Start_num TO Number  ! For each task

	EntData= GroupElement(Oper_grp,K)		! Get task
	Stat= GetUserDataName(EntData,Data_name$)	! Find name
	Is_tool= (UserDataType(EntData)= WsToolData)

! RECORD POST NAME: This is where the name of the post_processor is 
! recorded. DO NOT change /weber to be /usr/weber. Change only the name
! mill to be <post processor>. Nothing else in this section is to be changed.
	IF NOT In_post THEN
		Dir$= NamedVariableStr("post dir",WsSystemVars,"/usr/weber/post_formats/")
		Found= RecordPostName(FileNameString(Dir$,"AMO"),Units)
		IF NOT Found THEN Leave
		Stat= RemoveDirFiles("./","*","command_file")
		Stat= RemoveDirFiles("./","*","notepad")
		Stat= RemoveFile("command_buffer")
		First= InitCmdMenu(1,0)
		In_post= 1
	END IF

	IF Is_tool THEN 	! If current task is a tool
! Find a delete old group entity for this tool and get tool type
		Oldgroup=FindNamedGrp(Data_name$&" group")
		IF ValidGeometry(Oldgroup) THEN Stat= DeleteEntity(Oldgroup)
		Group= GrpByTool(EntData)
		IF NOT ValidGeometry(Group) THEN End_tool
		Stat=GetUserString(EntData,"Tool type:",Data_type$)

! Checks to see if base name of tool is the same as the last tools base name.
! This will insure a tool_change is not performed unnecessarily.
		Inst_spot= POS(Data_name$,"--")
		IF NOT Inst_spot THEN Inst_spot= LEN(Data_name$)+1
		Base_name$= Data_name$[1;Inst_spot-1]
		Tl_ch= 0
		IF Last_tool$<>Base_name$ THEN Tl_ch=1

		F_tl_num= K*(K<Number)+1
		IF Do_nxt THEN
		   Curr_tl= K*(K<Number)+1
		   LOOP
		      NextTool= GroupElement(Oper_grp,Curr_tl)
		      IF UserDataType(NextTool)<>WsToolData THEN End_loop
		      Stat= GetUserDataName(NextTool,Next_tool$)
                      Nxt_spot= POS(Next_tool$,"--")
                      IF NOT Nxt_spot THEN Nxt_spot= LEN(Next_tool$)+1
		      EXIT IF Base_name$<>Next_tool$[1;Nxt_spot-1]
End_loop:	      Curr_tl= Curr_tl*(Curr_tl<Number)+1
		      EXIT IF Curr_tl= F_tl_num 
		   END LOOP
		END IF

		IF Do_components AND NOT All_face THEN   ! ONE TASK->MANY ZONES
			FOR Cpass= 1 TO Num_comps	
				IF Comp(Cpass)=-1 THEN
					Stat=GetUserString(Complist,"Component "&VAL$(Cpass)&":",Answer$)
					Answer$= Answer$[10]
					EvData= FindUserData(Answer$,WsEventData)
					CALL Output_event(EvData)
				ELSE
					Do_workzones(EntData,NextTool,Group,Data_name$,Data_name$[1;Inst_spot-1],Comp(Cpass),Last_point,1)
					IF No_tool THEN End_tool
				END IF
			NEXT Cpass
		ELSE
		   IF Do_components THEN 	! MANY TASKS->ONE ZONE
			End_of_list= 0
Check_last_event: 	IF Comp(Big_pass)=-1 THEN
				Has_events= 1
				IF Big_pass=Num_comps THEN
					End_of_list= 1
					GOTO Setup_events
				ELSE
					Big_pass= Big_pass+1
					GOTO Check_last_event
				END IF
			ELSE
Setup_events:			IF Has_events THEN
					FOR C= Last_comp+1 TO Big_pass-(NOT End_of_list)
						Stat=GetUserString(Complist,"Component "&VAL$(C)&":",Answer$)
						Answer$= Answer$[10]
						EvData= FindUserData(Answer$,WsEventData)
						CALL Output_event(EvData)
					NEXT C
				END IF
				Last_comp= Big_pass
				Has_events= 0
				IF End_of_list THEN End_tool
				Newzero= ((NOT One_zone) OR (One_zone AND First_tool))
				Do_workzones(EntData,NextTool,Group,Data_name$,Data_name$[1;Inst_spot-1],Comp(Big_pass),Last_point,Newzero)
			END IF
		   ELSE				! DO TASKS ONLY
			Tool_cycle(EntData,NextTool,Group,Data_name$,Data_name$&" group",Last_point,0,0)
		   END IF
		END IF
	ELSE	! If current task is an event
		CALL Output_event(EntData)
	END IF

End_tool:No_tool=0
IF Do_components THEN Stat= DeleteEntity(Group)
First_tool=0
NEXT K
NEXT Big_pass

End_codes:!
IF In_post THEN 	! Add the end of the program if it was opened.
	Stat=OutputCmdBuff("MACRO: end_codes",1)
	Stat=OutputCmdBuff("ASSIGN: Batch= 10;",0)
	Stat=OutputCmdBuff("ASSIGN: Setup_time= 5;",0)
	Stat=OutputCmdBuff("ASSIGN: Machine_cost= 75;",0)
	Stat=OutputCmdBuff("MACRO: anlze_cost",0)
	UpdateCmdFile

	StdWorkplane
	Command_type=InterpretCmd("EXEC: ")	   ! EXECUTE Command File
	Stat=ListTapeFile("tapefile","tapelist",1) ! Make tapelist file

	Command_type=InterpretCmd("EXIT: ")	   ! EXIT Post
END IF

Leave:!
IF ValidGeometry(Last_point) THEN Stat= DeleteEntity(Last_point)
IF ValidGeometry(Start_work) THEN Stat=SetWorkplane(Start_work)
IF (NOT LEN(Destination$)) THEN
         LOAD "typecontrol"
ELSE
         LOAD Destination$
END IF
END

SUB Output_event(EvData)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
DIM Event_type$[256],Event_data$[128]
	Stat= GetUserString(EvData,"Event type:",Event_type$)
	Stat= GetUserString(EvData,"Event data:",Event_data$)
	Stat= OutputCmdBuff(Event_type$&": "&Event_data$,New_cb)
	UpdateCmdFile
	New_cb= 1
SUBEND

SUB Do_newzero(Component)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe

! This calculates the rotary movement and new workplane position
! The line rotary_zero can be change to reflect a differnet work zone
! change MACRO.

WsPplane= 7746279
DIM Pne$[256],Hnum$[256]

! As of to 2/3/98
	G1= GrpByComponent(Component)
	IF NOT GroupCount(G1) THEN Old_way
	Group1= GrpByEntType(G1,WsHoleEnt)
	Stat= DeleteEntity(G1)
	IF NOT GroupCount(Group1) THEN Old_way
	FOR J= 1 TO GroupCount(Group1)
		Hle= GroupElement(Group1,J)
		Pplane=GetAssocEnt(Hle,WsPplane)
		IF ValidGeometry(Pplane) THEN Out_zero
	NEXT J

! Previous to 2/3/98

Old_way:!
	Pplane= FindUserData(VAL$(Component),WsPplane)
	IF NOT ValidGeometry(Pplane) THEN SUBEXIT
        Group1= GrpByProp(Pplane,WsPplane)
        IF NOT ValidGeometry(Group1) THEN SUBEXIT
	IF NOT GroupCount(Group1) THEN Leave
	Hle= GroupElement(Group1,1)

Out_zero:!
	Hnum$= VAL$(HoleNumber(Hle))

	Stat=OutputCmdBuff("ASSIGN: Start_hole= "&Hnum$&"; Holestartpt",New_cb)
	Stat=OutputCmdBuff("ASSIGN: Zone_num= "&VAL$(Component)&";",0)
	Stat=OutputCmdBuff("MACRO: rotary_zero",0)
	New_cb= 0

Leave:	Stat= DeleteEntity(Group1)
SUBEND

SUB Do_cycle(EntData,Group,Last_point)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$,Last_tool$

	SELECT Data_type$    ! Determines tool cycle based upon the tool type.
	CASE "ENDMILL","TAPERMILL","BULLMILL","BALLMILL","CHAMFERMILL"
		Milling(EntData,Group,Last_point)
	CASE "CENTER DRILL","TWIST DRILL","COUNTERBORE","COUNTER SINK","REAMER","TAP","BORING BAR"
		Drilling(EntData,Group,Last_point)
	CASE "THREADER"
		Threading(EntData,Group,Last_point)
	CASE ELSE
		No_tool= 1
		SUBEXIT
	END SELECT
SUBEND

SUB Do_workzones(EntData,NextTool,Group,Data_name$,Base_name$,Component,Last_point,Newzero)

DIM Dname$[256]

Dname$=Data_name$&" group Component "&VAL$(Component)	
Oldgroup=FindNamedGrp(Dname$)
Stat= DeleteEntity(Oldgroup)
Grp2= GrpByComponent(Group,Component)
IF ValidGeometry(Grp2) THEN
	Tool_cycle(EntData,NextTool,Grp2,Base_name$,Dname$,Last_point,Component,Newzero)
END IF
SUBEND

SUB Tool_cycle(EntData,NextTool,Group,Data_name$,Dname$,Last_point,Component,Newzero)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$,Last_tool$
COM /Amo_defaults/ Zfeed,Ret_3d
COM /Amo_defaults2/ Lencomp$,Diacomp$

	IF Tl_ch OR S_up THEN
		Toolch(EntData,NextTool,Last_point)
		Tl_ch= 0
		New_cb=0
		Last_tool$= Data_name$
	ELSE
		New_cb=1
	END IF
	Stat= ChangeGrpName(Group,Dname$)
	IF Newzero THEN CALL Do_newzero(Component)
	Do_cycle(EntData,Group,Last_point)
	IF NOT No_tool THEN
		UpdateCmdFile
		New_cb= 1
	END IF
SUBEND

SUB Toolch(EntData,NextTool,Last_point)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$,Last_tool$
COM /Amo_defaults/ Zfeed,Ret_3d
COM /Amo_defaults2/ Lencomp$,Diacomp$

DIM Data_id$[128],Tool_image$[128],Tool_file$[256],Tool_name$[256]

Stat=GetUserString(EntData,"Tool id name:",Data_id$)  ! Get Tool Name
Stat=GetUserString(EntData,"Tool image:",Tool_image$) ! Get Animation Image Name
Stat=GetUserString(EntData,"Tool file name:",Tool_file$) ! Get Animation Image Name
Stat= GetUserDataName(EntData,Tool_name$)
Tool_dia=GetUserValue(EntData,"Diameter:")	! Get Tool Diameter (Optional)
Tool_rad= Tool_dia/2				! Set Tool Radius (Optional)

Stat=OutputCmdBuff("ASSIGN: Tool_desc= """&Data_id$&""";",1)
Stat=OutputCmdBuff("ASSIGN: Tool_symbol= """&Tool_image$&""";",0)
Stat=OutputCmdBuff("ASSIGN: Tool_file= """&Tool_file$&""";",0)
Stat=OutputCmdBuff("ASSIGN: Syn_tool_desc= """&Tool_name$&""";",0)
IF S_up THEN		! For startup macro only
	Stat= OutputCmdBuff("ASSIGN: Prog_desc= ""AMO"";",0)
	Last_point= PointXYZ(0,0,0)	! Set to zero. Can be any X,Y,Z Values.
END IF

! CALL Output_tool_number(EntData,NextTool)
! Stat=OutputCmdBuff("ASSIGN: Length_comp= "&Lencomp$&";",0)

CALL Get_feed_speed(EntData,Feed,Speed)

SELECT Data_type$
CASE "ENDMILL","TAPERMILL","BULLMILL","BALLMILL","CHAMFERMILL"
	Stat=OutputCmdBuff("ASSIGN: Tfeed= "&VAL$(Feed)&";",0)
	Stat=OutputCmdBuff("ASSIGN: Zfeed= "&VAL$(Zfeed)&";",0)
CASE ELSE
	Stat=OutputCmdBuff("ASSIGN: Zfeed= "&VAL$(Feed)&";",0)
END SELECT

Stat=OutputCmdBuff("ASSIGN: Speed= "&VAL$(Speed)&";",0)
IF Do_safe THEN
	Stat=OutputCmdBuff("ASSIGN: Tool_ch= 1;",0)
END IF

IF S_up THEN
	Stat= OutputCmdBuff("MACRO: s_up",0)	
ELSE
	Stat= OutputCmdBuff("MACRO: tool_change",0)	
END IF
S_up= 0
SUBEND

SUB Output_tool_number(EntData,NextTool)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe

! This subroutine is where the tool number is output.  There are options at
! the beginning of the script to help configure most systems. This section
! will output Startup or tool_change tool numbers.  Also included is
! the ability to do pre-staging. Also, if the tool numbers are specified by
! the CNC tool-pocket number: field in the tool then this number is output.

! The On_last_tool section is important when the last tool change is being done.
! Such as setting Next_tool to the first tool OR setting it to zero.
! By default this switch is NOT used.  It is provided for convenience.
! It can only be used when using Next_tool capabilities
IF ValidGeometry(NextTool) THEN
	On_last_tool= 0
	Oper_grp= FindNamedGrp("AMO OPERATIONS")	! Task list
	Ent_is_which=ElementGrpNum(Oper_grp,EntData)
	Nxt_is_which=ElementGrpNum(Oper_grp,NextTool)
	IF Ent_is_which>=Nxt_is_which THEN On_last_tool= 1
END IF

IF NOT Use_tdata THEN			! Do sequential tools output
  IF Do_nxt THEN				  ! Do Pre- Staging tools
    IF S_up THEN
	Stat=OutputCmdBuff("ASSIGN: Tool_num= 1;",0)	      ! On Startup
    ELSE
	Stat=OutputCmdBuff("ASSIGN: Tool_num= Next_tool;",0)  ! On Tool_change
    END IF
    Stat=OutputCmdBuff("ASSIGN: Next_tool= Tool_num+1;",0)    ! Pre_staged Tool
  ELSE						  ! No Pre-staging tools
    IF S_up THEN
	Stat=OutputCmdBuff("ASSIGN: Tool_num= 1;",0)	      ! On Startup
    ELSE
	Stat=OutputCmdBuff("ASSIGN: Tool_num= Tool_num+1;",0) ! On Tool_change
    END IF
  END IF
ELSE					! Get Pocket numbers from tool entity
    Tnum=GetUserValue(EntData,"CNC tool-pocket number:")
    IF NOT Tnum THEN			  ! Blank tool number in tool entity
	IF Do_nxt THEN				  ! Pre-Staging tools
	  IF S_up THEN
	    Stat=OutputCmdBuff("ASSIGN: Tool_num= 1;",0)	   ! On Startup
	  ELSE
	    Stat=OutputCmdBuff("ASSIGN: Tool_num= Next_tool;",0)! On Tool_change
	  END IF
	ELSE					  ! No Pre-staging tools
	  IF S_up THEN
	    Stat=OutputCmdBuff("ASSIGN: Tool_num= 1;",0)	! On Startup
	  ELSE
	    Stat=OutputCmdBuff("ASSIGN: Tool_num= Tool_num+1;",0) ! Tool_change
	  END IF
	END IF
    ELSE
	Stat=OutputCmdBuff("ASSIGN: Tool_num= "&VAL$(Tnum)&";",0) ! Tool#
    END IF
    IF Do_nxt THEN
	Nxttl=GetUserValue(NextTool,"CNC tool-pocket number:") ! Next tool
	IF NOT Nxttl THEN
	    Stat=OutputCmdBuff("ASSIGN: Next_tool= Tool_num+1;",0)  ! Blank tool
	ELSE
	    Stat=OutputCmdBuff("ASSIGN: Next_tool= "&VAL$(Nxttl)&";",0) ! Tool#
	END IF
    END IF
END IF
SUBEND

SUB Milling(EntData,Group,Last_point)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$[128],Last_tool$[256]
COM /Amo_defaults/ Zfeed,Ret_3d
COM /Amo_defaults2/ Lencomp$[64],Diacomp$[64]

DIM Dname$[128],Name$[128],Mill_dir$[32],Plungename$[256],Data_name$[256]
ENTITY PlungGrp

! This is the section that describes what to do with endmills.
! The Group of geometry is divided into individual groups and then
! the different types of functions can be ordered. NOTE: DO NOT
! reorder the group creation. Only reorder the milling functions.

Stat= GroupName(Group,Data_name$)
Tool_dia=GetUserValue(EntData,"Diameter:")
Stat= OutputCmdBuff("ASSIGN: Dia_comp= "&Diacomp$&";",New_cb)
New_cb= 0
Comp_dir= 0
Done_finish= 0

HoleGrp=GrpByEntType(Group,WsHoleEnt)
IF ValidGeometry(HoleGrp) THEN
        MachGrp=SubtractGrps(Group,HoleGrp)
        First=1
        FOR Hole_ent=1 TO GroupCount(HoleGrp)
                E1=GroupElement(HoleGrp,Hole_ent)
                Hdia=EntityValue(E1,WsHoleDia)
                IF Hdia=Tool_dia THEN
                        IF First THEN
				Plungename$= Data_name$&" -- Plunge"
				PlungeGrp= FindNamedGrp(Plungename$)
				IF ValidGeometry(PlungeGrp) THEN
					Stat= DeleteEntity(PlungeGrp)
				END IF
                                PlungeGrp=CreateGroup(Plungename$)
                                First=0
                        END IF
                        Stat=AddToGroup(PlungeGrp,E1)
                END IF
        NEXT Hole_ent
        IF ValidGeometry(PlungeGrp) THEN
                TmpGrp=SubtractGrps(HoleGrp,PlungeGrp)
                Stat= DeleteEntity(HoleGrp)
                IF ValidGeometry(TmpGrp) THEN
                        IF GroupCount(TmpGrp) THEN HoleGrp= TmpGrp
                END IF
        END IF
ELSE
        MachGrp= Group
END IF
IF NOT ValidGeometry(MachGrp) THEN Do_passes
CpathsGrp= GrpByEntType(MachGrp,WsCPathFileEnt)
IF ValidGeometry(CpathsGrp) THEN
        GeomGrp= SubtractGrps(MachGrp,CpathsGrp)
ELSE
        GeomGrp= MachGrp
END IF
IF NOT ValidGeometry(GeomGrp) THEN Do_passes
PathsGrp= FindGrpsByEle(GeomGrp,WsTempGroup)
IF NOT ValidGeometry(PathsGrp) THEN Do_passes

I= 1
First= 1
LOOP
EXIT IF NOT ValidGeometry(PathsGrp)
Number= GroupCount(PathsGrp)
EXIT IF I>Number
E1= GroupElement(PathsGrp,I)
EXIT IF NOT ValidGeometry(E1)
PathGroup= ParentGroup(E1,WsPathGroup)
IF ValidGeometry(PathGroup) THEN
	IF One_pkt AND GroupCount(PathGroup)=1 THEN Next_path
	Num2= GroupCount(PathGroup)
	FOR Each_path= 1 TO Num2
		E12= GroupElement(PathGroup,Each_path)
		E12_num= ElementGrpNum(PathsGrp,E12)
		Stat= RemoveFromGrp(PathsGrp,E12_num)
	NEXT Each_path
	IF First THEN
		PathGrpGrp= CreateGroup("AMOPATH GRPS",WsPathGroup)
		First= 0
	END IF
	Stat= AddToGroup(PathGrpGrp,PathGroup)
ELSE
Next_path: I= I+1
END IF
END LOOP

Do_passes:! ************ BEGIN Individual milling functions ****************
! Reorder these GOSUB's in the order they should be performed
! in case a single tool is performing multiple milling functions.
! to get rid of a cycle the GOSUB can be commented out.

GOSUB Plunge_holes
GOSUB Cut_surfaces
GOSUB Cut_pathgroups
GOSUB Cut_contours
GOSUB Round_cut
! ************************* END Individual milling functions *****************

End_mill:! Do not touch this section of Deletes
IF ValidGeometry(HoleGrp) THEN Stat= DeleteEntity(HoleGrp)
IF ValidGeometry(Group) THEN Stat= DeleteEntity(Group)
IF ValidGeometry(MachGrp) THEN Stat= DeleteEntity(MachGrp)
IF ValidGeometry(GeomGrp) THEN Stat= DeleteEntity(GeomGrp)
IF ValidGeometry(PathsGrp) THEN Stat= DeleteEntity(PathsGrp)
IF ValidGeometry(PathGrpGrp) THEN Stat= DeleteEntity(PathGrpGrp)
SUBEXIT

Plunge_holes:! ************ BEGIN Plunging function ************************
IF ValidGeometry(PlungeGrp) THEN CALL Drilling(EntData,PlungeGrp,Last_point)
RETURN
! ************************** END Plunging function *************************

Cut_surfaces:! ************ BEGIN Surface function *************************
IF ValidGeometry(CpathsGrp) THEN
	Num_cpaths= GroupCount(CpathsGrp)
	Stat= OutputCmdBuff("ASSIGN: Retract_z= "&VAL$(Ret_3d)&";",0)
	FOR I= 1 TO Num_cpaths
		E1= GroupElement(CpathsGrp,I)
		Stat=CPathFileName(E1,Name$)
		Cpath3d=GetAssocEnt(E1,Ws3dCpath)
                IF ValidGeometry(Cpath3d) THEN Zig_zag= GetUserValue(Cpath3d,"ZIGZAGS:")
		Stat=OutputCmdBuff("ASSIGN: Cutter_file= """&Name$&""";",0)
		IF Zig_zag THEN
                        Stat=OutputCmdBuff("MACRO: cut_zigzag",0)
		ELSE
			Stat=OutputCmdBuff("MACRO: cut_surface",0)
                END IF
	NEXT I
	Stat= DeleteEntity(CpathsGrp)
END IF
RETURN
! *************************** END Surface function **************************

Cut_pathgroups:! ******* BEGIN Cut_pathgroups section **********************
IF ValidGeometry(PathGrpGrp) THEN
	IF NOT Done_finish THEN
		Stat= OutputCmdBuff("ASSIGN: Finish_depth= 0;",0)
		Stat= OutputCmdBuff("ASSIGN: Depth_of_cut= Finish_depth;",0)
		Done_finish= 1
	END IF
	Num2= GroupCount(PathGrpGrp)
	FOR Per_path= 1 TO Num2
		PathGroup= GroupElement(PathGrpGrp,Per_path)
		Stat= GroupName(PathGroup,Dname$)
		Stat= OutputCmdBuff("ASSIGN: Group_name= """&Dname$&""";",0)
		Stat= OutputCmdBuff("MACRO: mcut_path_grp",0)
	NEXT Per_path
	Num2= GroupCount(PathGroup)
	LastPath= GroupElement(PathGroup,Num2)
	IF ValidGeometry(Last_point) THEN Stat= DeleteEntity(Last_point)
	Last_point= ContEndPt(LastPath)
END IF
RETURN
! *********************** END Cut_pathgroups section ***********************

Cut_contours:! ********** BEGIN Cut_contours section ***********************
! This section will describe how to cut a single contour
IF ValidGeometry(PathsGrp) THEN
	Num_paths= GroupCount(PathsGrp)
	IF NOT Num_paths THEN RETURN
	IF NOT Done_finish THEN
		Stat= OutputCmdBuff("ASSIGN: Finish_depth= 0;",0)
		Stat= OutputCmdBuff("ASSIGN: Depth_of_cut= Finish_depth;",0)
		Done_finish= 1
	END IF
	FOR I= 1 TO Num_paths
		E1= GroupElement(PathsGrp,I)
		Stat= GroupName(E1,Name$)

! If the path is a POCKET OR AN OFFSET then find climb or conventional milling
! to set the comp_dir variable. If the path is NOT DEFINED AS A POCKET OR AN 
! OFFSET then call a routine for no_comp. The sections for comp_path and 
! No_comp_path are at the end of this SUB.

		OffsetBdy= GetAssocEnt(E1,WsOffsetBdy)
		IF NOT ValidGeometry(OffsetBdy) THEN
			OffsetBdy= GetAssocEnt(E1,WsPocketBdy)
			IF ValidGeometry(Offsetbdy) THEN
			   GOSUB Comp_path	! POCKET
			ELSE
			   GOSUB No_comp_path	! NO COMP (i.e. Face Cycle)
			END IF
		ELSE
			GOSUB Comp_path 	! OFFSET
		END IF
	NEXT I
	IF ValidGeometry(Last_point) THEN Stat= DeleteEntity(Last_point)
	Last_point= ContEndPt(E1)
END IF
RETURN

Comp_path: Stat= GetUserString(OffsetBdy,"MILL DIRECTION:",Mill_dir$)
	IF Mill_dir$="Climb" THEN
                IF Comp_dir=41 THEN Do_path
                Comp_dir=41
                Stat= OutputCmdBuff("ASSIGN: Comp_dir= 41;",0)
        ELSE
                IF Comp_dir=42 THEN Do_path
                Comp_dir=42
                Stat= OutputCmdBuff("ASSIGN: Comp_dir= 42;",0)
        END IF
Do_path:Stat= OutputCmdBuff("ASSIGN: Path_name= """&Name$&""";",0)
	Stat= OutputCmdBuff("MACRO: contour_cut",0)
	RETURN

No_comp_path:! This section can be changed if needed.
	Stat= OutputCmdBuff("ASSIGN: Path_name= """&Name$&""";",0)
	Stat= OutputCmdBuff("MACRO: contour_cut",0)
	RETURN
! ************************** END Cut_contours section ************************

Round_cut:! **************** BEGIN Round_cut section ************************
IF NOT ValidGeometry(HoleGrp) THEN RETURN
Hdiar= 0
Hdepr= 0
OptimizeHoles(HoleGrp,Last_point)

Tool_rad= Tool_dia/2
Stat= OutputCmdBuff("ASSIGN: Cutter_offset= "&VAL$(Tool_rad)&";",0)
Num_holes= GroupCount(HoleGrp)
FOR I= 1 TO Num_holes
	E1= GroupElement(HoleGrp,I)
	Hdia= EntityValue(E1,WsHoleDia)
	Hdepth= EntityValue(E1,WsHoleDepth)

	IF Hdia<>Hdiar THEN
		Stat= OutputCmdBuff("ASSIGN: Diameter= "&VAL$(Hdia)&";",0)
		Clrnce= Hdia/2-Tool_rad
		Stat= OutputCmdBuff("ASSIGN: Clearance= "&VAL$(Clrnce)&";",0)
		Hdiar= Hdia
	END IF
	IF Hdepr<>Hdepth THEN
                Stat= OutputCmdBuff("ASSIGN: Finish_depth= "&VAL$(Hdepth)&";",0)
                Stat= OutputCmdBuff("ASSIGN: Depth_of_cut= Finish_depth;",0)
                Hdepr= Hdepth
        END IF
	Hnum= HoleNumber(E1)
	Stat= OutputCmdBuff("ASSIGN: Start_hole= "&VAL$(Hnum)&"; Holestartpt",0)
	Stat= OutputCmdBuff("MACRO: round_cut",0)
NEXT I
Last_point= ParameterPt(E1,0)
RETURN
! *************************** END Round_cut section *************************
SUBEND

SUB Drilling(EntData,Group,Last_point)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$[128],Last_tool$[256]
COM /Amo_defaults/ Zfeed,Ret_3d
COM /Amo_defaults2/ Lencomp$[64],Diacomp$[64]

DIM Data_name$[128],Dtype$[128]

! This is the area for the Canned Cycles. The Do_safe variable is used to
! call safe_start with a pre_position for most people who use a safe start
! macro.  The Repeats variable used is tool_ch. Change that name to match
! the turbo post's variable name.

OptimizeHoles(Group,Last_point)
Stat= GroupName(Group,Data_name$)

IF Do_safe THEN
	E1=GroupElement(Group,1)
	Hnum=HoleNumber(E1)
	Stat=OutputCmdBuff("ASSIGN: Start_hole= "&VAL$(Hnum)&";  Holestartpt",New_cb)
	Stat=OutputCmdBuff("MACRO: safe_start  Repeats= (Tool_ch<>0);",0)
	New_cb= 0
END IF

Stat= OutputCmdBuff("ASSIGN: Group_name= """&Data_name$&""";",New_cb)
Stat= OutputCmdBuff("ASSIGN: Groupon",0)

SELECT Data_type$
CASE "CENTER DRILL"
	Stat= OutputCmdBuff("DRILL: Allholes",0)

! This section for a TWIST DRILL is to seperate the case where
! a spot drill is used.  Spot drills use a DRILL cycle and a TWIST DRILL
! will use a PECK_DRILL cycle.

CASE "TWIST DRILL"
	IF CheckUserField(EntData,"Drill type:",1) THEN
                Stat= GetUserString(EntData,"Drill type:",Dtype$)
		SELECT Dtype$
		CASE "SPOT"
			Stat= OutputCmdBuff("DRILL: Allholes",0)
		CASE ELSE
			Stat= OutputCmdBuff("PECK_DRILL: Allholes",0)
		END SELECT
	ELSE
                Stat= OutputCmdBuff("PECK_DRILL: Allholes",0)
        END IF
CASE "ENDMILL","TAPERMILL","BULLMILL","BALLMILL","CHAMFERMILL"
	Stat= OutputCmdBuff("DRILL: Allholes",0)
CASE "COUNTERBORE"
	Stat= OutputCmdBuff("COUNTERBORE: Allholes",0)
CASE "COUNTER SINK","REAMER"
	Stat= OutputCmdBuff("BORE(G85): Allholes",0)
CASE "BORING BAR"
	Stat= OutputCmdBuff("BORE(G86): Allholes",0)
CASE "TAP"
	Stat= OutputCmdBuff("TAP: Allholes",0)
END SELECT

Stat= OutputCmdBuff("ASSIGN: Groupoff",0)

Number= GroupCount(Group)
E1= GroupElement(Group,Number)
Last_point= ParameterPt(E1,0)
SUBEND

SUB Threading(EntData,Group,Last_point)
COM /Amo_vars1/ Tl_ch,S_up,New_cb,No_tool,Use_tdata,Do_nxt,One_pkt,Do_safe
COM /Amo_vars2/ Data_type$[128],Last_tool$[256]
COM /Amo_defaults/ Zfeed,Ret_3d
COM /Amo_defaults2/ Lencomp$[64],Diacomp$[64]

DIM Data_name$[256],Dtype$[256]
DIM Holes_name$[256],Paths_name$[256]

! This is the area for the Threading Cycles. The Do_safe variable is used to
! call safe_start with a pre_position for most people who use a safe start
! macro.  The Repeats variable used is tool_ch. Change that name to match
! the turbo post's variable name.

Quote$= CHR$(34)
Stat= GroupName(Group,Data_name$)
Holes_name$= Data_name$&" thread holes"
Paths_name$= Data_name$&" threads"
Oldgroup=FindNamedGrp(Holes_name$)
Stat= DeleteEntity(Oldgroup)
Oldgroup=FindNamedGrp(Paths_name$)
Stat= DeleteEntity(Oldgroup)

HoleGrp=GrpByEntType(Group,WsHoleEnt)
IF ValidGeometry(HoleGrp) THEN
        MachGrp=SubtractGrps(Group,HoleGrp)

	OptimizeHoles(HoleGrp,Last_point)
	Stat= ChangeGrpName(HoleGrp,Holes_name$)
        IF ValidGeometry(MachGrp) THEN
                Stat=ChangeGrpName(MachGrp,Paths_name$)
        END IF

	IF Do_safe THEN
		E1=GroupElement(HoleGrp,1)
		Hnum=HoleNumber(E1)
		Stat=OutputCmdBuff("ASSIGN: Start_hole= "&VAL$(Hnum)&";  Holestartpt",New_cb)
		Stat=OutputCmdBuff("MACRO: safe_start  Repeats= (Tool_ch<>0);",0)
		New_cb= 0
	END IF

	Holes_name$= Quote$&Holes_name$&Quote$
	Stat= OutputCmdBuff("ASSIGN: Group_name= "&Holes_name$&";",New_cb)
	Stat= OutputCmdBuff("ASSIGN: Groupon",0)

	Hdiar= 0
	Hdepr= 0
	Stat= OutputCmdBuff("ASSIGN: Helix_up= 1;",0)

	Tool_dia=GetUserValue(EntData,"Diameter:")
	IF CurrentUnits= WsMetric THEN
		Pitch=GetUserValue(EntData,"Pitch:")
		Tpi= 1/Pitch
		Stat= OutputCmdBuff("ASSIGN: Thread_type=2;",0)
	ELSE
		Tpi=GetUserValue(EntData,"Threads-inch:")
		Pitch= 1/Tpi
		Stat= OutputCmdBuff("ASSIGN: Thread_type=1;",0)
	END IF
	Stat= OutputCmdBuff("ASSIGN: Pitch= "&VAL$(Pitch)&";",0)
	Stat= OutputCmdBuff("ASSIGN: Tpi= "&VAL$(Tpi)&";",0)

	Thread_len=GetUserValue(EntData,"Thread length:")
	Stat= OutputCmdBuff("ASSIGN: Thread_length= "&VAL$(Thread_len)&";",0)

	Num_threads= Thread_len/Pitch
	Tool_dia=GetUserValue(EntData,"Diameter:")
	Tool_rad= Tool_dia/2
	Stat= OutputCmdBuff("ASSIGN: Cutter_offset= "&VAL$(Tool_rad)&";",0)
	Num_holes= GroupCount(HoleGrp)
	New_cb= 0

	FOR I= 1 TO Num_holes
		E1= GroupElement(HoleGrp,I)
		Hdia= EntityValue(E1,WsHoleDia)
		Hdepth= EntityValue(E1,WsHoleDepth)

		IF Hdia<>Hdiar THEN
			Clrnce= Hdia/2-Tool_rad
			Stat= OutputCmdBuff("ASSIGN: Diameter= "&VAL$(Hdia)&";",0)
			Stat= OutputCmdBuff("ASSIGN: Clearance= "&VAL$(Clrnce)&";",0)
			Hdiar= Hdia
		END IF
		IF Hdepr<>Hdepth THEN
			Stat= OutputCmdBuff("ASSIGN: Finish_depth= "&VAL$(Hdepth)&";",0)
			Hdepr= Hdepth
		END IF
		Stat= OutputCmdBuff("ASSIGN: Start_hole= "&VAL$(I)&"; Holestartpt",0)
		Stat= OutputCmdBuff("MACRO: threadmill",0)
	NEXT I
	Stat= OutputCmdBuff("ASSIGN: Groupoff",0)
	Last_point= ParameterPt(E1,0)
	IF ValidGeometry(MachGrp) THEN Machine_thread
ELSE
        MachGrp= Group
Machine_thread:!
	IF GroupCount(MachGrp)=0 THEN SUBEXIT
	IF NOT ValidGeometry(MachGrp) THEN Do_passes
	PathsGrp= FindGrpsByEle(MachGrp,WsTempGroup)
	IF NOT ValidGeometry(PathsGrp) THEN Do_passes

Thread_paths:!
IF ValidGeometry(PathsGrp) THEN
	Num_paths= GroupCount(PathsGrp)
	IF NOT Num_paths THEN RETURN
	IF NOT Done_finish THEN
		Stat= OutputCmdBuff("ASSIGN: Finish_depth= 0;",0)
		Stat= OutputCmdBuff("ASSIGN: Depth_of_cut= Finish_depth;",0)
		Done_finish= 1
	END IF
	FOR I= 1 TO Num_paths
		E1= GroupElement(PathsGrp,I)
		Stat= GroupName(E1,Name$)

		OffsetBdy= GetAssocEnt(E1,WsOffsetBdy)
		IF NOT ValidGeometry(OffsetBdy) THEN
			OffsetBdy= GetAssocEnt(E1,WsPocketBdy)
			IF ValidGeometry(Offsetbdy) THEN
			   GOSUB Comp_path	! POCKET
			ELSE
			   GOSUB No_comp_path	! NO COMP (i.e. Face Cycle)
			END IF
		ELSE
			GOSUB Comp_path 	! OFFSET
		END IF
	NEXT I
	IF ValidGeometry(Last_point) THEN Stat= DeleteEntity(Last_point)
	Last_point= ContEndPt(E1)
END IF
SUBEXIT

Comp_path: Stat= GetUserString(OffsetBdy,"MILL DIRECTION:",Mill_dir$)
	IF Mill_dir$="Climb" THEN
                IF Comp_dir=41 THEN Do_path
                Comp_dir=41
                Stat= OutputCmdBuff("ASSIGN: Comp_dir= 41;",0)
        ELSE
                IF Comp_dir=42 THEN Do_path
                Comp_dir=42
                Stat= OutputCmdBuff("ASSIGN: Comp_dir= 42;",0)
        END IF
Do_path:Stat= OutputCmdBuff("ASSIGN: Path_name= """&Name$&""";",0)
	Stat= OutputCmdBuff("MACRO: contour_cut",0)
	RETURN

No_comp_path:! This section can be changed if needed.
	Stat= OutputCmdBuff("ASSIGN: Path_name= """&Name$&""";",0)
	Stat= OutputCmdBuff("MACRO: contour_cut",0)
	RETURN
END IF

SUBEND

SUB OptimizeHoles(Group,Last_point)
Optimize_holes= NamedVarVal("optimize amo",WsSystemVars,WsTrue)
Opt_factor= 10

IF Optimize_holes THEN
	Stat= DisplayMessage("OPTIMIZING HOLES  factor: "&VAL$(Opt_factor))
! Both optimize routines works well together
!	Stat= ReOrderPtGrp(Group,90,WsForward,WsReverse)
	Stat= OptimizePointPath(Group,Last_point,Opt_factor)
END IF
IF ValidGeometry(Last_point) THEN Stat= DeleteEntity(Last_point)
SUBEND

SUB Read_material
DIM Dir$[256],Namelist$[32000],Namelist2$[32000]

Material=FindUserData("MATERIAL",WsMaterial)
IF ValidGeometry(Material) THEN SUBEXIT

Get_material:!
Stat= NamedVarStr("material dir",WsSystemVars,"/usr/weber/util/tooldata/materials/",Dir$)
Stat=DirectoryNames(Dir$,"*",Namelist$)
Stat= AlphabeticalOrder(Namelist$,",",WsAscending,Namelist2$)
Stat=GeneralListBox("MATERIALS","MATERIAL SELECTED",Namelist2$)
Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT MATERIAL FILE NAME",Answer$)
LowerSelectBox
IF ExitScript(Destination$) THEN Leave
IF Answer$="CANCEL" THEN Leave
Material=ReadUserData(WsMaterial,"MATERIAL",FileNameString(Dir$,Answer$))
IF NOT ValidGeometry(Material) THEN Get_material
SUBEND

SUB Get_feed_speed(EntData,Feed,Speed)
DIM Destination$[256],Answer$[256]
DIM Toolid$[256],Toolmat$[256]
DIM Search1$[256],Search2$[256]
DIM Namelist$[32000],Namelist2$[32000]
DIM Iormm$[64],Sformm$[64]

Get_material:!
Material=FindUserData("MATERIAL",WsMaterial)
IF NOT ValidGeometry(Material) THEN
	CALL Read_material
	GOTO Get_material
END IF

Feed= GetFeedrate(EntData,Material)
Speed= GetSpeed(EntData,Material)

IF Feed AND Speed THEN SUBEXIT

No_feed= (Feed<=0)
No_speed= (Speed<=0)

Is_metric= (CurrentUnits=WsMetric)

Stat= UserDataNames(WsToolData,Namelist$)

Stat=GetUserString(EntData,"Tool type:",Toolid$)
Stat=GetUserString(EntData,"Tool material:",Toolmat$)

Tool_dia=GetUserValue(EntData,"Diameter:")
Tool_length=GetUserValue(EntData,"Gage length:")
F1=GetUserValue(EntData,"Machinability factor:")

IF Is_metric THEN
	Ft_or_m=1000
ELSE
	Ft_or_m=12
END IF

SELECT Toolid$
CASE "ENDMILL","COUNTERBORE","BALLMILL","TAPERMILL","BULLMILL","CHAMFERMILL","THREADER"
	IF Is_metric THEN
		Iormm$="(mmpt)("&Toolmat$&"):"
		Sformm$="(smpm)("&Toolmat$&"):"
	ELSE
		Iormm$="(ipt)("&Toolmat$&"):"
		Sformm$="(sfpm)("&Toolmat$&"):"
	END IF
	SELECT Toolid$
	CASE "COUNTERBORE"
		Search1$="Counterbore/spotface"&Sformm$
		Search2$="Counterbore/spotface"&Iormm$
	CASE "THREADER"
		Search1$="Thread Milling"&Sformm$
		Search2$="Thread Milling"&Iormm$
	CASE ELSE
		Corner_radius=GetUserValue(EntData,"Corner radius:")
		Max_depth_cut=GetUserValue(EntData,"Maximum depth of cut:")
		Search1$="Finish "&Sformm$
		Search2$="Rough "&Iormm$
	END SELECT
	Nflutes=GetUserValue(EntData,"Number flutes:")
	Tdm=GetUserValue(EntData,"Manufacturability:")
	IF Tdm<=.001 THEN Tdm=1.0
	Ipt=GetUserValue(Material,Search2$)
	Factor= Tdm*Ipt*Nflutes
	Surface_speed=GetUserValue(Material,Search1$)

CASE "DRILL","TWIST DRILL","CENTER DRILL","COUNTER SINK","REAMER","BORING BAR"

	IF Is_metric THEN
		Sformm$="(smpm)("&Toolmat$&"):"
	ELSE
		Sformm$="(sfpm)("&Toolmat$&"):"
	END IF

	SELECT Toolid$
	CASE "REAMER"
		Search1$="Reaming"&Sformm$
		Tdm=GetUserValue(Material,"ReamManufacturability:")
	CASE "BORING BAR"
		Search1$="Boring"&Sformm$
		Tdm=GetUserValue(Material,"BoreManufacturability:")
	CASE ELSE
		Drill_angle=GetUserValue(EntData,"Point angle:")
		Search1$="Twist Drill"&Sformm$
		Tdm=GetUserValue(Material,"TdrillManufacturability:")
	END SELECT

	Ipr=GetFeedrate(EntData,Material,2)
	IF Tdm<=.001 THEN Tdm=1.0
	Factor= Tdm*Ipr
	Surface_speed=GetUserValue(Material,Search1$)

CASE "TAP"
	Surface_speed= GetTapSfpm(Tool,Material)
	IF Is_metric THEN
		Ipr= GetUserValue(Tool,"Pitch:")
	ELSE
		Tpi=GetUserValue(Tool,"Threads-inch:")
		Ipr= 1/Tpi
	END IF
	Factor= Ipr
END SELECT

IF No_speed THEN
	Speed=F1*(Ft_or_m*Surface_speed)/(PI*Tool_dia)
END IF
IF No_feed THEN
	Feed=Factor*Speed
END IF

Speed=PROUND(Speed,0)
Feed=PROUND(Feed,-2*(Units=WsInch))

IF No_feed THEN
	IF (Units=WsMetric) THEN
		Stat=PutUserValue(EntData,"Feed(mmpm):",Feed)
	ELSE
		Stat=PutUserValue(EntData,"Feed(ipm):",Feed)
	END IF
END IF
IF No_speed THEN Stat=PutUserValue(EntData,"Speed(rpm):",Speed)

End_tool:!
Leave:SUBEXIT
SUBEND
