import math # Python program to generate G Code for a diamond knurl using a rotary axis on the mill. # Parameters required: # Initial stock diameter # Number of circumferential "teeth" - this should be PRIME # Length of knurl defined as the number of longitudinal teeth # Feedrates - horizontal and vertical # Width of engraving point flat # Output filename with extension (e.g. .txt) # Horizontal axis height above machine zero. diameter = float(input("Stock diameter: ")) teeth = int(input("# teeth (prime): ")) length = int(input("# axial diamonds: ")) feed = float(input("feedrate: " )) Z_feedrate = float(input("Downfeed rate; ")) flat = float(input("Engraving point flat width: ")) height = float(input("axis height: ")) name = input("Output filename: ") #include .txt or other extension # Now do geometry calcs pi = 4*math.atan(1) lead = pi * diameter tooth_angle = 360 / teeth L_pitch = lead / teeth X_feed = round(length*L_pitch,3) A_feed = round(length*tooth_angle,3) Z_feed = round(diameter * 0.35355 * math.sin(pi/teeth) - flat/2,3) Z_safe = height + diameter/2 + 1 # print(flead, tooth_angle, L_pitch, X_feed, A_feed, Z_feed, Z_safe) myFile = open(name, 'w') # Now start generating g-code print(f"G0 G49 G40 G17 G80 G50 G90", file = myFile) print(f"G00 Z{Z_safe}", file = myFile) print(f"G00 X0 Y0", file = myFile) print(f"G91", file = myFile) print(f"G00 A5", file = myFile) print(f"G01 Z{-1-Z_feed} F{Z_feedrate}", file = myFile) count = 0 print(f"F{feed}", file = myFile) while count < teeth: print(f"G01 X{X_feed} A{A_feed} ({count+1})", file = myFile) print(f"G01 X{-X_feed} A{A_feed} ({count+1})", file = myFile) count += 1 print(f"G90", file = myFile) print(f"G00 Z{Z_safe}", file = myFile) print(f"M5 M30", file = myFile) myFile.close() print("Finished!")