
! script name: 3dcpaths.d/zgougesurf
! 3dtopic: cuts a surface using Z slices with gouge check

!A
!A NAME:
!A
!A 	Gouge: Slice Surface
!A
!A DESCRIPTION:
!A
!A 	Generates the cutter path of Z slice through a surface. The cutter is 
!A 	locked in the horizontal Z plane and allowed to move in the path which
!A 	is generated by placing the cutter in contact with the surface.
!A 	Gouge checking is performed on the cutter paths. 
!A
!B INSTRUCTIONS:
!B
!B 	MILL CLimb OR Conventional:
!B
!B 		C - Conventional mill will generate a path which is
!B 		    clockwise for an interior cut and counterclockwise
!B 		    for an exterior cut.
!B 		CL - Climb mill will generate a path which is clockwise for
!B 		    an exterior cut and counterclockwise for an interior cut.
!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 	CUTTER DIAMETER: Enter the diameter of the cutter to be used.
!B
!B 	CORNER RADIUS: Enter the corner radius of the endmill.
!B 		NOTE: This question is only asked for endmills.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!B 	Z START: Start of first pass in Z.
!B
!B 	Z END: Enter the last pass in Z.
!B
!B 	Z STEP: Z depth increment.
!B
!B 		The Z depth calculation is controlled by the user named variable
!B 		Z Step Method.  There are two methods available:
!B 			WsEqualZSteps - 0: Equalized steps in Z.
!B 			WsFullZDepth  - 1: Each Z pass is at full depth
!B 			increment, only the last Z pass is smaller.
!B
!B 		The Z rough method is controlled by the user named variable
!B 		Z Rough Method.  There are three methods available:
!B 			WsIncludeZAbove - 0: Includes Z cuts above Z limit
!B 		        plane.
!B 			WsZBetweenCuts - 1: Includes Z cuts between passes.
!B 			WsZOnPlaneOnly - 2: Includes Z cuts only on limit plane.
!B
!B 	PICK SURFACE GROUP: Pick the group of surfaces to be machined.
!B 		Surface Normal vectors must be properly aligned.
!B
!C NOTES:
!C
!C 	The gouge check is the same algorithm as in Parallel Gouge etc.
!C
!C 	This routine is calculation intensive if the cutter fits everywhere
!C 	use Part Slices under the 3D- Paths- Slices- Part Slices.
!C
!C 	The routine works best with sharp internal corners and rounded external
!C 	corners.
!C
!C 	Avoid internal radii which are approximately the same size as the
!C 	cutter.  A good cutter path may not be found by the routine dependent
!C 	upon these results.
!C
!C 	Avoid situations where the slice will coincide with either the edge of
!C 	a surface or with a flat area on the part.  Results, in this case, will
!C 	be unpredictable.
!C
!C 	The Auto edge extension used in gouge check will effect the gouge check,
!C 	but will not introduce any additional cutter motion.  It is recommended
!C 	that it be used.
!C
!C 	This routine is not perfect, Cutter paths may need to be repaired to be
!C 	functional. 
!C
!C OPTIONS:
!C
!C 	Circle output is controlled by the named variable "3D Circle Output".
!C 	There are two methods available.
!C
!C 		WsLinearMoves - 0: Outputs straight line moves.
!C 		WsCircleMoves - 1: Outputs circular arc moves.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: z gouge surface")

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

ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zstep=NamedVarVal("3D Default Zstep",Ws3DSystemVars,0.125)
ZStepMethod=NamedVarVal("3D Z Step Method",Ws3DSystemVars,0.0)
Circle_output=NamedVarVal("3D Circle Output",Ws3DSystemVars,1.0)
Start:!
Make_cpath=1
Cutter_type=WsBallCutter
Offset=0
Taper=0

Current_pen=CurrentColor
Start:!

Stat=SetInputFormat("a2=",",CL,C,")
Stat=NamedVarStr("mill direction",WsSystemVars,"CL",Answer$)
Stat=SetEqualsValue(0,Answer$)
Stat=GetValue("MILL CLimb OR Conventional",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="C" THEN
	Mill_dir=WsConventional
	Pen_color=WsConvCutPen
ELSE
	Mill_dir=WsClimb
	Pen_color=WsClimbCutPen
END IF

Cutter_type=NamedVarVal("cutter type",WsSystemVars,Cutter_type)
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

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

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)

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

Z_level: !

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zstart)
Zstart=GetValue("Z START",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zend)
Zend=GetValue("Z END",Answer$)
IF ExitScript(Destination$) THEN Leave

IF Zend=Zstart THEN
	Zstep=0
ELSE
	Stat=SetInputFormat("n4=","")
	Stat=SetEqualsValue(Zstep)
	Zstep=GetValue("Z STEP",Answer$)
	IF ExitScript(Destination$) THEN Leave
END IF

Z_slice:!

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

Stat=SetPickMask(WsPickGeometry)

Z_Group=ZStepsPtGrp(ZStepMethod,Zstart,Zend,Zstep)

Stat=SetCurrentColor(Pen_color)
Cutterpath=GougeSliceSurf(Surface,Z_group,Tol,Mill_dir,Make_cpath,Circle_output)

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: Z SURFACE GOUGE"&CHR$(10)
	Stat=SurfaceName(Surface,Answer$)
	Datastr$=Datastr$&"SURFACE: "&Answer$&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$&"MILL DIRECTION: "&VAL$(Mill_dir)&CHR$(10)
	Datastr$=Datastr$&"CIRCLES: "&VAL$(Circle_output)&CHR$(10)
	Datastr$=Datastr$&"MAKE CPATH: "&VAL$(Make_cpath)&CHR$(10)
	Datastr$=Datastr$&"Z START: "&VAL$(Zstart)&CHR$(10)
	Datastr$=Datastr$&"Z END: "&VAL$(Zend)&CHR$(10)
	Datastr$=Datastr$&"Z STEP: "&VAL$(Zstep)&CHR$(10)
	Datastr$=Datastr$&"Z STEP METHOD: "&VAL$(Zstepmethod)&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)

	ActivateUndo
        Stat= AdvanceUndoNumber("z surface gouge check")
        Stat= AddItemToUndo(WsUBCreateMode,Cpath3d)
        Stat= AddGroupToUndo(WsUBCreateMode,Cutterpath,1,WsTrue)

END IF

Stat=DeleteGroup(Z_group,1)

GOTO Z_level

Leave:!
Stat=SetCurrentColor(Current_pen)
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
