Spaces:
Sleeping
Sleeping
Update design.py
Browse files
design.py
CHANGED
|
@@ -2,41 +2,33 @@ import cadquery as cq
|
|
| 2 |
import os
|
| 3 |
import pyvista as pv
|
| 4 |
|
| 5 |
-
def generate_die(length, width, thickness):
|
| 6 |
-
try:
|
| 7 |
-
output_dir = "outputs"
|
| 8 |
-
os.makedirs(output_dir, exist_ok=True)
|
| 9 |
-
|
| 10 |
-
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 11 |
-
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 12 |
-
die = plate.cut(punch)
|
| 13 |
-
|
| 14 |
-
filename = os.path.join(output_dir, "progressive_die.step")
|
| 15 |
-
cq.exporters.export(die, filename)
|
| 16 |
-
|
| 17 |
-
return filename
|
| 18 |
-
except Exception as e:
|
| 19 |
-
return f"Error generating die: {str(e)}"
|
| 20 |
-
|
| 21 |
def visualize_die(length, width, thickness):
|
| 22 |
try:
|
| 23 |
output_dir = "outputs"
|
| 24 |
os.makedirs(output_dir, exist_ok=True)
|
| 25 |
|
|
|
|
| 26 |
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 27 |
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 28 |
die = plate.cut(punch)
|
| 29 |
|
|
|
|
| 30 |
stl_file = os.path.join(output_dir, "progressive_die.stl")
|
| 31 |
-
cq.exporters.
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
mesh = pv.read(stl_file)
|
| 35 |
plotter = pv.Plotter(off_screen=True)
|
| 36 |
plotter.add_mesh(mesh, color="blue")
|
| 37 |
screenshot = os.path.join(output_dir, "progressive_die_visualization.png")
|
| 38 |
plotter.screenshot(screenshot)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return screenshot
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error visualizing die: {str(e)}"
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import pyvista as pv
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def visualize_die(length, width, thickness):
|
| 6 |
try:
|
| 7 |
output_dir = "outputs"
|
| 8 |
os.makedirs(output_dir, exist_ok=True)
|
| 9 |
|
| 10 |
+
# Create die geometry
|
| 11 |
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 12 |
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
|
| 13 |
die = plate.cut(punch)
|
| 14 |
|
| 15 |
+
# Export STL file for visualization
|
| 16 |
stl_file = os.path.join(output_dir, "progressive_die.stl")
|
| 17 |
+
cq.exporters.export(die, stl_file) # Corrected line
|
| 18 |
|
| 19 |
+
# Generate 3D visualization screenshot
|
| 20 |
+
pv.global_theme.off_screen = True # Ensure off-screen rendering
|
| 21 |
mesh = pv.read(stl_file)
|
| 22 |
plotter = pv.Plotter(off_screen=True)
|
| 23 |
plotter.add_mesh(mesh, color="blue")
|
| 24 |
screenshot = os.path.join(output_dir, "progressive_die_visualization.png")
|
| 25 |
plotter.screenshot(screenshot)
|
| 26 |
|
| 27 |
+
# Ensure the screenshot file exists
|
| 28 |
+
if not os.path.exists(screenshot):
|
| 29 |
+
raise FileNotFoundError("Screenshot file was not generated.")
|
| 30 |
+
|
| 31 |
return screenshot
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error visualizing die: {str(e)}"
|
| 34 |
+
|