This commit is contained in:
2026-05-12 10:23:58 +08:00
parent 74bbca90e3
commit fd1f43f278
+36 -25
View File
@@ -6,8 +6,8 @@ from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut, BRepAlgoAPI_Section, BRepAlgoA
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeHalfSpace from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeHalfSpace
from OCC.Core.gp import gp_Pln, gp_Dir, gp_Pnt, gp_Trsf, gp_Ax2 from OCC.Core.gp import gp_Pln, gp_Dir, gp_Pnt, gp_Trsf, gp_Ax2
from OCC.Core.TopoDS import TopoDS_Face, topods from OCC.Core.TopoDS import TopoDS_Face, TopoDS_Compound, topods
from OCC.Core.BRep import BRep_Tool from OCC.Core.BRep import BRep_Tool, BRep_Builder
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Core.GProp import GProp_GProps from OCC.Core.GProp import GProp_GProps
from OCC.Core.BRepGProp import brepgprop from OCC.Core.BRepGProp import brepgprop
@@ -297,27 +297,25 @@ class BaseMoldGenerator:
xmax: float, ymax: float, zmax: float, xmax: float, ymax: float, zmax: float,
) -> Any: ) -> Any:
""" """
构建带底座的型芯:底座平板 + 产品突出体融合。 构建带底座的型芯。
底座从模具边界延伸到分型面(沿分型方向), 核心逻辑:底座平板沿分型方向覆盖模具半空间,
并略微伸入产品体积以确保布尔融合成功。 与产品形状做布尔融合,形成"底座+产品突出体"。
融合失败时用 TopoDS_Compound 兜底,确保底座永不会丢失。
""" """
normal = parting_plane.Axis().Direction() normal = parting_plane.Axis().Direction()
origin = parting_plane.Location() origin = parting_plane.Location()
nx, ny, nz = float(normal.X()), float(normal.Y()), float(normal.Z()) nx, ny, nz = float(normal.X()), float(normal.Y()), float(normal.Z())
prod_extent = (xmax - xmin) + (ymax - ymin) + (zmax - zmin) prod_span = max((xmax - xmin), (ymax - ymin), (zmax - zmin))
overlap = max(prod_extent * 0.01, 0.5) overlap = max(prod_span * 0.15, 8.0)
base_p1 = [mold_xmin, mold_ymin, mold_zmin] base_p1 = [mold_xmin, mold_ymin, mold_zmin]
base_p2 = [mold_xmax, mold_ymax, mold_zmax] base_p2 = [mold_xmax, mold_ymax, mold_zmax]
for i, (n, o, lo, hi) in enumerate(zip( for i in range(3):
[nx, ny, nz], n = [nx, ny, nz][i]
[float(origin.X()), float(origin.Y()), float(origin.Z())], o = [float(origin.X()), float(origin.Y()), float(origin.Z())][i]
[mold_xmin, mold_ymin, mold_zmin],
[mold_xmax, mold_ymax, mold_zmax],
)):
if abs(n) < 0.001: if abs(n) < 0.001:
continue continue
if n > 0: if n > 0:
@@ -325,12 +323,13 @@ class BaseMoldGenerator:
else: else:
base_p1[i] = o - overlap base_p1[i] = o - overlap
base_plate = None
try: try:
base_plate = BRepPrimAPI_MakeBox( base_plate = BRepPrimAPI_MakeBox(
gp_Pnt(base_p1[0], base_p1[1], base_p1[2]), gp_Pnt(base_p1[0], base_p1[1], base_p1[2]),
gp_Pnt(base_p2[0], base_p2[1], base_p2[2]) gp_Pnt(base_p2[0], base_p2[1], base_p2[2])
).Shape() ).Shape()
logger.info("型芯底座平板构建完成") logger.info(f"型芯底座构建: 重叠量={overlap:.1f}mm")
except Exception as e: except Exception as e:
logger.warning(f"底座构建失败: {e}") logger.warning(f"底座构建失败: {e}")
return shape return shape
@@ -339,21 +338,33 @@ class BaseMoldGenerator:
fuse_op = BRepAlgoAPI_Fuse(base_plate, shape) fuse_op = BRepAlgoAPI_Fuse(base_plate, shape)
if fuse_op.IsDone(): if fuse_op.IsDone():
core = fuse_op.Shape() core = fuse_op.Shape()
logger.info("型芯(底座+产品突出体)融合成功") explorer = TopExp_Explorer(core, TopAbs_FACE)
return core face_count = 0
while explorer.More():
face_count += 1
explorer.Next()
if face_count > 0:
logger.info(f"型芯融合成功 (面数={face_count})")
return core
logger.warning("Fuse 结果无几何,尝试备用方案")
except Exception as e: except Exception as e:
logger.warning(f"底座融合失败: {e}") logger.warning(f"底座融合失败: {e}")
try: return self._build_core_compound(base_plate, shape)
cut_op = BRepAlgoAPI_Cut(mold_block, shape)
if cut_op.IsDone():
logger.info("型芯通过布尔减回退构建")
return cut_op.Shape()
except Exception:
pass
logger.info("型芯回退为产品形状") @staticmethod
return shape def _build_core_compound(base_plate: Any, shape: Any) -> Any:
"""
兜底方案:构建 TopoDS_Compound 包含底座平板 + 产品。
即使布尔融合失败,底座也绝不会丢失。
"""
compound = TopoDS_Compound()
builder = BRep_Builder()
builder.MakeCompound(compound)
builder.Add(compound, base_plate)
builder.Add(compound, shape)
logger.info("型芯 Compound 兜底构建 (底座+产品)")
return compound
def _get_parting_plane(self, parting_surface: Any, shape: Any) -> Optional[gp_Pln]: def _get_parting_plane(self, parting_surface: Any, shape: Any) -> Optional[gp_Pln]:
"""从分型面提取平面方程""" """从分型面提取平面方程"""