Files
HartOMat/backend/app/api/routers/cad.py
T
Hartmut ca62319688 feat: sharp edge pipeline V02, tessellation presets, media cache-bust, GMSH plan
Sharp Edge Pipeline V02:
- export_step_to_gltf.py: replace BRep_Tool.Polygon3D_s (returns None in XCAF) with
  GCPnts_UniformAbscissa curve sampling at 0.3mm step — extracts 17,129 segment pairs
- Inject sharp_edge_pairs + sharp_threshold_deg into GLB extras (scenes[0].extras)
  via binary GLB JSON-chunk patching (no extra dependency)
- export_gltf.py: read schaeffler_sharp_edge_pairs from Blender scene custom props,
  apply via KD-tree to mark edges sharp=True + seam=True (OCC mm Z-up → Blender transform)
- tools/restore_sharp_marks.py: dual-pass (dihedral angle + OCC pairs), updated coordinate
  transform (X, -Z, Y) * 0.001

Tessellation:
- Admin UI: Draft / Standard / Fine preset buttons with active-state highlighting
- Default angular deflection: preview 0.5→0.1 rad, production 0.2→0.05 rad
- export_glb.py: read updated defaults from system_settings

Media / Cache:
- media/service.py: get_download_url appends ?v={file_size_bytes} cache-buster
- media/router.py: Cache-Control: no-cache for all download/thumbnail endpoints

Render pipeline:
- still_render.py / turntable_render.py: shared GPU activation + camera improvements
- render_order_line.py: global render position support
- render_thumbnail.py: updated defaults

Frontend:
- InlineCadViewer: file_size_bytes-aware URL update triggers re-fetch on regeneration
- ThreeDViewer: material panel, part selection, PBR mode improvements
- Admin.tsx: tessellation preset cards, GMSH setting dropdown
- MediaBrowser, ProductDetail, OrderDetail, Orders: various UI improvements
- New: MaterialPanel, GlobalRenderPositionsPanel, StepIndicator components
- New: renderPositions.ts API client

Plans / Docs:
- plan.md: GMSH Frontal-Delaunay tessellation plan (6 tasks)
- LEARNINGS.md: OCC Polygon3D_s None issue + GCPnts fix
- .gitignore: add backend/core (core dump from root process)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-11 14:40:36 +01:00

437 lines
15 KiB
Python

