
! script name: wscmill.d/hgridpoints
! var_topic: create grid pattern of points

!A 
!A NAME:
!A
!A 	Grid Points 
!A
!A DESCRIPTION:
!A
!A 	This script creates a grid pattern of points.
!A
!B INSTRUCTIONS:
!B
!B 	NUMBER OF POINTS IN A LINE: Enter the number of points
!B 		that should occur in each line.
!B
!B 	ANGLE OF LINE OF POINTS: Enter the angle of the line
!B 		the points should be constructed along.
!B
!B 	SPACING BETWEEN POINTS: Enter the distance between
!B 		the points along the line.
!B
!B 	NUMBER OF LINES OF POINTS: Enter the number of lines of
!B 		points that should be created at this angle.
!B
!B 	   NOTE: If there are more than one line of points to be created
!B 		 the following TWO questions will also be asked.
!B
!B 	   X SPACING BETWEEN LINES: Enter the distance that the lines
!B 		of points should be spaced in X.
!B
!B 	   Y SPACING BETWEEN LINES: ENTER the distance that the lines
!B 		of points should be spaced in Y.
!B
!B 	GRID START POINT ABS X,Y: Enter the X,Y coordinates of the
!B 		start point of the grid pattern.  NOTE: See XYZ INPUT OPTIONS.
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: grid points ")

DIM Answer$[256]
DIM Destination$[256]
DIM Name$[256]
DIM Center$[256]
DIM Xspace_var$[256]
DIM Yspace_var$[256]
DIM Space_var$[256]
DIM Angle_var$[256]
DIM Num_points$[256]
DIM Num_lines$[256]
DIM Depth_var$[256]
DIM Toolid$[256]
DIM Tool_name$[256]
DEG

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

Space= .5
Num_lines= 3
Num_pts= 4
Angle= 0

Stat=SetPickMask(WsPickNoSurf)
ActivateUndo

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/hgridpoints"
  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/hgridpoints"
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

