123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #!/usr/bin/env python
-
- import sys
-
- pwr = 1
- iterations = 100
- speed = 3000
-
- def file_bounding_box(f):
- x_min = None
- x_max = None
- y_min = None
- y_max = None
- mode_g1 = False
-
- for line in f:
- if line.startswith("G1"):
- mode_g1 = True
- elif line.startswith("G0"):
- mode_g1 = False
-
- if mode_g1:
- x_pos = line.find("X")
- if x_pos > -1:
- x_str = line[x_pos + 1:]
- x_num = float(x_str.split()[0])
- #print("found x: " + str(x_num))
- if (x_min == None) or (x_num < x_min):
- x_min = x_num
- if (x_max == None) or (x_num > x_max):
- x_max = x_num
-
- y_pos = line.find("Y")
- if y_pos > -1:
- y_str = line[y_pos + 1:]
- y_num = float(y_str.split()[0])
- #print("found y: " + str(y_num))
- if (y_min == None) or (y_num < y_min):
- y_min = y_num
- if (y_max == None) or (y_num > y_max):
- y_max = y_num
-
- return ( x_min, x_max, y_min, y_max )
-
- def write_outline(f, bb):
- def write(s):
- #print(s)
- f.write(s + "\n")
-
- x_min, x_max, y_min, y_max = bb
-
- print("x_min: " + str(x_min))
- print("x_max: " + str(x_max))
- print("y_min: " + str(y_min))
- print("y_max: " + str(y_max))
-
- pos = [
- ( x_min, y_min ),
- ( x_max, y_min ),
- ( x_max, y_max ),
- ( x_min, y_max ),
- ]
-
- # header
- write("G90")
- write("G28")
- write("G92 X0 Y-20")
- write("M3 I")
- write("M3 S0")
-
- write("G0 X0 Y0 S0 F" + str(speed))
- write("G1 X1 Y0 S1 F" + str(speed))
- write("G0 X0 Y0 S0 F" + str(speed))
- write("G1 X0 Y1 S1 F" + str(speed))
- write("G0 X0 Y0 S0 F" + str(speed))
-
- write("G0 X" + str(x_min) + " Y" + str(y_min) + " S0 F" + str(speed))
-
- for i in range(0, iterations):
- for x, y in pos:
- write("G1 X" + str(x) + " Y" + str(y) + " S" + str(pwr) + " F" + str(speed))
-
- write("M5")
- write("G0 X" + str(x_min) + " Y" + str(y_min) + " S0 F" + str(speed))
-
- def main():
- if len(sys.argv) < 3:
- print("Usage:")
- print(" " + sys.argv[0] + " input.nc output.gcode")
- sys.exit(1)
-
- in_file = sys.argv[1]
- out_file = sys.argv[2]
-
- with open(in_file, 'r') as f:
- bb = file_bounding_box(f)
-
- with open(out_file, 'w') as f:
- write_outline(f, bb)
-
- if __name__ == '__main__':
- main()
|