
! script name: lathe.d/profile
! topic: roughing - profile cutting

!A
!A NAME:
!A
!A 	Profile: roughing cycle
!A
!A DESCRIPTION:
!A
!A 	This script creates a group of paths spaced at equalized
!A 	distances to remove stock in length and diameter.
!A
!A~ image location - images/profilecycle.gif
!A
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the tool for profiling.
!B
!B 	NUMBER OF CUTS: Enter the number of passes.
!B
!B 	FINISH ALLOWANCE: Enter the diameter and length
!B 		amount of material to be left for a finish pass.
!B
!B 	PICK CONTOUR: Pick the contour to be roughed.
!B
!B	If a tool is not selected the following questions will appear:
!B
!B 	TOOL NOSE RADIUS: Enter the tool nose radius of the tool
!B 		to be used.
!B
!B 	TOOL QUADRANT: Enter the tool nose quadrant.
!B
!C NOTES:
!C
!C	lathe.d/setvals sets user definable constants.
!C
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: rough ")

COM /Rough/ Allow_delta,Conv,Depth_code,Cut_type$[18],Rough$[18],Stock$[18]
DIM Destination$[256], Answer$[256], Rough_name$[40]
DIM Name$[256], Datastr$[1024], Tool_name$[256]
INTEGER Id_contour,Dp_code

LOAD "lathe.d/setvals:profile"
Restart:!
ActivateUndo
Animate_tool=1
Current_pen= CurrentColor
Work= CurrentWorkplane
StdWorkplane
Id_contour= 0


Tool_assoc=0
Tool_val=FindNamedVar("tool association",WsToolVars)
IF ValidGeometry(Tool_val) THEN Tool_assoc=GetVariableVal(Tool_val)

Start:!
IF NOT Tool_assoc THEN
	GOSUB Get_tool_data
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
	IF Stat THEN
		Tool_assoc=0
		GOTO Start
	ELSE
Tool_ques:!
		Stat=SetInputFormat("a99","")
		Stat=GetValue("PATH TOOL",Answer$)
		IF (ExitScript(Destination$)) THEN Leave
		IF Answer$= "CANCEL" THEN
			LowerSelectBox
			Tool_assoc=0
			GOTO Start
		END IF
		Tool=FindUserData(Answer$,WsToolData)
		Tnose_rad=GetUserValue(Tool,"Tool nose radius:")
		Quad=GetUserValue(Tool,"Tool quadrant:")
		LowerSelectBox
	END IF
END IF

Stat=SetInputFormat("n4=","")
Ent=CreateVariable("tool nose radius",WsLatheVars,Tnose_rad)
Ent=CreateVariable("tool quadrant",WsLatheVars,Quad)

