
! script name: wscmill.d/slotcontour
! var_topic: pocket mill a slot.

!A
!A NAME:
!A
!A 	Pocket Mill Slot
!A
!A DESCRIPTION:
!A
!A 	This script pocket mills an slot
!A
!B INSTRUCTIONS:
!B
!B 	WIDTH OF CUT: Enter the actual width of cut (the distance
!B 		between successive offsets).
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for
!B 		a finish pass.
!B
!B 	MILL CLimb OR Conventional:
!B
!B 		C - Conventional mill will generate a path which is clockwise for an interior cut and counterclockwise for an exterior cut.
!B 		CL - Climb mill will generate a path which is clockwise for an exterior cut and counterclockwise for an interior cut.
!B
!C NOTES:
!C
!C 	The cutter offset path is calculated from the cutter diameter
!C 	and the finish allowance.
!C
!C 	( offset=.5*cutter_diameter + finish_allowance)
!C
!C      This csript can generate the following versions of a slot:
!C
!C 		type= 1: Full slot, circle center to circle center.
!C 		type= 2: Full slot, end to end.
!C 		type= 3: Upper half, circle center to circle center.
!C 		type= 4: Upper half, end to end.
!C 		type= 5: Lower half, circle center to circle center.
!C 		type= 6: Lower half, end to end.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: pocket slot")

DIM Destination$[256]
DIM Answer$[256]
DIM Datastr$[1256]
DIM Tool_name$[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 Xlength_var$[256]
DIM Ylength_var$[256]
DIM Point1$[256]
DIM Point2$[256]
DIM Radius_var$[256]
DIM Name$[256]

Var_object= CurrentVarObject
IF NOT ValidGeometry(Var_object) THEN
	Var_object=OpenVarObject("Program")
END IF
! Var_object= CheckVarObject
! IF (ExitScript(Destination$)) THEN Leave

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

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "wscmill.d/readmilltool:wscmill.d/slotcontour"
  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 "wscmill.d/readmilltool:wscmill.d/slotcontour"
END IF

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

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

Mill_direction:!
Stat=SetInputFormat("a2b=",",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 (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Start
        GOTO Leave
END IF
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

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("n4b=","")
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Mill_direction
        GOTO Leave
END IF
Width_cut=ABS(Width_cut)
Vwidth=CreateVariable("width of cut",WsSystemVars,Width_cut)
No_width_var= LastVariableInput(Width_var$)


Fin_allow:!
Stat=SetInputFormat("n4b=","")
Finish_allow=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_allow)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Width_cut
        GOTO Leave
END IF
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finish_allow)
No_finish_var= LastVariableInput(Finish_var$)

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


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


Slot_data:!
Ask_radius:Stat=SetInputFormat("n4b=","")
Stat= SetEqualsValue(Radius)
Radius=InputRadius("RADIUS ")
IF ExitScript(Destination$) THEN Leave
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Depth_cut
        GOTO Leave
