
! script name: wscturn.d/turnprofile
! topic: make a simple turning profile

!A
!A NAME:
!A
!A 	Simple Turn Profile
!A
!A DESCRIPTION:
!A	
!A	This script creates a simple turning profile consisting
!A	of multiple diameters and lengths.
!A
!A
!B INSTRUCTIONS:
!B
!B	Id or Od profile:    Specify that this is an Od or Id profile.
!B			This allows for certain assumptions which minimize
!B			input.
!B
!B	STARTING DIA:   Input the diameter at which the profile starts.
!B
!B	STARTING Z:     Input the Z at which the profile starts. Remember
!B			that left of the origin is negative.
!B
!B
!B	NEXT DIA  End profile:	Input the next diameter on the profile.
!B
!B	LENGTH: 	Input the length until the next diameter. For
!B			brevity of input a positive Length will result
!B			in a negative Z movement.
!B
!C NOTES:
!C
!C	The profile consists of a series of L shaped step moves.
!C	For an Od contour if the next diameter is larger than the 
!C	current diameter, the profile will proceed at the current
!C	diameter for the programmed length and then step up to the 
!C	next diameter. If the next diameter is smaller than the 
!C	current diameter, the profile will step down to the next diameter,
!C	then travel the programmed length.
!C
!C	Corner chamfers and radii may be added after the profile is 
!C	ended.
!C
!C	Each L shaped step contains parameters for turn taper angles
!C	and face angles. These may be changed at any time.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: simple turn profile ")

OPTION BASE 1
DIM Answer$[256]
DIM Destination$[256]
DEG


Var_object= CurrentVarObject
IF NOT ValidGeometry(Var_object) THEN
	Var_object=OpenVarObject("Program")
END IF
!  Var_object= CheckVarObject
!  IF (ExitScript(Destination$)) THEN Leave

