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

!A
!A NAME:
!A
!A 	Lathe 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 root.
!B
!B		# THREADS PER INCH: Input the number of threads per inch.
!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: lathe thread ")

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

ActivateUndo
Units= CurrentUnits
IF Units= WsMetric THEN
        Thread_radius= 25
        Pitch= 0.125*25.4
	Thread_length= 25
	Thread_depth= Pitch/2
	Thread_angle= 60
	Thread_taper= 0
	Nthreads= 1.0/Pitch
	ask_pitch=1
ELSE
        Thread_radius= 1
        Pitch= 0.125
	Thread_length= 1
	Thread_depth= Pitch/2
	Thread_angle= 60
	Thread_taper= 0
	NThreads= 8
	ask_pitch=0
END IF

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

IF Ask_pitch THEN
	Stat=SetInputFormat("n4=","")
	Stat=SetEqualsValue(Pitch)
	Pitch=GetValue("PITCH",Answer$)
	IF ExitScript(Destination$) THEN Leave
ELSE
	Stat=SetInputFormat("n4=","")
	Stat=SetEqualsValue(Nthreads)
	Nthreads=GetValue("# THREADS PER INCH ",Answer$)
	IF ExitScript(Destination$) THEN Leave
	IF Nthreads<= 0 THEN Start
	Pitch= 1/Nthreads
END IF

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)

WPKClearErrorFlag
PlotOff
E1= WPKThreadShape(Hand,Thread_diameter,Pitch,(-Thread_length),Odid*Thread_depth,Thread_width,Start_angle,Thread_angle,(-Thread_taper),Zstart,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,(-Thread_length))
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("lathe 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
