File size: 2,920 Bytes
8e0b903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
import streamlit as st
import pyvista as pv
import tempfile
from stpyvista import stpyvista
import os

def delmodel():
    del st.session_state.fileuploader

def option_stl_1():
    """πŸ“€ Upload a STL file"""

    st.header("πŸ“€   Upload a x3D STL file", anchor=False, divider="rainbow")

    placeholder = st.empty()
    " "

    with placeholder:
        uploadedFile = st.file_uploader(
            "Upload a x3D file:",
            ["stl"],
            accept_multiple_files=False,
            key="fileuploader",
        )

    if uploadedFile:
        st.info(f"Uploaded file size: {uploadedFile.size} bytes")

        # Save to temporary file
        with tempfile.NamedTemporaryFile(suffix=".stl", delete=False) as f:
            f.write(uploadedFile.getbuffer())
            f.flush()
            temp_file_path = f.name

        try:
            reader = pv.STLReader(temp_file_path)
            mesh = reader.read()

            st.info(f"Mesh points: {mesh.n_points}, Mesh cells: {mesh.n_cells}")

            if mesh.n_points == 0:
                st.error("The uploaded STL file is empty or invalid. Please upload a valid file.")
            else:
                plotter = pv.Plotter(border=False, window_size=[500, 400])
                plotter.background_color = "#f0f8ff"
                plotter.add_mesh(mesh, color="orange", specular=0.5)
                plotter.view_xz()

                with placeholder.container():
                    st.button("πŸ”™ Restart", "btn_rerender", on_click=delmodel)
                    stpyvista(plotter)
        finally:
            os.remove(temp_file_path)  # Clean up the temp file

def option_stl_2(file_path):
    st.header("πŸ“€   Using embed exported file x3D", anchor=False, divider="rainbow")
    placeholder = st.empty()
    " "
    if file_path:
        if os.path.exists(file_path) and file_path.endswith(".stl"):
            try:
                reader = pv.STLReader(file_path)
                mesh = reader.read()

                st.info(f"Mesh points: {mesh.n_points}, Mesh cells: {mesh.n_cells}")

                if mesh.n_points == 0:
                    st.error("The uploaded STL file is empty or invalid. Please upload a valid file.")
                else:
                    plotter = pv.Plotter(border=False, window_size=[500, 400])
                    plotter.background_color = "#f0f8ff"
                    plotter.add_mesh(mesh, color="orange", specular=0.5)
                    plotter.view_xz()

                    with placeholder.container():
                        st.button("πŸ”™ Restart", "btn_rerender")
                        stpyvista(plotter)
            finally:
                st.info("Processing completed.")
        else:
            st.error("Invalid file path. Please ensure the file exists and is an STL file.")