File size: 2,470 Bytes
68c04e9
fdbde7f
 
68c04e9
 
 
93fee96
68c04e9
 
 
 
 
 
93fee96
68c04e9
 
 
 
 
 
 
 
 
93fee96
fdbde7f
93fee96
fdbde7f
 
 
 
 
 
 
 
 
 
 
93fee96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import pyvista as pv
from stl import mesh
import tempfile
import os
import trimesh

# Page configuration
st.set_page_config(page_title="AR CAD Viewer", layout="wide")

# Title
st.title("Augmented Reality CAD Viewer")
st.write("Upload your CAD models, preview them, and convert them to AR-compatible formats.")

# File uploader for CAD files
uploaded_file = st.file_uploader("Upload a CAD file (.stl format)", type=["stl"])

if uploaded_file is not None:
    # Save uploaded file to a temporary location
    with tempfile.NamedTemporaryFile(delete=False, suffix=".stl") as temp_file:
        temp_file.write(uploaded_file.read())
        temp_filepath = temp_file.name

    # Load the STL file using pyvista for interactive 3D rendering
    st.subheader("3D Model Preview")
    
    # Load the STL using pyvista
    mesh_data = pv.read(temp_filepath)
    
    # Create the plotter and add the mesh to it
    plotter = pv.Plotter(off_screen=True)
    plotter.add_mesh(mesh_data, color="cyan")
    plotter.view_isometric()
    
    # Show the plot in the Streamlit app
    st.pydeck_chart(plotter.show())

    # Convert to glTF for AR
    st.subheader("Convert to AR Format")
    st.write("Click below to convert the STL file into an AR-compatible `.glTF` format.")

    if st.button("Convert to glTF"):
        # Use Trimesh for conversion
        trimesh_mesh = trimesh.load(temp_filepath)
        gltf_filepath = temp_filepath.replace(".stl", ".gltf")
        trimesh.exchange.gltf.export_gltf(trimesh_mesh, gltf_filepath)

        # Display download link for glTF
        with open(gltf_filepath, "rb") as gltf_file:
            st.download_button(
                label="Download glTF Model",
                data=gltf_file,
                file_name="model.gltf",
                mime="model/gltf+json"
            )

        # Clean up temporary files
        os.remove(gltf_filepath)
        os.remove(temp_filepath)

    # AR Hosting Info
    st.subheader("Host in AR")
    st.markdown(
        """
        Upload your `.glTF` file to [GitHub Pages](https://pages.github.com/) or use a service like [Google Poly](https://poly.google.com/) (now integrated into other platforms).
        
        For AR.js:
        - Host the `.glTF` file on a web server.
        - Create an AR marker or use markerless AR for viewing.

        Example: `<a-scene><a-entity gltf-model="model.gltf"></a-entity></a-scene>`
        """
    )