Desktop app for designing parametric 3D models in CadQuery together with the coding agent of your choice
TypeScript 3d-modeling3d-printing3d-printing-toolsagentagentic-aiai-cadcadcadqueryclaude-codecodexelectronfemllmmacdmakeropencodeparametric-designpythonsteptext-to-cad
soma: FEM meldet Vernetzungsfehler verständlich, findet CalculiX aus der Projektumgebung, Ausgabe als UTF-8
Kai-Jungsthoefelfa43b3d8445c
diff --git a/python/soma/ccx.py b/python/soma/ccx.pyindex 6a275ac..0f37d1a 100644--- a/python/soma/ccx.py+++ b/python/soma/ccx.py@@ -6,8 +6,8 @@ nachgerechnete Lagerkraefte hatten 0.004 N Querkraft am Kragbalken) und Kontaktgrechnet `fem.py` aus den Verschiebungen selbst (je Teil, CalculiX mittelt ueber Werkstoffgrenzen hinweg).Gefunden wird ccx in dieser Reihenfolge: SOMA_CCX (Pfad zum Programm), mitgeliefert neben der Python-Umgebung-(<sys.prefix>/ccx/ccx.exe unter Windows, <sys.prefix>/ccx/bin/ccx sonst, geholt mit python/scripts/fetch_ccx.py),-dann `ccx` im PATH.+(<sys.prefix>/ccx/ccx.exe unter Windows, <sys.prefix>/ccx/bin/ccx sonst, geholt mit python/scripts/fetch_ccx.py; aus der+Projektumgebung heraus neben deren Basis), dann `ccx` im PATH.Robustheit: ccx bricht bei einem Teil, das wegrutscht oder abhebt, nicht von selbst ab, sondern rechnet minutenlangweiter (die Verschiebungen wachsen auf 1e11 mm). Die Ausgabe wird deshalb mitgelesen und die Rechnung abgebrochen,@@ -51,8 +51,9 @@ def find():cands = []if os.environ.get("SOMA_CCX"):cands.append(Path(os.environ["SOMA_CCX"]))- base = Path(sys.prefix) / "ccx"- cands += [base / "ccx.exe", base / "bin" / "ccx"] if sys.platform == "win32" else [base / "bin" / "ccx", base / "ccx"]+ for prefix in [Path(sys.prefix)] + _base_prefixes():+ base = prefix / "ccx"+ cands += [base / "ccx.exe", base / "bin" / "ccx"] if sys.platform == "win32" else [base / "bin" / "ccx", base / "ccx"]w = shutil.which("ccx")if w:cands.append(Path(w))@@ -61,6 +62,19 @@ def find():return exe+def _base_prefixes():+ """Ordner der Basisumgebung, wenn soma aus der Projektumgebung (.soma/env, E67) laeuft. So rufen Agent-Apps ausserhalb+ des SOMA-Terminals soma auf; sys.prefix ist dann die Projektumgebung, ccx liegt aber neben der Basis. Deren Python steht+ in soma-env.json. venv: <prefix>/Scripts/python.exe bzw. bin/python, eigenstaendiges Python unter Windows: <prefix>/python.exe."""+ try:+ import json+ info = json.loads((Path(sys.prefix) / "soma-env.json").read_text(encoding="utf-8"))+ exe = Path(info["base_python"])+ except (OSError, ValueError, KeyError, TypeError):+ return []+ return [exe.parent.parent, exe.parent]++def version(exe=None):"""Version als Text ("2.23") oder None, wenn ccx fehlt oder nicht startet."""exe = exe or find()diff --git a/python/soma/cli.py b/python/soma/cli.pyindex 6cdfd15..87f5a5d 100644--- a/python/soma/cli.py+++ b/python/soma/cli.py@@ -538,6 +538,13 @@ def main(argv=None):x.set_defaults(func=cmd_dxfview)args = ap.parse_args(argv)+ # Ausgabe immer UTF-8: Agenten ausserhalb des SOMA-Terminals (ohne PYTHONUTF8) lesen die Pipe als UTF-8, Python schriebe+ # dort die ANSI-Codepage, und ° oder ² kaemen als "�" an. In der Windows-Konsole aendert das nichts (die schreibt UTF-16)+ for stream in (sys.stdout, sys.stderr):+ try:+ stream.reconfigure(encoding="utf-8", errors="replace")+ except (AttributeError, ValueError, OSError):+ pass# Zusatzpakete des Projekts (E67): soma laeuft mit dem Python der Basis, das Modell darf sie trotzdem importierenif args.cmd != "env" and getattr(args, "project", None) is not None:from . import envdiff --git a/python/soma/fem.py b/python/soma/fem.pyindex 000fea3..afa9ae8 100644--- a/python/soma/fem.py+++ b/python/soma/fem.py@@ -305,6 +305,20 @@ EDGES = [(0, 1), (1, 2), (2, 0), (3, 0), (3, 2), (3, 1)]FACES = [(1, 2, 3), (0, 3, 2), (0, 1, 3), (0, 2, 1)] # Flaeche k liegt gegenueber Ecke k+def _mesh_failure(e, parts, h):+ """Verstaendliche Meldung, wenn gmsh nicht vernetzen kann (die Meldung von gmsh steht am Ende fuer Rueckfragen)."""+ msg = str(e).strip().splitlines()[-1] if str(e).strip() else type(e).__name__+ other = [round(h * 1.6, 2), round(h / 2, 2)]+ text = f"The mesher could not mesh the {'parts' if len(parts) > 1 else 'part'} (edge length {h:g} mm). "+ if len(parts) > 1:+ text += ("This usually comes from parts that overlap or almost touch, which leaves very thin slivers between them, "+ "or from very small details. Check the overlaps with soma analyze clearance, calculate fewer parts at once, or ")+ else:+ text += "This usually comes from very small details or sharp spots. "+ size = f"set a different mesh size, e.g. Study(mesh={other[0]:g}) or Study(mesh={other[1]:g})"+ return text + (size if len(parts) > 1 else size[0].upper() + size[1:]) + f". (gmsh: {msg[:200]})"++def _mesh(parts, bcs, h, progress=None):"""Vernetzt die Teile gemeinsam. Liefert Knoten, Tet10, Teil je Element und je Randbedingung die Flaechendreiecke(Ecken als Knotenindizes) samt Flaechenvergleich."""@@ -340,10 +354,15 @@ def _mesh(parts, bcs, h, progress=None):gmsh.option.setNumber("Mesh.MeshSizeMin", h / 20)gmsh.option.setNumber("Mesh.MeshSizeFromCurvature", 12)gmsh.option.setNumber("Mesh.Optimize", 1)- gmsh.model.mesh.generate(3)- if progress:- progress.step(1) # die Vernetzung als ein Schritt, danach zaehlen die Elemente- gmsh.model.mesh.setOrder(2)+ # gmsh wirft rohe Meldungen ("unable to find 106 1110 0 1" aus der Randrekonstruktion). Typisch bei mehreren Teilen,+ # die sich etwas durchdringen: fragment hinterlaesst dort hauchduenne Splitter, an denen das 3D-Netz scheitert+ try:+ gmsh.model.mesh.generate(3)+ if progress:+ progress.step(1) # die Vernetzung als ein Schritt, danach zaehlen die Elemente+ gmsh.model.mesh.setOrder(2)+ except Exception as e:+ raise FemError(_mesh_failure(e, parts, h)) from Nonedef extract():tags, xyz, _ = gmsh.model.mesh.getNodes()@@ -1342,7 +1361,12 @@ def run(root, study_rel, name=None, threshold=None):fine = _analyse(parts, study, mats, h, progress, bolts)coarse = Noneif study.check_mesh:- coarse = _analyse(parts, study, mats, h * 1.6, None, bolts)+ # Die Gegenprobe darf das fertige Ergebnis nicht verhindern: scheitert nur das grobe Netz, fehlt die Pruefung (mit Hinweis)+ try:+ coarse = _analyse(parts, study, mats, h * 1.6, None, bolts)+ except FemError as e:+ fine["notes"] = list(fine.get("notes") or []) + [f"The check with a coarser mesh ({h * 1.6:g} mm) failed, so it is "+ f"not known whether the peak stress depends on the mesh: {e}"]progress.finish()seconds = time.perf_counter() - t_startreturn _write(root, rel, study, all_parts, parts, mats, fine, coarse, h, diag, safety, name, seconds)