
! script name: holefeature.d/pmaketoolfeat
! var_topic: create tool hole feature

!A
!A NAME:
!A
!A 	Feature: Lathe Power Pick Tool 		VARIATIONAL
!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      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: pick tools ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Toolid$[256]
DIM Center$[256]
DIM Dia_var$[256],Ang_var$[256],Z_var$[256]
DIM Depth_var$[256]
DIM Hole_name$[256],Mach_def$[256],Ent_name$[256]
DIM Mdef_group$[256]
DIM Dtype$[256]
DIM Descr$[256]
DIM Param_name$[256]
DIM Namelist$[32000]

ActivateUndo
IF ARGC>1 THEN
	Hole_type$= UPC$(ARGV(1))
ELSE
	Stat= SetInputFormat("a1",",O,I,F,")
	Stat= GetValue("TYPE OF CYCLE.  Od, Id, OR Face ",Hole_type$)
	IF ExitScript(Destination$) THEN Leave
	IF Hole_type$= "O" THEN Hole_type$="OD"
	IF Hole_type$= "I" THEN Hole_type$="ID"
	IF Hole_type$= "F" THEN Hole_type$="FACE"
END IF
IF Hole_type$<>"OD" AND Hole_type$<>"FACE" AND Hole_type$<>"ID" THEN Leave

IF CurrentUnits= WsMetric THEN
	Std_ch= 1
	Std_allow= .5
ELSE
	Std_ch= .03
	Std_allow= .015
END IF

Stat= UserDataNames(WsToolData,Namelist$)
IF NOT LEN(Namelist$) THEN Leave

Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

Start:!
Stat=ToolSelectBox("TOOLS","selected tool")
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
IF POS(Name$," (od)") THEN Hole_type$="OD"
IF POS(Name$," (id)") THEN Hole_type$="ID"
IF POS(Name$," (face)") THEN Hole_type$="FACE"

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

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

No_depth_var=1
No_dia_var=1
Is_spotdrill= 0
Dtype_set= CheckUserField(Tool,"Drill type:",1)
IF Dtype_set THEN
	Stat= GetUserString(Tool,"Drill type:",Dtype$)		
	IF Dtype$= "SPOT" THEN Is_spotdrill= 1
	Stat=SetInputFormat("a1",",C,S,")
	Stat=GetValue("Chamfer /spot OR Spot only ",Answer$)
        IF ExitScript(Destination$) THEN Leave
	IF Answer$= "C" THEN
		Is_spotdrill= 2
		GOTO Ask_dia
	END IF
END IF
SELECT Toolid$
CASE "COUNTER SINK"
Ask_dia:Ques_phase= 1
	Dia1=InputRadius("HOLE DIAMETER TO CHAMFER",WsDiaInput)
	IF ExitScript(Destination$) THEN Leave
	No_dia_var= LastVariableInput(Dia_var$)
	Param_name$= "Diameter to chamfer"
CASE "BALLMILL","BULLMILL","ENDMILL","TAPERMILL"
	Ques_phase= 2
	Stat= SetInputFormat("a1",",R,F,P,")
	Stat= GetValue("USED TO: Rough , Finish, or Plunge ",Answer$)
	IF ExitScript(Destination$) THEN Leave
	Mill_type= 1*(Answer$="R")+2*(Answer$="F")+3*(Answer$="P")

	IF Mill_type<3 THEN
		Dia1=InputRadius("HOLE DIAMETER TO ROUND MILL",WsDiaInput)
		IF ExitScript(Destination$) THEN Leave
		No_dia_var= LastVariableInput(Dia_var$)
		Param_name$= "Diameter to round mill"
	END IF
	GOTO Depth_ques
CASE "CENTER DRILL"
	Ques_phase= 3
	Depth= GetUserValue(Tool,"Maximum depth of cut:")
CASE ELSE
	Ques_phase= 4
Depth_ques:Depth=InputDepth("FINISH DEPTH")
	IF ExitScript(Destination$) THEN Leave
	No_depth_var= LastVariableInput(Depth_var$)
END SELECT