END IF
No_radius_var= LastVariableInput(Radius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
         IF NOT ValidDependency(C1) THEN Ask_radius
         No_radius_var=0
         Stat= EntityNameCheck(C1,Radius_var$)
END IF

Pick_point1:Stat=SetInputFormat("n4b=","")
Stat= SetInputFormat("n4b","")
Stat= PointInputMode(1)
Ps=XyzInput("START POINT",WsXyzInput)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Slot_data
        GOTO Leave
END IF
IF NOT ValidGeometry(Ps) THEN Pick_point1
Stat=SetLastPoint(Ps)
E1= LastPickEnt
Para1= LastPickPara
Pick_return1= CurrPickReturn
IF NOT ValidDependency(E1) THEN
        E1= CopyEntity(Ps)
        Stat= EntityNameCheck(E1,Point1$)
        Ps_created=WsTrue
ELSE
        Stat= EntityNameCheck(E1,Point1$)
        Ps_created=WsFalse
END IF
Stat=DeleteEntity(Ps)

Pick_point2:Stat=SetInputFormat("n4b=","")
Stat= SetInputFormat("n4b","")
Stat= PointInputMode(1)
Pe=XyzInput("END POINT",WsXyzInput)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Pick_point1
        GOTO Leave
END IF
IF NOT ValidGeometry(Pe) THEN Pick_point2
Stat=SetLastPoint(Pe)
E2= LastPickEnt
Para2= LastPickPara
Pick_return2= CurrPickReturn
IF NOT ValidDependency(E2) THEN
        E2= CopyEntity(Pe)
        Stat= EntityNameCheck(E2,Point2$)
        Pe_created=WsTrue
ELSE
        Stat= EntityNameCheck(E2,Point2$)
        Pe_created=WsFalse
END IF
Stat=DeleteEntity(Pe)

Stat=SetCurrentColor(Pen_color)
Stat= GenerateVarName(WsGroupEnt,Name$)
Stat= GenerateVarName(WsGroupEnt,Contour$)
RecordFunctionOn
Stat= RecordDescr("SLOT POCKET: "&Tool_name$)
Stat= SetRecDelLastExec
SELECT Pick_return1
CASE WsGeometryEnd
        Ps= ParameterReturnPt(Point1$,Para1,WsGeometryEnd)
CASE WsGeometryMid
        Ps= ParameterReturnPt(Point1$,Para1,WsGeometryMid)
CASE WsGeomCenter
        Ps= ParameterReturnPt(Point1$,Para1,WsGeomCenter)
END SELECT
SELECT Pick_return2
CASE WsGeometryEnd
        Pe= ParameterReturnPt(Point2$,Para2,WsGeometryEnd)
CASE WsGeometryMid
        Pe= ParameterReturnPt(Point2$,Para2,WsGeometryMid)
CASE WsGeomCenter
        Pe= ParameterReturnPt(Point2$,Para2,WsGeomCenter)
END SELECT
V1= FreeParameter("Type",Slot_type)
IF No_radius_var THEN
        V2= FreeParameter("Start radius",Radius)
        V3= FreeParameter("End radius",Radius)
        Contour= SlotTwoPoints(Ps,Pe,V2,V3,V1)
ELSE
        Contour= SlotTwoPoints(Ps,Pe,Radius_var$,Radius_var$,V1)
END IF
Stat= DeleteEntityList(Ps,Pe)
Stat= AutoNameGroup(Contour,Contour$)
Stat= SetColor(Contour,Current_pen)
Stat= DeleteEntity(Pc)
Isles=CreateGroup("")
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_depth_var THEN
	V3= FreeParameter("Finish depth",Finish_depth)
ELSE
	V3= FreeParameter("Finish depth",Depth_var$)
END IF
IF No_depthcut_var THEN
	V4= FreeParameter("Depth cut",Depth_cut)
ELSE
	V4= FreeParameter("Depth cut",Depthcut_var$)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
IF Mill_dir= WsConventional THEN
        Mdef1= ToolMillingDef(Tool,WsConventional)
ELSE
        Mdef1= ToolMillingDef(Tool,WsClimb)
END IF
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,V2,V1)
Stat= MillDefZInfo(Mdef1,1,V3,V4,V0)
Paths=PocketMilling(Contour,Isles,Mdef1,WsCircleMoves)
Stat= AutoNameGroup(Paths,Name$)
Stat= UnAssocMachDef(Paths,Mdef1)
V7= FreeParameter("Dia comp dir (+1 left  -1 right)",Dcomp_dir)
Stat= SetDCompDirection(Paths,V7)
Stat= DeleteEntity(Mdef1)
Stat= DeleteEntity(Isles)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
Stat=SetCurrentColor(Current_pen)
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
! 	Stat= AnimateTool(Tool,Paths)
	ActivateUndo
	Stat= AdvanceUndoNumber("pocket mill slot")
	IF Ps_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point1$)
	IF Pe_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point2$)
	Stat= AddGroupToUndo(WsUBCreateMode,Contour,1,WsTrue)
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pick_point1

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

