
! script name: wscmill.d/holexyz
! var_topic: create hole using XYZ input mode as default

!A
!A NAME:
!A
!A 	Single Hole
!A
!A DESCRIPTION:
!A
!A 	This script creates a hole of a certain depth and diameter.
!A
!B INSTRUCTIONS:
!B
!B 	FINISH DEPTH: Enter the depth of the hole to be constructed.
!B
!B 	INPUT ABS X,Y,Z: Enter the X,Y,Z coordinates of the hole.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: xyz hole ")

DIM Answer$[256]
DIM Destination$[256]
DIM Point1$[256],Hole_name$[256]
DIM Depth_var$[256],Dia_var$[256]
DIM Toolid$[256]
DIM Tool_name$[256]
DIM Pattern$[256]

DEG
ActivateUndo

Stat=SetPickMask(WsPickNoSurf)
Var_object= CurrentVarObject
IF NOT ValidGeometry(Var_object) THEN
	Var_object=OpenVarObject("Program")
END IF
! Var_object= CheckVarObject
! IF (ExitScript(Destination$)) THEN Leave

IF ARGC>1 THEN
        Answer$= ARGV(1)
        GOTO Have_tool
END IF

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

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
Hole_dia=GetUserValue(Tool,"Diameter:")
Pen_color= GetUserValue(Tool,"Pen color:")
LowerSelectBox

Ask_depth:!
Stat=SetInputFormat("n4b=","")
Depthr=NamedVarVal("hole depth",WsPointVars,0.0)
Stat=SetDepthEquals(Depthr)
Depth=InputDepth("FINISH DEPTH")
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Start
        GOTO Leave
END IF
No_depth_var= LastVariableInput(Depth_var$)
Depthr=Depth

Stat=SetPickReturn(WsHoleXYZ)
Stat=SetXyzInput(WsXyzInput)

First_hole= 1


Ploop: Stat= SetInputFormat("n4b","")
Ps=XyzInput("INPUT",WsXyzInput)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Ask_depth
        GOTO Leave
END IF
IF NOT ValidGeometry(Ps) THEN Ploop
Stat=SetLastPoint(Ps)
E1= LastPickEnt
Para1= LastPickPara
Pick_return1= CurrPickReturn
IF NOT ValidDependency(E1) THEN 
	E1= CopyEntity(Ps)
	Stat= EntityNameCheck(E1,Point1$)
	Ps_created=WsTrue
ELSE
	Stat= EntityNameCheck(E1,Point1$)
	Ps_created=WsFalse
END IF
Stat=DeleteEntity(Ps)

IF First_hole THEN
        Stat=GenerateVarName(WsGroupEnt,Pattern$)

	RecordFunctionOn
	Stat= RecordDescr("HOLE PATTERN: "&Pattern$)
	Stat= SetRecDelLastExec
	G1= CreateGroup(Pattern$,WsHoleGroup)
	Stat= NameEntity(G1,Pattern$)
	RecordFunctionOff
	CloseVarRecord
	
	First_hole= 0
END IF

Curr_pen= CurrentColor
Stat= SetCurrentColor(Pen_color)

Stat= GenerateVarName(WsHoleEnt,Hole_name$)
RecordFunctionOn
Stat= RecordDescr("HOLE: "&Tool_name$)
Stat= SetRecDelLastExec
SELECT Pick_return1
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Point1$,Para1,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Point1$,Para1,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Point1$,Para1,WsGeomCenter)
CASE WsNearest
        Pc= ParameterReturnPt(Point1$,Para1,WsNearest)
END SELECT
Tool=FindUserData(Tool_name$,WsToolData)
IF No_depth_var THEN
	 V1= FreeParameter("Finish depth",Depth)
ELSE
	 V1= FreeParameter("Finish depth",Depth_var$)
END IF
RecordFunctionOff
IF NOT TipLengthCalc(Tool) THEN
   RecordFunctionOn
   H1=Hole(Pc,ToolDiameter(Tool),V1,ToolPointAngle(Tool))
   RecordFunctionOff
ELSE
   RecordFunctionOn
   V2=FreeParameter("Tip ratio",VariableValue(V1)/TipLengthCalc(Tool))
   V3=FreeParameter("Calc diameter",ToolDiameter(Tool)*VariableValue(V2))
   V4=FreeParameter("Hole diameter",MIN(ToolDiameter(Tool),VariableValue(V3)))
   H1=Hole(Pc,V4,V1,ToolPointAngle(Tool))
   RecordFunctionOff
END IF
RecordFunctionOn
Stat= NameEntity(H1,Hole_name$)
Stat=AssociateTool(H1,Tool)
Stat= DeleteEntity(Pc)

RecordFunctionOff
Stat= AdvanceUndoNumber("hole xyz")
Stat= AddItemToUndo(WsUBModifyMode,Pattern$)
RecordFunctionOn

Stat= AddToGroup(Pattern$,H1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= SetCurrentColor(Curr_pen)
Stat= PlotEntity(H1)

IF Ps_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point1$)
Stat= AddItemToUndo(WsUBCreateMode,H1)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)

GOTO Ploop

Leave:!
Stat=SetInputFormat("n4","")
Vdepth=CreateVariable("hole depth",WsPointVars,Depthr)
Stat=SetPickMask(WsPickGeometry)
Stat= PointInputMode(0)
Stat=DeleteEntity(Ps)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 IF Destination$= "SCRIPTSTART" THEN Start
         IF Destination$= "BACKUP" THEN Start
	 LOAD Destination$
END IF
END
