engrharis commited on
Commit
c0ed18c
·
verified ·
1 Parent(s): 69304bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -4,11 +4,7 @@ import tempfile
4
  from io import BytesIO
5
  from fpdf import FPDF
6
  import requests
7
- import json
8
- import pythonocc
9
- from pathlib import Path
10
- from OCC.Extend.DataExchange import read_step_file
11
- from OCC.Core.STEPControl import STEPControl_Reader
12
 
13
  # Groq API endpoint and headers
14
  GROQ_API_URL = "https://api.groq.com/v1/query"
@@ -36,22 +32,16 @@ def generate_report(cad_data):
36
  st.error(f"Error in report generation: {response.text}")
37
  return "Error generating report."
38
 
39
- # Function to parse STEP CAD files using pythonOCC
40
- def parse_cad_file(file_path):
41
- # Read the STEP file and extract data using pythonOCC
42
- step_reader = STEPControl_Reader()
43
- status = step_reader.ReadFile(str(file_path))
44
 
45
- if status != 1: # If the file couldn't be read properly
46
- st.error("Failed to read CAD file.")
47
- return None
48
 
49
- step_reader.TransferRoot()
50
- shape = step_reader.Shape()
51
-
52
- # Extract basic information from the shape
53
- # Here we can extract dimension, material properties, and more based on the available data
54
- cad_data = f"Extracted information from {file_path.name}: \n - Shape contains {shape.NbChildren()} components.\n - Additional analysis can be performed."
55
  return cad_data
56
 
57
  # Function to create a PDF report
@@ -78,7 +68,7 @@ def create_pdf_report(report_text):
78
  st.title("CAD File Analysis and Technical Report Generation")
79
 
80
  # File uploader
81
- uploaded_file = st.file_uploader("Upload a CAD file", type=["stl", "step", "dxf", "iges"])
82
 
83
  if uploaded_file is not None:
84
  # Save uploaded file temporarily
@@ -86,8 +76,8 @@ if uploaded_file is not None:
86
  with open(temp_file_path, "wb") as f:
87
  f.write(uploaded_file.getbuffer())
88
 
89
- # Parse the CAD file and extract relevant data
90
- cad_data = parse_cad_file(temp_file_path)
91
 
92
  if cad_data:
93
  # Display extracted data as a preview
 
4
  from io import BytesIO
5
  from fpdf import FPDF
6
  import requests
7
+ from stl import mesh
 
 
 
 
8
 
9
  # Groq API endpoint and headers
10
  GROQ_API_URL = "https://api.groq.com/v1/query"
 
32
  st.error(f"Error in report generation: {response.text}")
33
  return "Error generating report."
34
 
35
+ # Function to parse STL CAD files using pySTL
36
+ def parse_stl_file(file_path):
37
+ # Load the STL file and extract basic information
38
+ cad_mesh = mesh.Mesh.from_file(file_path)
 
39
 
40
+ # Example: Get the number of faces and volume
41
+ num_faces = cad_mesh.vectors.shape[0]
42
+ volume = cad_mesh.get_mass_properties()[0] # Get volume from mass properties
43
 
44
+ cad_data = f"Extracted information from {file_path.name}:\n - Number of Faces: {num_faces}\n - Volume: {volume} cubic units."
 
 
 
 
 
45
  return cad_data
46
 
47
  # Function to create a PDF report
 
68
  st.title("CAD File Analysis and Technical Report Generation")
69
 
70
  # File uploader
71
+ uploaded_file = st.file_uploader("Upload a CAD file", type=["stl"])
72
 
73
  if uploaded_file is not None:
74
  # Save uploaded file temporarily
 
76
  with open(temp_file_path, "wb") as f:
77
  f.write(uploaded_file.getbuffer())
78
 
79
+ # Parse the STL file and extract relevant data
80
+ cad_data = parse_stl_file(temp_file_path)
81
 
82
  if cad_data:
83
  # Display extracted data as a preview