File size: 2,742 Bytes
c042949
8bbb154
 
c042949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bbb154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b38f031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import trimesh
import shutil
import base64
import os

def save_file(input_file, output_file):
    """
    Copy a file from input location to output location.

    Args:
    input_file (str): Path to the input file.
    output_file (str): Path to the output file.

    Returns:
    bool: True if the file is successfully saved, False otherwise.
    """
    try:
        shutil.copy(input_file, output_file)
        return True
    except Exception as e:
        print(f"Error: {e}")
        return False
    

def convert_obj_to_stl(input_file: str, output_file: str):
    # Load the OBJ file
    mesh = trimesh.load(input_file)

    # Export as STL
    mesh.export(output_file)
    
def change_file_extension(file_path: str, new_extension: str) -> str:
    """
    Change the extension of a file path.

    Args:
        file_path (str): The original file path.
        new_extension (str): The new file extension (without the dot).

    Returns:
        str: The modified file path with the new extension.
    """
    base_path, _ = os.path.splitext(file_path)
    new_file_path = base_path + '.' + new_extension
    return new_file_path

        
def file_to_base64(file_location: str) -> str:
    """
    This function is to convert files into base64
    ## Input
    file_location (str) : the location of the file on the machine
    
    ## Output
    the base64 encoding of the file
    """
    with open(file_location, "rb") as file:
        file_content = file.read()
        base64_encoded = base64.b64encode(file_content)
        return base64_encoded.decode("utf-8")
    
def base64_to_file(base64_string: str, output_file_location: str) -> None:
    """
    Decodes a base64-encoded string and writes the resulting binary data to a file.

    Args:
        base64_string (str): The base64-encoded string to decode.
        output_file_location (str): The file path where the decoded binary data will be written.

    Returns:
        None
    """
    binary_data = base64.b64decode(base64_string)
    with open(output_file_location, "wb") as output_file:
        output_file.write(binary_data)

def copy_file_to_location(file_path: str, destination_path: str) -> None:
    """
    Copy a file to a new location with the same name.

    Args:
        file_path: The path of the file to be copied.
        destination_path: The path of the destination directory.
    """
    shutil.copy(file_path, destination_path)
    
def remove_files(file_paths):
    """Remove files given a list of file paths."""
    for file_path in file_paths:
        try:
            os.remove(file_path)
            print(f"File '{file_path}' removed successfully.")
        except OSError as e:
            print(f"Error removing file '{file_path}': {e}")