
! script name: vlathe.d/fixedcycle
! var_topic: lathe fixed cycle

!A
!A NAME:
!A
!A      Lathe Fixed Cycle VARIATIONAL
!A
!A DESCRIPTION:
!A
!A      This script constructs a fixed cycle turning path.
!A
!A~ image location - images/fixedturn.jpg
!A~ image location - images/fixedface.jpg
!B INSTRUCTIONS:
!B
!B	SELECT TOOL: Select the tool to be used.
!B
!B	STARTING POINT: Select the starting point of the cycle.
!B
!B	ENDING POINT: Select the ending point of the cycle.
!B
!B	The standard longitudinal fixed cycle will position the tool
!B	at the starting point. It will then rapid to the diameter of the 
!B	end point. It will feed to the end point. Then it feeds back
!B	to the start diameter. It then rapids to the start point.
!B
!B	The standard radial fixed cycle is the same as the above
!B	with the roles of length and diameter interchanged.
!B
!B NOTES:
!B	
!B		Cycle_type= WsLongitudinalTurn
!B		Cycle_type= WsRadialTurn
!B
!B		Method= 0,1 Rapid to dia, Feed to endpt, Feed out, Rapid back.
!B		Method= 2   Feed to dia, Feed to endpt, Feed out, Rapid back.
!B		Method= 3   Rapid to dia, Feed to endpt, Rapid out, Rapid back.
!B		Method= 4   Rapid to dia, Feed to endpt, Feed out, No return.
!B		Method= 5   Feed to dia, Feed to endpt, Feed out, No return.
!B		Method= 6   Rapid to dia, Feed to endpt, Rapid out, No return.
!B		Method= 7   Start at dia, Feed to endpt, Feed out.
!B	
!C
!C      This cycle positions the Tool Tip Tangent Location of the turning
!C      tool. Since contours in turbopost need tool nose center locations
!C      the final contour is shifted based on the tool quadrant and
!C      the tool nose radius.
!C
!B
!# see also - points.d/dyninput.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: lathe fixed cycle ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Point1$[256]
DIM Point2$[256]
DIM Tool_name$[256]
REAL Params(0:10)

Graphical_mode=NamedVarVal("Graphical mode",WsSystemVars,1.0)

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Cycle_type= WsLongitudinalTurn
Method= 1
Slow_in_dist= 0.0
Slow_out_dist= 0.0
Feed_factor= 0.6

IF ARGC>1 THEN
	A$=ARGV(1)
	IF POS(A$,"radial") THEN 
		Cycle_type= WsRadialTurn
	ELSE
		Cycle_type= WsLongitudinalTurn
	END IF
	IF ARGC>2 THEN
		A$=ARGV(2)
		Method= VAL(A$)
	END IF
END IF
Stat=SetXyzInput(WsLatheInput)


Start:!
Stat=SetPickMask(WsPickNoSurf)
ActivateUndo


Start:!
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("TOOL",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN Leave
Tool_name$= Answer$
Tool=FindUserData(Tool_name$,WsToolData)
IF NOT ValidGeometry(Tool) THEN Tool_ques

Stat=GetUserString(Tool,"Tool image:",Tool_image$)
Tool_group=FindNamedGrp(Tool_image$)
Tool_plot=0
IF ValidGeometry(Tool_group) THEN Tool_plot=1

Pick_axis: PlotOn
stat= SetInputFormat("n4b","")
Stat= PointInputMode(1)
Ps=XyzInput("STARTING CORNER",WsXyOnly)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Ps) THEN Pick_axis
E1= LastPickEnt
Para1= LastPickPara
Pick_return1= CurrPickReturn
IF NOT ValidDependency(E1) THEN
        E1= CopyEntity(Ps)
        Stat= EntityNameCheck(E1,Point1$)
	Ps_created= WsTrue
ELSE
        Stat= EntityNameCheck(E1,Point1$)
	Ps_created= WsFalse
END IF
Stat=SetLastPoint(Ps)

IF Tool_plot THEN
CALL PlotLatheToolImage(Tool_group,Ps)
END IF

IF NOT Graphical_mode THEN
End_point:!
        Stat= PointInputMode(1)
        Pe=XyzInput("ENDING",WsXyOnly)
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT ValidGeometry(Pe) THEN End_point
        E2= LastPickEnt
        Para2= LastPickPara
        Pick_return2= CurrPickReturn
        IF NOT ValidDependency(E2) THEN
                 E2= CopyEntity(Pe)
                 Stat= EntityNameCheck(E2,Point2$)
		 Pe_created= WsTrue
        ELSE
                 Stat= EntityNameCheck(E2,Point2$)
		 Pe_created= WsFalse
        END IF
        Stat=SetLastPoint(Pe)
