Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,44 +4,64 @@ from cadquery import exporters
|
|
| 4 |
import trimesh
|
| 5 |
import pyvista as pv
|
| 6 |
from io import BytesIO
|
| 7 |
-
import matplotlib.pyplot as plt
|
| 8 |
import json
|
| 9 |
import os
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def generate_2d_view(step_file):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def generate_3d_view(step_file):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Wrapper function to generate
|
| 26 |
def generate_views(step_file):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# APDL
|
| 33 |
def generate_apdl_script(file, press_force, material_json):
|
| 34 |
try:
|
| 35 |
material = json.loads(material_json)
|
| 36 |
elastic_modulus = material.get("elastic_modulus", 2e11)
|
| 37 |
poisson = material.get("poisson", 0.3)
|
| 38 |
|
|
|
|
| 39 |
step_file_path = file.name
|
| 40 |
model = cq.importers.importStep(step_file_path)
|
|
|
|
| 41 |
stl_file = step_file_path.replace(".stp", ".stl")
|
| 42 |
exporters.export(model, stl_file)
|
| 43 |
|
| 44 |
-
apdl_script = f"""\
|
| 45 |
/PREP7
|
| 46 |
! Importing Geometry
|
| 47 |
/import, '{step_file_path}', geom, STEP
|
|
@@ -74,8 +94,8 @@ def main():
|
|
| 74 |
step_input = gr.File(label="Upload STEP File")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
-
two_d_output = gr.
|
| 78 |
-
three_d_output = gr.
|
| 79 |
generate_views_btn = gr.Button("Generate 2D & 3D Views")
|
| 80 |
|
| 81 |
with gr.Row():
|
|
@@ -92,7 +112,7 @@ def main():
|
|
| 92 |
|
| 93 |
# Events for Visualization
|
| 94 |
generate_views_btn.click(
|
| 95 |
-
fn=generate_views,
|
| 96 |
inputs=step_input,
|
| 97 |
outputs=[two_d_output, three_d_output]
|
| 98 |
)
|
|
@@ -109,7 +129,7 @@ def main():
|
|
| 109 |
outputs=[script_output, download_button]
|
| 110 |
)
|
| 111 |
|
| 112 |
-
app.launch()
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
main()
|
|
|
|
| 4 |
import trimesh
|
| 5 |
import pyvista as pv
|
| 6 |
from io import BytesIO
|
|
|
|
| 7 |
import json
|
| 8 |
import os
|
| 9 |
+
import atexit
|
| 10 |
+
import glob
|
| 11 |
|
| 12 |
+
# Cleanup function to remove temporary files
|
| 13 |
+
def cleanup_temp_files():
|
| 14 |
+
temp_files = glob.glob("*.stl") + glob.glob("*.inp")
|
| 15 |
+
for temp_file in temp_files:
|
| 16 |
+
try:
|
| 17 |
+
os.remove(temp_file)
|
| 18 |
+
except OSError:
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
atexit.register(cleanup_temp_files)
|
| 22 |
+
|
| 23 |
+
# Define 2D and 3D view generation functions with error handling
|
| 24 |
def generate_2d_view(step_file):
|
| 25 |
+
try:
|
| 26 |
+
model = cq.importers.importStep(step_file.name)
|
| 27 |
+
two_d_image = model.toSvg()
|
| 28 |
+
return two_d_image
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error generating 2D view: {str(e)}"
|
| 31 |
|
| 32 |
def generate_3d_view(step_file):
|
| 33 |
+
try:
|
| 34 |
+
model = cq.importers.importStep(step_file.name)
|
| 35 |
+
mesh = trimesh.Trimesh(vertices=model.val().vertices(), faces=model.val().faces())
|
| 36 |
+
three_d_image = mesh.show()
|
| 37 |
+
return three_d_image
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error generating 3D view: {str(e)}"
|
| 40 |
|
| 41 |
+
# Wrapper function to generate views
|
| 42 |
def generate_views(step_file):
|
| 43 |
+
try:
|
| 44 |
+
two_d_view = generate_2d_view(step_file)
|
| 45 |
+
three_d_view = generate_3d_view(step_file)
|
| 46 |
+
return two_d_view, three_d_view
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error: {str(e)}", None
|
| 49 |
|
| 50 |
+
# APDL script generation
|
| 51 |
def generate_apdl_script(file, press_force, material_json):
|
| 52 |
try:
|
| 53 |
material = json.loads(material_json)
|
| 54 |
elastic_modulus = material.get("elastic_modulus", 2e11)
|
| 55 |
poisson = material.get("poisson", 0.3)
|
| 56 |
|
| 57 |
+
# Use temporary file path for processing
|
| 58 |
step_file_path = file.name
|
| 59 |
model = cq.importers.importStep(step_file_path)
|
| 60 |
+
|
| 61 |
stl_file = step_file_path.replace(".stp", ".stl")
|
| 62 |
exporters.export(model, stl_file)
|
| 63 |
|
| 64 |
+
apdl_script = f"""\n
|
| 65 |
/PREP7
|
| 66 |
! Importing Geometry
|
| 67 |
/import, '{step_file_path}', geom, STEP
|
|
|
|
| 94 |
step_input = gr.File(label="Upload STEP File")
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
+
two_d_output = gr.Textbox(label="2D View/Error Message", interactive=False)
|
| 98 |
+
three_d_output = gr.Textbox(label="3D View/Error Message", interactive=False)
|
| 99 |
generate_views_btn = gr.Button("Generate 2D & 3D Views")
|
| 100 |
|
| 101 |
with gr.Row():
|
|
|
|
| 112 |
|
| 113 |
# Events for Visualization
|
| 114 |
generate_views_btn.click(
|
| 115 |
+
fn=generate_views,
|
| 116 |
inputs=step_input,
|
| 117 |
outputs=[two_d_output, three_d_output]
|
| 118 |
)
|
|
|
|
| 129 |
outputs=[script_output, download_button]
|
| 130 |
)
|
| 131 |
|
| 132 |
+
app.launch(server_port=7861, debug=True)
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
main()
|