
! script name: v3dcpaths.d/zcontgouge
! var_topic: contour gouge with Z limit planes

!A
!A NAME:
!A
!A 	Gouge: Z Contour	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script projects a path onto a group of surfaces.  The center of
!A 	the cutter is constrained to move along the contour while staying in
!A 	contact with the part.  The corresponding cutter path is generated with
!A 	gouge avoidance.
!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 the part boundary.
!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 	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 	Z START: Start of first pass in Z.
!B
!B 	Z END: Enter the last pass in Z.
!B
!B 	Z STEP: Enter the 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
!B 	PROJECT CONTOUR # : Pick the contour to be projected onto the part.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B

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

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Contour$[256]
DIM Zstart_var$[256]
DIM Zend_var$[256]
DIM Zstep_var$[256]
DIM Finish_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)
Layer_sw=NamedVarVal("3D Layer Switch",Ws3DSystemVars,1.0)
Level_clear=NamedVarVal("3D Level Clear",Ws3DSystemVars,1.0)
ZStepMethod=NamedVarVal("3D Z Step Method",Ws3DSystemVars,0.0)
ZRoughMethod=NamedVarVal("3D Z Rough Method",Ws3DSystemVars,1.0)
Ext_dist=NamedVarVal("3D Auto Extend Dist",Ws3DSystemVars,0.010)
Units=CurrentUnits

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

Start:!
Past_dia:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:v3dcpaths.d/zcontgouge"
  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/zcontgouge"
END IF
Have_tool:!
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques



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)

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



Pick_loop:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,")
Part_group=PickGroupEnt("PICK SURFACE GROUP, Name ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "N" THEN
        GOSUB Get_named_group
END IF
IF NOT ValidDependency(Part_group) THEN Pick_loop
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)


Cut_part:!
Group=CreateGroup("TEMP")
Stat=SetPickMask(WsPickGeometry)
Cont_count=0
Pick_contour: !
Stat=SetInputFormat("a1",",P,")
Contour=PickGroupEnt("PROJECT CONTOUR #"&VAL$(Cont_count+1)&", Proceed ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="P" THEN Start_variation
IF (NOT ValidDependency(Contour)) THEN Pick_contour
Stat= EntityNameCheck(Contour,Contour$)
IF NOT LEN(Contour$) THEN Pick_contour
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)
Stat=AppendToGroup(Group,Contour)
Cont_count=Cont_count+1
GOTO Pick_contour



Start_variation:!

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Z Contour Gouge Check")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
Contours=CreateGroup("")
RecordFunctionOff
FOR I= 1 TO Cont_count
        Contour= GroupElement(Group,I)
        Stat= EntityNameCheck(Contour,Contour$)
        RecordFunctionOn
        Stat= AppendToGroup(Contours,Contour$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Z step method",ZStepMethod)
IF No_zstart_var THEN
        V6= FreeParameter("Z start",Zstart)
ELSE
        V6= FreeParameter("Z start",Zstart_var$)
END IF
IF No_zend_var THEN
        V7= FreeParameter("Z end",Zend)
ELSE
        V7= FreeParameter("Z end",Zend_var$)
END IF
IF No_zstep_var THEN
        V8= FreeParameter("Z step",Zstep)
ELSE
        V8= FreeParameter("Z step",Zstep_var$)
END IF
V9= FreeParameter("Extension dist",Ext_dist)
V10= FreeParameter("Z rough method",ZRoughMethod)
V11= FreeParameter("Layer switch",Layer_sw)
V12= FreeParameter("Level clear",Level_clear)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,0.0,V1)
Stat=Setup3dCutter(Mdef1)
Z_Group=ZStepsPtGrp(V5,V6,V7,V8)
Stat= PrintToVarRecord("PlotOn")
Cpath_group=ZContGrpGouge(0,Part_group$,Z_group,Contours,0,V4,V10,V11,V12,V9)
Stat= PrintToVarRecord("PlotOff")
Stat= AutoNameGroup(Cpath_group,Name$)
Stat=AssociateTool(Cpath_group,Tool)
Stat= DeleteGroup(Z_group,WsTrue)
Stat= DeleteEntitylist(Mdef1,Contours)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cpath_group) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("z contour gouge check")
	Stat= AddGroupToUndo(WsUBCreateMode,Cpath_group,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
Stat= DeleteEntity(Group)
GOTO Pick_loop


Leave:!
Stat=SetInputFormat("n4","")
LowerSelectBox
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF


Get_named_group:!
Stat=ListGrpNames("SURFACE GROUPS","selected group","",WsSurfaceGroup)
IF Stat=1 THEN Leave_name

Stat=SetInputFormat("a99","")
Stat=GetValue("SURFACE GROUP NAME",Answer$)

IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave_name
Part_group=FindNamedGrp(Answer$)
IF NOT ValidGeometry(Part_group) THEN Get_named_group
Grp_type= GroupType(Part_group)
IF Grp_type <> WsSurfaceGroup THEN Get_named_group
LowerSelectBox

Leave_name:!
Stat=SetInputFormat("n4","")

RETURN


END
