
! script name: v3drotary_cut.d/rotaryflowtan
! var_topic: cut flow curve using rotary axis cutting

!A
!A NAME:
!A
!A 	Curves: Rotary axis  Cut Flow Curve	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script drives a cutter along the a flow curve of a surface using 
!A	Rotary axis machining.
!A
!B INSTRUCTIONS:
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B      Z CUTTER DISPLACEMENT: A positive value will move the cutter
!B                              that distance in the direction of the
!B                              cutter axis. A negative value in the
!B                              reverse direction.
!B
!B
!B
!B      ROTARY AXIS: X, Y, Z:  Select the rotary axis.
!B
!B      CUTTER TILT ANGLE: For a pure rotary cut on a five axis machine
!B                              the cutter can be tilted at a fixed angle
!B                              relative to the rotary axis.
!B
!B      TOLERANCE: Enter the amount of variation allowed.
!B
!B      PICK SURFACE: Pick the surface to be used.
!B
!B      PICK CUT DIRECTION: Pick the direction of cut.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: Rotary axis cut flow curve ")
DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Surface_dir$[256]
DIM Toolid$[256]
DIM Surface$[256]
DIM Finish_var$[256]
DIM Angle_var$[256]
DIM Axis_line$[256]
DIM Name$[256]

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


ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Zaddon=NamedVarVal("Z Cutter Displacement",Ws3DSystemVars,0.0)
Forward_color=3
Reverse_color=4

IF ARGC>1 THEN
	Tool_name$= ARGV(1)
	GOTO Have_tool
END IF

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:v3drotary_cut.d/rotaryflowtan"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("CUTTER PATH TOOL, New_tool ",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave
IF Tool_name$="New_tool" THEN
	LowerSelectBox
	LOAD "vtools.d/readmilltool:v3drotary_cut.d/rotaryflowtan"
END IF
Have_tool:!
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
LowerSelectBox



Past_dia:!
Finishr=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Finishr)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Finishr=Finish_allow
No_finish_var= LastVariableInput(Finish_var$)

Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zaddon)
Zaddon=GetValue("Z CUTTER DISPLACEMENT",Answer$)
IF ExitScript(Destination$) THEN Leave
Vzaddon=CreateVariable("Z Cutter Displacement",Ws3DSystemVars,Zaddon)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Tol)
Tol=GetValue("TOLERANCE",Answer$)
IF ExitScript(Destination$) THEN Leave