Descr$="SINGLE TOOL FEATURE DATA: "&Name$
Stat= GenerateVarName(WsMachDefEnt,Mach_def$)
Stat= GenerateVarName(WsMachDefEnt,Ent_name$)
RecordFunctionOn
Stat= RecordDescr(Descr$)
Vstr=StringConstant(Name$)
Tool=FindUserData(Name$,WsToolData)
IF Ques_phase<>1 THEN
	IF No_depth_var THEN
       		 V1= FreeParameter("Finish depth",Depth)
	ELSE
       		 V1= FreeParameter("Finish depth",Depth_var$)
	END IF
END IF
IF Ques_phase=1 OR (Ques_phase=2 AND Mill_type<3) THEN
	IF No_dia_var THEN
		V2= FreeParameter(Param_name$,Dia1)
	ELSE
		V2= FreeParameter(Param_name$,Dia_var$)
	END IF
END IF

SELECT Toolid$
CASE "CENTER DRILL"
	Mdef=HoleFeatureDef(WsDrilledHole,V1,0,ToolDiameter(Tool),WsThruHole)
	H1= CdrillDefinition(Mdef,Tool)
	Stat= SetMachDefValue(H1,WsHoleDefDepth,V1)
CASE "TWIST DRILL"
	SELECT Is_spotdrill
	CASE 0
		Mdef=HoleFeatureDef(WsDrilledHole,V1,0,ToolDiameter(Tool),WsThruHole)
		H1= DrillDefinition(Mdef,Tool)
	CASE 1
		Mdef=HoleFeatureDef(WsDrilledHole,V1,0,ToolDiameter(Tool),WsThruHole)
		H1= SpotDefinition(Mdef,Tool,WsSpotOnly)
	CASE 2
		V3= FreeParameter("Chamfer",Std_ch)
		Mdef=HoleFeatureDef(WsDrilledHole,0,V3,V2,WsThruHole)
		H1= SpotDefinition(Mdef,Tool,WsSpotChamfer,V2)
	END SELECT
CASE "TAPERED DRILL"
	Mdef=HoleFeatureDef(WsDrilledHole,V1,0,ToolDiameter(Tool),WsThruHole)
	H1= DrillDefinition(Mdef,Tool)
CASE "COUNTER SINK"
	V3= FreeParameter("Chamfer",Std_ch)
	Mdef=HoleFeatureDef(WsDrilledHole,0,V3,V2,WsThruHole)
	H1= CsinkDefinition(Mdef,Tool,V2)
CASE "COUNTERBORE"
	Mdef=HoleFeatureDef(WsCboredHole,V1,0,ToolDiameter(Tool),WsThruHole)
	H1= CboreDefinition(Mdef,Tool)
CASE "TAP"
	Mdef=HoleFeatureDef(WsTappedHole,V1,0,ToolDiameter(Tool),WsThruHole)
	H1= TapDefinition(Mdef,Tool)
CASE "REAMER"
	Mdef=HoleFeatureDef(WsReamedHole,V1,0,ToolDiameter(Tool),WsThruHole)
	H1= ReamDefinition(Mdef,Tool)
CASE "BALLMILL","BULLMILL","ENDMILL","TAPERMILL"
	SELECT Mill_type
	CASE 1
		V3= FreeParameter("Rough allowance",Std_allow)
		Mdef=HoleFeatureDef(WsRoundCut,V1,0,V2,WsThruHole)
		H1= RoundDefinition(Mdef,Tool,V1,V2,V3)
	CASE 2
		Mdef=HoleFeatureDef(WsRoundCut,V1,0,V2,WsThruHole)
		H1= RoundDefinition(Mdef,Tool,V1,V2)
	CASE 3
		Mdef=HoleFeatureDef(WsPlungeHole,V1,0,ToolDiameter(Tool),WsThruHole)
		H1= PlungeDefinition(Mdef,Tool)
	END SELECT
CASE "BORING BAR"
	Mdef=HoleFeatureDef(WsBoredHole,V1,0,ToolDiameter(Tool),WsThruHole)
	H1= BoreDefinition(Mdef,Tool)
