
! script name: vpkscrews.d/screwtoolprofile
! topic: calculates shape cut by a tool cutting a screw.

!A
!A NAME:
!A
!A	Screw Tool Profile
!A
!A DESCRIPTION:
!A
!A
!B INSTRUCTIONS:
!B
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: screw tool profile")

DIM Destination$[256]
DIM Answer$[256]
DIM A$[256]

DEG
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Direction=WsForward
Pitch_line_angle=0.0

ActivateUndo
Start:!
Pen_color=CurrentPen
Cutter_type=NamedVarVal("cutter type",WsSystemVars,Cutter_type)
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("TOOL USED",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
LowerSelectBox

Screw_pitch:!
Stat=SetInputFormat("n6")
Lead=GetValue("LEAD",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Od_diameter:!
Stat=SetInputFormat("n6")
Od_dia=GetValue("SCREW OD DIAMETER",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Root_diameter:!
Stat=SetInputFormat("n6")
Root_dia=GetValue("SCREW ROOT DIAMETER",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Pitch_diameter:!
Stat=SetInputFormat("n6")
Pitch_dia=GetValue("SCREW PITCH DIAMETER",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Pitch_line_angle:!
Stat=SetInputFormat("n6")
Pitch_line_angle=GetValue("PITCH LINE ANGLE",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Z_depth:!
Stat=SetInputFormat("n6")
Zdepth=GetValue("Z DEPTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave

DeActivateUndo
Y_offset:!
Stat=SetInputFormat("n6")
Y_offset=GetValue("Y OFFSET",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Cutter_tilt:!
Stat=SetInputFormat("n6")
Cutter_tilt=GetValue("CUTTER TILT",Answer$)
IF (ExitScript(Destination$)) THEN Leave

GOSUB Calc_shape

Main_loop:!
Stat=DynLabel("Lead:"&VAL$(Lead)&":Y Off:"&VAL$(Y_offset)&":Depth:"&VAL$(Zdepth)&":Tilt:"&VAL$(Cutter_tilt)&":")
Stat=SetInputFormat("a1",",C,E,N,Z,")
Stat=GetValue("Calculate, New data, Z adjust,  Exit ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "Z"
	FOR I= 1 TO 10
	GOSUB Calculate_zmin
	D1= 0.5*Root_dia-Zmin
	IF ABS(D1)< Tol THEN Main_loop
	Zdepth=Zdepth-D1
        Lead= DynBufferValue(0,Lead)
        Y_offset= DynBufferValue(1,Y_offset)
        Cutter_tilt= DynBufferValue(3,Cutter_tilt)
        GOSUB Calc_shape
	NEXT I
	GOTO Main_loop
CASE "N"
	IF ValidGeometry(Profile_group) THEN 
		Stat=DeleteGroup(Profile_group,WsTrue)
	END IF
	GOTO Start
CASE "E"
	IF ValidGeometry(Profile_group) THEN 
		Stat=DeleteGroup(Profile_group,WsTrue)
	END IF
	GOTO Leave
CASE "C"
	IF ValidGeometry(Profile_group) THEN 
		Stat=DeleteGroup(Profile_group,WsTrue)
	END IF
	Lead= DynBufferValue(0,Lead)
	Y_offset= DynBufferValue(1,Y_offset)
	Zdepth= DynBufferValue(2,Zdepth)
	Cutter_tilt= DynBufferValue(3,Cutter_tilt)
	GOSUB Calc_shape
END SELECT
GOTO Main_loop

Calculate_zmin:!
	L1= GroupMaxMin(Profile_group)
	P1= ParameterPt(L1,0)
	P2= ParameterPt(L1,0)
	Z1= YPointValue(P1)
	Z2= YPointValue(P2)
	IF Z1< Z2 THEN
		Zmin=Z1
	ELSE
		Zmin=Z2
	END IF
	Stat=DeleteEntity(L1)
	Stat=DeleteEntity(P1)
	Stat=DeleteEntity(P2)
RETURN
	

Calc_shape:!
IF ValidGeometry(Profile_group) THEN 
	Stat=DeleteGroup(Profile_group,WsTrue)
END IF
Mdef1= ToolMillingDef(Tool,WsConventional)
Stat=Setup3dCutter(Mdef1)
Profile_group= ScrewToolProfile(WsRight,Od_dia,Root_dia,Pitch_dia,Lead,Y_offset,Zdepth,Direction,Tol,Cutter_tilt,Roll_angle,Pitch_line_angle)
Stat= DeleteEntity(Mdef1)
RETURN

Leave:!
IF ValidGeometry(Profile_group) THEN 
	Stat=DeleteGroup(Profile_group,WsTrue)
END IF
Stat=DynLabel("")
Stat=SetInputFormat("n4","")
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END
