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

!A
!A NAME:
!A
!A 	Feature: 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	HOLE TYPE. 0)Blind or 1)Thru: Thru holes will add the tool tip
!B		depth onto the stated finish depth.  Blind holes will
!B		drill the point to finish depth.
!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 Feat_name$[256]
DIM Toolid$[256]
DIM Center$[256]
DIM Dia_var$[256]
DIM Depth_var$[256]
DIM Hole_name$[256],Mach_def$[256],Ent_name$[256],Grp_name$[256]
DIM Work_name$[256]
DIM Mdef_group$[256]
DIM Dtype$[256]
DIM Descr$[256]
DIM Param_name$[256]
DIM Namelist$[32000]

ActivateUndo
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

Set_workplane=0
Rev_workplane=0

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$
Feat_name$=Answer$

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

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","THREADER"
	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 "CHAMFERMILL"
	Ques_phase= 5
	Stat= SetInputFormat("a1",",R,P,")
	Stat= GetValue("USED TO: Round or Plunge ",Answer$)
	IF ExitScript(Destination$) THEN Leave
	Mill_type= 1*(Answer$="R")+3*(Answer$="P")

	IF Mill_type<3 THEN
		Dia1=InputRadius("HOLE DIAMETER TO ROUND MILL",WsDiaInput)
		Param_name$= "Diameter to round mill"
	ELSE
		Dia1=InputRadius("HOLE DIAMETER TO PLUNGE",WsDiaInput)
		Param_name$= "Diameter to chamfer"
	END IF
	IF ExitScript(Destination$) THEN Leave
	No_dia_var= LastVariableInput(Dia_var$)
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$)
Stat= GenerateVarName(WsGroupEnt,Grp_name$)
RecordFunctionOn
Stat= RecordDescr(Descr$)
IF Variational_tool THEN
	Tool=FindUserData(Name$,WsToolData)
ELSE
	Vstr=StringConstant(Name$)
	Tool=FindUserData(Vstr,WsToolData)
END IF
IF Ques_phase<>1 AND Ques_phase<>5 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=5 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
		RecordFunctionOff
		Stat=SetInputFormat("n0","")
		Thru_hole=GetValue("HOLE TYPE. 0)Blind or 1)Thru")
		IF ExitScript(Destination$) THEN Leave
		RecordFunctionOn
		V3= FreeParameter("Thru hole",Thru_hole)
		
		Mdef=HoleFeatureDef(WsDrilledHole,V1,0,ToolDiameter(Tool),V3)
		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 "PORT CUTTER"
	Mdef=HoleFeatureDef(WsDrilledHole,V1,0,ToolDiameter(Tool),WsThruHole)
	H1=PortHoleDefinition(Mdef,Tool,V1,WsThruHole)
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 "CHAMFERMILL"
	SELECT Mill_type
	CASE 1,2
		V3= FreeParameter("Depth_addon",Std_ch)
		V4= FreeParameter("Chamfer",Std_ch)
		Mdef= HoleFeatureDef(WsRoundCut,0.0,0,V2,WsThruHole)
		H1= ChamferMillDef(Mdef,Tool,V3,V4)
	CASE 3
		V3=FreeParameter("Chamfer",Std_ch)
		Mdef=HoleFeatureDef(WsPlungeHole,0.0,0,V2,WsThruHole)
		H1=ChamferPlungeDef(Mdef,Tool,V2,V3,WsPlungeDiameter)
	END SELECT
CASE "BORING BAR"
	Mdef=HoleFeatureDef(WsBoredHole,V1,0,ToolDiameter(Tool),WsThruHole)
	H1= BoreDefinition(Mdef,Tool)
CASE "THREADER"
	Mdef=HoleFeatureDef(WsRoundCut,V1,0,V2,WsBlindHole)
	H1= RoundDefinition(Mdef,Tool,V1,V2,V3)
END SELECT
Stat=AssociateTool(H1,Tool)
Stat=NameEntity(Mdef,Mach_def$)
Stat=SetMachDefDescr(Mdef,EntityNameStr(Tool))
V11= FreeParameter("Z Offset",0.0)
Stat=SetMachDefValue(H1,WsHoleDefZOffset,V11)
Stat=NameEntity(H1,Ent_name$)
Vstr=StringConstant(Name$)
Odata=CreateUserData(WsOperation,Vstr,JoinStrings("New func: 1",CHR$(10)))
Vo2=StringConstant("Group name:")
Stat=PutUserString(Odata,Vo2,Grp_name$)
Group1=CreateGroup(Grp_name$,WsMachDefGroup)
Stat= AddToGroup(Grp_name$,H1)
RecordFunctionOff
CloseVarRecord
GOTO Ploop

