Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,10 +31,32 @@ def generate_2d_view(file):
|
|
| 31 |
|
| 32 |
def generate_3d_view(file):
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error generating 3D view: {str(e)}"
|
| 40 |
|
|
|
|
| 31 |
|
| 32 |
def generate_3d_view(file):
|
| 33 |
try:
|
| 34 |
+
# Import CAD file
|
| 35 |
+
model = cq.importers.importStep(file.name)
|
| 36 |
+
|
| 37 |
+
# Extract all solids from the Compound object
|
| 38 |
+
solids = model.solids()
|
| 39 |
+
if not solids:
|
| 40 |
+
raise ValueError("No solids found in the CAD model.")
|
| 41 |
+
|
| 42 |
+
# Convert solids into trimesh meshes
|
| 43 |
+
meshes = []
|
| 44 |
+
for solid in solids:
|
| 45 |
+
vertices = []
|
| 46 |
+
faces = []
|
| 47 |
+
for face in solid.tessellate():
|
| 48 |
+
vertices.extend(face[0]) # Extract vertices
|
| 49 |
+
faces.extend([[face[1][i], face[1][i + 1], face[1][i + 2]] for i in range(0, len(face[1]), 3)]) # Faces
|
| 50 |
+
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
| 51 |
+
meshes.append(mesh)
|
| 52 |
+
|
| 53 |
+
# Combine all meshes into one (if multiple solids)
|
| 54 |
+
combined_mesh = trimesh.util.concatenate(meshes) if len(meshes) > 1 else meshes[0]
|
| 55 |
+
|
| 56 |
+
# Visualize the mesh
|
| 57 |
+
combined_mesh.show()
|
| 58 |
+
|
| 59 |
+
return "3D view generated successfully."
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error generating 3D view: {str(e)}"
|
| 62 |
|