导出step文件合并为整体,方便后续修改

This commit is contained in:
2026-05-27 14:57:03 +08:00
parent 26613676f8
commit 9882d3becc
11 changed files with 129 additions and 111 deletions
+34 -22
View File
@@ -29,6 +29,7 @@ import os
import re
from typing import Dict, List, Any, Optional, Tuple
from pathlib import Path
from OCC.Core.TopoDS import TopoDS_Shape
from utils.logger import get_logger
logger = get_logger(__name__)
@@ -70,7 +71,7 @@ class CADExporter:
relative = Path(os.path.basename(filepath))
return relative.as_posix()
def export_step(self, shape: Any, filepath: str,
def export_step(self, shape: TopoDS_Shape, filepath: str,
schema: str = "AP214") -> bool:
"""
导出 STEP 文件
@@ -118,7 +119,7 @@ class CADExporter:
logger.error(f"STEP 导出失败: {e}")
return False
def export_iges(self, shape: Any, filepath: str) -> bool:
def export_iges(self, shape: TopoDS_Shape, filepath: str) -> bool:
"""
导出 IGES 文件
@@ -156,7 +157,7 @@ class CADExporter:
logger.error(f"IGES 导出失败: {e}")
return False
def export_stl(self, shape: Any, filepath: str,
def export_stl(self, shape: TopoDS_Shape, filepath: str,
ascii_mode: bool = True,
deflection: float = 0.1) -> bool:
"""
@@ -201,7 +202,7 @@ class CADExporter:
logger.error(f"STL 导出失败: {e}")
return False
def export_brep(self, shape: Any, filepath: str) -> bool:
def export_brep(self, shape: TopoDS_Shape, filepath: str) -> bool:
"""
导出 BRep 文件(OpenCASCADE 原生格式)
@@ -240,18 +241,6 @@ class CADExporter:
components: List[str] = None,
task_id: Optional[str] = None,
scheme_id: Optional[str] = None) -> Dict[str, Any]:
"""
批量导出模具设计结果
Args:
cavity_data: generate_mold_cavities() 的返回结果
base_filename: 基础文件名(不含扩展名)
formats: 导出格式列表 ["step", "iges", "stl", "brep"]
components: 导出组件列表 ["cavity", "core", "parting_surface", "all"]
Returns:
导出结果摘要
"""
if formats is None:
formats = ["step", "stl"]
if components is None:
@@ -279,7 +268,8 @@ class CADExporter:
"parting_surface": ("parting_surface", "分型面"),
}
shapes_to_export = []
shapes_to_export: List[Tuple[str, str, TopoDS_Shape]] = []
assembly_shapes: List[Tuple[TopoDS_Shape, str]] = []
for comp in components:
if comp == "all":
@@ -287,23 +277,45 @@ class CADExporter:
shape = cavity_data.get(data_key)
if shape is not None:
shapes_to_export.append((key, label, shape))
assembly_shapes.append((shape, label))
break
elif comp in shape_map:
data_key, label = shape_map[comp]
shape = cavity_data.get(data_key)
if shape is not None:
shapes_to_export.append((comp, label, shape))
assembly_shapes.append((shape, label))
else:
results["errors"].append(f"{label}形状不可用")
# STEP: 所有组件合并为一个装配体文件
if "step" in formats and assembly_shapes:
filepath = os.path.join(export_dir, f"{base_filename}_mold.step")
success = self.export_assembly_step(assembly_shapes, filepath)
if success:
file_size = os.path.getsize(filepath)
relative_path = self.get_relative_path(filepath)
results["files"].append({
"component": "assembly",
"component_label": "模具装配体",
"format": "step",
"filepath": filepath,
"relative_path": relative_path,
"filename": os.path.basename(filepath),
"size_bytes": file_size,
"size_readable": self._format_file_size(file_size),
})
else:
results["errors"].append("装配体 STEP 导出失败")
# IGES / STL / BRep: 逐组件导出
non_assembly_formats = [f for f in formats if f != "step"]
for comp_name, label, shape in shapes_to_export:
for fmt in formats:
for fmt in non_assembly_formats:
filepath = os.path.join(export_dir, f"{base_filename}_{comp_name}.{fmt}")
success = False
if fmt == "step":
success = self.export_step(shape, filepath)
elif fmt == "iges":
if fmt == "iges":
success = self.export_iges(shape, filepath)
elif fmt == "stl":
success = self.export_stl(shape, filepath)
@@ -337,7 +349,7 @@ class CADExporter:
return results
def export_assembly_step(self, shapes_with_names: List[Tuple[Any, str]],
def export_assembly_step(self, shapes_with_names: List[Tuple[TopoDS_Shape, str]],
filepath: str,
schema: str = "AP214") -> bool:
"""