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

!A
!A NAME:
!A
!A 	XYZ Hole               VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script creates a hole of a certain depth a and diameter. 
!A	This hole will not have a tool associated with it, so it will
!A	not be recognized by automatic post processing. A tool must be
!A	assigned for it to be post processed.
!A
!B INSTRUCTIONS:
!B
!B 	HOLE DIAMETER: Enter the diameter of the hole to be constructed.
!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]

DEG
ActivateUndo

Start:!
Stat=SetPickMask(WsPickNoSurf)
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Stat=SetInputFormat("n4=","")
Depth=NamedVarVal("hole diameter",WsPointVars,0.0)
Stat=SetRadiusEquals(Diar/2)
Dia=InputRadius("HOLE DIAMETER",WsDiaInput)
IF (ExitScript(Destination$)) THEN Leave
No_dia_var= LastVariableInput(Dia_var$)
Diar=Dia

Depthr=NamedVarVal("hole depth",WsPointVars,0.0)
Stat=SetDepthEquals(Depthr)
Depth=InputDepth("FINISH DEPTH")
IF (ExitScript(Destination$)) THEN Leave
No_depth_var= LastVariableInput(Depth_var$)
Depthr=Depth

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

Stat= SetInputFormat("n4b","")
Stat= PointInputMode(1)
Ploop:Ps=XyzInput("INPUT",WsXyzInput)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Ps) THEN Ploop
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)

Stat= GenerateVarName(WsHoleEnt,Hole_name$)
RecordFunctionOn
Stat= RecordDescr("HOLE: XYZ Hole")
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
IF No_dia_var THEN
	 V1= FreeParameter("Hole diameter",Dia)
ELSE
	 V1= FreeParameter("Hole diameter",Dia_var$)
END IF
IF No_depth_var THEN
	 V2= FreeParameter("Finish depth",Depth)
ELSE
	 V2= FreeParameter("Finish depth",Depth_var$)
END IF
H1=Hole(Pc,V1,V2)
Stat= NameEntity(H1,Hole_name$)
Stat= DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= PlotEntity(H1)

Stat= AdvanceUndoNumber("var hole")
IF Ps_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point1$)
Stat= AddItemToUndo(WsUBCreateMode,H1)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)

GOTO Ploop

Leave:!
Stat=SetInputFormat("n4","")
Vdia=CreateVariable("hole diameter",WsPointVars,Diar)
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
