
! script name: v4axisturn.d/dualrough
! var_topic: dual turret roughing - horizontal or radial

!A
!A NAME:
!A
!A 	Rough: Four Axis Rough		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates roughing cuts for a dual turret four axis
!A	turning machine tool. The cuts can be longitudinal or radial.
!A	There may be a stock contour.
!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
!B 		distance between successive passes).
!B
!B 	FINISH ALLOWANCE: Enter the diameter and
!B 		length amount of material to be left for a finish pass.
!B
!B	LEAD TURRET DISTANCE: The distance which the
!B 		primary turret will start ahead of the secondary turret.
!B
!B 	PICK CONTOUR: Pick the contour to be roughed.
!B
!B	PICK STOCK CONTOUR: Pick the contour
!B 		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
!C	Bar Stock: When called with the bar stock option, the variational
!C		record is created with default stock parameters. The
!C		defaults are:
!C
!C	Diameter stock: Extra stock above the highest(lowest for id)
!C		point in the contour.
!C
!C	Face stock: Extra stock on the face(back face for reverse)
!C		of the part.
!C
!C	Overcut: Extra cut length added to the contour in the
!C		direction to the cut. For example, if a cutoff
!C		tool is being used, a positive overcut will
!C		cut past the end of the contour. If the part
!C		is being held in a chuck, then a negative
!C		overcut will keep the cut away from the end
!C		of the contour.
!C
!C	The following options are available as free parameters:
!C
!C		Speed method: Calculates rpm from surface speed for the 
!C		              cutting move. The turbopost
!C		              must be in direct rpm mode and there must be
!C		              an S word in the LINEAR and FEED blocks.
!C		              The options are:
!C		              0     No rpm calculations performed.
!C		              1     Rpm calculated at primary turret diameter.
!C		              2	    Rpm calculated at secondary turret diameter.
!C		              3	    Rpm calculated midway.
!C
!C		Sync mode: Determines where the sync points are placed.
!C		           The options are:
!C
!C		           1    sync point placed at start of every cut.
!C			        The primary turret starts its cut first
!C				the secondary turret starts cutting only ehen
!C				the primary turret has reached the lead dist
!C				away.
!C
!C		           2    In addition the the sync points on the start
!C			        of the cut. The primary turret will wait at
!C			        the retract point and both turrets will rapid
!C				back together.
!C
!C			   3    The sync points will only be placed at the
!C			        start of the first cut.
!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 Tool1name$[256]
DIM Tool2name$[256]
DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Scontour$[256]
DIM Spoint$[256]
DIM Depth_var$[256]
DIM Dfinish$[256]
DIM Zfinish$[256]
DIM Name$[256]
DIM Rough$[18],Stock$[18]
INTEGER Id_contour

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

Cut_type= WsAutoRoughCuts ! or WsFixedRoughCuts  
Speed_method= 1
Sync_mode= 1 
Stat=SetXyzInput(WsLatheInput)

Start:!
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

ActivateUndo
Animate_tool=1
Units= CurrentUnits
IF Units= WsMetric THEN
        Tool_clearance= .125
        Dia_stock= 6.0
        Face_stock= 3.0
        Overcut= 0.0
ELSE
        Tool_clearance= .05
        Dia_stock= 0.250
        Face_stock= 0.125
        Overcut= 0.0
END IF
Current_pen= CurrentColor
Work= CurrentWorkplane
Id_contour= 0

Reverse=(Rough$[LEN(Rough$)]="r")
Long=(Rough$[1;4]="long")
IF POS(Stock$,"barstock") OR POS(Stock$,"Barstock") THEN
        Stock$=""
        Bar_stock=1
END IF


Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
IF Stat THEN 
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("TURRET #1 TOOL",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
                LowerSelectBox
                GOTO Leave
END IF
Tool1name$= Answer$
Tool1=FindUserData(Tool1name$,WsToolData)
Tnose_rad1=GetUserValue(Tool1,"Tool nose radius:")
Quad1=GetUserValue(Tool1,"Tool quadrant:")

Stat=ToolSelectBox("TOOLS","selected tool",WsAllTurn)
IF Stat THEN Leave
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("TURRET #2 TOOL",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
                LowerSelectBox
                GOTO Leave
END IF
Tool2name$= Answer$
Tool2=FindUserData(Tool2name$,WsToolData)
Tnose_rad2=GetUserValue(Tool2,"Tool nose radius:")
IF ABS(Tnose_rad1-Tnose_rad2)> 0.0001 THEN Start
Quad1=GetUserValue(Tool2,"Tool quadrant:")
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)
No_depth_var= LastVariableInput(Depth_var$)
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
No_dfinish_var= LastVariableInput(Dfinish$)

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
No_zfinish_var= LastVariableInput(Zfinish$)
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)
No_dist_var= LastVariableInput(Lead_var$)