Grid_loop:!
Stat=SetInputFormat("n0b=","")
Stat=SetEqualsValue(Num_pts)
Num_pts=GetValue("NUMBER OF POINTS IN A LINE",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Ask_depth
        GOTO Leave
END IF
No_npts_var= LastVariableInput(Num_points$)

Line_angle:!
Stat=SetInputFormat("n4b=","")
Stat=SetAngleEquals(Angle)
Angle=InputAngle("ANGLE OF LINE OF POINTS")
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Grid_loop
        GOTO Leave
END IF
No_angle_var= LastVariableInput(Angle_var$)

Point_space:!
Stat=SetInputFormat("n4b=","")
Stat=SetEqualsValue(Space)
Space=GetValue("SPACING BETWEEN POINTS",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Line_angle
        GOTO Leave
END IF
No_space_var= LastVariableInput(Space_var$)

Num_lines:!
Stat=SetInputFormat("n0b=","")
Stat=SetEqualsValue(Num_lines)
Num_lines=GetValue("NUMBER OF LINES OF POINTS",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Point_space
        GOTO Leave
END IF
No_nlines_var= LastVariableInput(Num_lines$)

IF (Num_lines>1) THEN

Xspace:!
	Stat=SetInputFormat("n4b=","")
	Stat=SetEqualsValue(0.0)
	Xspace=GetValue("X SPACING BETWEEN LINES",Answer$)
	IF (ExitScript(Destination$)) THEN
       		 IF Destination$= "BACKUP" THEN Num_lines
       		 GOTO Leave
	END IF
	No_xspace_var= LastVariableInput(XSpace_var$)

Yspace:!
	Stat=SetInputFormat("n4b=","")
	Stat=SetEqualsValue(Space)
	Yspace=GetValue("Y SPACING BETWEEN LINES",Answer$)
	IF (ExitScript(Destination$)) THEN
        	IF Destination$= "BACKUP" THEN Xspace
        	GOTO Leave
	END IF
	No_yspace_var= LastVariableInput(YSpace_var$)

ELSE

	Xspace=0
	Yspace=0
	No_xspace_var=1
	No_yspace_var=1

END IF

Center_point:Stat=SetInputFormat("n4b=","")
Stat= SetInputFormat("n4b","")
Stat= PointInputMode(1)
Ploop:Pc=XyzInput("GRID START POINT ",WsXyzInput)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Num_lines
        GOTO Leave
END IF
IF NOT ValidGeometry(Pc) THEN Ploop
Stat=SetLastPoint(Pc)
E1= LastPickEnt
Para1= LastPickPara
Pick_return1= CurrPickReturn
IF NOT ValidDependency(E1) THEN
        E1= CopyEntity(Pc)
        Stat= EntityNameCheck(E1,Center$)
        Pc_created=WsTrue
ELSE
        Stat= EntityNameCheck(E1,Center$)
        Pc_created=WsFalse
END IF
Stat=DeleteEntity(Pc)

Curr_pen= CurrentColor
Stat= SetCurrentColor(Pen_color)

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("HOLE GRID: "&Tool_name$)
Stat= SetRecDelLastExec
SELECT Pick_return1
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Center$,Para1,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Center$,Para1,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Center$,Para1,WsGeomCenter)
END SELECT
IF No_npts_var THEN
        V1= FreeParameter("Number points in a line",Num_pts)
ELSE
        V1= FreeParameter("Number points in a line",Num_points$)
END IF
IF No_angle_var THEN
        V2= FreeParameter("Angle of line of points",Angle)
ELSE
        V2= FreeParameter("Angle of line of points",Angle_var$)
END IF
IF No_space_var THEN
        V3= FreeParameter("Spacing between points",Space)
ELSE
        V3= FreeParameter("Spacing between points",Space_var$)
END IF
IF No_nlines_var THEN
        V4= FreeParameter("Number lines of points",Num_lines)
ELSE
        V4= FreeParameter("Number lines of points",Num_lines$)
END IF
IF No_xspace_var THEN
        V5= FreeParameter("X line spacing ",Xspace)
ELSE
        V5= FreeParameter("X line spacing ",Xspace_var$)
END IF
IF No_yspace_var THEN
        V6= FreeParameter("Y line spacing",Yspace)
ELSE
        V6= FreeParameter("Y line spacing",Yspace_var$)
END IF
IF No_depth_var THEN
         V7= FreeParameter("Finish depth",Depth)
ELSE
         V7= FreeParameter("Finish depth",Depth_var$)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
Xvector= PointPolar(V3,V2,0)
Yvector=PointXYZ(V5,V6,0)
RecordFunctionOff
IF NOT TipLengthCalc(Tool) THEN
   RecordFunctionOn
   G1=GridPoints(Pc,Xvector,Yvector,V1,V4,ToolDiameter(Tool),V7,ToolPointAngle(Tool))
   RecordFunctionOff
ELSE
   RecordFunctionOn
   V8=FreeParameter("Tip ratio",VariableValue(V7)/TipLengthCalc(Tool))
   V9=FreeParameter("Calc diameter",ToolDiameter(Tool)*VariableValue(V8))
   V10=FreeParameter("Hole diameter",MIN(ToolDiameter(Tool),VariableValue(V9)))
   G1=GridPoints(Pc,Xvector,Yvector,V1,V4,V10,V7,ToolPointAngle(Tool))
   RecordFunctionOff
END IF
RecordFunctionOn
Stat=AssociateTool(G1,Tool)
Stat= AutoNameGroup(G1,Name$)
Stat= DeleteEntityList(Pc,Xvector,Yvector)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= SetCurrentColor(Curr_pen)
Stat= PlotEntity(Center$)
Stat= PlotEntity(Name$)

ActivateUndo
Stat= AdvanceUndoNumber("hole grid")
Stat= AddGroupToUndo(WsUBCreateMode,G1,1,WsFalse)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Grid_loop

Leave:!
Stat=SetPickMask(WsPickAll)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 IF Destination$= "SCRIPTSTART" THEN Start
         IF Destination$= "BACKUP" THEN Start
	 LOAD Destination$
END IF
END
