Files
2026-09-24 10:40:57 +08:00

32 lines
1.6 KiB
Python

"""Complete display/export meshes without dropping small CAD faces."""
from OCP.BRep import BRep_Tool
from OCP.TopLoc import TopLoc_Location
def ensure_mesh(shape, tolerance=.05, angular_tolerance=.15):
shape.mesh(tolerance, angular_tolerance)
missing=[f for f in shape.faces() if BRep_Tool.Triangulation_s(f.wrapped,TopLoc_Location()) is None]
if not missing:
return
from OCP.BRepMesh import BRepMesh_IncrementalMesh
from OCP.BRepTools import BRepTools
from OCP.IMeshTools import IMeshTools_Parameters, IMeshTools_MeshAlgoType
for face in missing:
for deflection in (min(.001,tolerance/5), min(.00001,tolerance/50)):
# A global coarse mesh can cross the sides of a narrow face. Refine
# only that face with a constrained triangulator, preserving BRep.
BRepTools.Clean_s(face.wrapped)
p=IMeshTools_Parameters()
p.Deflection=deflection
p.Angle=min(.05,angular_tolerance)
p.MinSize=1e-7
p.AdjustMinSize=True
p.MeshAlgo=IMeshTools_MeshAlgoType.IMeshTools_MeshAlgoType_Delabella
BRepMesh_IncrementalMesh(face.wrapped,p)
if BRep_Tool.Triangulation_s(face.wrapped,TopLoc_Location()) is not None:
break
else:
raise ValueError('MESH_INCOMPLETE: 局部面无法完整网格化,需要修复该几何;未跳过面或丢弃实体')
if any(BRep_Tool.Triangulation_s(f.wrapped,TopLoc_Location()) is None for f in shape.faces()):
raise ValueError('MESH_INCOMPLETE: 网格仍有缺失面')