
! script name: lathe.d/dualrough
! topic: dual turret roughing - horizontal or radial

!A
!A NAME:
!A
!A 	Rough: dual turret roughing cycle
!A
!A DESCRIPTION:
!A
!A 	This script roughs a contour with an optional stock boundary
!A	with tools on dual turrets.
!A
!A
!A~ image location - images/fouraxisrough.jpg
!B INSTRUCTIONS:
!B
!B	TURRET #1 TOOL: Select the tool for turret #1.
!B
!B	TURRET #2 TOOL: Select the tool for turret #2.
!B
!B 	DEPTH OF CUT: Enter the depth of cut (the distance between
!B		successive passes).
!B
!B 	FINISH ALLOWANCE: Enter the diameter and length amount of material
!B		to be left for a finish pass.
!B
!B      LEAD TURRET DISTANCE: The distance which the primary turret will
!B                              start ahead of the secondary turret.
!B
!B
!B 	PICK CONTOUR: Pick the contour to be roughed.
!B
!B	PICK STOCK CONTOUR: Pick the contour used for the stock.(optional)
!B
!C NOTES:
!C
!C	lathe.d/setvals sets user definable constants.
!C
!C	Stock contour: The stock contour must have no undercuts. It
!C			must be described in the same direction as
!C			the roughing contour. With a stock boundary
!C			the Clearance angle is assumed to be zero.
!C
!# see also - htdocs/help.d/fouraxisturn.htm
!# see also - lathe.d/dialenfin.hlp
!# see also - lathe.d/tooltiptan.hlp

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

DIM Destination$[256], Answer$[256]
DIM Toolname$[256]
DIM Rough$[40]
DIM Stock$[40]
INTEGER Id_contour


ActivateUndo
Rough$=ARGV(1)
IF ARGC>1 THEN
        Rough$=ARGV(1)
ELSE
        Rough$= "long"
END IF
IF ARGC>2 THEN
        Stock$=ARGV(2)
ELSE
        Stock$=""
END IF

Start:!
IF Units= WsMetric THEN
        Conv= 25.4
ELSE
        Conv= 1
END IF
Animate_tool=1
Tool_clearance= .05*Conv
Current_pen= CurrentColor
Work= CurrentWorkplane
StdWorkplane
Id_contour= 0

Reverse=(Rough$[LEN(Rough$)]="r")
Long=(Rough$[1;4]="long")
! 		Cut_method	1= auto cuts  2= fixed cuts
Cut_method= 1	

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
IF Stat THEN Leave
Tool_ques:!
	Stat=SetInputFormat("a99","")
	Stat=GetValue("TURRET #1 TOOL ",Toolname$)
	IF (ExitScript(Destination$)) THEN Leave
	IF Toolname$= "CANCEL" THEN Leave

	Tool1=FindUserData(Toolname$,WsToolData)
	Tnose_rad1=GetUserValue(Tool1,"Tool nose radius:")
	Quad1=GetUserValue(Tool1,"Tool quadrant:")
	LowerSelectBox

Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
IF Stat THEN Leave
Tool_ques:!
        Stat=SetInputFormat("a99","")
        Stat=GetValue("TURRET #2 TOOL ",Toolname$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Toolname$= "CANCEL" THEN Leave

        Tool2=FindUserData(Toolname$,WsToolData)
        Tnose_rad2=GetUserValue(Tool2,"Tool nose radius:")

	IF ABS(Tnose_rad1-Tnose_rad2)> 0.0001 THEN Start
	Quad2=GetUserValue(Tool2,"Tool quadrant:")

	Tnose_rad= Tnose_rad1
	Quad= Quad1
	
        LowerSelectBox

Depth_of_cut=NamedVarVal("depth of cut",WsLatheVars,.1)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Depth_of_cut)
Depth_of_cut=GetValue("DEPTH OF CUT",Answer$)
IF ExitScript(Destination$) THEN Leave
Depth_of_cut=ABS(Depth_of_cut)
Ent=CreateVariable("depth of cut",WsLatheVars,Depth_of_cut)

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)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Lead_dist)
Lead_dist=GetValue("LEAD TURRET DISTANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Lead_dist=ABS(Lead_dist)

Pick_loop:!
Stat= SetInputFormat("n4","")
Contour= PickGroupEnt("PICK CONTOUR",0)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Contour) THEN Pick_loop

