
! script name: v3dinter.d/ztoolhole
! var_topic: associate tool with hole on 3D Part

!A
!A NAME:
!A
!A      Hole on 3D Part 	
!A
!A DESCRIPTION:
!A
!A 	This script allows a hole to be constructed and associated with
!A 	a specific tool on a 3D Part. The hole will be constructed
!A	in such a way that the depth of the hole will be the depth
!A	at which the tool comes in contact with the 3D part.
!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	FINISH ALLOWANCE:  Give the finish allowance. 	
!B 
!B	PICK GROUP, Single: Pick a group of points to determine
!B				the tops of the holes or select S for
!B				using single points for the top of the holes.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: part tool hole ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Finish_var$[256]
DIM Pt_group$[256]
DIM Point$[256]
DIM Name$[256]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Tol=NamedVarVal("3D Intersect Tol",Ws3DSystemVars,0.005)

ActivateUndo
Start:!
Stat=ToolSelectBox("TOOLS","selected tool")
IF Stat THEN Leave
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("SELECT TOOL",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
LowerSelectBox

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



Hole_rad= Dia/2
Cutter_type= WsEndMill
stat= Setup3dCutter(Cutter_type,Hole_rad,0.0,0.0,Finish_allow,0.0)

Pick_part:!
Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n4","")
Part_group=PickGroupEnt("PICK PART GROUP ")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Part_group) THEN Pick_part
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)
Stat= SetPickMask(WsPickNoSurf)


Pick_loop:!
Stat= SetPickMask(WsPickNoSurf)
Stat= SetEqualsValue(0,"S")
Stat=SetInputFormat("a1=",",S,")
Pt_group=PickGroupEnt("PICK GROUP, Single ",0,Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "S", "s"
Pick_point:stat= SetInputFormat("n4","")
	E1=PickEntity("PICK POINT")
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidDependency(E1) THEN Start_point
	Stat= EntityNameCheck(E1,Point$)
	Pick_para= LastPickPara
	Pick_return= CurrPickReturn

	Stat= GenerateVarName(WsHoleEnt,Name$)
	RecordFunctionOn
	Stat= RecordDescr("HOLE: Part Hole")
	Stat= SetRecDelLastExec
	Stat= NullReference(Part_group$)
	Stat= NullReference(Point$)
	SELECT Pick_return
	CASE WsGeometryEnd
       		 Ps= ParameterReturnPt(Point$,Pick_para,WsGeometryEnd)
	CASE WsGeometryMid
       		 Ps= ParameterReturnPt(Point$,Pick_para,WsGeometryMid)
	CASE WsGeomCenter
       		 Ps= ParameterReturnPt(Point$,Pick_para,WsGeomCenter)
	END SELECT
	Group=CreateGroup("")
	stat= AddToGroup(Group,Ps)
	IF No_finish_var THEN
       		 V1= FreeParameter("Finish allowance",Finish_allow)
	ELSE
       		 V1= FreeParameter("Finish allowance",Finish_var$)
	END IF
	Tool=FindUserData(Tool_name$,WsToolData)
	V2= FreeParameter("Hole diameter",ToolDiameter(Tool))
	V3= FreeParameter("Hole tip angle",ToolPointAngle(Tool))
	V4= FreeParameter("Z addon",Zaddon)
	V5= FreeParameter("Z offset",Zoffset)
	V6= FreeParameter("Tolerance",Tol)
	V7= FreeParameter("Hole radius",0.5*ToolDiameter(Tool))
	Stat= Setup3dCutter(WsEndMill,V7,0.0,0.0,V1,0.0)
	Work=CurrentWorkplane
	Outgroup= ProjectPtGrpHoles(Work,Group,Part_group$,V6,V2,V3,V4,V5)
	H1= GroupElement(Outgroup,1)
	Stat= NameEntity(H1,Name$)
	Stat=AssociateTool(H1,Tool)
	stat= DeleteEntity(Outgroup)
	Stat= DeleteGroup(Group,WsTrue)
	RecordFunctionOff
	Curr_record= CurrentVarRecord
	CloseVarRecord
	IF NOT ValidGeometry(H1) THEN
       		DelLastVarRecord
	ELSE
		ActivateUndo
		Stat= AdvanceUndoNumber("z tool hole")
		Stat= AddItemToUndo(WsUBCreateMode,H1)
		Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
	END IF
	GOTO Pick_point
END SELECT


IF NOT ValidDependency(Pt_group) THEN Pick_loop
Stat= EntityNameCheck(Pt_group,Pt_group$)


Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("HOLES: Part Hole Group")
Stat= SetRecDelLastExec
Stat= NullReference(Part_group$)
Stat= NullReference(Pt_group$)
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
V2= FreeParameter("Hole diameter",ToolDiameter(Tool))
V3= FreeParameter("Hole tip angle",ToolPointAngle(Tool))
V4= FreeParameter("Z addon",Zaddon)
V5= FreeParameter("Z offset",Zoffset)
V6= FreeParameter("Tolerance",Tol)
V7= FreeParameter("Hole radius",0.5*ToolDiameter(Tool))
Stat= Setup3dCutter(WsEndMill,V7,0.0,0.0,V1,0.0)
Work=CurrentWorkplane
Outgroup= ProjectPtGrpHoles(Work,Pt_group$,Part_group$,V6,V2,V3,V4,V5)
Stat= AutoNameGroup(Outgroup,Name$)
Stat=AssociateTool(Outgroup,Tool)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Outgroup) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("z tool holes")
	Stat= AddGroupToUndo(WsUBCreateMode,Outgroup,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_loop



Leave:!
LowerSelectBox
stat= DeleteEntity(Pt)
stat= DeleteEntity(Point)
stat= SetInputFormat("n4","")
Stat= SetPickMask(WsPickAll)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
