jithenderchoudary commited on
Commit
f460ca7
·
verified ·
1 Parent(s): 5f5a54f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -36
app.py CHANGED
@@ -8,45 +8,58 @@ import matplotlib.pyplot as plt
8
  import json
9
  import os
10
 
11
- # 2D View Function
12
- def generate_2d_view(step_file):
13
- try:
14
- mesh = trimesh.load(step_file.name)
15
- projection = mesh.section(plane_origin=[0, 0, 0], plane_normal=[0, 0, 1])
16
- projection_img = projection.to_image() if projection else None
17
-
18
- fig, ax = plt.subplots()
19
- ax.imshow(projection_img, cmap="gray")
20
- ax.axis("off")
21
-
22
- buffer = BytesIO()
23
- plt.savefig(buffer, format="png")
24
- buffer.seek(0)
25
- return buffer
26
- except Exception as e:
27
- return f"Error in generating 2D view: {e}"
28
 
29
- # 3D View Function
30
- def generate_3d_view(step_file):
31
- try:
32
- model = cq.importers.importStep(step_file.name)
33
- stl_file = "temp_model.stl"
34
- exporters.export(model, stl_file)
35
 
36
- mesh = pv.read(stl_file)
37
- plotter = pv.Plotter(off_screen=True)
38
- plotter.add_mesh(mesh, color="lightblue")
39
- screenshot = plotter.screenshot(asarray=True)
40
-
41
- buffer = BytesIO()
42
- plt.imshow(screenshot)
43
- plt.axis("off")
44
- plt.savefig(buffer, format="png")
45
- buffer.seek(0)
46
- return buffer
47
- except Exception as e:
48
- return f"Error in generating 3D view: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
 
50
  # APDL Script Generation
51
  def generate_apdl_script(file, press_force, material_json):
52
  try:
 
8
  import json
9
  import os
10
 
11
+ # Wrapper function to generate both 2D and 3D views
12
+ def generate_views(step_file):
13
+ # Call the individual functions and collect their outputs
14
+ two_d_view = generate_2d_view(step_file)
15
+ three_d_view = generate_3d_view(step_file)
16
+ return two_d_view, three_d_view
17
+
18
+ # Updated Gradio Interface
19
+ def main():
20
+ with gr.Blocks() as app:
21
+ gr.Markdown("# Press Tool Design and APDL Script Generator")
 
 
 
 
 
 
22
 
23
+ with gr.Row():
24
+ step_input = gr.File(label="Upload STEP File")
 
 
 
 
25
 
26
+ with gr.Row():
27
+ two_d_output = gr.Image(label="2D View")
28
+ three_d_output = gr.Image(label="3D View")
29
+ generate_views_btn = gr.Button("Generate 2D & 3D Views")
30
+
31
+ with gr.Row():
32
+ force_input = gr.Number(label="Press Force (N)", value=1000)
33
+ material_input = gr.Textbox(
34
+ label="Material Properties (JSON)",
35
+ value='{"elastic_modulus": 2e11, "poisson": 0.3}'
36
+ )
37
+
38
+ with gr.Row():
39
+ script_output = gr.Textbox(label="Generated APDL Script", interactive=False)
40
+ download_button = gr.File(label="Download Script")
41
+ generate_apdl_btn = gr.Button("Generate APDL Script")
42
+
43
+ # Events for Visualization
44
+ generate_views_btn.click(
45
+ fn=generate_views, # Use the wrapper function
46
+ inputs=step_input,
47
+ outputs=[two_d_output, three_d_output]
48
+ )
49
+
50
+ # Events for APDL Script
51
+ def handle_download(apdl_script, apdl_file_path):
52
+ if apdl_file_path and os.path.exists(apdl_file_path):
53
+ return apdl_file_path
54
+ return None
55
+
56
+ generate_apdl_btn.click(
57
+ fn=generate_apdl_script,
58
+ inputs=[step_input, force_input, material_input],
59
+ outputs=[script_output, download_button]
60
+ )
61
 
62
+
63
  # APDL Script Generation
64
  def generate_apdl_script(file, press_force, material_json):
65
  try: