import streamlit as st import numpy as np from myplot import plot_verticles, plot_mesh import matplotlib.pyplot as plt from scipy import spatial import pymesh from solid import * from solid.utils import * import os # Function to plot vertices and meshes def plot_vertices_and_mesh(vertices, faces): # Assuming 'plot_verticles' and 'plot_mesh' are defined in 'myplot' plot_verticles(vertices=vertices, isosurf=False) plot_verticles(vertices=vertices, isosurf=True) # Create and plot mesh myramid_mesh = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype)) for i, f in enumerate(faces): for j in range(3): myramid_mesh.vectors[i][j] = vertices[f[j],:] plot_mesh(myramid_mesh) # Function to plot convex hull def plot_convex_hull(points): hull = spatial.ConvexHull(points) plt.plot(points[:,0], points[:,1], 'o') for simplex in hull.simplices: plt.plot(points[simplex, 0], points[simplex, 1], 'k-') st.pyplot(plt) # Function to create and save 3D meshes def create_3d_meshes(): box_a = pymesh.generate_box_mesh([0,0,0], [1,1,1]) pymesh.save_mesh("pymesh_example_01.stl", box_a, ascii=False) box_b = pymesh.generate_box_mesh([0.4,0.4,0], [0.6,0.6,1]) box_c = pymesh.boolean(box_a, box_b, operation='difference', engine="igl") pymesh.save_mesh("pymesh_example_02.stl", box_c, ascii=False) # Function to create and save solid models def create_solid_models(): d = difference()(cube(size=10, center=True), sphere(r=6.5, segments=300)) scad_render_to_file(d, '/tmp/solidpython_example_01.scad') c = circle(r=1) t = translate([2, 0, 0])(c) e = linear_extrude(height=10, center=True, convexity=10, twist=-500, slices=500)(t) col = color('lightgreen')(e) scad_render_to_file(col, 'solidpython_example_02.scad') # Streamlit UI st.title("3D Graphics and Geometry with Streamlit") if st.button('Plot Vertices and Meshes'): # Add vertices and faces data here vertices = np.array([[-3, -3, 0], [+3, -3, 0], [+3, +3, 0], [-3, +3, 0], [+0, +0, +3]]) faces = np.array([[4, 1,0], [4, 2, 1], [3, 4, 0], [3, 4, 2], [3, 2, 1], [3, 1, 0]], dtype=int32) plot_vertices_and_mesh(vertices, faces) if st.button('Plot Convex Hull'): points = np.array([[0, 0], [-2, 0], [-2, 2], [0, 1.5], [2, 2], [2, 0]]) plot_convex_hull(points) if st.button('Create and Save 3D Meshes'): create_3d_meshes() st.write("3D meshes saved to files.") if st.button('Create and Save Solid Models'): create_solid_models() st.write("Solid models saved to files.")