Gloop:Stat=SetInputFormat("a1=",",S,N,")
Stat=SetEqualsValue(0,"S")
Hole_group=PickGroupEnt("GROUP, Single, New ",0,Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$="N" THEN LOAD "holefeature.d/maketoolfeat"

IF Answer$="S" OR Answer$="s" THEN Ploop
IF NOT ValidGeometry(Hole_group) THEN Gloop
IF NOT ValidDependency(Hole_group) THEN Gloop
Stat= EntityNameCheck(Hole_group,Center$)

Descr$= "SINGLE TOOL: Place group "&Feat_name$
Stat= GenerateVarName(WsVarRecordEnt,Hole_name$)
Stat= GenerateVarName(WsGroupEnt,Mdef_group$)
RecordFunctionOn
Stat= RecordDescr(Descr$)
Grp1= CreateGroup(Mdef_group$)
Stat= AddToGroup(Grp1,Ent_name$)
G1=HolesGrpOperation(Mdef_group$,Center$)
Stat= AutoNameGroup(G1,Hole_name$)

ActivateUndo
Stat= AdvanceUndoNumber("place "&Feat_name$)
Stat= AddItemToUndo(WsUBCreateMode,Ent_name$)
Stat= AddGroupToUndo(WsUBCreateMode,G1,0,WsTrue)

Stat= DeleteEntity(G1)
Stat= DeleteEntity(Grp1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Gloop

Ploop:!
Pc_created= WsFalse
IF Point_input_on THEN Input_point
Stat=SetInputFormat("a2=",",G,N,I,W,RW,")
Stat= SetEqualsValue(0,"G")
E1=PickEntity("PICK HOLE CENTER POINT, Group, Input, New", Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(E1) THEN
	SELECT Answer$
	CASE "W"
		GOSUB Pick_workplane
		Set_workplane=1
		Rev_workplane=0
	CASE "RW"
		GOSUB Pick_workplane
		Set_workplane=0
		Rev_workplane=1
	CASE "I"
		GOTO Input_point
	CASE "G"
		GOTO Gloop
	CASE "N"
		LOAD "holefeature.d/maketoolfeat"
	CASE ELSE
		GOTO Ploop
	END SELECT
END IF
IF NOT ValidDependency(E1) THEN Ploop
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)
GOTO Make_record


Input_point:Stat=SetInputFormat("n4=","")
Pc=XyzInput("CENTER POINT",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Pc) THEN Input_point
E1= LastPickEnt
T= LastPickPara
Pick_return= CurrPickReturn
IF NOT ValidDependency(E1) THEN
         E1= CopyEntity(Pc)
         Stat= EntityNameCheck(E1,Center$)
         Pc_created= WsTrue
ELSE
         Stat= EntityNameCheck(E1,Center$)
         Pc_created= WsFalse
END IF
Stat=SetLastPoint(Pc)
Stat= DeleteEntity(Pc)
Point_input_on=1
GOTO Make_record

Make_record:!
Stat= GenerateVarName(WsHoleEnt,Hole_name$)
Descr$= "SINGLE TOOL: Place hole "&Feat_name$
RecordFunctionOn
Stat= RecordDescr(Descr$)
IF Set_workplane THEN
	Stat=SetWorkplane(Work_name$)
END IF
IF Rev_workplane THEN
	Work= ReverseZTrans(Work_name$)
	Stat=SetWorkplane(Work)
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,Ent_name$)
Stat= NameEntity(H1,Hole_name$)
Stat= DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

ActivateUndo
Stat= AdvanceUndoNumber("place "&Feat_name$)
IF Pc_created THEN Stat= AddItemToUndo(WsUBCreateMode,Center$)
Stat= AddItemToUndo(WsUBCreateMode,H1)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Ploop

Pick_workplane:!
stat= SetInputFormat("n4","")
Work_ent=PickEntity("PICK ENTITY FOR WORKPLANE")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Work_ent) THEN Pick_workplane
Stat= EntityNameCheck(Work_ent,Work_name$)
RETURN

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

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