This commit is contained in:
2026-06-12 13:55:38 +08:00
parent c4616cbbee
commit f71a48d3b8
3 changed files with 41 additions and 8 deletions
+24 -8
View File
@@ -85,6 +85,18 @@ class BaseMoldGenerator:
except Exception: except Exception:
return gp_Dir(0, 0, 1) return gp_Dir(0, 0, 1)
@staticmethod
def _create_mold_block(analysis: Dict, margin: float = 30.0) -> TopoDS_Shape:
"""基于产品边界框创建模具包围盒(用于切分 A/B 半模等可视化用途)。"""
bbox = analysis.get("bounding_box", {})
dims = bbox.get("dimensions", [100, 100, 100])
center = bbox.get("center", [0, 0, 0])
half = [d / 2 + margin for d in dims]
return BRepPrimAPI_MakeBox(
gp_Pnt(center[0] - half[0], center[1] - half[1], center[2] - half[2]),
gp_Pnt(center[0] + half[0], center[1] + half[1], center[2] + half[2]),
).Shape()
def _find_draftable_faces(self, shape: TopoDS_Shape, draft_direction: gp_Dir) -> List[TopoDS_Face]: def _find_draftable_faces(self, shape: TopoDS_Shape, draft_direction: gp_Dir) -> List[TopoDS_Face]:
draftable = [] draftable = []
explorer = TopExp_Explorer(shape, TopAbs_FACE) explorer = TopExp_Explorer(shape, TopAbs_FACE)
@@ -257,7 +269,8 @@ class BaseMoldGenerator:
cavity = self._subtract_product_from_plate(mold_block, shape, "型腔") cavity = self._subtract_product_from_plate(mold_block, shape, "型腔")
if cavity is None: if cavity is None:
cavity = mold_block logger.error("型腔布尔减运算失败,无法生成有效型腔几何")
raise RuntimeError("型腔布尔减运算失败:无法从模具块中减去产品形状")
core = self._build_core_with_base( core = self._build_core_with_base(
shape, mold_block, parting_plane, shape, mold_block, parting_plane,
@@ -440,8 +453,8 @@ class BaseMoldGenerator:
return None, None return None, None
def _subtract_product_from_plate(self, plate: TopoDS_Shape, product: TopoDS_Shape, def _subtract_product_from_plate(self, plate: TopoDS_Shape, product: TopoDS_Shape,
plate_name: str) -> TopoDS_Shape: plate_name: str) -> Optional[TopoDS_Shape]:
"""从模板中减去产品形状,生成型腔或型芯""" """从模板中减去产品形状,生成型腔或型芯。失败时返回 None 而非静默返回纯方块。"""
try: try:
cut_op = BRepAlgoAPI_Cut(plate, product) cut_op = BRepAlgoAPI_Cut(plate, product)
if cut_op.IsDone(): if cut_op.IsDone():
@@ -449,11 +462,11 @@ class BaseMoldGenerator:
logger.info(f"{plate_name}减去产品成功") logger.info(f"{plate_name}减去产品成功")
return result return result
else: else:
logger.warning(f"{plate_name}布尔减运算失败") logger.warning(f"{plate_name}布尔减运算失败,返回 None 而非纯方块")
return plate return None
except Exception as e: except Exception as e:
logger.warning(f"{plate_name}减产品失败: {e}") logger.warning(f"{plate_name}减产品失败: {e}")
return plate return None
def _split_cavity_core_fallback(self, shape: TopoDS_Shape, def _split_cavity_core_fallback(self, shape: TopoDS_Shape,
mold_block: Optional[TopoDS_Shape] = None) -> Tuple[TopoDS_Shape, TopoDS_Shape]: mold_block: Optional[TopoDS_Shape] = None) -> Tuple[TopoDS_Shape, TopoDS_Shape]:
@@ -476,7 +489,8 @@ class BaseMoldGenerator:
cavity = self._subtract_product_from_plate(mold_block, shape, "型腔(回退)") cavity = self._subtract_product_from_plate(mold_block, shape, "型腔(回退)")
if cavity is None: if cavity is None:
cavity = mold_block logger.error("回退方案型腔布尔减运算也失败")
raise RuntimeError("回退方案型腔布尔减运算失败")
center_z = (zmin + zmax) / 2 center_z = (zmin + zmax) / 2
parting_plane = gp_Pln(gp_Pnt(0, 0, center_z), gp_Dir(0, 0, 1)) parting_plane = gp_Pln(gp_Pnt(0, 0, center_z), gp_Dir(0, 0, 1))
@@ -503,7 +517,9 @@ class BaseMoldGenerator:
gp_Pnt(xmax + margin, ymax + margin, zmax + margin) gp_Pnt(xmax + margin, ymax + margin, zmax + margin)
).Shape() ).Shape()
cavity = self._subtract_product_from_plate(cavity_block, shape, "型腔(兜底)") cavity = self._subtract_product_from_plate(cavity_block, shape, "型腔(兜底)")
return cavity or cavity_block, shape if cavity is None:
raise RuntimeError("兜底型腔布尔减运算失败")
return cavity, shape
except Exception: except Exception:
return shape, shape return shape, shape
+3
View File
@@ -266,6 +266,9 @@ class CADExporter:
"cavity": ("cavity", "型腔"), "cavity": ("cavity", "型腔"),
"core": ("core", "型芯"), "core": ("core", "型芯"),
"parting_surface": ("parting_surface", "分型面"), "parting_surface": ("parting_surface", "分型面"),
"product": ("product", "产品本体"),
"a_plate": ("a_plate", "A板(上模)"),
"b_plate": ("b_plate", "B板(下模)"),
} }
shapes_to_export: List[Tuple[str, str, TopoDS_Shape]] = [] shapes_to_export: List[Tuple[str, str, TopoDS_Shape]] = []
@@ -161,6 +161,17 @@ class MultiSchemeMoldPlanner:
cavity_data["metadata"]["core_required"] = mold_structure["core_required"] cavity_data["metadata"]["core_required"] = mold_structure["core_required"]
cavity_data["metadata"]["structure_decision_reason"] = mold_structure["decision_reason"] cavity_data["metadata"]["structure_decision_reason"] = mold_structure["decision_reason"]
# 生成可视化辅助形状:A/B 半模(分型面切分)、产品本体
a_plate, b_plate, product_for_export = None, None, scaled_shape
try:
mold_block_shape = generator._create_mold_block(analysis, margin=30.0)
a_plate, b_plate = generator._split_mold_block_by_plane(
mold_block_shape,
generator._get_parting_plane(parting_surface, shape),
)
except Exception as exc:
logger.debug(f"A/B 半模生成失败(不影响主流程): {exc}")
return { return {
"scheme_id": candidate["scheme_id"], "scheme_id": candidate["scheme_id"],
"method": candidate["method"], "method": candidate["method"],
@@ -188,6 +199,9 @@ class MultiSchemeMoldPlanner:
"cavity": cavity, "cavity": cavity,
"core": core, "core": core,
"parting_surface": parting_surface, "parting_surface": parting_surface,
"product": product_for_export,
"a_plate": a_plate,
"b_plate": b_plate,
}, },
} }