From 02d93e68e1cc3aa50ab7dc971cc4f4a9ec6beccf Mon Sep 17 00:00:00 2001 From: Bruno Postle Date: Fri, 24 Apr 2026 17:19:27 +0100 Subject: [PATCH] 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 --- README.md | 11 +---------- ifcurl/service.py | 11 +---------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 136a667..091dc49 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/ifcurl/service.py b/ifcurl/service.py index c8883a1..3216db7 100644 --- a/ifcurl/service.py +++ b/ifcurl/service.py @@ -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 # ---------------------------------------------------------------------------