Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,19 @@ import re
|
|
| 3 |
import math
|
| 4 |
import streamlit as st
|
| 5 |
import cadquery as cq
|
|
|
|
| 6 |
from PyPDF2 import PdfReader
|
| 7 |
from groq import Groq
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Initialize Groq API client
|
| 10 |
client = Groq(api_key="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")
|
| 11 |
|
| 12 |
# Function to parse profile description
|
| 13 |
def parse_profile(description):
|
| 14 |
-
"""
|
| 15 |
-
Parse text input for dimensions and angles.
|
| 16 |
-
Example input: "profile with 50mm then 90 degree bend..."
|
| 17 |
-
"""
|
| 18 |
pattern = r"(\d+)\s*mm|(\d+)\s*degree"
|
| 19 |
matches = re.findall(pattern, description)
|
| 20 |
-
|
| 21 |
profile = []
|
| 22 |
for match in matches:
|
| 23 |
if match[0]:
|
|
@@ -28,13 +26,9 @@ def parse_profile(description):
|
|
| 28 |
|
| 29 |
# Function to generate CAD model using CadQuery
|
| 30 |
def generate_cad_model(profile, is_3d=False):
|
| 31 |
-
"""
|
| 32 |
-
Generate CAD model using CadQuery based on parsed profile data.
|
| 33 |
-
"""
|
| 34 |
model = cq.Workplane("XY")
|
| 35 |
x, y = 0, 0
|
| 36 |
angle = 0
|
| 37 |
-
|
| 38 |
for segment in profile:
|
| 39 |
if segment["type"] == "length":
|
| 40 |
x += segment["value"] * math.cos(math.radians(angle))
|
|
@@ -42,27 +36,54 @@ def generate_cad_model(profile, is_3d=False):
|
|
| 42 |
model = model.lineTo(x, y)
|
| 43 |
elif segment["type"] == "angle":
|
| 44 |
angle += segment["value"]
|
| 45 |
-
|
| 46 |
if is_3d:
|
| 47 |
-
model = model.extrude(10)
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return model
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Function to export CAD model
|
| 52 |
def export_model(model, output_format):
|
| 53 |
-
"""
|
| 54 |
-
Export CAD model to the desired format.
|
| 55 |
-
"""
|
| 56 |
output_file = f"custom_profile.{output_format}"
|
| 57 |
-
shape = model.val()
|
| 58 |
-
if output_format
|
| 59 |
-
|
| 60 |
-
elif output_format == "
|
| 61 |
-
|
| 62 |
-
elif output_format == "svg":
|
| 63 |
-
cq.exporters.export(shape, output_file)
|
| 64 |
-
else:
|
| 65 |
-
raise ValueError(f"Unsupported format: {output_format}")
|
| 66 |
return output_file
|
| 67 |
|
| 68 |
# Streamlit UI
|
|
@@ -89,10 +110,21 @@ if st.button("Generate CAD Model") and query:
|
|
| 89 |
try:
|
| 90 |
profile = parse_profile(query)
|
| 91 |
is_3d = st.checkbox("Generate 3D Model", value=False)
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
st.success("CAD Model Generated!")
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
output_file = export_model(model, output_format)
|
| 97 |
with open(output_file, "rb") as f:
|
| 98 |
st.download_button("Download CAD File", f.read(), file_name=output_file)
|
|
|
|
| 3 |
import math
|
| 4 |
import streamlit as st
|
| 5 |
import cadquery as cq
|
| 6 |
+
from cadquery import exporters
|
| 7 |
from PyPDF2 import PdfReader
|
| 8 |
from groq import Groq
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
|
| 11 |
|
| 12 |
# Initialize Groq API client
|
| 13 |
client = Groq(api_key="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")
|
| 14 |
|
| 15 |
# Function to parse profile description
|
| 16 |
def parse_profile(description):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
pattern = r"(\d+)\s*mm|(\d+)\s*degree"
|
| 18 |
matches = re.findall(pattern, description)
|
|
|
|
| 19 |
profile = []
|
| 20 |
for match in matches:
|
| 21 |
if match[0]:
|
|
|
|
| 26 |
|
| 27 |
# Function to generate CAD model using CadQuery
|
| 28 |
def generate_cad_model(profile, is_3d=False):
|
|
|
|
|
|
|
|
|
|
| 29 |
model = cq.Workplane("XY")
|
| 30 |
x, y = 0, 0
|
| 31 |
angle = 0
|
|
|
|
| 32 |
for segment in profile:
|
| 33 |
if segment["type"] == "length":
|
| 34 |
x += segment["value"] * math.cos(math.radians(angle))
|
|
|
|
| 36 |
model = model.lineTo(x, y)
|
| 37 |
elif segment["type"] == "angle":
|
| 38 |
angle += segment["value"]
|
|
|
|
| 39 |
if is_3d:
|
| 40 |
+
model = model.extrude(10)
|
| 41 |
+
return model
|
| 42 |
|
| 43 |
+
# Function to create gear profile
|
| 44 |
+
def generate_gear_model(teeth, module, thickness):
|
| 45 |
+
pitch_diameter = teeth * module
|
| 46 |
+
model = cq.Workplane("XY").gear(teeth, pitch_diameter / teeth, thickness=thickness)
|
| 47 |
return model
|
| 48 |
|
| 49 |
+
# Function to preview 2D profile
|
| 50 |
+
def preview_2d(profile):
|
| 51 |
+
x, y = [0], [0]
|
| 52 |
+
angle = 0
|
| 53 |
+
for segment in profile:
|
| 54 |
+
if segment["type"] == "length":
|
| 55 |
+
x.append(x[-1] + segment["value"] * math.cos(math.radians(angle)))
|
| 56 |
+
y.append(y[-1] + segment["value"] * math.sin(math.radians(angle)))
|
| 57 |
+
elif segment["type"] == "angle":
|
| 58 |
+
angle += segment["value"]
|
| 59 |
+
plt.figure(figsize=(6, 6))
|
| 60 |
+
plt.plot(x, y, marker="o")
|
| 61 |
+
plt.title("2D Profile Preview")
|
| 62 |
+
plt.xlabel("X (mm)")
|
| 63 |
+
plt.ylabel("Y (mm)")
|
| 64 |
+
plt.grid(True)
|
| 65 |
+
st.pyplot(plt)
|
| 66 |
+
|
| 67 |
+
# Function to preview 3D model
|
| 68 |
+
def preview_3d(model):
|
| 69 |
+
shape = model.val()
|
| 70 |
+
faces = shape.faces().toWires()
|
| 71 |
+
verts = [tuple(p.toTuple()) for p in shape.Vertices()]
|
| 72 |
+
fig = plt.figure()
|
| 73 |
+
ax = fig.add_subplot(111, projection="3d")
|
| 74 |
+
ax.add_collection3d(Poly3DCollection(faces))
|
| 75 |
+
ax.scatter(*zip(*verts))
|
| 76 |
+
plt.show()
|
| 77 |
+
st.pyplot(fig)
|
| 78 |
+
|
| 79 |
# Function to export CAD model
|
| 80 |
def export_model(model, output_format):
|
|
|
|
|
|
|
|
|
|
| 81 |
output_file = f"custom_profile.{output_format}"
|
| 82 |
+
shape = model.val()
|
| 83 |
+
if output_format in ["step", "iges", "stl"]:
|
| 84 |
+
exporters.export(shape, output_file)
|
| 85 |
+
elif output_format == "prt":
|
| 86 |
+
raise NotImplementedError("CreoParametric .prt export requires additional tools.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
return output_file
|
| 88 |
|
| 89 |
# Streamlit UI
|
|
|
|
| 110 |
try:
|
| 111 |
profile = parse_profile(query)
|
| 112 |
is_3d = st.checkbox("Generate 3D Model", value=False)
|
| 113 |
+
if "gear" in query.lower():
|
| 114 |
+
teeth = st.number_input("Enter number of teeth", min_value=10, value=20)
|
| 115 |
+
module = st.number_input("Enter module size", min_value=1, value=2)
|
| 116 |
+
thickness = st.number_input("Enter gear thickness (mm)", min_value=1, value=5)
|
| 117 |
+
model = generate_gear_model(teeth, module, thickness)
|
| 118 |
+
else:
|
| 119 |
+
model = generate_cad_model(profile, is_3d=is_3d)
|
| 120 |
|
| 121 |
st.success("CAD Model Generated!")
|
| 122 |
+
if is_3d:
|
| 123 |
+
preview_3d(model)
|
| 124 |
+
else:
|
| 125 |
+
preview_2d(profile)
|
| 126 |
+
|
| 127 |
+
output_format = st.selectbox("Select Download Format", ["step", "iges", "stl"])
|
| 128 |
output_file = export_model(model, output_format)
|
| 129 |
with open(output_file, "rb") as f:
|
| 130 |
st.download_button("Download CAD File", f.read(), file_name=output_file)
|