
! script name: v3dcpaths.d/zgougesurf
! var_topic: cuts a surface using Z slices with gouge check

!A
!A NAME:
!A
!A 	Gouge: Slice Surface  VARIATIONAL
!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 	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 Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Surface$[256]
DIM Finish_var$[256]
DIM Zstart_var$[256]
DIM Zend_var$[256]
DIM Zstep_var$[256]
DIM Name$[256]

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

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)
Make_cpath=1
Current_pen=CurrentColor

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:v3dcpaths.d/zgougesurf"
  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
LowerSelectBox
IF Tool_name$="New_tool" THEN
	LOAD "vtools.d/readmilltool:v3dcpaths.d/zgougesurf"
END IF
Have_tool:!
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques

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

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$)

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

Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
VWidth=CreateVariable("width of cut",WsSystemVars,Width_cut)

Z_level: !
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zstart)
Zstart=GetValue("Z START",Answer$)
IF ExitScript(Destination$) THEN Leave
No_zstart_var= LastVariableInput(Zstart_var$)

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

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


Z_slice: !

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

Stat=SetPickMask(WsPickGeometry)
Stat=SetCurrentColor(Pen_color)

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Z Slice Surface with gouge check ")
Stat= SetRecDelLastExec
Stat= NullReference(Surface$)
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
V3= FreeParameter("Tolerance",Tol)
V4= FreeParameter("Z step method",ZStepMethod)
IF No_zstart_var THEN
        V5= FreeParameter("Z start",Zstart)
ELSE
        V5= FreeParameter("Z start",Zstart_var$)
END IF
IF No_zend_var THEN
        V6= FreeParameter("Z end",Zend)
ELSE
        V6= FreeParameter("Z end",Zend_var$)
END IF
IF No_zstep_var THEN
        V7= FreeParameter("Z step",Zstep)
ELSE
        V7= FreeParameter("Z step",Zstep_var$)
END IF
IF Mill_dir= WsConventional THEN
        V9= FreeParameter("Mill direction",WsConventional)
ELSE
        V9= FreeParameter("Mill direction",WsClimb)
END IF
V10= FreeParameter("Make circles",Circle_output)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,V9)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,0,V1)
Stat=Setup3dCutter(Mdef1)
Z_Group=ZStepsPtGrp(V4,V5,V6,V7)
Stat= PrintToVarRecord("PlotOn")
Cutter_path=GougeSliceSurf(Surface$,Z_group,V3,V9,WsTrue,V10)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntity(Mdef1)
Stat= DeleteGroup(Z_group,WsTrue)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
        ActivateUndo
        Stat= AdvanceUndoNumber("z slice surface")
        Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF


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


END
