This repository has been archived on 2025-05-05. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
internship/olaflow/processing/stl.py

23 lines
667 B
Python
Raw Normal View History

2022-03-04 13:11:31 +01:00
import pathlib
import shutil
import subprocess
import tempfile
import numpy as np
2022-03-04 13:20:44 +01:00
def stl_from_1d(data, output, scale=[1, 1, 1], translate=[0, 0, 0]):
2022-03-04 13:11:31 +01:00
with tempfile.TemporaryDirectory() as tmppath:
tmpdir = pathlib.Path(tmppath)
np.savetxt(tmpdir.joinpath("data.dat"), np.stack((data, data)))
with open(tmpdir.joinpath("scad"), "wt") as scad:
2022-03-04 13:20:44 +01:00
scad.write(
f"translate({translate})"
f"scale({scale})"
f"""surface("data.dat");"""
)
2022-03-04 13:11:31 +01:00
subprocess.run(("openscad", "scad", "-o", "data.stl"), cwd=tmpdir)
shutil.copy2(tmpdir.joinpath("data.stl"), output)