I have a STL, OBJ, and other CAD files stored on my server.
How can I convert those to a data URL?
I found the following library:
https://pypi.org/project/data-url/
However, how do I save as a CAD file format and not an image?
Hello
To convert CAD files to a data URL, you can use Python libraries such as base64 and pathlib. Here's a short example:
import base64
from pathlib import Path
def convert_cad_to_data_url(file_path):
with open(file_path, "rb") as file:
cad_data = file.read()
base64_data = base64.b64encode(cad_data).decode("utf-8")
mime_type = get_mime_type(Path(file_path).suffix.lower())
data_url = f"data:{mime_type};base64,{base64_data}"
return data_url
def get_mime_type(file_extension):
mime_types = {
".stl": "application/vnd.ms-pki.stl",
".obj": "text/plain",
}
return mime_types.get(file_extension, "application/octet-stream")
You can use the convert_cad_to_data_url function by passing the file path as an argument, and it will return the corresponding data URL for the CAD file.
Hope it helps you.
How come STL uses application/vnd.ms-pki.stl and not text/plain? How could I also apply this to STEP files?
There are many mime-type for the same extension. I found this:
https://www.wikidata.org/wiki/Q105852795
You need the mime-type, what your application expects. If the application requires "application/octet-stream" for *.stl, then use it.