
! script name: pkvmach.d/pkzigzagface
! var_topic: generate zigzag pocket for face

!A
!A NAME:
!A
!A 	Zigzag Face	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates a path for roughing an area using
!A 	a back and forth tool motion at some angle.
!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 	WIDTH OF CUT: Enter the distance between successive passes.
!B 		The width of cut should not exceed the smallest X or
!B 		Y component of the most acute angle generated by the
!B 		zigzag routine, without the danger of stock being left.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for
!B 		a finish pass.
!B
!B 	ANGLE OF ZIGZAG LINES: Enter the angle at which the cutter will
!B 		travel as it goes back and forth across the area to be
!B 		roughed.
!B
!B 	DEPTH OF CUT: Enter the depth of cut.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: zigzag rough face ")


DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Face_name$[256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Iname$[256]
DIM Stock_var$[256]
DIM Depth_var$[256]
DIM Depthcut_var$[256]
DIM Width_var$[256]
DIM Angle_var$[256]
DIM Name$[256]


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

ActivateUndo
Current_pen=CurrentColor

Interiors_on=NamedVarVal("Interior islands on",WsSystemVars,1.0)
Boundary_off=NamedVarVal("Boundary offset off",WsSystemVars,0.0)
V1=CreateVariable("Interior islands on",WsSystemVars,Interiors_on)
V2=CreateVariable("Boundary offset off",WsSystemVars,Boundary_off)


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

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:pkvmach.d/pkzigzagface"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("PATH TOOL, New_tool ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
	LowerSelectBox
	GOTO Leave
END IF
IF Answer$="New_tool" THEN
	LowerSelectBox
  	LOAD "vtools.d/readmilltool:pkvmach.d/pkzigzagface"
END IF

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
Cutter_dia=GetUserValue(Tool,"Diameter:")
LowerSelectBox

Mill_direction:!
Stat=SetInputFormat("a2=",",CL,C,")
Stat=NamedVarStr("mill direction",WsSystemVars,"CL",Answer$)
Stat=SetEqualsValue(0,Answer$)
Mill_dir=GetValue("MILL CLimb OR Conventional",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF (Answer$="C") THEN
	Mill_dir=WsConventional
	Dcomp_dir= WsRight
ELSE
	Mill_dir=WsClimb
	Dcomp_dir= WsLeft
END IF
Pen_color=WsZigzagCutPen
Stat=SetCurrentColor(Pen_color)

Fin_allow:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)

Width_cut:!
Width_cut= 0
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
	Width_factor=GetVariableVal(Width_val)
	Width_cut=Cutter_dia*Width_factor
END IF
IF NOT Width_cut THEN Width_cut=NamedVarVal("width of cut",WsSystemVars,0.0)

Stat=SetEqualsValue(Width_cut)
Stat=SetInputFormat("n4=","")
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Width_cut=ABS(Width_cut)
Vwidth=CreateVariable("width of cut",WsSystemVars,Width_cut)
No_width_var= LastVariableInput(Width_var$)


Stat=SetInputFormat("n4=","")
Finish_allow=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_allow)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finish_allow)
No_finish_var= LastVariableInput(Finish_var$)

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)

Pen_color= CurrentColor
Stat=SetCurrentColor(Pen_color)



Pick_loop:!
Face_ent=WPKPickEntity("PICK PLANAR FACE TO BE POCKETED",WsPKPickFace)
IF (ExitScript(Destination$)) THEN Leave
Stat= EntityNameCheck(Face_ent,Face_name$)

IF IsSurfacePlanar(Face_ent)= WsFalse THEN Pick_loop

Stock_depth:!
Stock_depth=NamedVarVal("stock on face",WsSystemVars,0.0)
Stat=SetInputFormat("n4=","")
Stat=SetDepthEquals(Stock_depth)
Stock_depth=InputDepth("STOCK ON FACE")
IF (ExitScript(Destination$)) THEN Leave
No_stock_var= LastVariableInput(Stock_var$)
Vstock=CreateVariable("stock on face",WsSystemVars,Stock_depth)


Depth_cut:!
Depth_cut=NamedVarVal("depth of cut",WsSystemVars,0.0)
Stat=SetDepthEquals(Depth_cut)
Stat=SetInputFormat("n4=","")
Depth_cut=InputDepth("DEPTH OF CUT")
IF (ExitScript(Destination$)) THEN Leave
Depth_cut=ABS(Depth_cut)
Vdepth=CreateVariable("depth of cut",WsSystemVars,Depth_cut)
No_depthcut_var= LastVariableInput(Depthcut_var$)


Stat=SetCurrentColor(Pen_color)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("POCKET: Zigzag face")
Stat= SetRecDelLastExec
V0= FreeParameter("Bottom allowance",0)
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
	V3= FreeParameter("Angle",Angle)
ELSE
	IF pick_angle_line THEN
		V3= FreeParameter("Angle",LineAngle(Angle_var$))
	ELSE
		V3= FreeParameter("Angle",Angle_var$)
	END IF
END IF
IF No_stock_var THEN
        V4= FreeParameter("Stock on face",Stock_depth)
ELSE
        V4= FreeParameter("Stock on face",Stock_var$)
END IF
IF No_depthcut_var THEN
	V5= FreeParameter("Depth cut",Depth_cut)
ELSE
	V5= FreeParameter("Depth cut",Depthcut_var$)
END IF
V6= FreeParameter("Semifinish cut on",1)
V7= FreeParameter("Exterior offset off",Boundary_off)
V8= FreeParameter("Island offset off",0)
V9= FreeParameter("Islands on",Interiors_on)
Tool=FindUserData(Tool_name$,WsToolData)
IF Mill_dir= WsConventional THEN
        Mdef1= ToolMillingDef(Tool,WsConventional)
ELSE
        Mdef1= ToolMillingDef(Tool,WsClimb)
END IF
PlotOff
Work1= CurrentWorkplane
Stat= SetWorkplane(0)
Trans1= SurfNormalTrans(Face_name$,0.5,0.5,WsForward)
Stat= SetWorkplane(Trans1)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1)
Stat= MillDefZInfo(Mdef1,1,V4,V5,V0)
G1= WPKConvertEntity(Face_name$,WsPKConvertTrims,WsFalse)
Pm=PointXYZ(0.0,0.0,V4)
T1=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
Stat=TransGroupTrans(G1,T1,0,1)
G2= FindClosedContours(G1,WsFalse,WsTrue)
G3= CreateBoundarys(G2)
Bdy1= GroupElement(G3,1)
Paths=ZigZagMillBoundary(Bdy1,Mdef1,V3,V6,V7,V8,WsCircleMoves,V9)
Stat= AutoNameGroup(Paths,Name$)
Stat= UnAssocMachDef(Paths,Mdef1)
V10= FreeParameter("Dia comp dir (+1 left  -1 right)",Dcomp_dir)
Stat= SetDCompDirection(Paths,V10)
Stat= DeleteBoundary(Bdy1)
Stat= DeleteSubGroups(G2)
Stat= DeleteEntityList(Pm,Mdef1,G1,G3)
Stat= SetWorkplane(Work1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
PlotOn
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
	Stat= PlotEntity(Paths)
	Stat= AnimateTool(Tool,Paths)
	ActivateUndo
	Stat= AdvanceUndoNumber("zigzag pocket face")
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pick_loop

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

