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: Mesher schreibt Normale ebener Flächen und Achse von Zylinder und Kegel in die Flächendaten
Kai-Jungsthoefelc06387cee0f3
diff --git a/python/soma/mesher.py b/python/soma/mesher.pyindex 8a3a71e..7f3b144 100644--- a/python/soma/mesher.py+++ b/python/soma/mesher.py@@ -9,9 +9,10 @@ import structimport numpy as npfrom OCP.BRep import BRep_Tool-from OCP.BRepAdaptor import BRepAdaptor_Curve+from OCP.BRepAdaptor import BRepAdaptor_Curve, BRepAdaptor_Surfacefrom OCP.BRepMesh import BRepMesh_IncrementalMeshfrom OCP.GCPnts import GCPnts_TangentialDeflection+from OCP.GeomAbs import GeomAbs_Cone, GeomAbs_Cylinder, GeomAbs_Planefrom OCP.TopAbs import TopAbs_REVERSEDfrom OCP.TopLoc import TopLoc_Location@@ -20,6 +21,26 @@ def srgb_to_linear(c):return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4+def _direction_of(face):+ """Normale einer ebenen Flaeche (nach aussen) oder Achse eines Zylinders oder Kegels. Die Explosionsansicht nimmt sie als+ Richtungen, in denen ein Teil herauskommen kann (schraeg eingesetzte Stifte, Wellen, Platten)."""+ try:+ s = BRepAdaptor_Surface(face.wrapped)+ kind = s.GetType()+ if kind == GeomAbs_Plane:+ n = face.normalAt()+ return dict(normal=[round(n.x, 6) + 0.0, round(n.y, 6) + 0.0, round(n.z, 6) + 0.0])+ if kind == GeomAbs_Cylinder:+ d = s.Cylinder().Axis().Direction()+ elif kind == GeomAbs_Cone:+ d = s.Cone().Axis().Direction()+ else:+ return {}+ return dict(axis=[round(d.X(), 6) + 0.0, round(d.Y(), 6) + 0.0, round(d.Z(), 6) + 0.0])+ except Exception: # unbrauchbare Flaeche: dann eben ohne Richtung, der Rest der Vernetzung geht weiter+ return {}++def mesh_shape(shape, tol=0.05, ang=0.3):"""Tesselliert ein cq.Shape. Liefert Dict mit numpy-Arrays und Flaechen-/Kanten-Tabellen."""BRepMesh_IncrementalMesh(shape.wrapped, tol, False, ang, True)@@ -54,11 +75,13 @@ def mesh_shape(shape, tol=0.05, ang=0.3):vn = np.where(ln > 1e-12, vn / np.maximum(ln, 1e-12), 0).astype(np.float32)c = face.Center()- faces.append(dict(+ entry = dict(id=fi, surface=face.geomType(), area=round(face.Area(), 4),center=[round(c.x, 4), round(c.y, 4), round(c.z, 4)],triStart=tri_total, triCount=int(t.shape[0]),- ))+ )+ entry.update(_direction_of(face))+ faces.append(entry)pos.append(p); nrm.append(vn); idx.append(t + voff)voff += ntri_total += t.shape[0]