feat: extract workflow bbox services phase 3

This commit is contained in:
2026-04-07 09:42:06 +02:00
parent 8f8d2e68b7
commit 9c93ecef49
6 changed files with 172 additions and 54 deletions
@@ -17,59 +17,17 @@ logger = logging.getLogger(__name__)
def _bbox_from_glb(glb_path: str) -> dict | None:
"""Extract bounding box from a GLB file (meters → converted to mm).
"""Backward-compatible wrapper for GLB bbox extraction."""
from app.domains.rendering.workflow_runtime_services import extract_bbox_from_glb
Returns {"dimensions_mm": {x,y,z}, "bbox_center_mm": {x,y,z}} or None on failure.
OCC GLB output is in meters; multiply by 1000 to get mm.
"""
try:
import trimesh
p = Path(glb_path)
if not p.exists():
return None
scene = trimesh.load(str(p), force="scene")
bounds = getattr(scene, "bounds", None)
if bounds is None:
return None
mins, maxs = bounds
dims = maxs - mins
return {
"dimensions_mm": {
"x": round(float(dims[0]) * 1000, 2),
"y": round(float(dims[1]) * 1000, 2),
"z": round(float(dims[2]) * 1000, 2),
},
"bbox_center_mm": {
"x": round(float((mins[0] + maxs[0]) / 2) * 1000, 2),
"y": round(float((mins[1] + maxs[1]) / 2) * 1000, 2),
"z": round(float((mins[2] + maxs[2]) / 2) * 1000, 2),
},
}
except Exception as exc:
logger.debug(f"_bbox_from_glb failed for {glb_path}: {exc}")
return None
return extract_bbox_from_glb(glb_path)
def _bbox_from_step_cadquery(step_path: str) -> dict | None:
"""Fallback: extract bounding box by re-parsing STEP via cadquery."""
try:
import cadquery as cq
bb = cq.importers.importStep(step_path).val().BoundingBox()
return {
"dimensions_mm": {
"x": round(bb.xlen, 2),
"y": round(bb.ylen, 2),
"z": round(bb.zlen, 2),
},
"bbox_center_mm": {
"x": round((bb.xmin + bb.xmax) / 2, 2),
"y": round((bb.ymin + bb.ymax) / 2, 2),
"z": round((bb.zmin + bb.zmax) / 2, 2),
},
}
except Exception as exc:
logger.debug(f"_bbox_from_step_cadquery failed for {step_path}: {exc}")
return None
"""Backward-compatible wrapper for STEP bbox fallback extraction."""
from app.domains.rendering.workflow_runtime_services import extract_bbox_from_step_cadquery
return extract_bbox_from_step_cadquery(step_path)
@celery_app.task(bind=True, name="app.tasks.step_tasks.process_step_file", queue="step_processing")
@@ -267,6 +225,7 @@ def reextract_cad_metadata(cad_file_id: str):
from sqlalchemy.orm import Session
from app.config import settings as app_settings
from app.models.cad_file import CadFile
from app.domains.rendering.workflow_runtime_services import resolve_cad_bbox
pl = PipelineLogger(task_id=None)
pl.step_start("reextract_cad_metadata", {"cad_file_id": cad_file_id})
@@ -289,7 +248,8 @@ def reextract_cad_metadata(cad_file_id: str):
try:
p = Path(step_path)
glb_path = p.parent / f"{p.stem}_thumbnail.glb"
patch = _bbox_from_glb(str(glb_path)) or _bbox_from_step_cadquery(step_path)
bbox_result = resolve_cad_bbox(step_path, glb_path=str(glb_path))
patch = bbox_result.bbox_data
if patch:
with Session(eng) as session:
set_tenant_context_sync(session, _tenant_id)