
! script name: pksolids.d/pkthreadshape
! solids: construct  threadshape

!A
!A NAME:
!A
!A 	Thread Shape: 
!A
!A DESCRIPTION:
!A
!A	This script constructs a solid thread shape. The thread shape is
!A	constructed to be used as a solid to be subtracted or united
!A	with a cylinder or cone to form the actual thread. The end of the
!A	thread shape is left flat. A real thread would require trimming
!A	to properly form the end shape.
!A
!B INSTRUCTIONS:
!B
!B		THREAD DIAMETER: Diameter of thread tip.
!B
!B		# THREADS PER INCH:
!B
!B		THREAD LENGTH:  Length of thread.
!B
!B		INCLUDED ANGLE: Usually 60 deg.
!B
!B		THREAD DEPTH: Depth of thgead shape.
!B
!B		THREAD 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: thread shape ")

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
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("n4=","")
Stat=SetRadiusEquals(0.5*Thread_diameter)
Thread_diameter=InputRadius("THREAD 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("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 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(Zstart)
Zstart=GetValue("Z START",Answer$)
IF ExitScript(Destination$) THEN Leave

Start_angle= 0.0


WPKClearErrorFlag
E1= WPKThreadShape(Hand,Thread_diameter,Pitch,Thread_length,Thread_depth,Thread_width,Start_angle,Thread_angle,Thread_taper,Zstart,Taper_type)


ActivateUndo
Stat= AdvanceUndoNumber("thread shape")
Stat= AddItemToUndo(WsUBPKSessionMark)
WPKErrorCheck
GOTO Cloop

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