; MACROCOMMAND=Finish Diameter,Surface Speed,DOC,FPR,Finish Length,Radius,Angle,Tool Number, Max RPM
; MACRODEFAULTS=1,500,.02,.007,-2,0,0,1,1500
; MACROSVG=LatheMacro.svg,1

; MACROOPTIONS=load:yes,save:yes,default:default.txt,path:~/macros
; O<boring> call [${bore.x-f}] [${bore.sf-f}] [${bore.cut-f}] [${bore.feed-f}] [${bore.z-f}] [${bore.rad-f}] [${bore.angle-f}] [${bore.tool-s}]

; #1 finish diameter
; #2 surface speed
; #3 depth of cut
; #4 feed/rpm
; #5 finish length
; #6 radius
; #7 angle
; #8 tool number
; #9 max RPM

;boring

O<boring> sub

O110 IF [#<_imperial>]
    (MSG, IMPERIAL sizes)
O110  ELSE
    (MSG, METRIC sizes)
O110  ENDIF


(debug, Start Diameter %.3f#<_x>)
(debug, Finish Diameter %.3f#1)
(debug, Z Start %.3f#<_z>)
(debug, Z Finish %.3f#5)

O107 IF [#<_imperial>]
    (MSG, IMPERIAL ON)
    #1 = [#1 * 25.4]; X
    #2 = [#2 * 3.28]; surface speed
    #3 = [#3 * 25.4]; doc
    #4 = [#4 * 25.4]; feed
    #5 = [#5 * 25.4]; length
    #6 = [#6 * 25.4]; radius
O107  ENDIF

M73 ;save/restore codes

G8 ; Radius mode (easier maths)
G18 ; XZ Plane
G21 ; Metric Units
G90 ; Absolute Distance
G91.1 ; but not for arcs

#1 = [#1 / 2] ; because of radius mode

#14 = [#<_x>] (starting X)
#13 = #<_z> (starting Z)

#20 = [#6 * SIN[#7]]
#21 = [-#6 * COS[#7]]
#22 = [#6 / COS[#7]]
#23 = [#5 + #6 - #20]
#24 = [[#23 - #13] * TAN[#7]]

(MSG, Unpause To Start Boring Macro)
m0

M6 T#8 G43

G96 D#9 S#2 ; Constant Surface Speed Mode
m3 ;Start Spindle
g95 F#4 ; Feed-Per-Rev Mode

g4p1 ; Wait to reach speed

	(debug, Turning finish dia #1 start  dia #14 start length #13 finish length #5)
	O100 WHILE [#14 LT [#1 - #3]]
        g0 X #14
	#14=[#14 + #3]
	G1 X #14
	G1 Z #23 X[#14 + #24]
        O101 IF [#6 GT 0]
                G3 Z#5 X[#14 + #24 + #21] I#21 K#20
		G1 X[#14 + #24 + #21 - #3]
        O101 ELSE
	        G1 X[#14 + #24 - [#3 * 1.5]]
        O101 ENDIF
	G0 Z[#13]
	O100 ENDWHILE

	G0 x#1
	G1 Z #23 X[#1 + #24]
        O102 IF [#6 GT 0]
                G3 Z#5 X[#1 + #24 + #21] I#21 K#20
		G1 X[#1 + #24 + #21 - #3]
        O102 ELSE
	        G1 X[#1 + #24 - #3]
        O102 ENDIF
	G0 Z #13
	G0 X #1 ; For touch-off
        M5
	G7
O<boring> endsub

M2
