
! script name: pkvmach.d/vpkztoolhole
! solids: plunge a tool until it contacts a solid body.

!A
!A NAME:
!A
!A      Plunge Rough Solid
!A
!A DESCRIPTION:
!A
!A 	This script will generate holes which start at a plane and
!A 	are as deep as the top surface of a solid body. The locations
!A	are either specified as single points or as a group of
!A	points.
!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 POINT 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: solid tool hole ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Part_group$[256]
DIM Body$[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
IF ARGC>1 THEN
        Tool_name$= ARGV(1)
        GOTO Have_tool
END IF
Start:!

Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN
  LOAD "vtools.d/readmilltool:pkvmach.d/vpkztoolhole"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("CUTTER PATH TOOL, New_tool ",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave
IF Tool_name$="New_tool" THEN
        LowerSelectBox
        LOAD "vtools.d/readmilltool:pkvmach.d/vpkztoolhole"
END IF

Have_tool:!
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_body:!
Stat=SetInputFormat("a1",",F,A,")
E1=WPKPickEntity("PICK BODY  Face group, Assembly ",WsPKPickAll,Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "A"
        Pkassembly= WPKCurrentAssembly
        IF NOT WPKIsEntity(Pkassembly) THEN
                 Stat=SetInputFormat("n4","")
                 E1=WPKPickEntity("PICK ASSEMBLY")
                 IF (ExitScript(Destination$)) THEN Leave
                 IF NOT WPKIsEntity(E1) THEN Pick_body

                 Pkbody= WPKParentBody(E1)
                 Pkassembly= WPKParentAssembly(Pkbody)
        END IF
        IF NOT WPKIsEntity(Pkassembly) THEN Pick_body
        Stat= EntityNameCheck(Pkassembly,Body_name$)
CASE "F"
        Stat=SetInputFormat("n4","")
        E1=WPKPickEntity("PICK GROUP TO BE MACHINED")
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT WPKIsEntity(E1) THEN Pick_body
        G1= WPKContainingGroup(E1,1,WsTrue)
        IF NOT WPKIsEntity(G1) THEN Pick_body
        Stat= EntityNameCheck(G1,Body_name$)
CASE ELSE
        IF NOT WPKIsEntity(E1) THEN Pick_body
        Body_ent= WPKParentBody(E1)
        Stat= EntityNameCheck(Body_ent,Body_name$)
END SELECT

Pick_surfaces:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n4a1",",N,P,")
Part_group=PickGroupEnt("PICK ADDITIONAL SURFACE GROUP, Name, Proceed ",WsSurfaceGroup,Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "N"
        GOSUB Get_named_group
CASE "P"
        Extra_surfs=0
        GOTO Pick_loop
END SELECT
IF NOT ValidDependency(Part_group) THEN Pick_surfaces
Stat= EntityNameCheck(Part_group,Part_group$)
Stat=Highlight(Part_group,WsPickPen,WsDirArrowOff)
Extra_surfs=1



Pick_loop:!
Stat= SetPickMask(WsPickNoSurf)
Stat= SetEqualsValue(0,"S")
Stat=SetInputFormat("a1=",",S,")
Pt_group=PickGroupEnt("PICK POINT 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
	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)
	PlotOff
	Stat= PrintToVarRecord("PlotOff")
	Body_group= WPKConvertBody(Body_name$,WsPKConvertFaces)
	Stat= SetEdgeExt(Body_group)
	Stat= HideEntity(Body_group)
	RecordFunctionOff
	IF Extra_surfs THEN
		RecordFunctionOn
       		Part= CopyEntity(Part_group$)
        	Stat= CombineGroups(Part,Body_group)
		RecordFunctionOff
	ELSE
		RecordFunctionOn
       		Part= CopyEntity(Body_group)
		RecordFunctionOff
	END IF
	RecordFunctionOn
	Stat= PrintToVarRecord("PlotOn")
	PlotOn
	Work=CurrentWorkplane
	Outgroup= ProjectPtGrpHoles(Work,Group,Part,V6,V2,V3,V4,V5)
	H1= GroupElement(Outgroup,1)
	Stat= NameEntity(H1,Name$)
	Stat=AssociateTool(H1,Tool)
	Stat= DeleteGroup(Body_group,WsTrue)
	Stat= DeleteEntity(Part)
	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(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)
PlotOff
Stat= PrintToVarRecord("PlotOff")
Body_group= WPKConvertBody(Body_name$,WsPKConvertFaces)
Stat= SetEdgeExt(Body_group)
Stat= HideEntity(Body_group)
RecordFunctionOff
IF Extra_surfs THEN
     RecordFunctionOn
     Part= CopyEntity(Part_group$)
     Stat= CombineGroups(Part,Body_group)
     RecordFunctionOff
ELSE
     RecordFunctionOn
     Part= CopyEntity(Body_group)
     RecordFunctionOff
END IF
RecordFunctionOn
Stat= PrintToVarRecord("PlotOn")
PlotOn
Work=CurrentWorkplane
Outgroup= ProjectPtGrpHoles(Work,Pt_group$,Part,V6,V2,V3,V4,V5)
Stat= AutoNameGroup(Outgroup,Name$)
Stat=AssociateTool(Outgroup,Tool)
Stat= DeleteGroup(Body_group,WsTrue)
Stat= DeleteEntity(Part)
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