Pick_loop:!
Stat= SetInputFormat("n4","")
Contour= PickGroupEnt("PICK CONTOUR",0)
IF ExitScript(Destination$) THEN Leave
IF (NOT ValidDependency(Contour)) THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(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

Stock_question:!
Ps_created= 0
IF Long THEN
	Uncut_angle= 0.0*(NOT Reverse)+180*(Reverse)
ELSE
	Uncut_angle= 270.0*(NOT Reverse)+90*(Reverse)
END IF
IF LEN(Stock$) THEN
Spick_loop:!
	Stat= SetInputFormat("n4","")
	Stock_contour= PickGroupEnt("PICK STOCK CONTOUR",0)
	IF (ExitScript(Destination$)) THEN Leave 
	IF NOT ValidGeometry(Stock_contour) THEN Spick_loop
	IF (NOT ValidDependency(Stock_contour)) THEN Pick_loop
	Stat= EntityNameCheck(Stock_contour,SContour$)
	IF NOT LEN(SContour$) THEN Spick_loop
	Stock_boundary=1
ELSE
	IF NOT Bar_stock THEN
		Stat=SetInputFormat("n4","")
		Pstart=XyzInput("INPUT START POINT",WsXyOnly)
		IF (ExitScript(Destination$)) THEN Leave
		IF NOT ValidGeometry(Pstart) THEN Stock_question

		E1= LastPickEnt
		Pick_para= LastPickPara
		Pick_return= CurrPickReturn
		IF NOT ValidDependency(E1) THEN
        		E1= CopyEntity(Pstart,WsFalse)
        		Stat= EntityNameCheck(E1,Spoint$)
        		Ps_created= 1
		ELSE
        		Stat= EntityNameCheck(E1,Spoint$)
        		Ps_created= 0
		END IF
		Stat= DeleteEntity(Pstart,WsTrue)
	END IF
	Stock_boundary=0
END IF
IF Id_contour THEN
        Pen_color=WsConvCutPen
ELSE
        Pen_color=WsClimbCutPen
END IF
Stat= SetCurrentColor(Pen_color)


Otype= 1*(Long)+ 2*(NOT Long)
Stype= 4*(Long)+ 5*(NOT Long)
Rough_cycle= WsLongitudinalTurn*(Long)+ WsRadialTurn*(NOT Long)

Answer$= RPT$("Longitudinal ",Long)&RPT$("Radial ",NOT Long)
Answer$= Answer$&RPT$("od ",NOT Id_contour)&RPT$("id ",Id_contour)
Answer$= Answer$&RPT$("reverse ",Reverse)

PlotOff
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("DUAL TURRET ROUGH: "&Answer$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
Tool1=FindUserData(Tool1name$,WsToolData)
Tool2=FindUserData(Tool2name$,WsToolData)
V1= FreeParameter("Id contour",Id_contour)
V2= FreeParameter("Reverse",Reverse)
V3= FreeParameter("Tool nose radius",ToolNoseRadius(Tool1))
V4= FreeParameter("Tool quadrant",ToolQuadrant(Tool1))
IF No_dfinish_var THEN
        V5= FreeParameter("Diameter finish",Dia_allow)
ELSE
        V5= FreeParameter("Diameter finish",Dfinish$)
END IF
IF No_zfinish_var THEN
        V6= FreeParameter("Length finish",Len_allow)
ELSE
        V6= FreeParameter("Length finish",Zfinish$)
END IF
IF No_depth_var THEN
        V7= FreeParameter("Depth cut",Depth_of_cut)
ELSE
        V7= FreeParameter("Depth cut",Depth_var$)
END IF
IF No_dist_var THEN
        V8= FreeParameter("Lead dist",Lead_dist)
ELSE
        V8= FreeParameter("Lead dist",Lead_var$)
END IF
V9= FreeParameter("Auto cuts",Cut_type)
V10= FreeParameter("Speed method",Speed_method)
V11= FreeParameter("Sync mode",Sync_mode)
Path= LatheToolPtShift(Contour$,V3,V5,V6,Otype,V1,V4,V2,Uncut_angle)
If Stock_boundary THEN
	V12= FreeParameter("Tool clearance",Tool_clearance)
        Stock_path= LatheToolPtShift(Scontour$,0.0,V12,V12,Stype,V1,V4,V2)
	Group= DualTurretRough(Path,Stock_path,0,V7,V8,V1,V2,Long,V9,V10,V11)
	Stat= DeleteGroup(Stock_path,WsTrue)
ELSE
	IF Bar_stock THEN
           V13= FreeParameter("Diameter stock",Dia_stock)
           V14= FreeParameter("Face stock",Face_stock)
           V15= FreeParameter("Overcut",Overcut)
           Ps= BarStockContMod(Path,V13,V14,V15,Long,V1,V4,V2)
        ELSE
	   SELECT Pick_return
	   CASE WsGeometryEnd
       		 Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeometryEnd)
	   CASE WsGeometryMid
        	Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeometryMid)
	   CASE WsGeomCenter
       		Ps= ParameterReturnPt(Spoint$,Pick_para,WsGeomCenter)
	   END SELECT
	END IF
	Group= DualTurretRough(Path,0,Ps,V7,V8,V1,V2,Long,V9,V10,V11)
	Stat= DeleteEntity(Ps)
END IF
Stat= AutoNameGroup(Group,Name$)
Path1= GroupElement(Group,1)
Path2= GroupElement(Group,2)
Stat= AssociateTool(Path1,Tool1)
Stat= AssociateTool(Path2,Tool2)
Stat= DeleteGroup(Path,WsTrue)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn
IF NOT ValidGeometry(Group) THEN
        DelLastVarRecord
ELSE
	Stat= PlotEntity(Group)
	IF Animate_tool THEN
        	Work_trans= CurrentWorkplane
        	Method=2
        	Speed= 500
        	Rapid= 600
        	Feed= ToolFeed(Tool1,2,0.012)
        	Stat= AnimateSyncPaths(Tool1,Tool2,Path1,Path2,Speed,Feed,Rapid,Method,WsTrue,WsTrue,Lead_tool)
	END IF
	ActivateUndo
	Stat= AdvanceUndoNumber("dual rough")
	IF Ps_created THEN
                Stat= AddItemToUndo(WsUBCreateMode,Spoint$)
        END IF
	Stat= AddGroupToUndo(WsUBCreateMode,Group,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_loop

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



