
! script name: voffsets.d/trochoidmill
! var_topic: trochoid mill contour

!A
!A NAME:
!A
!A 	Trochoid Mill		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates a trochoidal milling path. This type of path
!A	is used in high speed machining to maintain a constant feed rate
!A	and maintain a uniform removal of material.
!A
!A~ image location - images/trochoidmilling.gif
!A
!B INSTRUCTIONS:
!B
!B 	MILL CLimb OR Conventional:
!B
!B
!B 	FINISH ALLOWANCE: Enter the amount of
!B 		material to be left for a finish pass.
!B
!B	OUTER RADIUS: Radius of arc swung by outer edge of cutter during
!B			the trochoidal cut.
!B
!B	WIDTH OF CUT: Amount of material removed with each revolution.
!B
!B 	PICK AN ELEMENT OF CONTOUR TO BE MACHINED: Pick
!B 		an element on the contour to be offset.
!B
!B 	PICK WHICH SIDE: Pick which side of the
!B 		contour the path should be generated.
!B
!B 	FINISH DEPTH: Enter the finish depth.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: trochoid mill")

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

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 "vtools.d/readmilltool:voffset.d/trochoidmill"
  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:voffset.d/trochoidmill"
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
	Pen_color=WsConvCutPen
	Dcomp_dir= WsRight
ELSE
	Mill_dir=WsClimb
	Pen_color=WsClimbCutPen
	Dcomp_dir= WsLeft
END IF
Stat=SetCurrentColor(Pen_color)

Outer_radius:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(.875*Cutter_dia)
Outer_radius=GetValue("OUTER RADIUS",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_radius_var= LastVariableInput(Radius_var$)

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


Fin_allow:!
Vcutter=CreateVariable("cutter diameter",WsSystemVars,Cutter_dia)
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 TO BE MACHINED")
IF (ExitScript(Destination$)) THEN Leave
Contour= ParentGroup(E1,0)
IF NOT ValidDependency(Contour) 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)

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)

Stat=SetCurrentColor(Pen_color)
Rough_stock= 0.0
Side1= -Side

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Trochoid mill "&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
V2= FreeParameter("Rough stock",Rough_stock)
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
V6= FreeParameter("Depth cut",Depth_cut)
IF No_radius_var THEN
        V7= FreeParameter("Outer radius",Outer_radius)
ELSE
        V7= FreeParameter("Outer radius",Radius_var$)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
IF Mill_dir= WsConventional THEN
	Mdef1= ToolMillingDef(Tool,WsConventional)
ELSE
	Mdef1= ToolMillingDef(Tool,WsClimb)
END IF
V8= FreeParameter("Contour direction",1)
V9= FreeParameter("Circle sw",1)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V4,V3,V1,V2,1)
Stat= MillDefZInfo(Mdef1,1,V5,V6,V0)
Paths=TrochoidMilling(Contour$,Mdef1,V7,V8,V9)
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)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Paths)
	ActivateUndo
	Stat= AdvanceUndoNumber("trochoid mill")
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF


GOTO Pick_loop

Leave:!
Stat=SetPickMask(WsPickGeometry)
LowerSelectBox
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

