
! script name: voffset.d/roughcorners
! var_topic: rough mill contour corners

!A
!A NAME:
!A
!A 	Rough Corners	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script will machine the corner areas of a contour
!A	previously roughed with a larger tool. Either the
!A	inside or outside of the contour can be roughed. The contour
!A	must be a closed contour. It is assumed that the contour will
!A	later be finished with an appropriate tool.
!A
!A~ image location - images/roughcorners.gif
!A
!B INSTRUCTIONS:
!B
!B	CORNER ROUGHING TOOL: Select the tool used to pick out the corners.
!B
!B	PREVIOUS ROUGHING TOOL: Select the tool used for the previous
!B				roughing operation.
!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 	FINISH ALLOWANCE: Enter the amount of material
!B 		to be left for a finish pass.
!B
!B 	PICK AN ELEMENT OF CONTOUR: Pick
!B 		an element on the contour to be machined.
!B
!B 	PICK WHICH SIDE: Pick which side of the
!B 		contour the path should be generated.
!B
!C NOTES:
!C
!C 	The cutter offset path is calculated from the cutter diameter and
!C 	the finish allowance.
!C
!C 	(offset=.5*cutter_diameter + finish_allowance)
!C
!C~ image location - images/roughcorners_dtl.gif
!C
!C	The following parameters are available:
!C
!C	Overcut: A positive number will add a tangent lead-in and lead-out
!C		 to each corner pass.
!C
!C	Zigzag cuts: A value of 0 will keep cuts in milling direction
!C		 and will connect the end of a path to the start of
!C		 the next in the same corner.
!C		 A value of 1 will connect the corner paths in a zigzag fashion.
!C		 A value of 2 will not connect paths.
!C
!C	Z order: A value of zero will do all corners on a z level first.
!C		 A value of 1 will do all z levels in a corner first.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: rough corners")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Boundary_name$[256]
DIM Tool_name$[256]
DIM Tool_name2$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Finish_var$[256]
DIM Depth_var$[256]
DIM Depthcut_var$[256]
DIM Rough_var$[256]
DIM Width_var$[256]
DIM Name$[256]

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

ActivateUndo
Current_pen=CurrentColor

IF Units= WsMetric THEN
        Overcut= 2.5
ELSE
        Overcut= 0.1
END IF

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:voffset.d/roughcorners"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("CORNER ROUGHING 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:voffset.d/roughcorners"
END IF

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

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


Prev_tool_ques:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
Stat=SetInputFormat("a99","")
Stat=GetValue("PREVIOUS ROUGHING TOOL ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
        LowerSelectBox
        GOTO Leave
END IF
E1= LastPickEnt
IF ValidGeometry(E1) THEN
	Rough_tool= GetAssocTool(E1)
	IF NOT ValidGeometry(Rough_tool) THEN Prev_tool_ques
	Stat= GetUserDataName(Rough_tool,Tool_name2$)
	IF Stat THEN Prev_tool_ques
ELSE
	Tool_name2$= Answer$
	Rough_tool=FindUserData(Answer$,WsToolData)
END IF
Stat=GetUserString(Rough_tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Prev_tool_ques
Cutter_dia2=GetUserValue(Rough_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
	Pen_color=WsConvCutPen
	Dcomp_dir= WsRight
ELSE
	Mill_dir=WsClimb
	Pen_color=WsClimbCutPen
	Dcomp_dir= WsLeft
END IF
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$)


Pen_color= CurrentColor
Stat=SetCurrentColor(Pen_color)



Pick_loop:!
Stat=SetPickMask(WsPickNoSurf)
E1=PickEntity("PICK AN ELEMENT OF CONTOUR")
IF (ExitScript(Destination$)) THEN Leave
Contour= ParentGroup(E1,0)
IF NOT ValidDependency(Contour) THEN Pick_loop
Closed=IsContClosed(Contour)
IF NOT Closed THEN Pick_loop
Stat= EntityNameCheck(Contour,Contour$)


Pt=InputFreePoint("PICK WHICH SIDE")
IF (ExitScript(Destination$)) THEN Leave

Side=OffsetLeftRight(E1,Pt)
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

Direction_sign=MillDirection(E1,Pt)
Dist=-Side*Offset

Direction=WsForward
IF (Direction_sign=1)AND(Mill_dir=WsConventional) THEN
	Direction=WsReverse
END IF
IF (Direction_sign=-1)AND(Mill_dir=WsClimb) THEN
	Direction=WsReverse
END IF
Stat=DeleteEntity(Pt)

Finish_depth:!
Finish_depth=NamedVarVal("finish depth",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_depth)
Stat=SetInputFormat("n4=","")
Finish_depth=GetValue("FINISH DEPTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_depth_var= LastVariableInput(Depth_var$)
Vdepth=CreateVariable("finish depth",WsSystemVars,Finish_depth)


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

Side1= -Side
Stat=SetCurrentColor(Pen_color)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Rough corners "&Tool_name$)
Stat= SetRecDelLastExec
Stat= NullReference(Contour$)
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
	V3= FreeParameter("Width cut",Width_cut)
ELSE
	V3= FreeParameter("Width cut",Width_var$)
END IF
V4= FreeParameter("Side",Side1)
IF No_depth_var THEN
	V5= FreeParameter("Finish depth",Finish_depth)
ELSE
	V5= FreeParameter("Finish depth",Depth_var$)
END IF
IF No_depthcut_var THEN
	V6= FreeParameter("Depth cut",Depth_cut)
ELSE
	V6= FreeParameter("Depth cut",Depthcut_var$)
END IF

Rough_tool=FindUserData(Tool_name2$,WsToolData)
V2= FreeParameter("Rough stock",0.5*ToolDiameter(Rough_tool))
IF Mill_dir= WsConventional THEN
        Mdef1= ToolMillingDef(Rough_tool,WsConventional)
ELSE
        Mdef1= ToolMillingDef(Rough_tool,WsClimb)
END IF
Stat=AssociateTool(Mdef1,Rough_tool)
Stat= MillDefCutInfo(Mdef1,V4,V3,V1,V2,1)
Stat= MillDefZInfo(Mdef1,1,V5,V6,V0)

Corner_tool=FindUserData(Tool_name$,WsToolData)
IF Mill_dir= WsConventional THEN
        Mdef2= ToolMillingDef(Corner_tool,WsConventional)
ELSE
        Mdef2= ToolMillingDef(Corner_tool,WsClimb)
END IF
Stat=AssociateTool(Mdef2,Corner_tool)
Stat= MillDefCutInfo(Mdef2,V4,V3,V1,V2,1)
V7= FreeParameter("Z order",1)
Stat= MillDefZInfo(Mdef2,1,V5,V6,V0,0,0,V7)
V8= FreeParameter("Dia comp dir (+1 left  -1 right)",Dcomp_dir)
V9= FreeParameter("Overcut",Overcut)
V10= FreeParameter("Zigzag cuts",0)
Paths=MillCorners(Contour$,Mdef1,Mdef2,V9,V10,WsCircleMoves)
Stat= AutoNameGroup(Paths,Name$)
Stat= UnAssocMachDef(Paths,Mdef2)
Stat= SetDCompDirection(Paths,V8)
Stat=DeleteEntityList(Mdef1,Mdef2)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
	Stat= AnimateTool(Corner_tool,Paths)
	ActivateUndo
	Stat= AdvanceUndoNumber("rough corners")
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF


GOTO Pick_loop

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