
! script name: vholes.d/toolhole
! var_topic: associate tool with hole constructed

!A
!A NAME:
!A
!A 	Pick Tools
!A
!A DESCRIPTION:
!A
!A 	This script allows a hole to be constructed and associated with
!A 	a specific tool.
!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 	HOLE DIAMETER: Enter the diameter of the hole.
!B
!B 	FINISH DEPTH: Enter the finish depth of the hole.
!B
!B 	PICK HOLE CENTER POINT: Pick the center point of the hole.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: pick tools ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Toolid$[256]
DIM Center$[256]
DIM Dia_var$[256]
DIM Depth_var$[256]
DIM Hole_name$[256]
DIM Mach_def$[256]

Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Start:!

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

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)
IF NOT ValidGeometry(Tool) THEN Start

LowerSelectBox

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

No_dia_var=1
SELECT Toolid$
CASE "COUNTER SINK"
	Ask_depth=0

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

	IF (Dia1<Dia) THEN
		Dia=Dia1
	END IF
CASE ELSE
	Ask_depth=1
END SELECT

No_depth_var=1
IF Ask_depth THEN
	Depth=InputDepth("FINISH DEPTH")
	IF ExitScript(Destination$) THEN Leave
	No_depth_var= LastVariableInput(Depth_var$)
END IF

CALL MachiningDef(Mach_def$,Name$,Toolid$,Dia,Depth)

Point_loop:!

Ploop:Stat=SetInputFormat("n4","")
E1=PickEntity("PICK HOLE CENTER POINT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(E1) THEN Ploop
IF NOT ValidDependency(E1) THEN Ploop
T= LastPickPara
Pick_return= CurrPickReturn
Stat= ReadEntityName(E1,Center$)
IF NOT LEN(Center$) THEN
        Stat= AutoNameEntity(E1)
        Stat= ReadEntityName(E1,Center$)
END IF
Pc= ParameterReturnPt(E1,T,Pick_return)
Stat=SetLastPoint(Pc,WsXyzInput)
Stat= DeleteEntity(Pc)


Stat= GenerateVarName(WsHoleEnt,Hole_name$)
RecordFunctionOn
Stat= RecordDescr("HOLE: Point")
IF Ask_depth THEN
	IF No_depth_var THEN
       		 V1= FreeParameter("Finish depth",Depth)
	ELSE
       		 V1= FreeParameter("Finish depth",Depth_var$)
	END IF
	Stat= HoleDefZInfo(Mach_def$,V1)
END IF
SELECT Pick_return
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Center$,T,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Center$,T,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Center$,T,WsGeomCenter)
END SELECT
H1=HoleInstance(Pc,Mach_def$)
Stat= NameEntity(H1,Hole_name$)
Stat= DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

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

Leave:!
LowerSelectBox
Stat=DeleteEntity(Pc)

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


SUB MachiningDef(Mach_def$,Name$,Toolid$,Hdia,Depth)

Tool=FindUserData(Name$,WsToolData)
IF NOT ValidGeometry(Tool) THEN 
	Mach_def$= ""
	SUBEXIT
END IF

Tdia=GetUserValue(Tool,"Diameter:")

SELECT Toolid$
CASE "DRILL"
	Cutter_type= WsDrill
CASE "CENTER DRILL"
	Cutter_type= WsCenterDrill
CASE "TWIST DRILL"
	Cutter_type= WsTwistDrill
CASE "TAPERED DRILL"
	Cutter_type= WsTaperedDrill
CASE "COUNTER SINK"
	Cutter_type= WsCounterSink
CASE "COUNTER BORE"
	Cutter_type= WsCounterBore
CASE "TAP"
	Cutter_type= WsTap
CASE "REAMER"
	Cutter_type= WsReamer
CASE "BALLMILL"
	Cutter_type= WsBallCutter
CASE "BULLMILL"
	Cutter_type= WsBullmill
CASE "ENDMILL"
	Cutter_type= WsEndmill
CASE "TAPERMILL"
	Cutter_type= WsTaperedEndmill
CASE "THREADER"
	Cutter_type= WsThreadedMill
CASE "BORING BAR"
	Cutter_type= WsBoringBar
END SELECT

SELECT Toolid$
CASE "DRILL","CENTER DRILL","TWIST DRILL"
	Hdia= Tdia
	Tip_angle=GetUserValue(Tool,"Point angle:")
	Zaddon=DepthAddon(Tdia,Tip_angle)
CASE "COUNTER SINK"
	Tip_angle=GetUserValue(Tool,"Point angle:")
	Depth= 0.0
	Zaddon=DepthAddon(Hdia,Tip_angle)
CASE ELSE
	Hdia= Tdia
	Tip_angle= 180.0
	Zaddon= 0.0
END SELECT

H1= HoleDefinition(Cutter_type,Hdia,Depth,Tip_angle,Zaddon)
Stat=AssociateTool(H1,Tool)
Stat= AutoNameEntity(H1)
Stat= ReadEntityName(H1,Mach_def$)


SUBEND
