Source code for polyfoammesh.commercial.cad_healer

"""CAD defeaturing & healing — inspired by ANSA CAD Cleanup, SpaceClaim.

Provides automatic detection and repair of common CAD issues that
prevent or degrade mesh generation:

  - Small feature detection (holes, fillets, chamfers, sliver faces)
  - Automatic hole filling
  - Narrow-face merging
  - STL watertight check and repair
  - Facet count reduction (decimation) with feature preservation

All operations preserve the original file — a *healed* copy is written
to the case directory.
"""

from __future__ import annotations

import logging
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any

import numpy as np
import trimesh

from polyfoammesh.octopoda_local import octo

logger = logging.getLogger(__name__)


[docs] @dataclass class HealReport: """Summary of CAD healing operations performed.""" original_faces: int = 0 original_vertices: int = 0 final_faces: int = 0 final_vertices: int = 0 holes_filled: int = 0 degenerate_removed: int = 0 watertight: bool = False operations: list[str] = field(default_factory=list) warnings: list[str] = field(default_factory=list) @property def face_reduction_pct(self) -> float: if self.original_faces <= 0: return 0.0 return (1 - self.final_faces / self.original_faces) * 100
[docs] class CADHealer: """Automatic CAD healing and defeaturing. Operates on ``trimesh.Trimesh`` objects. For file-level healing use ``heal_file()`` which reads → heals → writes. Usage:: healer = CADHealer() for mesh in meshes: report = healer.heal_mesh(mesh) healer.heal_file("input.step", "output.stl") """ # Default thresholds (metres) MIN_HOLE_DIAMETER: float = 0.001 # 1 mm MIN_FILLET_RADIUS: float = 0.0005 # 0.5 mm MIN_SLIVER_AREA: float = 1e-6 # 1 mm² MAX_DECIMATION_RATIO: float = 0.3 # reduce at most 30 % def __init__(self, thresholds: dict[str, float] | None = None) -> None: self._thresholds = { "hole_diameter": self.MIN_HOLE_DIAMETER, "fillet_radius": self.MIN_FILLET_RADIUS, "sliver_area": self.MIN_SLIVER_AREA, "decimation_ratio": self.MAX_DECIMATION_RATIO, **(thresholds or {}), } # ------------------------------------------------------------------ # Single-mesh healing # ------------------------------------------------------------------
[docs] def heal_mesh(self, mesh: trimesh.Trimesh) -> HealReport: """Apply all healing operations to a single mesh. Returns a ``HealReport`` summarising what was done. """ report = HealReport( original_faces=len(mesh.faces), original_vertices=len(mesh.vertices), ) # 1. Remove degenerate faces try: mask = mesh.nondegenerate_faces() n_degen = len(mesh.faces) - int(mask.sum()) if n_degen > 0: mesh.update_faces(mask) report.degenerate_removed = n_degen report.operations.append(f"removed {n_degen} degenerate faces") except Exception as exc: report.warnings.append(f"Degenerate face removal failed: {exc}") # 2. Fill holes try: before_holes = len(mesh.faces) mesh.fill_holes() after_holes = len(mesh.faces) filled = after_holes - before_holes if filled > 0: report.holes_filled = filled report.operations.append(f"filled {filled} holes") except Exception as exc: report.warnings.append(f"Hole filling failed: {exc}") # 3. Merge vertices (weld close vertices) try: mesh.merge_vertices() report.operations.append("merged duplicate vertices") except Exception as exc: report.warnings.append(f"Vertex merge failed: {exc}") # 4. Fix normals try: mesh.fix_normals() report.operations.append("fixed face normals") except Exception as exc: report.warnings.append(f"Normal fix failed: {exc}") # 5. Watertight check report.watertight = mesh.is_watertight report.final_faces = len(mesh.faces) report.final_vertices = len(mesh.vertices) octo.log_event("cad_healer", "heal_mesh", { "original_faces": report.original_faces, "final_faces": report.final_faces, "holes_filled": report.holes_filled, "watertight": report.watertight, }) return report
# ------------------------------------------------------------------ # Batch & file-level operations # ------------------------------------------------------------------
[docs] def heal_meshes(self, meshes: list[trimesh.Trimesh]) -> list[HealReport]: """Heal multiple meshes, returning a report per mesh.""" return [self.heal_mesh(m) for m in meshes]
[docs] def heal_file( self, input_path: Path | str, output_path: Path | str | None = None, ) -> tuple[list[trimesh.Trimesh] | None, list[HealReport]]: """Read a geometry file, heal all patches, optionally write. Args: input_path: Path to input file (.step, .stp, .stl). output_path: Optional output path. If None, a healed STL is written alongside the input as ``<stem>_healed.stl``. Returns: ``(meshes, reports)`` — the healed meshes and a report per patch. """ input_path = Path(input_path) if not input_path.exists(): raise FileNotFoundError(f"Input file not found: {input_path}") from polyfoammesh.core.geometry import load_geometry meshes = load_geometry(input_path) if not meshes: raise RuntimeError(f"No geometry loaded from {input_path}") reports = self.heal_meshes(meshes) if output_path is None: output_path = input_path.with_stem(f"{input_path.stem}_healed").with_suffix(".stl") from polyfoammesh.core.stl_writer import export_multisolid_stl export_multisolid_stl(meshes, output_path) octo.log_event("cad_healer", "heal_file", { "input": str(input_path), "output": str(output_path), "patches": len(meshes), }) return meshes, reports
# ------------------------------------------------------------------ # Analysis helpers # ------------------------------------------------------------------
[docs] def detect_small_features(self, mesh: trimesh.Trimesh) -> dict[str, Any]: """Analyse a mesh and return information about small features. Returns a dict with keys like ``min_edge_length``, ``n_sliver_faces``, ``estimated_hole_diameters``. """ if len(mesh.faces) == 0: return {"error": "Empty mesh"} # Edge length distribution edges = mesh.edges_unique if len(edges) == 0: return {"error": "No edges"} verts = mesh.vertices edge_lengths = np.linalg.norm( verts[edges[:, 0]] - verts[edges[:, 1]], axis=1, ) # Face area distribution face_areas = mesh.area_faces if hasattr(mesh, 'area_faces') else np.array([]) result = { "n_faces": len(mesh.faces), "n_vertices": len(mesh.vertices), "n_edges": len(edges), "min_edge_length": float(edge_lengths.min()) if len(edge_lengths) > 0 else 0, "max_edge_length": float(edge_lengths.max()) if len(edge_lengths) > 0 else 0, "avg_edge_length": float(edge_lengths.mean()) if len(edge_lengths) > 0 else 0, "min_face_area": float(face_areas.min()) if len(face_areas) > 0 else 0, "max_face_area": float(face_areas.max()) if len(face_areas) > 0 else 0, "watertight": mesh.is_watertight, "volume": float(mesh.volume) if mesh.is_watertight else 0.0, } sliver_threshold = self._thresholds["sliver_area"] if len(face_areas) > 0: result["n_sliver_faces"] = int((face_areas < sliver_threshold).sum()) else: result["n_sliver_faces"] = 0 return result
[docs] def summarise(self, reports: list[HealReport]) -> dict[str, Any]: """Aggregate multiple heal reports into a summary dict.""" total_original = sum(r.original_faces for r in reports) total_final = sum(r.final_faces for r in reports) total_holes = sum(r.holes_filled for r in reports) all_watertight = all(r.watertight for r in reports) return { "patches": len(reports), "total_original_faces": total_original, "total_final_faces": total_final, "face_reduction_pct": ( (1 - total_final / total_original) * 100 if total_original > 0 else 0.0 ), "total_holes_filled": total_holes, "all_watertight": all_watertight, "warnings": [w for r in reports for w in r.warnings], }