File size: 2,574 Bytes
871c846
bc198b6
871c846
bc198b6
 
 
 
871c846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc198b6
871c846
 
 
 
 
 
 
 
 
 
 
34e3bfe
871c846
 
 
34e3bfe
871c846
 
 
 
 
 
 
 
 
 
34e3bfe
871c846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34e3bfe
 
871c846
 
34e3bfe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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.")