File size: 10,811 Bytes
984abae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import shutil
import atexit
import tempfile
from typing import Literal
import streamlit.components.v1 as components


parent_dir = os.path.dirname(os.path.abspath(__file__))

class STLComponent:
    def __init__(self):
        """Initialize the STLComponent class and set up the environment."""
        self.has_setup = False
        self.temp_folder = None
        self.current_temp_files = []  # List to track created temporary files
        self.setup()  # Automatically call setup upon initialization

    def setup(self):
        """Set up the necessary directories for the Streamlit_stl component."""
        if not self.has_setup:

            ### Create a unique temporary directory for the component
            if self.temp_folder and os.path.exists(self.temp_folder):
                shutil.rmtree(self.temp_folder)
            self.temp_folder = tempfile.mkdtemp(suffix='_st_stl')
            
            ### Copy the current component directory to the temporary folder
            for file in os.listdir(parent_dir):
                src = parent_dir + os.sep + file
                dst = self.temp_folder + os.sep + file
                if os.path.isdir(src):
                    shutil.copytree(src, dst)
                else:
                    shutil.copy(src, dst)

            ### Mark setup as complete to prevent re-initialization
            self.has_setup = True  

    def stl_from_text(self, 
                        text: str,
                        color: str = '#696969', 
                        material: Literal['material', 'flat', 'wireframe'] = 'material',
                        auto_rotate: bool = False,
                        opacity: int = 1,
                        shininess: int = 100,
                        cam_v_angle: int = 60,
                        cam_h_angle: int = -90,
                        cam_distance: int = 0,
                        height: int = 500,
                        max_view_distance: int  =1000, 
                        **kwargs):
        """
        Create a 3D STL viewer component in Streamlit using a text-based STL file.

        Parameters:
        ----------
        text : str
            The text content of the STL file to render.
        color : str, optional
            The hexadecimal color (starting with '#') for the 3D object. Default is '#696969'.
        material : Literal['material', 'flat', 'wireframe'], optional
            The material style of the 3D object. Options are:
            - 'material': Basic physical material.
            - 'flat': Flat shading.
            - 'wireframe': Wireframe view.
            Default is 'material'.
        auto_rotate : bool, optional
            Whether to enable auto-rotation of the 3D object. Default is False.
        opacity : int, optional
            Opacity of the 3D object, ranging from 0 (fully transparent) to 1 (fully opaque). Default is 1.
        shininess : int, optional
            How shiny the specular highlight is, when using the 'material' material style. Default is 100.
        cam_v_angle : int, optional
            Vertical angle (in degrees) for the camera view. Default is 60.
        cam_h_angle : int, optional
            Horizontal angle (in degrees) for the camera view. Default is -90.
        cam_distance : int, optional
            Distance of the camera from the object. If zero, defaults to three times the largest bounding box size. Default is zero.
        height : int, optional
            Height of the 3D viewer component in pixels. Default is 500.
        max_view_distance : int, optional
            Maximum viewing distance for the camera. Default is 1000.
        **kwargs :
            Additional arguments passed to the Streamlit component.

        Returns:
        -------
        bool
            True if the component is successfully created, False otherwise.
        """
        self.setup()  # Ensure the environment is set up
        file_path = []  # The path of the created temporary file
        if material not in ('material', 'flat', 'wireframe'):
            raise ValueError(f'The possible materials are "material", "flat" or "wireframe", got {material} instead')
        if color[0] != '#':
            raise ValueError(f"The color must be a hexadecimal value starting with '#', got {color} instead")
        if text is not None:

            ### Create a temporary file in the temporary stl folder
            try:
                with tempfile.NamedTemporaryFile(dir=self.temp_folder, suffix='.stl', delete=False) as temp_file:
                    if isinstance(text, bytes):
                        temp_file.write(text)
                    elif isinstance(text, str):
                        # Write the text content to the file
                        temp_file.write(text.encode("utf-8"))  
                    else:
                        raise ValueError(f"Invalid text type for the stl file")
                    # Ensure all data is written to disk
                    temp_file.flush()  
                    # Store the relative path
                    file_path = temp_file.name.split(os.sep)[-1]  
                    # Keep track of the file for cleanup
                    self.current_temp_files.append(temp_file.name)  

            except Exception as e:
                print(f"Error processing the stl file: {e}")
                _component_func(files_text='', height=height **kwargs)
                return False

        ### Call the stl component with the list of file paths and their types
        _component_func(file_path=file_path, 
                        color=color, 
                        material=material, 
                        auto_rotate=bool(auto_rotate), 
                        opacity=opacity, 
                        shininess=shininess,
                        cam_v_angle=cam_v_angle,
                        cam_h_angle=cam_h_angle,
                        cam_distance=cam_distance,
                        height=height, 
                        max_view_distance=max_view_distance,
                        **kwargs)
        return True

    def stl_from_file(self, 
                      file_path: str, 
                      color: str = '#696969',
                      material: Literal['material', 'flat', 'wireframe'] = 'material',
                      auto_rotate: bool = False,
                      opacity: int = 1, 
                      shininess: int = 100,
                      cam_v_angle: int = 60,
                      cam_h_angle: int = -90,
                      cam_distance: int = 0,
                      height: int = 500,
                      max_view_distance: int = 1000, 
                      **kwargs):
        """
        Render a 3D STL file in Streamlit using a file path.

        Parameters:
        ----------
        file_path : str
            The path to the STL file to render.
        color : str, optional
            The hexadecimal color (starting with '#') for the 3D object. Default is '#696969'.
        material : Literal['material', 'flat', 'wireframe'], optional
            The material style of the 3D object. Options are:
            - 'material': Basic physical material.
            - 'flat': Flat shading.
            - 'wireframe': Wireframe view.
            Default is 'material'.
        auto_rotate : bool, optional
            Whether to enable auto-rotation of the 3D object. Default is False.
        opacity : int, optional
            Opacity of the 3D object, ranging from 0 (fully transparent) to 1 (fully opaque). Default is 1.
        shininess : int, optional
            How shiny the specular highlight is, when using the 'material' material style. Default is 100.
        cam_v_angle : int, optional
            Vertical angle (in degrees) for the camera view. Default is 60.
        cam_h_angle : int, optional
            Horizontal angle (in degrees) for the camera view. Default is -90.
        cam_distance : int, optional
            Distance of the camera from the object. If zero, defaults to three times the largest bounding box size. Default is zero.
        height : int, optional
            Height of the 3D viewer component in pixels. Default is 500.
        max_view_distance : int, optional
            Maximum viewing distance for the camera. Default is 1000.
        **kwargs :
            Additional arguments passed to the Streamlit component.

        Returns:
        -------
        bool
            True if the component is successfully created, False otherwise.
        """

        file_text = None

        ### Read the file content and add it to the list
        if file_path is not None:
            with open(file_path, "rb") as f:
                file_text = f.read()  
        
        ### Pass the file content to stl_from_text
        return self.stl_from_text(text=file_text, 
                                  color=color, 
                                  material=material, 
                                  auto_rotate=auto_rotate, 
                                  opacity=opacity,
                                  shininess=shininess,
                                  height=height, 
                                  cam_v_angle=cam_v_angle,
                                  cam_h_angle=cam_h_angle,
                                  cam_distance=cam_distance,
                                  max_view_distance=max_view_distance,
                                  **kwargs)

    def cleanup_temp_files(self):
        """Clean up temporary files and directories created during the session."""
        ### Remove the entire temporary directory
        try:
            if os.path.exists(self.temp_folder):
                shutil.rmtree(self.temp_folder)  

        except Exception as e:
            print(f"Error deleting temporary streamlit-stl folder {self.temp_folder}: {e}")
            # If the directory can't be deleted, try to delete each file individually
            for temp_file in self.current_temp_files:
                try: # Remove individual temporary files
                    os.unlink(temp_file)  
                except Exception as e:
                    print(f"Error deleting temp file {temp_file}: {e}")

# Instantiate the STLComponent class to set up the environment and handle resources
stl_component = STLComponent()
# Register the cleanup function to be called automatically when the program exits
atexit.register(stl_component.cleanup_temp_files)


### Declare the functions to be used in the Streamlit script
stl_from_text = stl_component.stl_from_text
stl_from_file = stl_component.stl_from_file

# Declare the Streamlit component and link it to the temporary directory
_component_func = components.declare_component(
    "streamlit_stl",
    path=stl_component.temp_folder,
)