
! script name: 3drotary_cut.d/rotaryflowtan
! 3dtopic: cut flow curve using rotary axis cutting

!A
!A NAME:
!A
!A 	Flow Curve (tangent cut)
!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 	CUTTER TYPE Ball, Endmill:
!B
!B 		B - Cutter is assumed to be a ball endmill.  Offset is
!B 		    measured from the center of the ball radius.  The bottom
!B 		    of the ball is the programmed point.
!B 		E - Cutter is assumed to be a standard, flat-bottom endmill,
!B 		    with possibly rounded corners.  The programmed point is the
!B 		    center of the bottom of the cutter.  It is this point which
!B 		    is kept away from BOUNDARY SOLIDS by the offset amount.
!B
!B 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	CORNER RADIUS: Enter the radius to be used in all corners.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!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 Datastr$[4000]
DIM Tool_name$[256]
DIM Tool_image$[256]
DIM Tool_type$[256]
DIM Surface_dir$[256]

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

Start:!
Cutter_type=WsBallCutter
Offset=0
Taper=0
StdWorkplane

Cutter_type=NamedVarVal("cutter type",WsSystemVars,Cutter_type)

Surface_dir$="F"

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

IF NOT Tool_assoc THEN
	GOSUB Get_mill_type
	GOSUB Get_cutter_dia
ELSE
	Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
	IF Stat THEN
		GOSUB Get_mill_type
		GOSUB Get_cutter_dia
		Tool_assoc=0
	ELSE
Tool_ques:!
		Stat=SetInputFormat("a99","")
		Stat=GetValue("CUTTER PATH TOOL",Tool_name$)
		IF ExitScript(Destination$) THEN Leave
		IF Tool_name$="CANCEL" THEN
			LowerSelectBox
			GOSUB Get_mill_type
			GOSUB Get_cutter_dia
			Tool_assoc=0
			GOTO Past_dia
		END IF
		Tool=FindUserData(Tool_name$,WsToolData)

		Stat=GetUserString(Tool,"Tool type:",Tool_type$)

		 SELECT Tool_type$
                    CASE "ENDMILL","BULLMILL"
                        Cutter_type=WsEndMill
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                        Corner_rad=GetUserValue(Tool,"Corner radius:")
                    CASE "TAPERMILL"
                        Cutter_type=WsTaperedEndmill
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                        Taper=GetUserValue(Tool,"Taper angle:")
                        Corner_rad=GetUserValue(Tool,"Corner radius:")
                        Length=GetUserValue(Tool,"Flute length:")
                    CASE "TAPERBALL"
                        Cutter_type=WsTaperedBall
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                        Taper=GetUserValue(Tool,"Taper angle:")
                        Length=GetUserValue(Tool,"Flute length:")
                    CASE "BALLMILL"
                        Cutter_type=WsBallCutter
                        Cutter_dia=GetUserValue(Tool,"Diameter:")
                    CASE ELSE
                        GOTO Tool_ques
                END SELECT

		LowerSelectBox
	END IF
END IF

Past_dia:!
Cutter_dia=ABS(Cutter_dia)
Cutter_rad=Cutter_dia/2

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

VCutter_type=CreateVariable("cutter type",WsSystemVars,Cutter_type)
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
VCorner=CreateVariable("corner radius",WsSystemVars,Corner_rad)

Stat=Setup3dCutter(Cutter_type,Cutter_rad,Corner_rad,Offset,Finish_allow,Taper,Length)

StdWorkplane
Rotary_axis= PointXYZ(0,0,1)
Locked_axis= PointXYZ(0,0,1)

