x
This commit is contained in:
@@ -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.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeHalfSpace
|
||||
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.BRep import BRep_Tool
|
||||
from OCC.Core.TopoDS import TopoDS_Face, TopoDS_Compound, topods
|
||||
from OCC.Core.BRep import BRep_Tool, BRep_Builder
|
||||
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
|
||||
from OCC.Core.GProp import GProp_GProps
|
||||
from OCC.Core.BRepGProp import brepgprop
|
||||
@@ -297,27 +297,25 @@ class BaseMoldGenerator:
|
||||
xmax: float, ymax: float, zmax: float,
|
||||
) -> Any:
|
||||
"""
|
||||
构建带底座的型芯:底座平板 + 产品突出体融合。
|
||||
构建带底座的型芯。
|
||||
|
||||
底座从模具边界延伸到分型面(沿分型方向),
|
||||
并略微伸入产品体积以确保布尔融合成功。
|
||||
核心逻辑:底座平板沿分型方向覆盖模具半空间,
|
||||
与产品形状做布尔融合,形成"底座+产品突出体"。
|
||||
融合失败时用 TopoDS_Compound 兜底,确保底座永不会丢失。
|
||||
"""
|
||||
normal = parting_plane.Axis().Direction()
|
||||
origin = parting_plane.Location()
|
||||
nx, ny, nz = float(normal.X()), float(normal.Y()), float(normal.Z())
|
||||
|
||||
prod_extent = (xmax - xmin) + (ymax - ymin) + (zmax - zmin)
|
||||
overlap = max(prod_extent * 0.01, 0.5)
|
||||
prod_span = max((xmax - xmin), (ymax - ymin), (zmax - zmin))
|
||||
overlap = max(prod_span * 0.15, 8.0)
|
||||
|
||||
base_p1 = [mold_xmin, mold_ymin, mold_zmin]
|
||||
base_p2 = [mold_xmax, mold_ymax, mold_zmax]
|
||||
|
||||
for i, (n, o, lo, hi) in enumerate(zip(
|
||||
[nx, ny, nz],
|
||||
[float(origin.X()), float(origin.Y()), float(origin.Z())],
|
||||
[mold_xmin, mold_ymin, mold_zmin],
|
||||
[mold_xmax, mold_ymax, mold_zmax],
|
||||
)):
|
||||
for i in range(3):
|
||||
n = [nx, ny, nz][i]
|
||||
o = [float(origin.X()), float(origin.Y()), float(origin.Z())][i]
|
||||
if abs(n) < 0.001:
|
||||
continue
|
||||
if n > 0:
|
||||
@@ -325,12 +323,13 @@ class BaseMoldGenerator:
|
||||
else:
|
||||
base_p1[i] = o - overlap
|
||||
|
||||
base_plate = None
|
||||
try:
|
||||
base_plate = BRepPrimAPI_MakeBox(
|
||||
gp_Pnt(base_p1[0], base_p1[1], base_p1[2]),
|
||||
gp_Pnt(base_p2[0], base_p2[1], base_p2[2])
|
||||
).Shape()
|
||||
logger.info("型芯底座平板构建完成")
|
||||
logger.info(f"型芯底座构建: 重叠量={overlap:.1f}mm")
|
||||
except Exception as e:
|
||||
logger.warning(f"底座构建失败: {e}")
|
||||
return shape
|
||||
@@ -339,21 +338,33 @@ class BaseMoldGenerator:
|
||||
fuse_op = BRepAlgoAPI_Fuse(base_plate, shape)
|
||||
if fuse_op.IsDone():
|
||||
core = fuse_op.Shape()
|
||||
logger.info("型芯(底座+产品突出体)融合成功")
|
||||
return core
|
||||
explorer = TopExp_Explorer(core, TopAbs_FACE)
|
||||
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:
|
||||
logger.warning(f"底座融合失败: {e}")
|
||||
|
||||
try:
|
||||
cut_op = BRepAlgoAPI_Cut(mold_block, shape)
|
||||
if cut_op.IsDone():
|
||||
logger.info("型芯通过布尔减回退构建")
|
||||
return cut_op.Shape()
|
||||
except Exception:
|
||||
pass
|
||||
return self._build_core_compound(base_plate, shape)
|
||||
|
||||
logger.info("型芯回退为产品形状")
|
||||
return shape
|
||||
@staticmethod
|
||||
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]:
|
||||
"""从分型面提取平面方程"""
|
||||
|
||||
Reference in New Issue
Block a user