
! script name: v3dcpaths.d/zigzagpart
! var_topic: zigzag rough part with zplane slices

!A
!A NAME:
!A
!A 	Zigzag
!A
!A DESCRIPTION:
!A
!A 	This script roughs a group of surfaces using zigzag roughing on
!A 	successive plane slices.  It is valid only for a standard endmill.
!A 	The Finish Allowance is on the sides only.  The Depth taper increases
!A 	finish allowance as the cut deepens.  A slice at a Z level must
!A 	generate closed contours.
!A
!B INSTRUCTIONS:
!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
!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 	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: Enter the Z depth increment.
!B
!B 		The Z depth calculation is controlled by the user named
!B 		variable 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 	ANGLE OF ZIGZAG LINES: Enter the angle of the zigzag cuts.
!B
!B 	PICK SURFACE GROUP: Pick the group of surfaces to be machined.
!B 		Surface Normal vectors must be properly aligned.
!B
!C OPTIONS:
!C
!C 	Circle output is controlled by the named variable "3D Circle Output".
!C
!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: zigzag part ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Finish_var$[256]
DIM Width_var$[256]
DIM Zstart_var$[256]
DIM Zend_var$[256]
DIM Zstep_var$[256]
DIM Angle_var$[256]
DIM Name$[256]

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


ActivateUndo
DEG
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zstep=NamedVarVal("3D Default Zstep",Ws3DSystemVars,0.125)
Depth_taper=NamedVarVal("3D Depth Taper",Ws3DSystemVars,(TAN(1.0)))
Circle_output=NamedVarVal("3D Circle Output",Ws3DSystemVars,1.0)
Layer_sw=NamedVarVal("3D Layer Switch",Ws3DSystemVars,1.0)
Level_clear=NamedVarVal("3D Level Clear",Ws3DSystemVars,1.0)
Level_fill=NamedVarVal("3D Level Fill",Ws3DSystemVars,1.0)
ZStepMethod=NamedVarVal("3D Z Step Method",Ws3DSystemVars,0.0)

Current_pen=CurrentColor

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

Start:!
Pen_color=WsZigzagCutPen
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:v3dcpaths.d/zigzagpart"
  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/zigzagpart"
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:!
Widthr= 0
Cutter_dia=GetUserValue(Tool,"Diameter:")
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
	Width_factor=GetVariableVal(Width_val)
	Widthr=Cutter_dia*Width_factor
END IF
IF NOT Widthr THEN Widthr=NamedVarVal("width of cut",WsSystemVars,0.0)

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
No_width_var= LastVariableInput(Width_var$)
Vwidth=CreateVariable("width of cut",WsSystemVars,Widthr)

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


Angle_ques:!
Pick_angle_line=0
Angle=NamedVarVal("zigzag angle",WsSystemVars,0.0)
Stat=SetAngleEquals(Angle)
Angle=InputAngle("ANGLE OF ZIGZAG LINES")
IF (ExitScript(Destination$)) THEN Leave
No_angle_var= LastVariableInput(Angle_var$)
L1= LastPickEnt
IF ValidGeometry(L1) THEN
        IF NOT ValidDependency(L1) THEN Angle_ques
        No_angle_var=0
        Stat= EntityNameCheck(L1,Angle_var$)
	Pick_angle_line=1
END IF
Vangle=CreateVariable("zigzag angle",WsSystemVars,Angle)

Z_slice:!

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 Z_slice
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=SetPickMask(WsPickGeometry)


Stat=SetCurrentColor(Pen_color)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Z zigzag pocket")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
IF No_width_var THEN
        V2= FreeParameter("Width cut",Width_cut)
ELSE
        V2= FreeParameter("Width cut",Width_var$)
END IF
IF No_angle_var THEN
	V11= FreeParameter("Angle",Angle)
ELSE
	IF pick_angle_line THEN
		V11= FreeParameter("Angle",LineAngle(Angle_var$))
	ELSE
		V11= FreeParameter("Angle",Angle_var$)
	END IF
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

V8= FreeParameter("Semi finish cut",1)
V9= FreeParameter("Semi finish mill direction",WsClimb)
V10= FreeParameter("Make circles",Circle_output)
V12= FreeParameter("Layer switch",Layer_sw)
V13= FreeParameter("Level clear",Level_clear)
V14= FreeParameter("Level fill",Level_fill)
V15= FreeParameter("Depth taper",Depth_taper)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,V9)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1)
Stat=Setup3dCutter(Mdef1)
Z_Group=ZStepsPtGrp(V4,V5,V6,V7)
Stat= PrintToVarRecord("PlotOn")
Path_group=ZigzagRoughPart(Part_group$,Z_group,V2,V11,V15,V3,V9,V8,V10,V12,V13,V14)
Stat= PrintToVarRecord("PlotOff")
Stat= AutoNameGroup(Path_group,Name$)
Stat=AssociateTool(Path_group,Tool)
Stat= DeleteEntity(Mdef1)
Stat= DeleteGroup(Z_group,1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Path_group) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("zigzag cut part")
	Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Z_level




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

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