
! script name: offsets.d/contourmill
! topic: generate cutter path associated with tool

!A
!A NAME:
!A
!A 	Contour Mill
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutter path that is
!A 	associated with a specific tool.  
!A
!B INSTRUCTIONS:
!B
!B 	INPUT TOOL ID NAME: The selection box for the TOOLS
!B 		available will automatically appear for a selection
!B 		to be made.  The tool selected must be an endmill.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left
!B 		for a finish pass.
!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 	PICK AN ELEMENT OF CONTOUR TO BE MACHINED: Pick an
!B 		element on the contour to be machined.
!B
!B 	PICK WHICH SIDE: Pick which side of the contour the path
!B 		should be generated.
!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 	The cutter diameter is automatically assigned the value from
!C 	the tool file associated with the specified tool.
!C

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

DIM Destination$[256]
DIM Answer$[256]
DIM Toolid$[256]
DIM Name$[256]

ActivateUndo
Toolbox:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat=1 THEN
	Destination$="offsets.d/offset"
	GOTO Leave
END IF

Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT TOOL ID NAME",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN Leave
Name$=Answer$

Tool=FindUserData(Name$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
Dia=GetUserValue(Tool,"Diameter:")

IF NOT POS(Toolid$, "MILL") THEN Toolbox

LowerSelectBox

Stat=SetInputFormat("n4=","")
Finishr=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetEqualsValue(Finishr)
Finish_allowance=GetValue("FINISH ALLOWANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Finishr=Finish_allowance

Offset=ABS(Dia+Finish_allowance)

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

Stat=SetInputFormat("n4","")

Pick_loop:!
Stat=SetPickMask(WsPickNoSurf)
E1=PickEntity("PICK AN ELEMENT OF CONTOUR TO BE MACHINED")
IF (ExitScript(Destination$)) THEN Leave
Stat=SetPickMask(WsPickGeometry)


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

S1=OffsetLeftRight(E1,Pt)
Stat=Highlight(E1,WsPickPen,WsDirArrowOff)

S2=MillDirection(E1,Pt)
Dist=-S1*Offset

Direction=WsForward
IF (S2=1)AND(Mill_dir=1) THEN
	Direction=WsReverse
END IF
IF (S2=-1)AND(Mill_dir=2) THEN
	Direction=WsReverse
END IF

E2=OffsetContour(E1,Dist,Direction,WsMinLook,WsCircleMoves,Pen_color,WsDirArrowOn)
Stat=AssociateTool(E2,Tool)

ActivateUndo
Stat= AdvanceUndoNumber("contour mill")
Stat= AddGroupToUndo(WsUBCreateMode,E2,1,WsTrue)

Stat=DeleteEntity(Pt)

GOTO Pick_loop

Leave:!
Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)
Stat=SetPickMask(WsPickGeometry)

LowerSelectBox

Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