Rotary_axis:Stat=SetInputFormat("a1=",",X,Y,Z,")
Stat=SetEqualsValue(0,"Y")
Stat=GetValue("ROTARY AXIS: X, Y, Z  Pick axis",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "X"
        Axis_number=1
CASE "Y"
        Axis_number=2
CASE "Z"
        Axis_number=3
CASE ELSE
        GOTO Rotary_axis
END SELECT

Cut_method:Stat=SetInputFormat("a1=",",N,T,L,")
Stat=SetEqualsValue(0,"N")
Stat=GetValue("CUT METHOD: Normal, Tangent, Locked ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "N"
        Cut_method= WsRotaryAxisCut
        Orient_sign= 1
CASE "T"
        Cut_method= WsRotaryAxisTan1
        Orient_sign= 1
CASE "L"
        Cut_method= WsLockedAxis
        Orient_sign= 1
CASE ELSE
        GOTO Cut_method
END SELECT

IF Cut_method= WsLockedAxis THEN
Axis_line:Axis_line=PickSpecificEnt("PICK AXIS LINE",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidDependency(Axis_line) THEN Axis_line
        Stat= EntityNameCheck(Axis_line,Axis_line$)
        T= LastPickPara
        IF T > 0.5 THEN Orient_sign= -1
        No_angle_var=1
        Tilt_angle=0
ELSE
        Stat=SetInputFormat("n4=","")
        Stat=SetEqualsValue(Tilt_angle)
        Tilt_angle=GetValue("CUTTER TILT ANGLE",Answer$)
        IF ExitScript(Destination$) THEN Leave
        No_angle_var= LastVariableInput(Angle_var$)
END IF


Pick_loop:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_loop
Stat= EntityNameCheck(Surface,Surface$)


GOSUB Which_flow_curve
IF NOT Surf_edge THEN Pick_loop
Which1=Surf_edge
Orient_sign1= Surface_sign*Orient_sign


Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Rotary Axis Flow Curve")
Stat= SetRecDelLastExec
Stat= NullReference(Surface$)
Work= CurrentWorkplane
Stat= SetWorkplane
V1= FreeParameter("Axis number",Axis_number)
Raxis= StdRotaryAxis(V1)
IF Cut_method= WsLockedAxis THEN
        Laxis= LineVector(Axis_line$)
        V1= FreeParameter("Cutter Tilt angle",0)
ELSE
        Laxis= LineVector("")
        IF No_finish_var THEN
                V1= FreeParameter("Cutter Tilt angle",Tilt_angle)
        ELSE
                V1= FreeParameter("Cutter Tilt angle",Angle_var$)
        END IF
END IF
IF No_finish_var THEN
        V2= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V2= FreeParameter("Finish allowance",Finish_var$)
END IF
SELECT Cut_method
CASE WsLockedAxis
        V3= FreeParameter("Cut method",WsLockedAxis)
CASE WsRotaryAxisCut
        V3= FreeParameter("Cut method",WsRotaryAxisCut)
CASE WsRotaryAxisTan1
        V3= FreeParameter("Cut method",WsRotaryAxisTan1)
CASE WsRotaryAxisTan2
        V3= FreeParameter("Cut method",WsRotaryAxisTan2)
END SELECT
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Direction",Cut_direction)
V6= FreeParameter("Orient sign",Orient_sign1)
V7= FreeParameter("Which edge",Which1)
V8= FreeParameter("Parameter value",Para_value)
V9= FreeParameter("Z cutter displacement",Zaddon)
V10= FreeParameter("Start parameter",0.0)
V11= FreeParameter("End parameter",1.0)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,0,V2)
Stat=Setup3dCutter(Mdef1)
Stat= Set3dZAddon(V9)
Stat= PrintToVarRecord("PlotOn")
Cutter_path=RotaryCutEdgeCurve(Surface$,Raxis,Laxis,V1,V3,V6,V7,V4,V5,V8,V10,V11)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntityList(Mdef1,Raxis,Laxis)
Stat= SetWorkplane(Work)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Cutter_path)
        ActivateUndo
        Stat= AdvanceUndoNumber("four axis cut")
        Stat= AddGroupToUndo(WsUBCreateMode,Cutter_path,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_loop





Which_flow_curve: Surf_edge=0
Edge_dir=WsForward
Pt=LastUVSurfPick(Surface)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)

Surface_sign=1
GOSUB Flow_direction
IF Cut_method= WsRotaryAxisTan1 OR Cut_method= WsRotaryAxisTan2 THEN
	Surface_sign= SurfOrientSign(Surface,U,V)
	GOSUB Tangent_dir
END IF

RETURN


Flow_direction:!
	GOSUB Show_tangent_dir
	Stat=SetInputFormat("n4","")
Pick_uorv: L1=PickSpecificEnt("PICK CUT DIRECTION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_uorv
        Pt=ParameterPt(L1,1.0)
        Du1=DistTwoPts(Pt,Puf)
        Dv1=DistTwoPts(Pt,Pvf)
        Du2=DistTwoPts(Pt,Pur)
        Dv2=DistTwoPts(Pt,Pvr)
        Uorv=1
	Cut_direction= WsForward
	Surf_edge=5
	IF (Du1<= Dv1) THEN
		IF (Du1<= Du2) THEN
			IF (Du1<= Dv2) THEN
				Uorv= 1
				Para_value= V
				Surf_edge=5
				Cut_direction= WsForward
			ELSE
				Uorv= 0
				Para_value= U
				Surf_edge=6
				Cut_direction= WsReverse
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
				Uorv= 1
				Para_value= V
				Surf_edge=5
				Cut_direction= WsReverse
			ELSE
				Uorv= 0
				Para_value= U
				Surf_edge=6
				Cut_direction= WsReverse
			END IF
		END IF
	ELSE
		IF (Dv1<= Du2) THEN
			IF (Dv1<= Dv2) THEN
				Uorv= 0
				Para_value= U
				Surf_edge=6
				Cut_direction= WsForward
			ELSE
				Uorv= 0
				Para_value= U
				Surf_edge=6
				Cut_direction= WsReverse
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
				Uorv= 1
				Para_value= V
				Surf_edge=5
				Cut_direction= WsReverse
			ELSE
				Uorv= 0
				Para_value= U
				Surf_edge=6
				Cut_direction= WsReverse
			END IF
		END IF
	END IF
	Stat=DeleteEntity(U_linef)
	Stat=DeleteEntity(U_liner)
	Stat=DeleteEntity(V_linef)
	Stat=DeleteEntity(V_liner)
	Stat=DeleteEntity(Puf)
	Stat=DeleteEntity(Pur)
	Stat=DeleteEntity(Pvf)
	Stat=DeleteEntity(Pvr)

	Stat=DeleteEntity(Pt)
RETURN


Show_tangent_dir:!
	U_linef= SurfFlowLine(Surface,U,V,Normal_len,1)
	Puf=ParameterPt(U_linef,1.0)

	U_liner= SurfFlowLine(Surface,U,V,-Normal_len,1)
	Pur=ParameterPt(U_liner,1.0)

	V_linef= SurfFlowLine(Surface,U,V,Normal_len,0)
	Pvf=ParameterPt(V_linef,1.0)

	V_liner= SurfFlowLine(Surface,U,V,-Normal_len,0)
	Pvr=ParameterPt(V_liner,1.0)

	Stat= SetForceVisible(U_linef,WsTrue)
        Stat= SetForceVisible(V_linef,WsTrue)
        Stat= SetForceVisible(U_liner,WsTrue)
        Stat= SetForceVisible(V_liner,WsTrue)
        Stat= SetForceVisible(Puf,WsTrue)
        Stat= SetForceVisible(Pvf,WsTrue)
        Stat= SetForceVisible(Pur,WsTrue)
        Stat= SetForceVisible(Pvr,WsTrue)

        Stat=SetColor(U_linef,Forward_color,WsTrue)
        Stat=SetColor(V_linef,Forward_color,WsTrue)
        Stat=SetColor(U_liner,Reverse_color,WsTrue)
        Stat=SetColor(V_liner,Reverse_color,WsTrue)
        Stat=SetColor(Puf,Forward_color,WsTrue)
        Stat=SetColor(Pvf,Forward_color,WsTrue)
        Stat=SetColor(Pur,Reverse_color,WsTrue)
        Stat=SetColor(Pvr,Reverse_color,WsTrue)
RETURN


Tangent_dir:!
	GOSUB Show_tangent_dir
	Stat=SetInputFormat("n4","")
Pick_uorv: L1=PickSpecificEnt("PICK CUTTER ORIENTATION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_uorv
        Pt=ParameterPt(L1,1.0)
        Du1=DistTwoPts(Pt,Puf)
        Dv1=DistTwoPts(Pt,Pvf)
        Du2=DistTwoPts(Pt,Pur)
        Dv2=DistTwoPts(Pt,Pvr)
	Cut_method= WsRotaryAxisTan1
        Orient_sign= 1
	IF (Du1<= Dv1) THEN
		IF (Du1<= Du2) THEN
			IF (Du1<= Dv2) THEN
				IF UorV=1 THEN
					Cut_method= WsRotaryAxisTan1
					Orient_sign= 1
					Surface_sign=1
				ELSE
					Cut_method= WsRotaryAxisTan2
					Orient_sign= -1
				END IF
			ELSE
				IF UorV=1 THEN
					Cut_method= WsRotaryAxisTan2
					Orient_sign= -1
				ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= -1
					Surface_sign=1
				END IF
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
				IF UorV=1 THEN
					Cut_method= WsRotaryAxisTan1
					Orient_sign= -1
				ELSE
					Cut_method= WsRotaryAxisTan2
					Orient_sign= 1
				END IF
			ELSE
				IF UorV=1 THEN
					Cut_method= WsRotaryAxisTan2
					Orient_sign= -1
				ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= -1
				END IF
			END IF
		END IF
	ELSE
		IF (Dv1<= Du2) THEN
			IF (Dv1<= Dv2) THEN
				IF UorV=1 THEN
					Cut_method= WsRotaryAxisTan2
					Orient_sign= 1
				ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= 1
					Surface_sign=1
				END IF
			ELSE
				IF UorV=1 THEN
					Cut_method= WsRotaryAxisTan2
					Orient_sign= -1
				ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= -1
				END IF
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
				IF UorV=1 THEN
					Cut_method= WsRotaryAxisTan1
					Orient_sign= -1
					Surface_sign=1
				ELSE
					Cut_method= WsRotaryAxisTan2
					Orient_sign= 1
				END IF
			ELSE
				IF UorV=1 THEN
					Cut_method= WsRotaryAxisTan2
					Orient_sign= -1
				ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= 1
				END IF
			END IF
		END IF
	END IF
	Stat=DeleteEntity(U_linef)
	Stat=DeleteEntity(U_liner)
	Stat=DeleteEntity(V_linef)
	Stat=DeleteEntity(V_liner)
	Stat=DeleteEntity(Puf)
	Stat=DeleteEntity(Pur)
	Stat=DeleteEntity(Pvf)
	Stat=DeleteEntity(Pvr)
	Stat=DeleteEntity(Pt)
RETURN


Leave:!
Stat=DeleteEntity(Vf)
Stat=DeleteEntity(Vr)
Stat=DeleteEntity(Pf)
Stat=DeleteEntity(Pr)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(U_linef)
Stat=DeleteEntity(U_liner)
Stat=DeleteEntity(V_linef)
Stat=DeleteEntity(V_liner)
Stat=DeleteEntity(Puf)
Stat=DeleteEntity(Pur)
Stat=DeleteEntity(Pvf)
Stat=DeleteEntity(Pvr)
Stat= DeleteEntity(Raxis)
Stat= DeleteEntity(Laxis)
LowerSelectBox
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF

END
