
! script name: wscmill.d/hboltcircle
! var_topic: create partial or full circular pattern of holes

!A
!A NAME:
!A
!A 	Bolt Hole Circle
!A
!A DESCRIPTION:
!A
!A 	This script creates a partial or full circular pattern of points.
!A
!B INSTRUCTIONS:
!B
!B 	BOLT HOLE RADIUS: Enter the radius to be used for the
!B 		circular pattern.
!B
!B 	STARTING ANGLE: Enter the angle to the first point of
!B 		the pattern.
!B
!B 	NUMBER OF POINTS: Enter the number of points to be
!B 		constructed.
!B
!B 	Full or Partial BOLT CIRCLE: Enter "F" for full or "P" for partial
!B 		to indicate which type should be created.
!B
!B 	NOTE: If partial is selected the following question will be asked.
!B
!B 	ANGULAR INCREMENT: Enter the number of degrees between
!B 		the points.  The angular increment will be calculated
!B 		automatically for a full bolt hole circle.
!B
!B 	CENTER POINT: Enter the X,Y coordinates of the center of the
!B		bolt hole circle pattern.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: bolt hole circle ")

DIM Answer$[256]
DIM Destination$[256]
DIM Bolt_hole$[256]
DIM Center$[256]
DIM Radius_var$[256]
DIM Angle_var$[256]
DIM Astart_var$[256]
DIM Num_points$[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

Num_pts= 8

ActivateUndo
Radiusr=NamedVarVal("bolt hole radius",WsPointVars,1.0)
Stat=SetRadiusEquals(Radiusr)

Stat=SetPickMask(WsPickNoSurf)
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/hboltcircle"
  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/hboltcircle"
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:!
Depthr=NamedVarVal("hole depth",WsPointVars,0.0)
Stat=SetInputFormat("n4b=","")
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

Bolt_loop:Stat=SetInputFormat("n4b=","")
Radius=InputRadius("BOLT HOLE RADIUS")
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Ask_depth
        GOTO Leave
END IF
Radiusr=Radius
No_radius_var= LastVariableInput(Radius_var$)
C1= LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Circle_loop
        No_radius_var=0
        Stat= EntityNameCheck(C1,Radius_var$)
END IF
Vradius=CreateVariable("bolt hole radius",WsPointVars,Radiusr)

Start_angle:Stat= SetAngleEquals(0)
Stat=SetInputFormat("n4b=","")
Astart=InputAngle("STARTING ANGLE")
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Bolt_loop
        GOTO Leave
END IF
No_astart_var= LastVariableInput(Astart_var$)

Num_times:Stat=SetInputFormat("n0b=","")
Stat= SetEqualsValue(Num_pts)
Num_pts=GetValue("NUMBER OF POINTS",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Start_angle
        GOTO Leave
END IF
No_num_var= LastVariableInput(Num_points$)

Full_partial:Stat=SetInputFormat("a1b=",",F,P,")
Stat= SetEqualsValue(0,"F")
Stat=GetValue("BOLT CIRCLE, Full or Partial ",Answer$)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Num_times
        GOTO Leave
END IF

IF (Answer$="P") THEN
	Full=WsPartial
ELSE
	Full=WsFull
END IF

Ang_inc=0
Stat=SetInputFormat("n4b","")

IF (Full=WsPartial) THEN
	Ang_inc=InputAngle("ANGULAR INCREMENT BETWEEN POINTS")
	IF (ExitScript(Destination$)) THEN
        	IF Destination$= "BACKUP" THEN Full_partial
        	GOTO Leave
	END IF
END IF

Center_point:Stat=SetInputFormat("n4=","")
Stat=SetPickReturn(WsHoleXYZ)
Stat=SetXyzInput(WsXyzInput)

Stat= SetInputFormat("n4b","")
Stat= PointInputMode(1)
Ploop:Pc=XyzInput("CENTER POINT ",WsXyzInput)
IF (ExitScript(Destination$)) THEN
        IF Destination$= "BACKUP" THEN Full_partial
        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("BOLT HOLE: "&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_radius_var THEN
	V1= FreeParameter("Radius",Radius)
ELSE
	V1= FreeParameter("Radius",Radius_var$)
END IF
IF No_astart_var THEN
	V2= FreeParameter("Starting angle",Astart)
ELSE
	V2= FreeParameter("Starting angle",Astart_var$)
END IF
IF No_num_var THEN
	V4= FreeParameter("Number points",Num_pts)
ELSE
	V4= FreeParameter("Number points",Num_points$)
END IF
IF No_depth_var THEN
         V5= FreeParameter("Finish depth",Depth)
ELSE
         V5= FreeParameter("Finish depth",Depth_var$)
END IF
Tool=FindUserData(Tool_name$,WsToolData)
RecordFunctionOff
IF NOT TipLengthCalc(Tool) THEN
   RecordFunctionOn
   IF (Full=WsPartial) THEN
	V3= FreeParameter("Angle increment",Ang_inc)
	G1=BoltHole(V1,Pc,V2,V3,V4,WsPartial,ToolDiameter(Tool),V5,ToolPointAngle(Tool))
   ELSE
	G1=BoltHole(V1,Pc,V2,0,V4,WsFull,ToolDiameter(Tool),V5,ToolPointAngle(Tool))
   END IF
   RecordFunctionOff
ELSE
   RecordFunctionOn
   V6=FreeParameter("Tip ratio",VariableValue(V5)/TipLengthCalc(Tool))
   V7=FreeParameter("Calc diameter",ToolDiameter(Tool)*VariableValue(V6))
   V8=FreeParameter("Hole diameter",MIN(ToolDiameter(Tool),VariableValue(V7)))
   IF (Full=WsPartial) THEN
	V3= FreeParameter("Angle increment",Ang_inc)
	G1=BoltHole(V1,Pc,V2,V3,V4,WsPartial,V8,V5,ToolPointAngle(Tool))
   ELSE
	G1=BoltHole(V1,Pc,V2,0,V4,WsFull,V8,V5,ToolPointAngle(Tool))
   END IF
   RecordFunctionOff
END IF
RecordFunctionOn
Stat= AutoNameGroup(G1,Name$)
Stat=AssociateTool(G1,Tool)
Stat= DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

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

ActivateUndo
Stat= AdvanceUndoNumber("bolt circle")
Stat= AddGroupToUndo(WsUBCreateMode,G1,1,WsFalse)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Bolt_loop

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