"""CAD file router - serve thumbnails, glTF models, parsed objects, and trigger reprocessing."""
import uuid
from datetime import datetime
from pathlib import Path
from typing import Literal
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import FileResponse
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from sqlalchemy.orm import selectinload
from app.database import get_db
from app.models.cad_file import CadFile, ProcessingStatus
from app.models.order import Order
from app.models.order_item import OrderItem
from app.models.user import User
from app.utils.auth import get_current_user, is_privileged
from app.services.product_service import link_cad_to_product, lookup_product
router = APIRouter(prefix="/cad", tags=["cad"])
# ---------------------------------------------------------------------------
# Part-materials schemas
# ---------------------------------------------------------------------------
class PartMaterialEntry(BaseModel):
type: Literal["library", "hex"]
value: str # material name or hex color string
class PartMaterialsResponse(BaseModel):
cad_file_id: str
part_materials: dict[str, PartMaterialEntry] | None
# ---------------------------------------------------------------------------
# Schemas for match-to-order
# ---------------------------------------------------------------------------
class MatchToOrderRequest(BaseModel):
order_id: uuid.UUID
cad_file_ids: list[str]
class MatchedItem(BaseModel):
item_id: str
cad_file_id: str
item_name: str
cad_name: str
class MatchToOrderResponse(BaseModel):
matched: list[MatchedItem]
unmatched_cad: list[str]
unmatched_items: list[str]
# ---------------------------------------------------------------------------
# Matching helper
# ---------------------------------------------------------------------------
def _normalize_stem(name: str) -> str:
"""Lowercase stem, strip .stp/.step extension for comparison."""
stem = name.strip()
for ext in (".step", ".stp"):
if stem.lower().endswith(ext):
stem = stem[: -len(ext)]
break
return stem.lower()
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@router.post("/match-to-order", response_model=MatchToOrderResponse)
async def match_cad_files_to_order(
body: MatchToOrderRequest,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""
Match uploaded CAD files to order items by filename similarity.
For each CAD file, compares the stem of original_name (case-insensitive,
.stp/.step normalised) to the stem of each item's name_cad_modell field.
Updates order_item.cad_file_id for successful matches.
"""
# Load order with items
order_result = await db.execute(
select(Order)
.where(Order.id == body.order_id)
.options(selectinload(Order.items))
)
order = order_result.scalar_one_or_none()
if not order:
raise HTTPException(404, detail="Order not found")
if user.role.value != "admin" and order.created_by != user.id:
raise HTTPException(403, detail="Access denied")
# Parse and validate CAD file IDs
cad_uuids: list[uuid.UUID] = []
for raw_id in body.cad_file_ids:
try:
cad_uuids.append(uuid.UUID(raw_id))
except ValueError:
raise HTTPException(400, detail=f"Invalid cad_file_id: {raw_id}")
# Load CAD files from DB
cad_result = await db.execute(
select(CadFile).where(CadFile.id.in_(cad_uuids))
)
cad_files: list[CadFile] = list(cad_result.scalars().all())
found_ids = {str(cf.id) for cf in cad_files}
missing = [i for i in body.cad_file_ids if i not in found_ids]
if missing:
raise HTTPException(404, detail=f"CAD files not found: {missing}")
# Build lookup: normalized stem -> first OrderItem with that stem
items: list[OrderItem] = order.items
item_by_stem: dict[str, OrderItem] = {}
for item in items:
if item.name_cad_modell:
stem = _normalize_stem(item.name_cad_modell)
if stem not in item_by_stem:
item_by_stem[stem] = item
matched: list[MatchedItem] = []
unmatched_cad: list[str] = []
matched_item_ids: set[str] = set()
for cad_file in cad_files:
cad_stem = _normalize_stem(cad_file.original_name or "")
if cad_stem in item_by_stem:
item = item_by_stem[cad_stem]
item.cad_file_id = cad_file.id
item.updated_at = datetime.utcnow()
matched.append(
MatchedItem(
item_id=str(item.id),
cad_file_id=str(cad_file.id),
item_name=item.name_cad_modell or "",
cad_name=cad_file.original_name or "",
)
)
matched_item_ids.add(str(item.id))
# Propagate the STEP link to the product so that:
# (a) the render pipeline can find it via product.cad_file_id
# (b) future orders for the same product inherit the STEP automatically
# (c) the split-missing-step correctly identifies which products have STEP
try:
product = await lookup_product(db, item.pim_id, item.produkt_baureihe)
if product and product.cad_file_id is None:
await link_cad_to_product(db, product.id, cad_file.id)
except Exception:
pass # non-critical — item link already set above
else:
unmatched_cad.append(str(cad_file.id))
await db.commit()
unmatched_items = [
str(item.id)
for item in items
if str(item.id) not in matched_item_ids
]
return MatchToOrderResponse(
matched=matched,
unmatched_cad=unmatched_cad,
unmatched_items=unmatched_items,
)
# ---------------------------------------------------------------------------
# Helper
# ---------------------------------------------------------------------------
async def _get_cad_file(cad_id: uuid.UUID, db: AsyncSession) -> CadFile:
result = await db.execute(select(CadFile).where(CadFile.id == cad_id))
cad = result.scalar_one_or_none()
if not cad:
raise HTTPException(status_code=404, detail="CAD file not found")
return cad
@router.get("/{id}/thumbnail")
async def get_thumbnail(
id: uuid.UUID,
db: AsyncSession = Depends(get_db),
):
"""Serve the thumbnail image for a CAD file (no auth — UUID is opaque enough)."""
from sqlalchemy import text
# Bypass RLS for this public endpoint (cad_files has tenant RLS but thumbnails are public)
await db.execute(text("SET LOCAL app.current_tenant_id = 'bypass'"))
cad = await _get_cad_file(id, db)
if not cad.thumbnail_path:
raise HTTPException(404, detail="Thumbnail not yet generated for this CAD file")
thumb_path = Path(cad.thumbnail_path)
if not thumb_path.exists():
raise HTTPException(404, detail="Thumbnail file missing from storage")
ext = thumb_path.suffix.lower()
media_type = "image/jpeg" if ext in (".jpg", ".jpeg") else "image/png"
return FileResponse(
path=str(thumb_path),
media_type=media_type,
filename=f"{id}{ext}",
headers={"Cache-Control": "max-age=3600, public"},
)
@router.get("/{id}/model")
async def get_model(
id: uuid.UUID,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Serve the glTF file for a CAD file."""
cad = await _get_cad_file(id, db)
if not cad.gltf_path:
raise HTTPException(
status_code=404,
detail="glTF model not yet generated for this CAD file",
)
gltf_path = Path(cad.gltf_path)
if not gltf_path.exists():
raise HTTPException(
status_code=404,
detail="glTF file missing from storage",
)
# glTF files may be either .gltf (JSON) or .glb (binary)
suffix = gltf_path.suffix.lower()
if suffix == ".glb":
media_type = "model/gltf-binary"
else:
media_type = "model/gltf+json"
return FileResponse(
path=str(gltf_path),
media_type=media_type,
filename=f"{id}{suffix}",
)
@router.get("/{id}/objects")
async def get_objects(
id: uuid.UUID,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Return the parsed_objects JSON extracted from the STEP file."""
cad = await _get_cad_file(id, db)
if cad.parsed_objects is None:
raise HTTPException(
status_code=404,
detail="Parsed objects not yet available for this CAD file",
)
return {
"cad_file_id": str(cad.id),
"original_name": cad.original_name,
"processing_status": cad.processing_status.value,
"parsed_objects": cad.parsed_objects,
}
@router.post("/{id}/generate-gltf-geometry", status_code=status.HTTP_202_ACCEPTED)
async def generate_gltf_geometry(
id: uuid.UUID,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Queue GLB geometry export directly from STEP via OCC (no STL required).
Stores the result as a MediaAsset with asset_type='gltf_geometry'.
Uses export_step_to_gltf.py (OCP/pythonocc) — no Blender needed.
"""
if not is_privileged(user):
raise HTTPException(status_code=403, detail="Insufficient permissions")
cad = await _get_cad_file(id, db)
if not cad.stored_path:
raise HTTPException(status_code=404, detail="STEP file not uploaded for this CAD file")
from app.tasks.step_tasks import generate_gltf_geometry_task
task = generate_gltf_geometry_task.delay(str(id))
return {"status": "queued", "task_id": task.id, "cad_file_id": str(id)}
@router.post("/{id}/generate-gltf-production", status_code=status.HTTP_202_ACCEPTED)
async def generate_gltf_production(
id: uuid.UUID,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Queue production GLB export (Blender + PBR materials) from a geometry GLB.
Requires a gltf_geometry MediaAsset to already exist (run generate-gltf-geometry first).
Stores result as a MediaAsset with asset_type='gltf_production'.
"""
if not is_privileged(user):
raise HTTPException(status_code=403, detail="Insufficient permissions")
cad = await _get_cad_file(id, db)
if not cad.stored_path:
raise HTTPException(status_code=404, detail="STEP file not uploaded for this CAD file")
from app.tasks.step_tasks import generate_gltf_production_task
task = generate_gltf_production_task.delay(str(id))
return {"status": "queued", "task_id": task.id, "cad_file_id": str(id)}
@router.post(
"/{id}/regenerate-thumbnail",
status_code=status.HTTP_202_ACCEPTED,
)
async def regenerate_thumbnail(
id: uuid.UUID,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Queue a Celery task to reprocess the STEP file and regenerate its thumbnail."""
if user.role.value != "admin":
raise HTTPException(
status_code=403,
detail="Only admins can trigger thumbnail regeneration",
)
cad = await _get_cad_file(id, db)
# Reset processing status so the worker will reprocess
cad.processing_status = ProcessingStatus.pending
await db.commit()
# Enqueue Celery task
task_id: str | None = None
try:
from app.tasks.step_tasks import process_step_file
result = process_step_file.delay(str(cad.id))
task_id = result.id
except Exception:
# Worker may not be running; status is already reset so it will pick up later
pass
return {
"cad_file_id": str(cad.id),
"original_name": cad.original_name,
"status": "queued",
"task_id": task_id,
}
@router.post("/{id}/reset-stuck", status_code=status.HTTP_200_OK)
async def reset_stuck_processing(
id: uuid.UUID,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Force-reset a CAD file that is stuck in 'processing' to 'failed'.
Use when a file shows 'processing' indefinitely due to a worker crash.
After resetting, click 'Regen thumbnail' to retry.
"""
if not is_privileged(user):
raise HTTPException(status_code=403, detail="Insufficient permissions")
cad = await _get_cad_file(id, db)
if cad.processing_status != ProcessingStatus.processing:
raise HTTPException(
status_code=400,
detail=f"CAD file is not stuck — current status: {cad.processing_status.value}",
)
cad.processing_status = ProcessingStatus.failed
cad.error_message = "Manually reset — worker may have crashed. Use 'Regen thumbnail' to retry."
await db.commit()
return {"cad_file_id": str(cad.id), "status": "failed", "message": "Reset to 'failed'. Use 'Regen thumbnail' to retry."}
# ---------------------------------------------------------------------------
# Part-material assignment endpoints
# ---------------------------------------------------------------------------
@router.get("/{id}/part-materials", response_model=PartMaterialsResponse)
async def get_part_materials(
id: uuid.UUID,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Return the saved part-material assignments for a CAD file."""
cad = await _get_cad_file(id, db)
return PartMaterialsResponse(
cad_file_id=str(cad.id),
part_materials=cad.part_materials,
)
@router.put("/{id}/part-materials", response_model=PartMaterialsResponse)
async def save_part_materials(
id: uuid.UUID,
body: dict[str, PartMaterialEntry],
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Replace the part-material assignment map for a CAD file.
Accepts a full dict of part-name -> {type, value} and overwrites the existing
assignment. Pass an empty dict to clear all assignments.
"""
if not is_privileged(user):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions")
cad = await _get_cad_file(id, db)
# Serialise Pydantic models to plain dicts for JSONB storage
cad.part_materials = {name: entry.model_dump() for name, entry in body.items()}
cad.updated_at = datetime.utcnow()
await db.commit()
await db.refresh(cad)
return PartMaterialsResponse(
cad_file_id=str(cad.id),
part_materials=cad.part_materials,
)