Od_contour= WsTrue
Stat=SetXyzInput(WsLatheInput)
FadeUndoButtons
Stat=SetPickMask(WsPickNoSurf)
Start: !
Stat=SetInputFormat("a1=",",I,O,")
Stat= SetEqualsValue(0,"O")
Stat=GetValue("Id or Od profile",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Od_contour= (Answer$="O")


Spoint:Last=FindLastType
Stat=SetInputFormat("n4=","")
IF NOT ValidGeometry(Last) THEN
	Stat= SetInputFormat("n4b=","")
	Stat= SetEqualsValue(Dia1)
	Dia1=GetValue("STARTING DIA",Answer$)
	IF (ExitScript(Destination$)) THEN
                IF Destination$= "BACKUP" THEN Start
                GOTO Leave
        END IF
	Pick_ent= LastPickEnt
	Para1= LastPickPara
	Pick_return1= CurrPickReturn
	IF ValidGeometry(Pick_ent) THEN
		IF EntityType(Pick_ent)= WsCircleEnt THEN
			Dia1= 2.0*ABS(CircleRadius(Pick_ent))
		ELSE
			PlotOff
			Pt= ParameterReturnPt(Pick_ent,Para1,Pick_return1)
			Dia1= 2.0*YPointValue(Pt)
			Stat= DeleteEntity(Pt)
			PlotOn
		END IF
	END IF
	Dia= Dia1

	Stat= SetInputFormat("n4b=","")
        Stat= SetEqualsValue(Z1)
        Z1=GetValue("STARTING Z",Answer$)
	IF (ExitScript(Destination$)) THEN
        	IF Destination$= "BACKUP" THEN Spoint
        	GOTO Leave
	END IF

	Pick_ent= LastPickEnt
	Para1= LastPickPara
	Pick_return1= CurrPickReturn
	IF ValidGeometry(Pick_ent) THEN
                PlotOff
                Pt= ParameterReturnPt(Pick_ent,Para1,Pick_return1)
                Z1= XPointValue(Pt)
                Stat= DeleteEntity(Pt)
                PlotOn
	END IF

	Ps= PointXYZ(Z1,Dia1/2,0.0)
        Stat=SetLastPoint(Ps,WsXyOnly)
        First=1
        R1=0
ELSE
        Ps=TypeEndPoint(Last,WsEnd)
        Stat=SetLastPoint(Ps,WsXyOnly)
	Dia1= 2.0*YPointValue(Ps)
	Dia= Dia1
	Z1= XPointValue(Ps)
        R1=TypeEndRadius(Last,WsEnd)
        First=0
END IF

Epoint:!
Stat= SetInputFormat("n4ba1=",",E,")
Stat= SetEqualsValue(Dia)
Dia=GetValue("NEXT DIA  End profile",Answer$)
IF (ExitScript(Destination$)) THEN
      Stat= DeleteEntity(Ps)
      IF Destination$= "BACKUP" THEN Start
      GOTO Leave
END IF
IF Answer$= "E" THEN
      Stat= DeleteEntity(Ps)
      Stat=SetPickMask(WsPickAll)
      Stat=SetPickReturn(WsCircleCenter)
      LOAD "wsccommon.d/endprofile"
END IF

Pick_ent= LastPickEnt
Para1= LastPickPara
Pick_return1= CurrPickReturn
IF ValidGeometry(Pick_ent) THEN
	IF EntityType(Pick_ent)= WsCircleEnt THEN
		Dia= 2.0*ABS(CircleRadius(Pick_ent))
	ELSE
		PlotOff
		Pt= ParameterReturnPt(Pick_ent,Para1,Pick_return1)
		Dia= 2.0*YPointValue(Pt)
		Stat= DeleteEntity(Pt)
		PlotOn
	END IF
END IF

Stat= SetInputFormat("n4b=","")
Stat= SetEqualsValue(Len1)
Len1=GetValue("LENGTH",Answer$)
IF (ExitScript(Destination$)) THEN
      IF Destination$= "BACKUP" THEN Epoint
      GOTO Leave
END IF
Pick_ent= LastPickEnt
Para1= LastPickPara
Pick_return1= CurrPickReturn
IF ValidGeometry(Pick_ent) THEN
      IF EntityType(Pick_ent)= WsLineEnt THEN
                Len1= LineLength(Pick_ent)
      ELSE
                PlotOff
                Pt= ParameterReturnPt(Pick_ent,Para1,Pick_return1)
                A= XPointValue(Pt)
		Len1= A- Z1
                Stat= DeleteEntity(Pt)
                PlotOn
      END IF
END IF

Z= Z1- Len1
Pe= PointXYZ(Z,Dia/2,0.0)
Stat=SetLastPoint(Pe,WsXyOnly)
Stat=DeleteEntity(Ps,WsTrue)
Stat=DeleteEntity(Pe,WsTrue)

Stat= GenerateVarName(WsTypeEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("TURN : turn segment")
Last=FindLastType
RecordFunctionOff
IF ValidGeometry(Last) THEN
	RecordFunctionOn
	Ps=TypeEndPoint(Last,WsEnd)
	RecordFunctionOff
ELSE
	RecordFunctionOn
	V1= FreeParameter("START DIA",Dia1)
	V2= FreeParameter("START Z",Z1)
	Ps= DiaLengthPt(0,V1,V2,WsForward)
	RecordFunctionOff
END IF
RecordFunctionOn
V3= FreeParameter("NEXT DIA",Dia)
V4= FreeParameter("LENGTH",Len1)
V5= FreeParameter("Turn taper",0.0)
V6= FreeParameter("Face angle",0.0)
Pe= DiaLengthPt(Ps,V3,V4,WsReverse)
RecordFunctionOff
IF ValidGeometry(Last) THEN
RecordFunctionOn
V10= FreeParameter("R1",TypeEndRadius(Last,WsEnd))
Tp=Type6L(6,Ps,Pe,V10,0.0,0.0,V5,V6,180,90,WsReverse,Od_contour)
ELSE
RecordFunctionOn
Tp=Type6L(6,Ps,Pe,0.0,0.0,0.0,V5,V6,180,90,WsReverse,Od_contour)
END IF
RecordFunctionOff
IF ValidGeometry(Tp) THEN
        ActivateUndo
        Stat= AdvanceUndoNumber("turn segment")
        IF ValidGeometry(Last) THEN
                Stat= AddGroupToUndo(WsUBModifyMode,Last)
                RecordFunctionOn
                Stat=JoinTypes(Last,Tp)
                RecordFunctionOff
        END IF
        RecordFunctionOn
        Stat= NameEntity(Tp,Name$)

        Stat= DeleteEntity(Ps)
        Stat= DeleteEntity(Pe)
        RecordFunctionOff
        Curr_record= CurrentVarRecord
        CloseVarRecord

        Stat= AddGroupToUndo(WsUBCreateMode,Tp)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)

ELSE
        RecordFunctionOn
        Stat= DeleteEntity(Ps)
        Stat= DeleteEntity(Pe)
        RecordFunctionOff
        Curr_record= CurrentVarRecord
        CloseVarRecord

        DelLastVarRecord
END IF
PlotAll
GOTO Spoint


Leave:!
Stat= DeleteEntity(Ps)
Stat= DeleteEntity(Pe)
Stat=SetPickMask(WsPickAll)
Stat=SetPickReturn(WsCircleCenter)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 IF Destination$= "SCRIPTSTART" THEN Start
         IF Destination$= "BACKUP" THEN Start
	 LOAD Destination$
END IF
END
