
! script name: v3dcpaths.d/ tapercontcuts
! var_topic:  generated cuts along a specially oriented tapered contour

!A
!A NAME:
!A
!A 	Tapered Contour Cuts
!A
!A DESCRIPTION:
!A	
!A 	This script genetated the cuts for a tapered contour.
!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 cuts ")

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 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/tapercontcuts"
  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/tapercontcuts"
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)


Stat=SetPickMask(WsPickGeometry)
Pick_contour: !
Stat=SetInputFormat("a1",",N,")
Drive_contour=PickGroupEnt("DRIVE CONTOUR, None ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="N" THEN 
	Has_exterior=0
	GOTO Ask_islands
END IF
IF (NOT ValidDependency(Drive_contour)) THEN Pick_contour
Stat= EntityNameCheck(Drive_contour,Contour$)
IF NOT LEN(Contour$) THEN Pick_contour
Stat=Highlight(Drive_contour,WsPickPen,WsDirArrowOff)
Has_exterior=1

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



Direction1= WsForward
Sign1= 1
Direction2= WsForward

Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Taperered Contour Cuts")
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_taper_var THEN
        V3= FreeParameter("Taper Angle",Taper_angle)
ELSE
        V3= FreeParameter("Taper Angle",Taper_var$)
END IF
IF No_depth_var THEN
        V4= FreeParameter("Z Depth",Zdepth)
ELSE
        V4= FreeParameter("Z Depth",Depth_var$)
END IF
V5= FreeParameter("Tolerance",Tol)
V6= FreeParameter("Direction1",Direction1)
V7= FreeParameter("Sign1",Sign1)
V8= FreeParameter("Direction2",Direction2)

Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1,0.0,WsCircleMoves)
Stat=Setup3dCutter(Mdef1)
Stat= PrintToVarRecord("PlotOn")
IF Has_exterior THEN
	Cutter_path=TaperContourCuts(Contour$,Isles,V6,V7,V3,V4,V8,V2,V5)
ELSE
	Cutter_path=TaperContourCuts(0,Isles,V6,V7,V3,V4,V8,V2,V5)
END IF
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("taper contour cuts")
	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
