
! script name: macros.d/easyshaft
! EXAMPLE: semiautomates the making of lathe shafts

!A
!A NAME:
!A
!A 	Shaft
!A
!A DESCRIPTION:
!A
!A 	This script semiautomates the making of lathe shafts.
!A 	The starting point is assumed to be 0,0.
!A
!B INSTRUCTIONS:
!B
!B 	CORNER RADIUS: Enter the radius to be used at the corner.
!B
!B 	CHAMFER LENGTH: Enter the chamfer length to be used.
!B
!B 	NEXT TURN END ABS Z: Enter the Z coordinate for the next
!B 		turn in the shaft (the start of the next section
!B 		of the shaft being described).
!B
!B 	CORNER Chamfer, Fillet, Sharp: Enter the type of corner
!B 		to be put at the end of this section of the shaft.
!B 		Fillet should not be used to end the last section,
!B 		if fillet is used on the last section it will be
!B 		ignored.
!B
!B 	TAPER ANGLE ON FACE: Enter the angle to be used for the
!B 		taper on the face of this section of the shaft.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: shaft ")



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

DEG

DeActivateUndo
Tapered_face= 1
Corner_type$= "C"
End_type$= "F"

Face_taper= 0
stat= SetAngleEquals(Face_taper)

Corner_radius=NamedVarVal("corner radius",WsLatheVars,.030)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Corner_radius)
Corner_radius=GetValue("CORNER RADIUS ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Vcorner=CreateVariable("corner radius",WsLatheVars,Corner_radius)

Chamfer_length=NamedVarVal("chamfer",WsLatheVars,.050)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Chamfer_length)
Chamfer_length=GetValue("CHAMFER LENGTH ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Vchamfer=CreateVariable("chamfer length",WsLatheVars,Chamfer_length)

Xstart= 0
Ystart= 0

stat= SetXyzInput(WsLatheInput)
Shaft_loop:!
Stat=SetInputFormat("n4=","")

Pe=XyzInput("NEXT TURN END ",WsXyOnly)
IF (ExitScript(Destination$)) THEN Exit_shaft

stat= SetLastPoint(pe)
Xend= XPointValue(Pe)
Yend= YPointValue(Pe)
stat= DeleteEntity(Pe)

delx= ABS(Xend- Xstart)
dely= ABS(Yend- Ystart)

dist= delx+ dely
if(dist< 0.001) THEN Exit_shaft

Stat=SetInputFormat("a1=",",C,F,S,")
Stat= SetEqualsValue(0,Corner_type$)
Stat= GetValue("CORNER Chamfer, Fillet, Sharp",Answer$)
IF (ExitScript(Destination$)) THEN Exit_shaft
Corner_type= 1*(Answer$= "C")+2*(Answer$= "F")
Corner_type$= Answer$

Stat=SetInputFormat("a1=",",C,F,S,")
Stat= SetEqualsValue(0,End_type$)
Stat= GetValue("END Chamfer, Fillet, Sharp",Answer$)
IF (ExitScript(Destination$)) THEN Exit_shaft
End_type= 1*(Answer$= "C")+2*(Answer$= "F")
End_type$= Answer$

IF Tapered_face THEN
	face_taper= InputAngle("TAPER ANGLE ON FACE")
	IF (ExitScript(Destination$)) THEN Exit_shaft
ELSE 
	face_taper= 0
END IF

dist= ABS(Xend-Xstart)+ ABS(Yend-Ystart)
if(dist< 0.001) THEN Exit_shaft

delx= ABS(Xend- Xstart)
dely= ABS(Yend- Ystart)

if Delx< Dely*TAN(Face_taper) THEN Face_taper=0

Sign1= (Xstart<= Xend)- (Xstart> Xend)
Xcorner= Xstart+ Dely*TAN(Face_taper)*Sign1
Ycorner= Yend

dely= ABS(Ycorner- Ystart)
if dely > 0.001 THEN
	tlast= FindLastType
	SELECT Corner_type
	CASE 0
		t1= Type1(1,Xstart,Ystart,Xcorner,Ycorner)
	CASE 1
		if(Chamfer_length>= dely) THEN Shaft_loop
		t1= Type10(11,Xstart,Ystart,Xcorner,Ycorner,Chamfer_length)
	CASE 2
		if(Corner_radius>= dely) THEN Shaft_loop
		t1= Type10(10,Xstart,Ystart,Xcorner,Ycorner,Corner_radius)
	END SELECT
	IF (ValidGeometry(tlast)) THEN
		stat= JoinTypes(tlast,t1)
	END IF
END IF

delx= ABS(Xend- Xcorner)
if delx > 0.001 THEN
	tlast= FindLastType
 	SELECT End_type
        CASE 0
                t2= Type1(1,Xcorner,Ycorner,Xend,Yend)
        CASE 1
		if(Chamfer_length>= delx) THEN Shaft_loop
                t2= Type10(11,Xcorner,Ycorner,Xend,Yend,Chamfer_length)
        CASE 2
		if(Corner_radius>= delx) THEN Shaft_loop
                t2= Type10(10,Xcorner,Ycorner,Xend,Yend,Corner_radius)
        END SELECT
	IF (ValidGeometry(tlast)) THEN
		stat= JoinTypes(tlast,t2)
	END IF
END IF

Xstart= Xend
Ystart= Yend
GOTO Shaft_loop

Exit_shaft:!
	tlast= FindLastType
	IF (ValidGeometry(tlast)) THEN
		stat= CloseType(tlast)
	END IF

	Stat=HighlightTypes(tlast,195)
	Stat=UnLinkTypes(tlast)
Leave:!

IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
