Spaces:
Sleeping
Sleeping
Update utils/gcode_utils.py
Browse files- utils/gcode_utils.py +45 -6
utils/gcode_utils.py
CHANGED
|
@@ -1,14 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
-
def generate_gcode(dimensions):
|
| 4 |
"""
|
| 5 |
-
Generates
|
|
|
|
| 6 |
"""
|
| 7 |
try:
|
|
|
|
| 8 |
output_dir = "/tmp/"
|
| 9 |
os.makedirs(output_dir, exist_ok=True)
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
gcode_content = f"""
|
| 13 |
G21 ; Set units to millimeters
|
| 14 |
G17 ; Select XY plane
|
|
@@ -19,14 +24,48 @@ def generate_gcode(dimensions):
|
|
| 19 |
G0 X0 Y0 ; Move to start position
|
| 20 |
G1 Z-1 F300 ; Lower Z-axis to cutting depth
|
| 21 |
|
| 22 |
-
G1
|
| 23 |
-
G1
|
| 24 |
-
G1 X0
|
| 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 |
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
+
def generate_gcode(dimensions, material, forces, tolerances, analysis_type):
|
| 4 |
"""
|
| 5 |
+
Generates G-Code for CNC machining based on input dimensions, material, forces, tolerances, and analysis type.
|
| 6 |
+
Returns the file path of the generated G-Code file.
|
| 7 |
"""
|
| 8 |
try:
|
| 9 |
+
# Ensure the output directory exists
|
| 10 |
output_dir = "/tmp/"
|
| 11 |
os.makedirs(output_dir, exist_ok=True)
|
| 12 |
|
| 13 |
+
# Dimensions input (length, width, height)
|
| 14 |
+
length, width, height = map(float, dimensions.split('x'))
|
| 15 |
+
|
| 16 |
+
# Define the G-Code content
|
| 17 |
gcode_content = f"""
|
| 18 |
G21 ; Set units to millimeters
|
| 19 |
G17 ; Select XY plane
|
|
|
|
| 24 |
G0 X0 Y0 ; Move to start position
|
| 25 |
G1 Z-1 F300 ; Lower Z-axis to cutting depth
|
| 26 |
|
| 27 |
+
G1 X{length} Y0 ; Cut to the first corner (along X)
|
| 28 |
+
G1 X{length} Y{width} ; Cut to the second corner (along Y)
|
| 29 |
+
G1 X0 Y{width} ; Cut to the third corner (along X)
|
| 30 |
G1 X0 Y0 ; Return to the start position
|
| 31 |
|
| 32 |
G0 Z5 ; Raise Z-axis to safe height
|
| 33 |
M30 ; End program
|
| 34 |
"""
|
| 35 |
+
|
| 36 |
+
# Apply forces (example: change cutting parameters based on forces)
|
| 37 |
+
if forces:
|
| 38 |
+
gcode_content += f"""
|
| 39 |
+
! Apply additional cutting force: {forces} N
|
| 40 |
+
G1 Z-{forces / 10} F300 ; Adjust depth based on forces (mock example)
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
# Handle specific tolerances (optional)
|
| 44 |
+
if tolerances:
|
| 45 |
+
gcode_content += f"""
|
| 46 |
+
! Apply tolerance: {tolerances}
|
| 47 |
+
G1 X{length - tolerances} Y{width - tolerances} ; Adjust final cut based on tolerance
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
# Handle analysis type for different G-Code behavior (optional)
|
| 51 |
+
if analysis_type == "Structural":
|
| 52 |
+
gcode_content += """
|
| 53 |
+
! Structural analysis toolpath adjustments
|
| 54 |
+
G1 X50 Y50 ; Move to a specific position for structural test (mock example)
|
| 55 |
+
"""
|
| 56 |
+
elif analysis_type == "Thermal":
|
| 57 |
+
gcode_content += """
|
| 58 |
+
! Thermal analysis toolpath adjustments
|
| 59 |
+
G1 X75 Y75 ; Modify path for thermal analysis test (mock example)
|
| 60 |
+
"""
|
| 61 |
+
elif analysis_type == "Acoustic":
|
| 62 |
+
gcode_content += """
|
| 63 |
+
! Acoustic analysis toolpath adjustments
|
| 64 |
+
G1 X25 Y25 ; Adjust path for acoustic testing (mock example)
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
# Save the G-Code to a file
|
| 68 |
+
gcode_file = os.path.join(output_dir, "generated_gcode.nc")
|
| 69 |
with open(gcode_file, "w") as f:
|
| 70 |
f.write(gcode_content)
|
| 71 |
|