from __future__ import annotations
import json
import math
import logging
import subprocess
import sys
from dataclasses import dataclass, field
from pathlib import Path
import numpy as np
logger = logging.getLogger(__name__)
[docs]
@dataclass
class SharpEdge:
point_a: tuple[float, float, float]
point_b: tuple[float, float, float]
angle: float
length: float
[docs]
@dataclass
class GapRegion:
center: tuple[float, float, float]
gap_width: float
normal: tuple[float, float, float]
[docs]
@dataclass
class FeatureMap:
sharp_edges: list[SharpEdge] = field(default_factory=list)
gap_regions: list[GapRegion] = field(default_factory=list)
curvature_radius: float = 1.0
suggested_min_cell: float = 0.01
suggested_max_cell: float = 0.1
[docs]
class FeatureDetector:
_cache: dict[str, FeatureMap] = {}
def __init__(self):
self._gmsh_initialized = False
[docs]
def analyze_step(
self, filepath: Path | str, detail: str = "medium", scale: float = 1.0,
) -> FeatureMap:
"""Analyse *filepath* and suggest cell sizes.
*scale* converts the CAD file's own units into the units the rest
of the app works in (metres) — the SAME factor MainWindow applies
to the loaded geometry. Without it, this returns sizes in the CAD
file's units while the caller treats them as metres: confirmed
live on a 3 m model authored in mm, where GMSH's bbox is 3000 and
the suggestion came back as 150 — applied as 150 METRES, i.e.
50x the whole model, which then got clamped and logged as
"maxCellSize clamped from 100.0000 to 1.5000".
"""
filepath = Path(filepath)
if not filepath.exists():
raise FileNotFoundError(f"STEP file not found: {filepath}")
# Check cache: keyed by (resolved path + mtime + detail + scale)
cache_key = f"{filepath.resolve()}::{filepath.stat().st_mtime}::{detail}::{scale}"
cached = self._cache.get(cache_key)
if cached is not None:
return cached
import gmsh
if not self._gmsh_initialized:
# interruptible=True (gmsh's default) calls signal.signal() to
# let Ctrl+C abort a long operation — but signal handlers can
# only be installed from the main thread, and this now runs
# on a background QThread (FeatureDetectWorker) to keep the
# GUI responsive. Confirmed live: every feature-detection call
# failed with "signal only works in main thread of the main
# interpreter" the moment it stopped running on the GUI
# thread. interruptible=False skips that call entirely.
gmsh.initialize(interruptible=False)
# Same fix as gmsh_wrapper._ensure_gmsh(): without this, GMSH's
# OCC STEP importer ignores the file's declared unit and keeps
# raw coordinate values unconverted (a real mm-scale part read
# as if the numbers were already metres — 1000x too large).
# This is a separate GMSH session (its own gmsh.initialize(),
# not gmsh_wrapper's _ensure_gmsh()), so it needs its own copy.
gmsh.option.setString("Geometry.OCCTargetUnit", "M")
self._gmsh_initialized = True
gmsh.clear()
gmsh.model.add("feature_analysis")
gmsh.open(str(filepath))
feature_map = FeatureMap()
# Curvature analysis
try:
curvature = gmsh.model.getCurvature(-1, [])
if curvature:
feature_map.curvature_radius = max(
1.0 / max(abs(c) for c in curvature if abs(c) > 1e-12), 1e-6
)
except Exception as e:
logger.debug("Curvature analysis skipped: %s", e)
# Sharp edge detection via dihedral angles (mesh-based)
try:
gmsh.model.mesh.generate(2)
# Get all node coordinates
node_tags, node_coords, _ = gmsh.model.mesh.getNodes()
node_map = {}
for k in range(len(node_tags)):
node_map[int(node_tags[k])] = node_coords[k*3:(k+1)*3]
# Get 2D elements and compute face normals
face_types, face_elem_tags, face_elem_nodes = gmsh.model.mesh.getElements(2)
face_normal_map = {}
for etype, etags, enodes in zip(face_types, face_elem_tags, face_elem_nodes):
npe = 3 if etype == 2 else 4
enodes = enodes.reshape(-1, npe)
for k in range(len(etags)):
n0 = node_map.get(int(enodes[k][0]))
n1 = node_map.get(int(enodes[k][1]))
n2 = node_map.get(int(enodes[k][2]))
if n0 is not None and n1 is not None and n2 is not None:
v1 = n1 - n0
v2 = n2 - n0
n = np.cross(v1, v2)
norm_n = np.linalg.norm(n)
if norm_n > 1e-12:
n = n / norm_n
face_normal_map[int(etags[k])] = n
# Build face-to-edge mapping
face_edge_map = {}
for etype, etags, enodes in zip(face_types, face_elem_tags, face_elem_nodes):
try:
_, elem_edges = gmsh.model.mesh.getElementEdges(etype, etags)
except Exception:
# degenerate element: skip it, keep detecting the rest
logger.debug("feature_detector: getElementEdges failed", exc_info=True)
continue
if elem_edges.ndim < 2:
continue
for k, ftag in enumerate(etags):
face_edge_map[int(ftag)] = [int(elem_edges[k][j]) for j in range(elem_edges.shape[1])]
# Reverse: edge -> adjacent face elements
edge_face_map = {}
for ftag, e_list in face_edge_map.items():
for etag in e_list:
edge_face_map.setdefault(etag, []).append(ftag)
# Edge node pairs for position/length
edge_tags, edge_nodes = gmsh.model.mesh.getEdges()
edge_nodes = edge_nodes.reshape(-1, 2)
edge_node_map = {int(edge_tags[k]): (int(edge_nodes[k][0]), int(edge_nodes[k][1])) for k in range(len(edge_tags))}
for etag, ftags in edge_face_map.items():
if len(ftags) < 2:
continue
n1 = face_normal_map.get(ftags[0])
n2 = face_normal_map.get(ftags[1])
if n1 is None or n2 is None:
continue
cos_angle = float(np.dot(n1, n2))
cos_angle = max(-1.0, min(1.0, cos_angle))
angle = math.degrees(math.acos(cos_angle))
en = edge_node_map.get(etag)
if en is None:
continue
p1 = node_map.get(en[0])
p2 = node_map.get(en[1])
if p1 is None or p2 is None:
continue
length = math.sqrt(np.sum((p2 - p1) ** 2))
if angle > 30.0 and length > 0.001:
feature_map.sharp_edges.append(
SharpEdge(
point_a=tuple(p1),
point_b=tuple(p2),
angle=angle,
length=length,
)
)
except Exception as e:
logger.debug("Sharp edge detection skipped: %s", e)
# Bounding box BEFORE the mesh is cleared below: gap detection and
# cell-size suggestion both need it. getBoundingBox(dim, tag) needs
# both positional args (missing the second one crashed every call:
# "missing 1 required positional argument: 'tag'", confirmed live)
# and returns one flat 6-tuple (xmin, ymin, zmin, xmax, ymax, zmax),
# not two 3-tuples.
bbox = gmsh.model.getBoundingBox(-1, -1)
bbox_raw = max(bbox[i + 3] - bbox[i] for i in range(3))
bbox_dim = bbox_raw * scale
# Gap detection: real proximity query between NON-adjacent surface
# pairs via gmsh.model.getClosestPoint on the surface mesh (the 2D
# mesh generated above). A "gap" is a passage narrower than 5% of
# the bounding box between two surfaces that do NOT share a boundary
# edge — adjacent faces of the same solid are close to each other by
# definition and are not gaps. Deliberately bounded (at most
# GAP_MAX_PAIRS pairs, GAP_SAMPLES points per pair, GAP_MAX_GAPS
# kept) so a pathological model cannot stall the out-of-process
# worker; any failure falls back to the empty-gap-list placeholder
# behaviour instead of corrupting cell sizes.
GAP_MAX_PAIRS = 64
GAP_SAMPLES = 32
GAP_MAX_GAPS = 8
try:
surfaces = [e[1] for e in gmsh.model.getEntities(2)]
if 2 <= len(surfaces) <= 64:
gap_threshold = max(bbox_raw * 0.05, 1e-9)
edge_cache: dict[int, set[int]] = {}
def _edges_of(surf_tag: int) -> set[int]:
es = edge_cache.get(surf_tag)
if es is None:
es = {
int(e[1]) for e in gmsh.model.getBoundary(
[(2, surf_tag)], combined=True,
oriented=False, recursive=False,
)
}
edge_cache[surf_tag] = es
return es
gaps: list[GapRegion] = []
gap_pairs = 0
for i in range(len(surfaces)):
for j in range(i + 1, len(surfaces)):
if gap_pairs >= GAP_MAX_PAIRS:
break
a, b = surfaces[i], surfaces[j]
if _edges_of(a) & _edges_of(b):
continue # adjacent surfaces, not a gap
gap_pairs += 1
try:
_, coords, _ = gmsh.model.mesh.getNodes(2, a)
except Exception:
# gap probe failed for this pair — try the next
logger.debug("feature_detector: getNodes failed", exc_info=True)
continue
coords = np.array(coords).reshape(-1, 3)
if len(coords) < 2:
continue
step = max(1, len(coords) // GAP_SAMPLES)
for pt in coords[::step]:
try:
closest, _ = gmsh.model.getClosestPoint(2, b, pt.tolist())
except Exception:
# closest-point probe failed — try the next sample
logger.debug("feature_detector: getClosestPoint failed", exc_info=True)
continue
dist = float(np.linalg.norm(np.array(closest) - pt))
if not (0.0 < dist < gap_threshold):
continue
gaps.append(GapRegion(
center=tuple((pt + np.array(closest)) / 2.0),
gap_width=dist,
normal=tuple((np.array(closest) - pt) / dist),
))
gaps.sort(key=lambda g: g.gap_width)
feature_map.gap_regions = gaps[:GAP_MAX_GAPS]
except Exception as e:
logger.debug("Gap detection skipped: %s", e)
try:
gmsh.model.mesh.clear()
except Exception:
# best-effort cleanup — a stale GMSH session must not abort detection
logger.debug("feature_detector: gmsh mesh.clear failed", exc_info=True)
# Every length gathered above (edge lengths, gap widths, curvature
# radius) is in the CAD file's own units, and suggest_cell_sizes()
# compares them against bbox_dim — so they all have to be converted
# together, or the comparisons silently mix mm with metres.
if scale != 1.0:
for e in feature_map.sharp_edges:
e.length *= scale
for g in feature_map.gap_regions:
g.gap_width *= scale
feature_map.curvature_radius *= scale
feature_map.suggested_min_cell, feature_map.suggested_max_cell = (
self.suggest_cell_sizes(feature_map, bbox_dim)
)
logger.info(
"Feature analysis complete: %d sharp edges, %d gaps, "
"curvature=%.4f, cells: %.4f/%.4f",
len(feature_map.sharp_edges),
len(feature_map.gap_regions),
feature_map.curvature_radius,
feature_map.suggested_min_cell,
feature_map.suggested_max_cell,
)
# Cache the result. The key MUST match the lookup key at the top
# of this method (path::mtime::detail::scale) — it used to omit
# scale, so the cache never hit and simply grew to its eviction
# limit on every run.
cache_key = f"{filepath.resolve()}::{filepath.stat().st_mtime}::{detail}::{scale}"
self._cache[cache_key] = feature_map
# Limit cache size
if len(self._cache) > 20:
for k in list(self._cache.keys())[:-10]:
del self._cache[k]
return feature_map
[docs]
def suggest_cell_sizes(
self, feature_map: FeatureMap, bbox_dim: float
) -> tuple[float, float]:
base_min = bbox_dim * 0.005
base_max = bbox_dim * 0.05
if feature_map.sharp_edges:
min_edge_len = min(e.length for e in feature_map.sharp_edges)
base_min = min(base_min, min_edge_len * 0.3)
if feature_map.gap_regions:
min_gap = min(g.gap_width for g in feature_map.gap_regions)
base_min = min(base_min, min_gap * 0.2)
if feature_map.curvature_radius < bbox_dim * 0.1:
curvature_cell = feature_map.curvature_radius * 0.1
base_min = min(base_min, curvature_cell)
base_min = max(base_min, 1e-6)
base_max = max(base_max, base_min * 3)
return base_min, base_max
[docs]
def shutdown(self):
if self._gmsh_initialized:
try:
import gmsh
gmsh.finalize()
except Exception:
# best-effort teardown
logger.debug("feature_detector: gmsh.finalize failed", exc_info=True)
self._gmsh_initialized = False
from PySide6.QtCore import QObject, Signal, Slot
[docs]
class FeatureDetectWorker(QObject):
"""Runs feature detection in a SEPARATE PROCESS, driven from a
background QThread.
Two independent reasons this cannot run in-process on the GUI thread:
1. Responsiveness: analyze_step() calls gmsh.model.mesh.generate(2), a
full 2D surface remesh. On a complex or poorly-defeatured CAD model
GMSH can spend a long time retrying ("Splitting those edges and
trying again", "N elements remain invalid in surface M") — the app
showed "Not Responding" for that whole time when it ran on the GUI
thread.
2. Crash isolation: gmsh bundles its OWN OpenCASCADE build, while the
app separately loads cadquery/OCP's OpenCASCADE for STEP handling.
Both live in one process, and driving gmsh's OCC STEP reader on a
complex model took the entire application down with a hard native
crash — no Python traceback, just a dead process (reported live on
both the serial and parallel meshing paths, on a geometry where
GMSH's log showed it fighting "3 intersections in the 1D mesh").
Feature detection is a *nice-to-have* that only refines suggested
cell sizes; it must never be able to kill a meshing run. Running it
out-of-process means even a segfault is just a non-zero exit code
here, and the caller falls back to the existing cell sizes.
"""
finished = Signal(object, object) # (FeatureMap | None, error_str | None)
# Generous vs. a normal run (seconds), tight enough that a pathological
# geometry can't stall meshing indefinitely.
TIMEOUT_S = 120
def __init__(self, step_path: str, detail: str, scale: float = 1.0, parent=None):
super().__init__(parent)
self._step_path = step_path
self._detail = detail
self._scale = scale
[docs]
@Slot()
def run(self):
# In a normal (non-frozen) run, sys.executable is a real python.exe
# that understands `-m modulename`. In a PyInstaller-frozen build,
# sys.executable IS this same exe, and the frozen bootloader does
# not implement Python's `-m` flag at all — it just re-runs the
# app's normal entry point regardless of arguments, which used to
# pop a second GUI window that immediately crashed on every
# meshing run (confirmed live). app.main() checks for
# "--feature-detect" as its first argument and dispatches to
# _main() below instead of starting the GUI — see app.py.
frozen = getattr(sys, "frozen", False)
if frozen:
cmd = [sys.executable, "--feature-detect", self._step_path, self._detail, str(self._scale)]
else:
# Direct file path, not `-m polyfoammesh.core.feature_detector`:
# `-m` fully imports the parent package first (whose __init__.py
# already imports several core/ modules), so by the time runpy
# executes this file as __main__ it's already in sys.modules —
# triggering Python's own "found in sys.modules ... prior to
# execution" RuntimeWarning on stderr, which can bury the real
# crash detail this worker's "native crash" fallback branch
# below relies on reading from stderr's tail. Same fix already
# applied to every subprocess launch in openfoam_runner.py.
script = str(Path(__file__).resolve())
cmd = [sys.executable, script, self._step_path, self._detail, str(self._scale)]
# The dev-mode cwd anchors relative imports when invoking the script
# from an arbitrary working directory; a frozen exe is self-contained
# (PyInstaller resolves everything via sys._MEIPASS) and doesn't
# need or benefit from it — inherit the parent's cwd instead.
run_cwd = None if frozen else str(Path(__file__).resolve().parents[2])
try:
proc = subprocess.run(
cmd,
capture_output=True, text=True, timeout=self.TIMEOUT_S,
cwd=run_cwd,
)
except subprocess.TimeoutExpired:
self.finished.emit(
None,
f"feature detection exceeded {self.TIMEOUT_S}s and was cancelled",
)
return
except Exception as exc:
self.finished.emit(None, str(exc))
return
# Parse stdout first even on a non-zero exit: a clean "couldn't
# read this file" still prints its reason as JSON before exiting 1,
# and that message is far more useful than "exit code 1".
payload = None
try:
payload = json.loads(proc.stdout.strip().splitlines()[-1])
except Exception:
payload = None
if payload is None:
if proc.returncode != 0:
# No parseable output AND a bad exit status: this is the
# native-crash case (segfault etc.) that running
# out-of-process exists to contain.
tail = (proc.stderr or "").strip().splitlines()
detail = tail[-1] if tail else f"exit code {proc.returncode}"
self.finished.emit(None, f"feature detection crashed ({detail})")
else:
self.finished.emit(None, "feature detection produced no usable output")
return
if not payload.get("ok"):
self.finished.emit(None, payload.get("error", "unknown error"))
return
feature_map = FeatureMap(
curvature_radius=payload["curvature_radius"],
suggested_min_cell=payload["suggested_min_cell"],
suggested_max_cell=payload["suggested_max_cell"],
)
# Only the counts matter downstream (they're logged); the full edge
# geometry isn't used by the caller, so it isn't serialised.
feature_map.sharp_edges = [None] * payload["n_sharp_edges"]
feature_map.gap_regions = [None] * payload["n_gap_regions"]
self.finished.emit(feature_map, None)
def _main(argv: list[str] | None = None) -> int:
"""CLI entry point: analyse a CAD file and print one line of JSON.
Deliberately isolated in its own process — see FeatureDetectWorker.
*argv* is ``[file, detail, scale]`` with no leading program name —
pass it explicitly when dispatching from app.main()'s frozen-exe
"--feature-detect" path (its own sys.argv still has the exe name and
the "--feature-detect" flag ahead of these). Defaults to sys.argv[1:]
for the normal ``python -m polyfoammesh.core.feature_detector``
invocation used in a non-frozen (dev) run.
"""
if argv is None:
argv = sys.argv[1:]
if len(argv) < 2:
print(json.dumps({"ok": False, "error": "usage: <file> <detail> [scale]"}))
return 2
path, detail = argv[0], argv[1]
scale = float(argv[2]) if len(argv) > 2 else 1.0
try:
fm = FeatureDetector().analyze_step(path, detail=detail, scale=scale)
except Exception as exc:
print(json.dumps({"ok": False, "error": str(exc)}))
return 1
print(json.dumps({
"ok": True,
"n_sharp_edges": len(fm.sharp_edges),
"n_gap_regions": len(fm.gap_regions),
"curvature_radius": fm.curvature_radius,
"suggested_min_cell": fm.suggested_min_cell,
"suggested_max_cell": fm.suggested_max_cell,
}))
return 0