
! script name: 3drotary_cut.d/flowcurves4
! 3dtopic: cuts a surface by flow curves using 4 axis

!A
!A NAME:
!A
!A 	Flow Curves
!A
!A DESCRIPTION:
!A
!A 	This script generates cuts along the flow curves on a surface.
!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.
!B
!B 	CORNER RADIUS: Enter the corner radius of the endmill.
!B 		NOTE: This question is only asked for endmills.
!B
!B 	CORNER RADIUS: Enter the radius to be used in all corners.
!B
!B 	WIDTH OF CUT: Enter the actual width of cut.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!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: Four axis flow curves ")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[4000]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Tool_image$[256]
DIM Surface_dir$[256]

ActivateUndo
Tilting_tool=1

Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zigzag_cuts=NamedVarVal("3D Zigzag Cuts",Ws3DSystemVars,1.0)
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

Widthr=NamedVarVal("width of cut",WsSystemVars,Width_cut)
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
	Cut_width=Cutter_dia*GetVariableVal(Width_val)
ELSE
	Cut_width= Cutter_dia
END IF
IF Widthr>Cut_width THEN Widthr= Cut_width

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Widthr)
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF ExitScript(Destination$) THEN Leave
Width_cut=ABS(Width_cut)
Widthr=Width_cut

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)
VWidth=CreateVariable("width of cut",WsSystemVars,Width_cut)

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

StdWorkplane

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"
        Axis_num=1
        Rotary_axis= PointXYZ(1,0,0)
        Locked_axis= PointXYZ(0,0,1)
CASE "Y"
        Axis_num=2
        Rotary_axis= PointXYZ(0,1,0)
        Locked_axis= PointXYZ(0,0,1)
CASE "Z"
        Axis_num=3
        Rotary_axis= PointXYZ(0,0,1)
        Locked_axis= PointXYZ(0,0,1)
CASE ELSE
        GOTO Rotary_axis
END SELECT

IF Tilting_tool THEN
        Stat=SetInputFormat("n4=","")
        Stat=SetEqualsValue(Tilt_angle1)
        Tilt_angle1=GetValue("CUTTER TILT ANGLE",Answer$)
        IF ExitScript(Destination$) THEN Leave
        Tilt_angle= Tilt_angle1
        IF ABS(Tilt_angle)> 0 THEN
                D1= DotVectors(Rotary_axis,Locked_axis)
                IF D1> 0.707 THEN Tilt_angle= 90-Tilt_angle
        END IF
ELSE
        Tilt_angle1=0
        Tilt_angle=0
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

Normal_sign=1
UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Pick_surf1
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)
GOSUB UorV_cut_dir
Stat= DeleteEntity(UVpoint)
IF UorV THEN
        U_direction= Direction
        V_direction= WsForward*(V<= 0.5)+ WsReverse*(V> 0.5)
	Surf_edge= 1*(V_direction=WsForward)+3*(V_direction= WsReverse)
	Edge_dir= U_direction
ELSE
        V_direction= Direction
        U_direction= WsForward*(U<= 0.5)+ WsReverse*(U> 0.5)
	Surf_edge= 2*(U_direction=WsReverse)+4*(U_direction= WsForward)
	Edge_dir= V_direction
END IF
IF U_direction=WsForward THEN
        Startu= 0.0
        Endu= 1.0
ELSE
        Startu= 1.0
        Endu= 0.0
END IF
IF V_direction=WsForward THEN
        Startv= 0.0
        Endv= 1.0
ELSE
        Startv= 1.0
        Endv= 0.0
END IF

Cut_method= WsRotaryAxisCut
Orient_sign= 1


SELECT Uorv
CASE 1
Cutterpath=RotaryUCuts(surface,Rotary_axis,Locked_axis,Tilt_angle,Cut_method,Orient_sign,Normal_sign,Startu,Endu,Startv,Endv,Width_cut,Tol,Zigzag_cuts)

CASE 0
Cutterpath=RotaryVCuts(surface,Rotary_axis,Locked_axis,Tilt_angle,Cut_method,Orient_sign,Normal_sign,Startu,Endu,Startv,Endv,Width_cut,Tol,Zigzag_cuts)

