File size: 2,603 Bytes
584295c
1aea4e5
 
584295c
 
 
7c08c48
584295c
 
1aea4e5
584295c
 
 
1aea4e5
 
 
 
 
 
 
 
7c08c48
1aea4e5
 
 
 
 
 
 
 
 
 
 
 
7c1781f
1aea4e5
584295c
1aea4e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c08c48
584295c
1aea4e5
 
 
 
 
 
 
 
 
 
 
7c08c48
1aea4e5
7c1781f
1aea4e5
7c08c48
584295c
 
 
7c08c48
1aea4e5
 
 
 
584295c
7c08c48
 
1aea4e5
 
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
import trimesh
import gradio as gr
import matplotlib.pyplot as plt
import io
import base64
from PIL import Image

def generate_cad_model(base_length, base_width, base_height):
    """
    Generate a CAD model as a 3D mesh.
    """
    # Create a 3D box (CAD model representation)
    mesh = trimesh.creation.box(extents=(base_length, base_width, base_height))
    return mesh

def render_3d_cad_model(base_length, base_width, base_height):
    """
    Render a 3D CAD model for interactive viewing.
    """
    # Generate the 3D CAD model
    mesh = generate_cad_model(base_length, base_width, base_height)
    
    # Convert the mesh to GLB format for 3D visualization
    glb_data = io.BytesIO()
    mesh.export(glb_data, file_type="glb")
    glb_data.seek(0)
    return glb_data

def render_2d_cad_projection(base_length, base_width, base_height):
    """
    Generate a 2D projection of the CAD model and return it as an image.
    """
    # Generate the 3D CAD model
    mesh = generate_cad_model(base_length, base_width, base_height)
    
    # Create a 2D projection of the model
    projection = mesh.section(plane_origin=(0, 0, 0), plane_normal=(0, 0, 1))
    if projection is not None:
        projection_img = trimesh.util.grid_2d(projection.to_image(), tile=(1, 1))
        
        # Save the projection as an image
        buffer = io.BytesIO()
        plt.imshow(projection_img, cmap="gray")
        plt.axis("off")
        plt.savefig(buffer, format="png", bbox_inches="tight")
        buffer.seek(0)
        
        # Convert to Base64 for Gradio display
        encoded_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
        return f"data:image/png;base64,{encoded_image}"
    else:
        return None

# Gradio Interface
def display_cad(base_length, base_width, base_height):
    """
    Render both 3D and 2D CAD outputs.
    """
    # Render 3D model
    glb_data = render_3d_cad_model(base_length, base_width, base_height)
    
    # Render 2D projection
    cad_image = render_2d_cad_projection(base_length, base_width, base_height)
    
    return glb_data, cad_image

# Gradio interface for 3D and 2D CAD visualization
demo = gr.Interface(
    fn=display_cad,
    inputs=[
        gr.Number(label="Base Length (mm)", value=10),
        gr.Number(label="Base Width (mm)", value=10),
        gr.Number(label="Base Height (mm)", value=5),
    ],
    outputs=[
        gr.Model3D(label="3D CAD Model"),  # Interactive 3D viewer
        gr.Image(label="2D CAD Projection"),  # Static 2D projection
    ],
    live=True,
)

if __name__ == "__main__":
    demo.launch()