Closed=IsContClosed(Contour)
IF Closed THEN Pick_loop

Calc_start_end:!
Id_contour= IdContourCheck(Contour,(Long)+2*(NOT Long),Reverse)
IF Id_contour=2 THEN
	Stat=SetInputFormat("a1",",I,O,")
	Stat=GetValue("Id or Od cycle",Answer$)
	IF ExitScript(Destination$) THEN Leave
	Id_contour= (Answer$="I")
END IF

IF Id_contour THEN
        Pen_color=WsConvCutPen
ELSE
        Pen_color=WsClimbCutPen
END IF
Stat= SetCurrentColor(Pen_color)

if Long THEN
	Uncut_angle= 0.0*(NOT Reverse)+180*(Reverse)
ELSE
	Uncut_angle= 270.0*(NOT Reverse)+90*(Reverse)
END IF
Shift_type= 1*(Long)+ 2*(NOT Long)

PlotOff
Path= LatheToolPtShift(Contour,Tnose_rad,Dia_allow,Len_allow,Shift_type,Id_contour,Quad,Reverse,Uncut_angle)
PlotOn
Stat= PlotEntity(Path)

IF LEN(Stock$) THEN
Spick_loop:!
	Stat= SetInputFormat("n4","")
	Stock_contour= PickGroupEnt("PICK STOCK CONTOUR",0)
	IF (ExitScript(Destination$)) THEN 
		Deletepaths(Path)
		GOTO Leave
	END IF
	IF NOT ValidGeometry(Stock_contour) THEN Spick_loop

	Shift_type= 4*(Long)+ 5*(NOT Long)
	PlotOff
	Stock_path= LatheToolPtShift(Stock_contour,0.0,Tool_clearance,Tool_clearance,Shift_type,Id_contour,Quad,Reverse)
	PlotOn
	IF ValidGeometry(Stock_path) THEN
		Closed=IsContClosed(Stock_path)
		IF Closed THEN Spick_loop
                Stock_copied=1
        END IF
ELSE
	!Stat=SetXyzInput(WsXyzInput)
	Startpt=XyzInput("PICK STARTING",WsXyOnly)
	IF (ExitScript(Destination$)) THEN 
		Deletepaths(Path)
		GOTO Leave
	END IF
END IF

Speed_method= 1

PlotOff
IF ValidGeometry(Stock_path) THEN
	Group= DualTurretRough(Path,Stock_path,0,Depth_of_cut,Lead_dist,Id_contour,Reverse,Long,Cut_method,Speed_method)
ELSE
	Group= DualTurretRough(Path,0,Startpt,Depth_of_cut,Lead_dist,Id_contour,Reverse,Long,Cut_method,Speed_method)
END IF

Rough_path1= GroupElement(Group,1) 
Rough_path2= GroupElement(Group,2) 
Stat= AssociateTool(Rough_path1,Tool1)
Stat= AssociateTool(Rough_path2,Tool2)

Deletepaths(Path)
Stat= DeleteEntity(Startpt)
IF Stock_copied THEN CALL Deletepaths(Stock_path)
PlotOn
Stat= PlotEntity(Rough_path1)
Stat= PlotEntity(Rough_path2)
IF Animate_tool THEN
        Work_trans= CurrentWorkplane
	Method=2
	Speed= 500
	Rapid= 600
	Feed= ToolFeed(Tool1,2,0.012)
	Stat= AnimateSyncPaths(Tool1,Tool2,Rough_path1,Rough_path2,Speed,Feed,Rapid,Method,WsTrue,WsTrue,Lead_tool)
END IF

ActivateUndo
Stat= AdvanceUndoNumber("dual turret rough")
Stat= AddGroupToUndo(WsUBCreateMode,Group,1,WsTrue)

GOTO Pick_loop


Leave:!
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


