
! script name: pksolids.d/pksimplescrew
! solids: adds screwshape to a body
!A
!A NAME:
!A
!A 	Variable Pitch Simple Screw: 
!A
!A DESCRIPTION:
!A
!A	This script creates a simple screw.
!A
!B INSTRUCTIONS:
!B
!B		PICK BODY: Pick an existing conical or cylindrical body.
!B
!B		SCREW DIAMETER: Diameter of outside of screw.
!B
!B		SCREW DEPTH: Depth
!B
!B		START PITCH: Give the starting pitch.
!B
!B		END PITCH: Give the ending pitch.
!B
!B		TOTAL SCREW LENGTH:  Total length of screw.
!B
!B		INCLUDED ANGLE: Usually 0 deg.
!B
!B		WIDTH:  Width of flight
!B
!B
!# see also - htdocs/help.d/parasolids.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: var pitch screw ")

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

ActivateUndo
Units= CurrentUnits
IF Units= WsMetric THEN
        Screw_radius= 25
        Start_pitch= 0.125*25.4
        End_pitch= 0.125*25.4
	Screw_length= 25
	Screw_depth= Pitch/2
	Screw_angle= 0
	Screw_taper= 0
ELSE
        Screw_radius= 1
        Start_pitch= 0.125
        End_pitch= 0.125
	Screw_length= 1
	Screw_depth= Pitch/2
	Screw_angle= 0
	Screw_taper= 0
END IF
astep= 5.0
Nflights=1

Start:!
Stat= GLPlotOn


Cloop:!
Stat=SetInputFormat("n0=","")
Stat=SetEqualsValue(Nflights)
Nflights=GetValue("NUMBER OF FLIGHTS",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("a1=",",L,R,")
Stat=SetEqualsValue(0,"R")
Stat=GetValue("Left or Right HAND SCREW ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "L" THEN
        Hand= WsLeft
ELSE
        Hand= WsRight
END IF


Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(0.5*Screw_diameter)
Screw_diameter=InputRadius("SCREW DIAMETER",WsDiaInput)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Start_pitch)
Start_pitch=GetValue("START PITCH",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(End_pitch)
End_pitch=GetValue("END PITCH",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Screw_length)
Screw_length=GetValue("TOTAL SCREW LENGTH",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Screw_angle)
Screw_angle=GetValue("SCREW SIDE INCLUDED ANGLE",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Start_depth)
Start_depth=GetValue("START DEPTH",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(End_depth)
End_depth=GetValue("END DEPTH",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Start_width)
Start_width=GetValue("START WIDTH",Answer$)
IF ExitScript(Destination$) THEN Leave

IF End_width=0 THEN End_width= Start_width
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(End_width)
End_width=GetValue("END WIDTH",Answer$)
IF ExitScript(Destination$) THEN Leave


IF ABS(Screw_taper)> 0.0 THEN
	Stat=SetInputFormat("a1=",",F,C,")
	Stat=SetEqualsValue(0,"F")
	Stat=GetValue(" Flat or Conical TAPER ",Answer$)
	IF ExitScript(Destination$) THEN Leave
	IF Answer$= "F" THEN
       		 Taper_type=0
	ELSE
       		 Taper_type=1
	END IF
END IF

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Start_angle)
Start_angle=GetValue("START ANGLE",Answer$)
IF ExitScript(Destination$) THEN Leave


Stat=SetInputFormat("n4","")
Ps=XyzInput("START POINT ",WsXyzInput)
IF ExitScript(Destination$) THEN Leave
Stat= SetLastPoint(Ps)



Workr= CurrentWorkplane
StdWorkplane
Pt= PointXYZ(0,0,0)
T1=AngleTransform(Pt,90,-90,0)
Stat=SetWorkplane(T1)
Stat= DeleteEntity(Pt)
Zstart= ZPointValue(Ps)

Screw_depth= MAX(Start_depth,End_depth)+0.05
D1= Screw_diameter
D2= Screw_depth
P1= Start_pitch
P2= End_pitch
Len1= Screw_length
W1= Start_width
W2= End_width


WPKClearErrorFlag
PlotOff
E1= WPKScrewShape(Hand,D1,P1,P2,Len1,D2,W1,W2,Start_angle,Screw_angle,0,Zstart,Astep,1)

StdWorkplane
Pe= PtIncrXyz(Ps,len1,0,0)
R1= 0.5*Screw_diameter- Start_depth
R2= 0.5*Screw_diameter- End_depth
Pkbody=WPKCone(Ps,Pe,R1,R2,Length1)
IF Nflights>1 THEN
   Angle= 360.0/Nflights
   Axis_line= LineTwoPoints(Ps,Pe)
   Group= WPKAxisRotateBody(E1,Axis_line,Angle,Nflights-1)
   Stat= AddToGroup(Group,E1)
   PlotOn
   E2= WPKUniteBodies(Pkbody,Group)
   Stat= DeleteEntity(Axis_line)
   Stat= DeleteEntity(Group)
ELSE
   PlotOn
   E2= WPKUniteBodies(Pkbody,E1)
END IF
Stat= DeleteEntity(Ps)
Stat= DeleteEntity(Pe)

IF ValidGeometry(Workr) THEN Stat=SetWorkplane(Workr)

ActivateUndo
Stat= AdvanceUndoNumber("var pitch screw")
Stat= AddItemToUndo(WsUBPKSessionMark)
!WPKErrorCheck
GOTO Start

Leave:!
PlotOn
Stat=SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
