
! script name: wscmill.d/holespattern
! var_topic: create holes at the locations in a pattern 

!A
!A NAME:
!A
!A 	Holes  Pattern
!A
!A DESCRIPTION:
!A
!A 	This script creates holes of a certain depth and diameter
!A	at the locations in a pattern.
!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
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: holes pattern ")

DIM Answer$[256]
DIM Destination$[256]
DIM Point1$[256],Name$[256]
DIM Depth_var$[256],Dia_var$[256]
DIM Group$[256]
DIM Toolid$[256]
DIM Tool_name$[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/holespattern"
  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/holespattern"
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=SetXyzInput(WsXyzInput)

Pick_group:!
Stat= SetInputFormat("n4b","")
E1=PickSpecificEnt("PICK HOLE PATTERN",WsPickHoles)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Ask_depth
        GOTO Leave
END IF
Group= ParentGroup(E1,WsHoleGroup)
IF (NOT ValidDependency(Group)) THEN Pick_group
Stat= EntityNameCheck(Group,Group$)
Stat=Highlight(Group,WsPickPen,WsDirArrowOff)

Curr_pen= CurrentColor
Stat= SetCurrentColor(Pen_color)
Pattern_dir= WsForward

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("HOLES: pattern "&Tool_name$)
Stat= SetRecDelLastExec
Tool=FindUserData(Tool_name$,WsToolData)
IF No_depth_var THEN
	 V1= FreeParameter("Finish depth",Depth)
ELSE
	 V1= FreeParameter("Finish depth",Depth_var$)
END IF
V5= FreeParameter("Pattern direction",Pattern_dir)
RecordFunctionOff
IF NOT TipLengthCalc(Tool) THEN
   RecordFunctionOn
   G1=HolesAtPtGrp(Group$,ToolDiameter(Tool),V1,ToolPointAngle(Tool),0,V5)
   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)))
   G1=HolesAtPtGrp(Group$,V4,V1,ToolPointAngle(Tool),0,V5)
   RecordFunctionOff
END IF
RecordFunctionOn
Stat= AutoNameGroup(G1,Name$)
Stat=AssociateTool(G1,Tool)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= SetCurrentColor(Curr_pen)
Stat= PlotEntity(G1)

Stat= AdvanceUndoNumber("hole pattern")
Stat= AddGroupToUndo(WsUBCreateMode,G1)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)

GOTO Pick_group

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