Rotary_axis:Stat=SetInputFormat("a1=",",X,Y,Z,")
Stat=SetEqualsValue(0,"Y")
Stat=GetValue("ROTARY AXIS: X, Y, Z ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "X"
        Rotary_axis= PointXYZ(1,0,0)
        Locked_axis= PointXYZ(0,0,1)
CASE "Y"
        Rotary_axis= PointXYZ(0,1,0)
        Locked_axis= PointXYZ(0,0,1)
CASE "Z"
        Rotary_axis= PointXYZ(0,0,1)
        Locked_axis= PointXYZ(0,0,1)
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 ValidGeometry(Axis_line) THEN Axis_line
        T= LastPickPara
        IF T > 0.5 THEN Orient_sign= -1
        Stat= DeleteEntity(Locked_axis)
        Locked_axis= LineVector(Axis_line)
        Tilt_angle=0
ELSE
        Stat=SetInputFormat("n4=","")
        Stat=SetEqualsValue(Tilt_angle)
        Tilt_angle=GetValue("CUTTER TILT ANGLE",Answer$)
        IF ExitScript(Destination$) THEN Leave
END IF

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

Pick_loop:!

Intersect2: StdWorkplane

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

GOSUB Which_flow_curve
IF NOT Surf_edge THEN Pick_loop
Which1=Surf_edge

Orient_sign1= Surface_sign*Orient_sign

Cutterpath=RotaryCutEdgeCurve(surface,Rotary_axis,Locked_axis,Tilt_angle,Cut_method,Orient_sign1,Which1,tol,Cut_direction,Para_value)

IF ValidGeometry(Cutterpath) THEN
	Datastr$=" 3D CUTTER PATH "&CHR$(10)
	Datastr$=Datastr$&" "&CHR$(10)
	Stat=CPathFileName(Cutterpath,Name$)
	Datastr$=Datastr$&"NAME: "&Name$&CHR$(10)
	Datastr$=Datastr$&"TYPE:  FIVE AXIS FLOW "&CHR$(10)
	Stat=SurfaceName(Surface,Answer$)
	Datastr$=Datastr$&"SURFACE: "&Answer$&CHR$(10)
	Datastr$=Datastr$&"FLOW U: "&VAL$(U)&CHR$(10)
	Datastr$=Datastr$&"FLOW V: "&VAL$(V)&CHR$(10)
	Datastr$=Datastr$&"FLOW DIRECTION: "&VAL$(Cut_direction)&CHR$(10)
	Datastr$=Datastr$&"FLOW CURVE: "&VAL$(Surf_edge)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"OFFSET: "&VAL$(Offset)&CHR$(10)
	Datastr$=Datastr$&"FINISH ALLOWANCE: "&VAL$(Finish_allow)&CHR$(10)
	Datastr$=Datastr$&"TOLERANCE: "&VAL$(Tol)&CHR$(10)
	Datastr$=Datastr$&"CORNER RAD: "&VAL$(Corner_rad)&CHR$(10)
	Datastr$=Datastr$&"TAPER: "&VAL$(Taper)&CHR$(10)
	Datastr$=Datastr$&"CUTTER TYPE: "&VAL$(Cutter_type)&CHR$(10)
	Datastr$=Datastr$&"CUT METHOD: "&VAL$(Cut_method)&CHR$(10)
	Datastr$=Datastr$&"ORIENT SIGN: "&VAL$(Orient_sign)&CHR$(10)
	Datastr$=Datastr$&"TILT ANGLE: "&VAL$(Tilt_angle)&CHR$(10)
	IF Cut_method= WsLockedAxis THEN
		X= XPointValue(Locked_axis)
		Y= YPointValue(Locked_axis)
		Z= ZPointValue(Locked_axis)
		Datastr$=Datastr$&"LOCKED AXIS X: "&VAL$(X)&CHR$(10)
		Datastr$=Datastr$&"LOCKED AXIS Y: "&VAL$(Y)&CHR$(10)
		Datastr$=Datastr$&"LOCKED AXIS Z: "&VAL$(Z)&CHR$(10)
	END IF

	Cpath3d=FindUserData(Name$,Ws3dCpath)
	IF ValidGeometry(Cpath3d) THEN Stat= DeleteEntity(Cpath3d)
	Cpath3d=ReadUserData(Ws3dCpath,Name$,Datastr$)

	Stat=AssocUserData(Cutterpath,Cpath3d,Ws3dCpath)
	IF Tool_assoc THEN Stat=AssociateTool(Cutterpath,Tool)

        Stat= AnimateTool(Tool,Cutterpath)
	ActivateUndo
        Stat= AdvanceUndoNumber("four axis cut")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Cutterpath,1,WsTrue)

END IF

GOTO Intersect2

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(U_liner,WsTrue)
        Stat= SetForceVisible(V_linef,WsTrue)
        Stat= SetForceVisible(V_liner,WsTrue)
        Stat= SetForceVisible(Puf,WsTrue)
        Stat= SetForceVisible(Pur,WsTrue)
        Stat= SetForceVisible(Pvf,WsTrue)
        Stat= SetForceVisible(Pvr,WsTrue)

        Stat=SetColor(U_linef,Forward_color)
        Stat=SetColor(V_linef,Forward_color)
        Stat=SetColor(U_liner,Reverse_color)
        Stat=SetColor(V_liner,Reverse_color)
        Stat=SetColor(Puf,Forward_color)
        Stat=SetColor(Pvf,Forward_color)
        Stat=SetColor(Pur,Reverse_color)
        Stat=SetColor(Pvr,Reverse_color)
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(Rotary_axis)
Stat= DeleteEntity(Locked_axis)
LowerSelectBox
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF

Get_mill_type:!
	SELECT Cutter_type
	    CASE WsBallCutter
		Answer$="B"
	    CASE WsEndMill
		Answer$="E"
	END SELECT
	Stat=SetEqualsValue(0,Answer$)
	Stat=SetInputFormat("a1=",",B,E,")
	Stat=GetValue("CUTTER TYPE Ball, Endmill",Answer$)
	IF ExitScript(Destination$) THEN Leave

	SELECT Answer$
	    CASE "B"
		Cutter_type=WsBallCutter
	    CASE "E"
		Cutter_type=WsEndMill
	END SELECT
RETURN

Get_cutter_dia:!
	Stat=SetInputFormat("n4=","")
	Cutter_dia=NamedVarVal("cutter diameter",WsSystemVars,0.0)
	Stat=SetRadiusEquals(Cutter_dia/2)
	Cutter_dia=InputRadius("CUTTER DIAMETER",WsDiaInput)
	IF ExitScript(Destination$) THEN Leave

	IF Cutter_type=WsEndMill THEN
		Corner_rad=NamedVarVal("corner radius",WsSystemVars,corner_rad)
		Stat=SetEqualsValue(Corner_rad)
		Corner_rad=GetValue("CORNER RADIUS",Answer$)
		IF ExitScript(Destination$) THEN Leave
		Corner_rad=Corner_rad
	END IF
RETURN

END
