mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-07-31 03:05:21 +00:00
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from OCP.StlAPI import StlAPI_Writer
|
|
|
|
from common.render import REPO_ROOT, part_stl_path
|
|
from common.step_scene import (
|
|
LoadedStepScene,
|
|
scene_export_shape,
|
|
)
|
|
|
|
|
|
def _display_path(path: Path) -> str:
|
|
resolved = path.resolve()
|
|
try:
|
|
return resolved.relative_to(REPO_ROOT).as_posix()
|
|
except ValueError:
|
|
return resolved.as_posix()
|
|
|
|
|
|
def export_part_stl_from_scene(step_path: Path, scene: LoadedStepScene, *, target_path: Path | None = None) -> Path:
|
|
target_path = target_path or part_stl_path(step_path)
|
|
export_shape_stl(scene_export_shape(scene), target_path)
|
|
return target_path
|
|
|
|
|
|
def export_shape_stl(shape: object, target_path: Path) -> Path:
|
|
target_path.parent.mkdir(parents=True, exist_ok=True)
|
|
writer = StlAPI_Writer()
|
|
writer.ASCIIMode = False
|
|
if not writer.Write(shape, str(target_path)):
|
|
raise RuntimeError(f"Failed to write STL output: {_display_path(target_path)}")
|
|
return target_path
|
|
|
|
|
|
def write_empty_stl(target_path: Path) -> Path:
|
|
target_path.parent.mkdir(parents=True, exist_ok=True)
|
|
header = b"cad-harness empty STL".ljust(80, b"\0")
|
|
target_path.write_bytes(header + (0).to_bytes(4, byteorder="little", signed=False))
|
|
return target_path
|