File size: 2,807 Bytes
ccf069c
 
 
 
 
5960bde
eabc96b
49bdc84
ccf069c
eabc96b
 
 
ccf069c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5960bde
eabc96b
 
49bdc84
 
eabc96b
 
 
49bdc84
 
eabc96b
 
 
 
 
 
49bdc84
 
eabc96b
49bdc84
eabc96b
 
daefba6
eabc96b
 
5960bde
 
ccf069c
 
e4f0166
ccf069c
 
 
 
 
 
76d75ac
 
 
 
 
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
import os
import streamlit as st
import tempfile
import trimesh
import pymeshlab
import cadquery as cq
from pathlib import Path
import subprocess

# Ensure FreeCAD is in the system path
os.environ["PATH"] += r";C:\Program Files\FreeCAD 0.21\bin"

# Set title
st.title("3D Model Converter")

# Supported formats
input_formats = ["obj", "stl", "ply", "glb", "gltf", "off", "3ds", "fbx", "dae", "stp", "skp"]
output_formats = ["obj", "stl", "ply", "glb", "gltf", "off", "3ds", "fbx", "dae"]

# Upload file
uploaded_file = st.file_uploader("Upload a 3D model file", type=input_formats)

if uploaded_file:
    file_ext = uploaded_file.name.split(".")[-1].lower()

    # Select output format
    output_format = st.selectbox("Select output format", [fmt for fmt in output_formats if fmt != file_ext])

    # Process and convert
    if st.button("Convert"):
        with tempfile.TemporaryDirectory() as temp_dir:
            input_path = os.path.join(temp_dir, uploaded_file.name)
            output_path = os.path.join(temp_dir, f"converted.{output_format}")

            # Save uploaded file
            with open(input_path, "wb") as f:
                f.write(uploaded_file.read())

            try:
                if file_ext == "stp":
                    temp_stl_path = os.path.join(temp_dir, "temp.stl")  # Temporary STL file

                    # Convert STEP to STL using FreeCAD
                    freecad_script = f"""
import FreeCAD
import Part
import Mesh

doc = FreeCAD.newDocument()
shape = Part.Shape()
shape.read("{input_path}")
Mesh.export([shape], "{temp_stl_path}")
                    """
                    script_path = os.path.join(temp_dir, "convert_stp.py")
                    with open(script_path, "w") as f:
                        f.write(freecad_script)

                    subprocess.run(["freecadcmd", script_path], check=True)

                    if output_format == "stl":
                        output_path = temp_stl_path
                    else:
                        mesh = trimesh.load(temp_stl_path)  # Convert STL to other formats
                        mesh.export(output_path)

                elif file_ext in ["obj", "stl", "ply", "off"]:
                    mesh = trimesh.load(input_path)
                    mesh.export(output_path)

                else:
                    ms = pymeshlab.MeshSet()
                    ms.load_new_mesh(input_path)
                    ms.save_current_mesh(output_path, binary=True)

                # Provide download link
                with open(output_path, "rb") as f:
                    st.download_button(label="Download Converted File", data=f, file_name=f"converted.{output_format}")

            except Exception as e:
                st.error(f"Conversion failed: {e}")  # Show error if conversion fails