service.py: use ifcopenshell.file.from_string() instead of temp file

ifcopenshell can parse IFC bytes in-memory via from_string(); no need to
write a temp file, read it back, and unlink it. Simpler and faster.
Update README library example to match.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Bruno Postle 2026-04-24 17:19:27 +01:00
parent 19ce25fdc0
commit 02d93e68e1
2 changed files with 2 additions and 20 deletions

View file

@ -88,7 +88,6 @@ ifcurl render "ifc://..." -o output.png
Use as a library:
```python
import os, tempfile
import ifcopenshell
from ifcurl import IfcUrl, fetch_ifc
from ifcurl.render import render
@ -98,15 +97,7 @@ url = IfcUrl.parse(
"?path=models/building.ifc&selector=IfcWall&visibility=ghost"
)
hexsha, ifc_bytes = fetch_ifc(url) # hexsha is stable even for mutable refs
# ifcopenshell.open() requires a file path, not bytes
tmp_fd, tmp_path = tempfile.mkstemp(suffix=".ifc")
try:
os.write(tmp_fd, ifc_bytes)
os.close(tmp_fd)
model = ifcopenshell.open(tmp_path)
finally:
os.unlink(tmp_path)
model = ifcopenshell.file.from_string(ifc_bytes.decode())
png = render(model, selector=url.selector, clips=url.clips or None,
camera=url.camera, fov=url.fov, visibility=url.visibility)

View file

@ -38,7 +38,6 @@ from __future__ import annotations
import hashlib
import ipaddress
import os
import tempfile
import threading
import time
@ -225,18 +224,10 @@ def _t4m_put(url: str, png: bytes) -> None:
# ---------------------------------------------------------------------------
def _load_model(ifc_bytes: bytes) -> ifcopenshell.file:
tmp_fd, tmp_path = tempfile.mkstemp(suffix=".ifc")
try:
os.write(tmp_fd, ifc_bytes)
os.close(tmp_fd)
return ifcopenshell.open(tmp_path)
return ifcopenshell.file.from_string(ifc_bytes.decode())
except Exception as exc:
raise HTTPException(status_code=422, detail=f"Could not parse IFC file: {exc}") from exc
finally:
try:
os.unlink(tmp_path)
except OSError:
pass
# ---------------------------------------------------------------------------