
! script name: v3dcpaths.d/ taperedpocket
! var_topic:  generated a tapered contour pocket

!A
!A NAME:
!A
!A 	Tapered Contour Pocket
!A
!A DESCRIPTION:
!A	
!A 	This script genetated the cuts for a tapered contour pocket.
!A 	The depth is positive for z negative.
!A 	The taper angle is positive to the right side of the contour.
!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 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	DRIVE CONTOUR: 
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: Tapered contour pocket ")
DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Island$[256]
DIM Finish_var$[256]
DIM Width_var$[256]
DIM Taper_var$[256]
DIM Depth_var$[256]
DIM Pwidth_var$[256]
DIM Name$[256]

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


ActivateUndo
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Units=CurrentUnits

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

Start:!
Offset=0
Taper=0
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:v3dcpaths.d/taperedpocket"
  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/taperedpocket"
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:!
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
        Pen_color=WsConvCutPen
ELSE
        Mill_dir=WsClimb
        Pen_color=WsClimbCutPen
END IF

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)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Width_cut)
Pwidth_cut=GetValue("POCKET WIDTH CUT",Answer$)
IF ExitScript(Destination$) THEN Leave
No_pwidth_var= LastVariableInput(Pwidth_var$)

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)

Stat=SetPickMask(WsPickGeometry)
Pick_contour: !
Stat=SetInputFormat("n4","")
Drive_contour=PickGroupEnt("DRIVE CONTOUR",0)
IF ExitScript(Destination$) THEN Leave
IF (NOT ValidDependency(Drive_contour)) THEN Pick_contour
Stat= EntityNameCheck(Drive_contour,Contour$)
IF NOT LEN(Contour$) THEN Boundary_loop
Stat=Highlight(Drive_contour,WsPickPen,WsDirArrowOff)

Ask_islands:!
Stat=SetInputFormat("n0=","")
Stat=SetEqualsValue(0.0)
Nisles=GetValue("NUMBER OF ISLANDS",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4","")
Islands=CreateGroup("ISLANDS")
FOR I=1 TO Nisles
        Stat=SetPickMask(WsPickNoSurf)
Pick_isle:Isle=PickGroupEnt("PICK ISLAND #"&VAL$(I))
        IF ExitScript(Destination$) THEN Leave
        Stat=SetPickMask(WsPickGeometry)
        IF (NOT ValidDependency(Isle)) THEN Pick_isle
        Stat= EntityNameCheck(Isle,Island$)
        IF NOT LEN(Island$) THEN Pick_isle
        Stat=AppendToGroup(Islands,Isle)
NEXT I

Taper_angle:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Taper_angle)
Taper_angle=GetValue("TAPER ANGLE",Answer$)
IF ExitScript(Destination$) THEN Leave
No_taper_var= LastVariableInput(Taper_var$)


Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zdepth)
Zdepth=GetValue("Z DEPTH",Answer$)
IF ExitScript(Destination$) THEN Leave
No_depth_var= LastVariableInput(Depth_var$)

Direction2= WsForward

Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Taperered Contour Pocket")
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
Isles=CreateGroup("")
RecordFunctionOff
FOR I= 1 TO Nisles
        Isle= GroupElement(Islands,I)
        Stat= EntityNameCheck(Isle,Island$)
        RecordFunctionOn
        Stat= AppendToGroup(Isles,Island$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
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_pwidth_var THEN
        V3= FreeParameter("Pocket width cut",Pwidth_cut)
ELSE
        V3= FreeParameter("Pocket width cut",Pwidth_var$)
END IF
IF No_taper_var THEN
        V4= FreeParameter("Taper Angle",Taper_angle)
ELSE
        V4= FreeParameter("Taper Angle",Taper_var$)
END IF
IF No_depth_var THEN
        V5= FreeParameter("Z Depth",Zdepth)
ELSE
        V5= FreeParameter("Z Depth",Depth_var$)
END IF
V6= FreeParameter("Tolerance",Tol)
IF Mill_dir= WsConventional THEN
        V7= FreeParameter("Mill direction",WsConventional)
ELSE
        V7= FreeParameter("Mill direction",WsClimb)
END IF
V9= FreeParameter("Direction2",Direction2)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,V7)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1,0.0,WsCircleMoves)
Stat=Setup3dCutter(Mdef1)
Stat= PrintToVarRecord("PlotOn")
Cutter_path=TaperContourPocket(Contour$,Isles,V7,V4,V5,V9,V2,V3,V6)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntityList(Mdef1,Isles)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("tapered pocket")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
Stat=DeleteEntity(Islands)
GOTO Start

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