END SELECT

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: ROTARY FLOWCURVES"&CHR$(10)
	Stat=SurfaceName(Surface,Answer$)
	Datastr$=Datastr$&"SURFACE: "&Answer$&CHR$(10)
	Datastr$=Datastr$&"EDGE U: "&VAL$(U)&CHR$(10)
	Datastr$=Datastr$&"EDGE V: "&VAL$(V)&CHR$(10)
	Datastr$=Datastr$&"EDGE DIRECTION: "&VAL$(Edge_dir)&CHR$(10)
	Datastr$=Datastr$&"SURFACE EDGE: "&VAL$(Surf_edge)&CHR$(10)
	Datastr$=Datastr$&"SIGN: "&VAL$(Normal_sign)&CHR$(10)
	Datastr$=Datastr$&"CUTTER DIA: "&VAL$(Cutter_dia)&CHR$(10)
	Datastr$=Datastr$&"WIDTH CUT: "&VAL$(Width_cut)&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$&"ZIGZAGS: "&VAL$(Zigzag_cuts)&CHR$(10)
	Datastr$=Datastr$&"CUT METHOD: "&VAL$(Cut_method)&CHR$(10)
        Datastr$=Datastr$&"ORIENT SIGN: "&VAL$(Orient_sign)&CHR$(10)
	Datastr$=Datastr$&"AXIS NUMBER: "&VAL$(Axis_num)&CHR$(10)
	Datastr$=Datastr$&"TILT ANGLE: "&VAL$(Tilt_angle1)&CHR$(10)


	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

ShowCutDirection:!
        U_linef= SurfFlowLine(Surface,U,V,Normal_len,1)
        U_liner= SurfFlowLine(Surface,U,V,-Normal_len,1)
        Puf=ParameterPt(U_linef,1.0)
        Pur=ParameterPt(U_linef,-1.0)
        V_linef= SurfFlowLine(Surface,U,V,Normal_len,0)
        V_liner= SurfFlowLine(Surface,U,V,-Normal_len,0)
        Pvf=ParameterPt(V_linef,1.0)
        Pvr=ParameterPt(V_linef,-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

UorV_cut_dir:!
        GOSUB ShowCutDirection
        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)
        D1=DistTwoPts(Pt,Puf)
        D2=DistTwoPts(Pt,Pvf)
        D3=DistTwoPts(Pt,Pur)
        D4=DistTwoPts(Pt,Pvr)
        Uorv=1
        Direction= WsForward
        Para_value= V
        IF (D2< D1) AND (D2< D3) AND (D2< D4)  THEN
                Uorv=0
                Direction= WsForward
                Para_value= U
        END IF
        IF (D3< D1) AND (D3< D2) AND (D3< D4)  THEN
                Uorv=1
                Direction= WsReverse
                Para_value= V
        END IF
        IF (D4< D1) AND (D4< D2) AND (D4< D3)  THEN
                Uorv=0
                Direction= WsReverse
                Para_value= U
        END IF
        Stat=DeleteEntity(U_linef)
        Stat=DeleteEntity(V_linef)
        Stat=DeleteEntity(U_liner)
        Stat=DeleteEntity(V_liner)
        Stat=DeleteEntity(Puf)
        Stat=DeleteEntity(Pvf)
        Stat=DeleteEntity(Pur)
        Stat=DeleteEntity(Pvr)
        Stat=DeleteEntity(Pt)
RETURN


Leave:!
Stat=DeleteEntity(U_linef)
Stat=DeleteEntity(V_linef)
Stat=DeleteEntity(U_liner)
Stat=DeleteEntity(V_liner)
Stat=DeleteEntity(Puf)
Stat=DeleteEntity(Pvf)
Stat=DeleteEntity(Pur)
Stat=DeleteEntity(Pvr)
Stat=DeleteEntity(Pt)
Stat=SetInputFormat("n4","")
LowerSelectBox
Stat=SetPickMask(WsPickAll)
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
