
! script name: pksolids.d/pkvarpitchthread
! solids: subtract threadshape from a body

!A
!A NAME:
!A
!A 	Variable Pitch Thread: 
!A
!A DESCRIPTION:
!A
!A      This script subtracts a thread from a solid body. This script is
!A      designed to be applied to an already existing cylindrical or conical
!A      body oriented along the negative x axis starting at the origin. The
!A      script makes the shape of the thread and subtracts it from the body.
!A
!B INSTRUCTIONS:
!B
!B		PICK BODY: Pick the cylindrical or conical body.
!B
!B		THREAD ROOT DIAMETER: Diameter of thread tip.
!B
!B		START PITCH: Give the pitch at the start of the thread.
!B			     For example 8 threads per inch is a pitch of 0.125.
!B
!B		END PITCH: Give the pitch at the end of the thread.
!B
!B		TOTAL THREAD LENGTH:  Total length of thread.
!B
!B		INCLUDED ANGLE: Usually 60 deg.
!B
!B		THREAD DEPTH: Depth of thread shape.
!B
!B		THREAD ROOT WIDTH (0.0 gives single point thread ):
!B
!B		THREAD TAPER:  Positive angle tapers outward.
!B
!# see also - htdocs/help.d/parasolids.htm

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

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

ActivateUndo
Units= CurrentUnits
IF Units= WsMetric THEN
        Thread_radius= 25
        Start_pitch= 0.125*25.4
        End_pitch= 0.125*25.4
	Thread_length= 25
	Thread_depth= Pitch/2
	Thread_angle= 60
	Thread_taper= 0
ELSE
        Thread_radius= 1
        Start_pitch= 0.125
        End_pitch= 0.125
	Thread_length= 1
	Thread_depth= Pitch/2
	Thread_angle= 60
	Thread_taper= 0
END IF
astep= 20.0

Start:!
Stat= GLPlotOn


Pick_body:!
E1=WPKPickEntity("PICK BODY")
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Pick_body
Pkbody= WPKParentBody(E1)

Cloop:!

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

Stat=SetInputFormat("a1=",",O,I,")
Stat=SetEqualsValue(0,"O")
Stat=GetValue(" Od or Id THREAD ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "I" THEN
        Odid= -1
ELSE
        Odid=1
END IF


Stat=SetInputFormat("n4=","")
Stat=SetRadiusEquals(0.5*Thread_diameter)
Thread_diameter=InputRadius("THREAD ROOT 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(Thread_length)
Thread_length=GetValue("TOTAL THREAD LENGTH",Answer$)
IF ExitScript(Destination$) THEN Leave

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

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

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Thread_width)
Thread_width=GetValue("THREAD ROOT WIDTH(0.0 gives single point thread )",Answer$)
IF ExitScript(Destination$) THEN Leave

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Thread_taper)
Thread_taper=GetValue("THREAD TAPER",Answer$)
IF ExitScript(Destination$) THEN Leave

Taper_type= 0
IF (ABS(Thread_width)> 0.0) AND (ABS(Thread_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)
Stat= DeleteEntity(Ps)

D1= Thread_diameter
D2= Odid*Thread_depth
P1= Start_pitch
P2= End_pitch
Len1= -Thread_length
W1= Thread_width


WPKClearErrorFlag
PlotOff
E1= WPKThreadShapeVP(Hand,D1,P1,P2,Len1,D2,W1,Start_angle,Thread_angle,(-Thread_taper),Zstart,Astep,Taper_type)

!    Trim Thread Shape at start and end
StdWorkplane
P1= PointXYZ(Zstart,0,0)
P2= PointXYZ(Zstart+1.0,0,0)
L1= LineTwoPoints(P1,P2)

T1=LineToVertTrans(L1,P1)
Stat=SetWorkplane(T1)
Stat= DeleteEntity(P1)
Stat= DeleteEntity(P2)
Stat= DeleteEntity(L1)

Pc= PointXYZ(0,0,0)
G1= WPKPlnSectionBody(E1,Pc,2)
Stat= DeleteEntity(Pc)

Pc= PointXYZ(0,0,Len1)
G1= WPKPlnSectionBody(E1,Pc,1)
Stat= DeleteEntity(Pc)
Stat= DeleteEntity(T1)

StdWorkplane
PlotOn
E2= WPKSubtractBodies(Pkbody,E1)

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

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

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