!CASE "THREADER"
	!Cutter_type= WsThreadedMill
END SELECT
Stat=AssociateTool(H1,Tool)
Stat=NameEntity(Mdef,Mach_def$)
Stat=NameEntity(H1,Ent_name$)
V11= FreeParameter("Z Offset",0.0)
Stat=SetMachDefValue(H1,WsHoleDefZOffset,V11)
Stat= SetMachDefDescr(Mdef,Vstr)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

ActivateUndo
Stat= AdvanceUndoNumber("place "&Feat_name$)
Stat= AddItemToUndo(WsUBCreateMode,Ent_name$)
Stat= AddItemToUndo(WsUBCreateMode,Mach_def$)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)

Place_hole:!
Stat=SetInputFormat("n4=","")
Diameter=InputRadius("DIAMETER TO PLACE HOLE",WsDiaInput)
IF (ExitScript(Destination$)) THEN Leave
No_dia_var= LastVariableInput(Dia_var$)

Stat=SetInputFormat("n4=","")
Angle=InputAngle("ANGLE POSITION OF HOLE")
IF (ExitScript(Destination$)) THEN Leave
No_ang_var= LastVariableInput(Ang_var$)

Stat=SetInputFormat("n4=","")
Stat= SetEqualsValue(Zvalue)
Zvalue=GetValue("Z COORDINATE OF HOLE")
IF (ExitScript(Destination$)) THEN Leave
No_z_var= LastVariableInput(Z_var$)

SELECT Hole_type$
CASE "OD"
	Descr$="OD FEATURE: Single tool point"
CASE "ID"
	Descr$="ID FEATURE: Single tool point"
CASE "FACE"
	Descr$="FACE FEATURE: Single tool point"
END SELECT
Stat= GenerateVarName(WsHoleEnt,Hole_name$)
RecordFunctionOn
Stat= RecordDescr(Descr$)
IF No_dia_var THEN
	V0= FreeParameter("Diameter",Diameter)
ELSE
	V0= FreeParameter("Diameter",Dia_var$)
END IF
IF No_ang_var THEN
	V1= FreeParameter("Angle",Angle)
ELSE
	V1= FreeParameter("Angle",Ang_var$)
END IF

IF No_z_var THEN
	V2= FreeParameter("Z_value",Zvalue)
ELSE
	V2= FreeParameter("Z_value",Z_var$)
END IF

V3= FreeParameter("Yc","Diameter/2*COS(Angle)")
V4= FreeParameter("Zc","Diameter/2*SIN(Angle)")
StdWorkplane
Stat=PrintToVarRecord("StdWorkplane")
Pc= PointXYZ(V2,V3,V4)

SELECT Hole_type$
CASE "OD"
	Paxis= PointXYZ(V2,0.0,0.0)
	L1= LineTwoPoints(Paxis,Pc,WsTrimmed)
	T1=LineToVertTrans(L1,Pc)
CASE "ID"
	Paxis= PointXYZ(V2,0.0,0.0)
	L1= LineTwoPoints(Pc,Paxis,WsTrimmed)
	T1=LineToVertTrans(L1,Pc)
CASE "FACE"
	Paxis= PointXYZ(0.0,0.0,0.0)
	T1=AngleTransform(Paxis,90,-90,0)
END SELECT
Stat=SetWorkplane(T1)
H1=HoleInstance(Pc,Ent_name$)
Stat= NameEntity(H1,Hole_name$)
StdWorkplane
Stat=PrintToVarRecord("StdWorkplane")
Stat=DeleteEntity(Pc)
IF Hole_type$="OD" OR Hole_type$= "ID" THEN
	Stat=DeleteEntity(L1)
END IF
Stat=DeleteEntity(Pz)
Stat=DeleteEntity(Paxis)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

ActivateUndo
Stat= AdvanceUndoNumber("place "&Feat_name$)
Stat= AddItemToUndo(WsUBCreateMode,H1)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Place_hole

Leave:!
LowerSelectBox
Stat=DeleteEntity(Pc)
Stat=SetInputFormat("n4","")

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