Spaces:
Sleeping
Sleeping
Create gcode_utils.py
Browse files- utils/gcode_utils.py +35 -0
utils/gcode_utils.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
def generate_gcode(step_file):
|
| 4 |
+
"""
|
| 5 |
+
Generates G-Code for CNC machining from a STEP file.
|
| 6 |
+
"""
|
| 7 |
+
try:
|
| 8 |
+
output_dir = "/mnt/data/"
|
| 9 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
gcode_file = os.path.join(output_dir, "generated_gcode.nc")
|
| 12 |
+
gcode_content = """
|
| 13 |
+
G21 ; Set units to millimeters
|
| 14 |
+
G17 ; Select XY plane
|
| 15 |
+
G90 ; Absolute positioning
|
| 16 |
+
G1 F1500 ; Set feed rate
|
| 17 |
+
|
| 18 |
+
G0 Z5 ; Raise Z-axis to safe height
|
| 19 |
+
G0 X0 Y0 ; Move to start position
|
| 20 |
+
G1 Z-1 F300 ; Lower Z-axis to cutting depth
|
| 21 |
+
|
| 22 |
+
G1 X10 Y0 ; Cut to the first corner
|
| 23 |
+
G1 X10 Y10 ; Cut to the second corner
|
| 24 |
+
G1 X0 Y10 ; Cut to the third corner
|
| 25 |
+
G1 X0 Y0 ; Return to the start position
|
| 26 |
+
|
| 27 |
+
G0 Z5 ; Raise Z-axis to safe height
|
| 28 |
+
M30 ; End program
|
| 29 |
+
"""
|
| 30 |
+
with open(gcode_file, "w") as f:
|
| 31 |
+
f.write(gcode_content)
|
| 32 |
+
|
| 33 |
+
return gcode_file
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error in G-Code generation: {e}"
|