
! script name: holes.d/odtoolhole
! topic: associate tool with hole constructed on OD for mill-turn

!A
!A NAME:
!A
!A 	Pick Tools for hole on OD of mill-turn.
!A
!A DESCRIPTION:
!A
!A	This script is specifically designed for describing holes for
!A	machining on a mill-turn combination machine. It uses the
!A	standard synergy lathe coordinate system with the real X axis
!A	mapped to the Z axis and the real Y axis described by diameters.
!A
!A
!B INSTRUCTIONS:
!B
!B 	INPUT TOOL ID NAME: The TOOLS selection box with the list of
!B 		available tools will automatically appear for a selection
!B 		to be made.
!B
!B	DIAMETER: Input the diameter location of the top of the hole.
!B
!B	ANGLE: Input the Angle location of the hole. The real Y axis is 
!B		zero degrees and the angles are measured CCW when viewing
!B		from the face of the part (real YZ Plane).
!B
!B	Z COORDINATE: Input the standard lathe Z coordinate (real X coordinate).
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: od mill-turn pick tool ")

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

ActivateUndo
Angle_calc=WsCCW

Active_workplane=0
Work=CurrentWorkplane
IF (ValidGeometry(Work)) THEN Active_workplane=1

Start:!
StdWorkplane

Stat=ToolSelectBox("TOOLS","selected tool",WsNonTurn)
IF Stat=1 THEN Leave

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

Tool=FindUserData(Name$,WsToolData)
IF NOT ValidGeometry(Tool) THEN Start

LowerSelectBox

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

SELECT Toolid$
CASE "DRILL","CENTER DRILL","TWIST DRILL"
	Ask_depth=1
	A11=TAN(Pt_angle/2)
	Depth_addon=0.5*Dia/A11

CASE "COUNTER SINK"
	Ask_depth=0

	Dia1=InputRadius("HOLE DIAMETER",WsDiaInput)
	IF (ExitScript(Destination$)) THEN Leave

	IF (Dia1<Dia) THEN
		Dia=Dia1
	END IF

	A11=TAN(Pt_angle/2)
	Depth=0.5*Dia/A11
	Depth_addon=0

CASE ELSE
	Ask_depth=1
	Depth_addon=0
	Pt_angle=180

END SELECT


IF Ask_depth THEN

	Depth=InputDepth("FINISH DEPTH")
	IF (ExitScript(Destination$)) THEN Leave

END IF
	Depth=Depth+Depth_addon

Point_loop:!

Stat=SetInputFormat("n4","")
Diameter=InputRadius("DIAMETER",WsDiaInput)
IF (ExitScript(Destination$)) THEN Leave

Angle=InputAngle("ANGLE")
IF (ExitScript(Destination$)) THEN Leave
SELECT Angle_calc
CASE WsCCW
	Angle= Angle
CASE WsCW
	Angle= -Angle
END SELECT

Stat=SetInputFormat("n4=","")
Stat= SetEqualsValue(Zvalue)
Zvalue=GetValue("Z COORDINATE")
IF (ExitScript(Destination$)) THEN Leave

StdWorkplane

X1= Zvalue
Y1= 0.0
Z1= 0.0

X2= Zvalue
Y2= Diameter/2*COS(Angle)
Z2= Diameter/2*SIN(Angle)

Paxis= PointXYZ(X1,Y1,Z1)
Pc= PointXYZ(X2,Y2,Z2)

L1= LineTwoPoints(Paxis,Pc,WsTrimmed)
T1=LineToVertTrans(L1,Pc)
Stat=SetWorkplane(T1)
H1=Hole(Pc,Dia,Depth,Pt_angle)
Stat=AssociateTool(H1,Tool)

Stat=SetLastPoint(Pc,WsXyzInput)

StdWorkplane
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Paxis)
Stat=DeleteEntity(L1)

ActivateUndo
Stat= AdvanceUndoNumber("od hole")
Stat= AddItemToUndo(WsUBCreateMode,H1)
GOTO Point_loop

Leave:!
Stat=SetInputFormat("n4=","")
LowerSelectBox
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Paxis)
Stat=DeleteEntity(L1)
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF

IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