ELSE
	PlotOff
	Angle= 0.0
	Length1= 2.0+ 2.0*Radius 
	Pe= PtIncrXyz(Ps,Length1,Length1,0.0)
	Contour= RectangleTwoPts(Ps,Pe,Radius,WsCornerFillet,Centerstart)
	Params(0)= WsCornerFillet
	Params(1)= Centerstart
	Params(2)= Radius
	Params(3)= Radius
	Params(4)= Angle
	IF NOT ValidGeometry(Contour) THEN
	    IF Ps_created THEN Stat= DeleteEntity(Point1$)
	    IF Tool_plot THEN
	    	CALL PlotLatheToolImage(Tool_group,Ps)
	    END IF
	    DetachGroupImage
	    Stat= DeleteEntityList(Ps,Pe)
	    PlotOn
	    GOTO Pick_axis
	END IF
	Stat= MakeInvisible(Contour)
	Stat= MakeInvisible(Ps)
	Stat= MakeInvisible(Pe)
	PlotOn

	Vnum= LastPickView
	Num_params=5
	Stat=SetInputFormat("n4b","")
	Stat=DynamicShape(Vnum,Contour,Ps,Pe,WsDynamicRectangle,"PICK ENDING CORNER ",Params,Num_params)
	IF (ExitScript(Destination$)) THEN
       		 Stat=DeleteGroup(Contour,WsTrue,WsTrue)
		 IF Tool_plot THEN
		 	PlotLatheToolImage(Tool_group,Ps)
		 END IF
		 DetachGroupImage
		 IF Ps_created THEN Stat=DeleteEntity(Point1$)
		 Stat= DeleteEntityList(Ps,Pe)
       		 IF Destination$= "BACKUP" THEN Pick_axis
       		 GOTO Leave
	END IF
	E2= LastPickEnt
        Para2= LastPickPara
        Pick_return2= CurrPickReturn
        Stat= DeleteGroup(Contour,WsTrue,WsTrue)
	Centerstart= Params(1)
        Radius= Params(2)
        Chamfer= Params(3)
        Angle= Params(4)
        no_radius_var=1
        no_chamfer_var=1
        IF NOT ValidDependency(E2) THEN
                 E2= CopyEntity(Pe)
                 Stat= EntityNameCheck(E2,Point2$)
                 Stat= MakeVisible(E2)
		 Pe_created= WsTrue
        ELSE
                 Stat= EntityNameCheck(E2,Point2$)
		 Pe_created= WsFalse
        END IF
END IF
IF Tool_plot THEN
	CALL PlotLatheToolImage(Tool_group,Ps)
END IF
DetachGroupImage
Stat= DeleteEntityList(Ps,Pe)

Make_record:!
PlotOff
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("LATHE: Fixed Cycle "&Tool_name$)
Stat= SetRecDelLastExec
SELECT Pick_return1
CASE WsGeometryEnd
        Ps= ParameterReturnPt(Point1$,Para1,WsGeometryEnd)
CASE WsGeometryMid
        Ps= ParameterReturnPt(Point1$,Para1,WsGeometryMid)
CASE WsGeomCenter
        Ps= ParameterReturnPt(Point1$,Para1,WsGeomCenter)
END SELECT
SELECT Pick_return2
CASE WsGeometryEnd
        Pe= ParameterReturnPt(Point2$,Para2,WsGeometryEnd)
CASE WsGeometryMid
        Pe= ParameterReturnPt(Point2$,Para2,WsGeometryMid)
CASE WsGeomCenter
        Pe= ParameterReturnPt(Point2$,Para2,WsGeomCenter)
END SELECT

Tool=FindUserData(Tool_name$,WsToolData)
V1= FreeParameter("Method",Method)
V2= FreeParameter("Feed factor",Feed_factor)
V3= FreeParameter("Slow feed in dist",Slow_in_dist)
V4= FreeParameter("Slow feed out dist",Slow_out_dist)
IF Cycle_type= WsLongitudinalTurn THEN
	G1= FixedCycleContour(WsLongitudinalTurn,V1,Ps,Pe,V2,V3,V4)
ELSE
	G1= FixedCycleContour(WsRadialTurn,V1,Ps,Pe,V2,V3,V4)
END IF
V1= FreeParameter("Diameter stock",Dia_stock)
V2= FreeParameter("Length stock",Len_stock)
V3= FreeParameter("Tool tip tangent",1)
V4= FreeParameter("Tool nose radius",ToolNoseRadius(Tool))
V5= FreeParameter("Tool quadrant",ToolQuadrant(Tool))
Path= LatheTipShift(G1,V4,V1,V2,V5,V3)
Stat= AssociateTool(Path,Tool)
Stat= AutoNameGroup(Path,Name$)
Stat= DeleteGroup(G1)
Stat= DeleteEntityList(Ps,Pe)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn

Stat= PlotEntity(Point1$)
Stat= PlotEntity(Point2$)
Stat= PlotEntity(Path)
IF NOT ValidGeometry(Path) THEN
        DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Path)
	ActivateUndo
	Stat= AdvanceUndoNumber("lathe fixed cycle")
	IF Ps_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point1$)
	IF Pe_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point2$)
	Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsFalse)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_axis

Leave:!
PlotOn
Stat=SetPickMask(WsPickGeometry)
Stat= PointInputMode(0)
Stat= DeleteEntityList(Ps,Pe)

IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	IF Destination$= "SCRIPTSTART" THEN Start
	IF Destination$= "BACKUP" THEN Start
	LOAD Destination$
END IF
END

SUB PlotLatheToolImage(ENTITY Tool_group,Pt)
        Trans1= AngleTransform(Pt,0.0,0.0,0.0)
        Stat= PlotGroupImage(Tool_group,Trans1,1,1,1,1)
        Stat= AttachGroupImage(Tool_group,Trans1,1,1,1,1)
SUBEND