Ncuts=NamedVarVal("number of cuts",WsLatheVars,3)
Stat=SetEqualsValue(Ncuts)
Ncuts=GetValue("NUMBER OF CUTS",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("number of cuts",WsLatheVars,Ncuts)

Dia_allow=NamedVarVal("diameter fin allowance",WsLatheVars,.04)
Stat=SetEqualsValue(Dia_allow)
Dia_allow=GetValue("DIAMETER FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("diameter fin allowance",WsLatheVars,Dia_allow)

Len_allow=NamedVarVal("length fin allowance",WsLatheVars,.02)
Stat=SetEqualsValue(Len_allow)
Len_allow=GetValue("LENGTH FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("length fin allowance",WsLatheVars,Len_allow)

Dia_stock=NamedVarVal("diameter stock",WsLatheVars,1)
Stat=SetEqualsValue(Dia_stock)
Dia_stock=GetValue("TOTAL STOCK IN DIAMETER",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("diameter stock",WsLatheVars,Dia_stock)

Len_stock=NamedVarVal("length stock",WsLatheVars,.5)
Stat=SetEqualsValue(Len_stock)
Len_stock=GetValue("TOTAL STOCK IN LENGTH",Answer$)
IF ExitScript(Destination$) THEN Leave
Ent=CreateVariable("length stock",WsLatheVars,Len_stock)

Pick_loop:!
Stat= SetInputFormat("n4a1",",N,")
Contour= PickGroupEnt("PICK CONTOUR,  New tool ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN Restart
IF NOT ValidGeometry(Contour) THEN Pick_loop

Closed=IsContClosed(Contour)
IF Closed THEN Pick_loop

Calc_start_end:!
Side= ContQuadrantSign(Contour,Quad)
IF Side= 1 THEN
	Pen_color=WsConvCutPen
ELSE
	Pen_color=WsClimbCutPen
END IF
Stat= SetCurrentColor(Pen_color)
Path= LatheOffsetContour(Contour,ABS(Tnose_rad),0.0,Dia_allow,Len_allow,Side,Quad,WsForward,WsMinLook)


Ploop:Psafe=XyzInput("PICK STARTING",WsXyOnly)
IF (ExitScript(Destination$)) THEN 
	Deletepaths(Path)
	GOTO Leave
END IF
Xs= XPointValue(Psafe)
Ys= YPointValue(Psafe)

PlotOff
Rough_path= LatheProfileCycle(Path,Psafe,Dia_stock,Len_stock,Ncuts,Quad)
PlotOn
Stat= DeleteEntity(Psafe)
Deletepaths(Path)
Stat= PlotEntity(Rough_path)
P1= LthToolInterfere(Tool,Rough_path,Contour)
IF ValidGeometry(P1) THEN
	Stat= DeleteEntity(P1)
ELSE
	IF Animate_tool THEN
		Work_trans= CurrentWorkplane
		Stat= AnimateTool(Tool,Rough_path,Work_trans)
	END IF
END IF

IF ValidGeometry(Rough_path) THEN

	Stat= GroupName(Rough_path,Rough_name$)
	Stat= GroupName(Contour,Name$)
	Datastr$=" TURNING ROUGH "&CHR$(10)&" "&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="NAME: "&Rough_name$&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="TYPE:  PROFILE"&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="CONTOUR: "&Name$&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="CONTOUR TYPE: "&RPT$("OD",NOT Id_contour)&RPT$("ID",Id_contour)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="TOOL NOSE RAD: "&VAL$(ABS(Tnose_rad))&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="TOOL QUADRANT: "&VAL$(Quad)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="NUMBER OF CUTS: "&VAL$(Ncuts)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="DIA ALLOWANCE: "&VAL$(Dia_allow)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="LEN ALLOWANCE: "&VAL$(Len_allow)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="DIA STOCK: "&VAL$(Dia_stock)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="LEN STOCK: "&VAL$(Len_stock)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="STARTING X: "&VAL$(Xs)&CHR$(10)
	Datastr$[LEN(Datastr$)+1]="STARTING Y: "&VAL$(Ys)&CHR$(10)

	Roughdata=FindUserData(Rough_name$,WsTurnRough)
	IF NOT ValidGeometry(Roughdata) THEN
       		 Roughdata=ReadUserData(WsTurnRough,Rough_name$,Datastr$)
	ELSE
		 Stat= DeleteEntity(Roughdata)
       		 Roughdata=ReadUserData(WsTurnRough,Rough_name$,Datastr$)
	END IF

	Stat= AssocUserData(Rough_path,Roughdata,WsTurnRough)
	IF Tool_assoc THEN Stat= AssociateTool(Rough_path,Tool)

	ActivateUndo
        Stat= AdvanceUndoNumber("profile")
        Stat= AddItemToUndo(WsUBCreateMode,Roughdata)
        Stat= AddGroupToUndo(WsUBCreateMode,Rough_path,1,WsTrue)

END IF

GOTO Pick_loop

Get_tool_data:!
	Tnose_rad= NamedVarVal("tool nose radius",WsLatheVars,.031)
	Stat= SetInputFormat("n4=","")
	Stat= SetRadiusEquals(Tnose_rad)
	Tnose_rad= InputRadius("TOOL NOSE RADIUS")
	IF ExitScript(Destination$) THEN Leave
	Tnose_rad= ABS(Tnose_rad)

	Quad= NamedVarVal("tool quadrant",WsLatheVars,1.)
	Stat=SetEqualsValue(Quad)
	Stat= SetInputFormat("n4=","")
	Quad= GetValue("TOOL QUADRANT",Answer$)
	IF ExitScript(Destination$) THEN Leave
RETURN

Leave:!
PlotOn
Stat=SetCurrentColor(Current_pen)
IF ValidGeometry(Work) THEN Stat=SetWorkplane(Work)
Stat=SetInputFormat("n4","")
LowerSelectBox
IF NOT LEN(Destination$) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
Deletepaths:SUB Deletepaths(Path)
IF ValidGeometry(Path) THEN 
	Stat= DeleteGroup(Path,1)
	Stat= DeleteEntity(Path)
END IF
SUBEND

