重构路由
This commit is contained in:
@@ -461,7 +461,7 @@ class AluminumFoamMoldGenerator:
|
||||
|
||||
try:
|
||||
parting_surface = BRepBuilderAPI_MakeFace(parting_plane).Face()
|
||||
except:
|
||||
except Exception:
|
||||
# 回退到默认平面
|
||||
parting_plane = gp_Pln(gp_Pnt(0, 0, center[2]), gp_Dir(0, 0, 1))
|
||||
parting_surface = BRepBuilderAPI_MakeFace(parting_plane).Face()
|
||||
@@ -488,7 +488,7 @@ class AluminumFoamMoldGenerator:
|
||||
"direction": [1, 0, 0],
|
||||
"reason": "产品扁平,需要垂直分型"
|
||||
})
|
||||
except:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {
|
||||
@@ -585,7 +585,7 @@ class AluminumFoamMoldGenerator:
|
||||
param = first_param + i * step
|
||||
point = curve.Value(param)
|
||||
edges.append([point.X(), point.Y(), point.Z()])
|
||||
except:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
explorer.Next()
|
||||
@@ -615,8 +615,9 @@ class AluminumFoamMoldGenerator:
|
||||
[xmin, ymax, center_z],
|
||||
[xmin, ymin, center_z]
|
||||
]
|
||||
except:
|
||||
return [[0, 0, 0], [100, 0, 0], [100, 100, 0], [0, 100, 0], [0, 0, 0]]
|
||||
except Exception:
|
||||
logger.warning("分型线简化计算失败,返回空列表")
|
||||
return []
|
||||
|
||||
def _smooth_parting_line(self, parting_line: List[List[float]]) -> List[List[float]]:
|
||||
"""
|
||||
@@ -678,7 +679,7 @@ class AluminumFoamMoldGenerator:
|
||||
|
||||
return 50.0
|
||||
|
||||
except:
|
||||
except Exception:
|
||||
return 50.0
|
||||
|
||||
def _apply_shrinkage_compensation(self, shape: Any) -> Any:
|
||||
@@ -850,7 +851,7 @@ class AluminumFoamMoldGenerator:
|
||||
"origin": [0, 0, 0],
|
||||
"bounds": {"u_range": [-200, 200], "v_range": [-200, 200]}
|
||||
}
|
||||
except:
|
||||
except Exception:
|
||||
return {
|
||||
"type": "plane",
|
||||
"normal": [0, 0, 1],
|
||||
@@ -870,7 +871,7 @@ class AluminumFoamMoldGenerator:
|
||||
|
||||
try:
|
||||
parting_surface = BRepBuilderAPI_MakeFace(parting_plane).Face()
|
||||
except:
|
||||
except Exception:
|
||||
parting_plane = gp_Pln(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))
|
||||
parting_surface = BRepBuilderAPI_MakeFace(parting_plane).Face()
|
||||
|
||||
@@ -930,7 +931,7 @@ class AluminumFoamMoldGenerator:
|
||||
avg_thickness = (volume / surface_area) * 0.6
|
||||
return f"{avg_thickness * 0.7:.2f} - {avg_thickness * 1.3:.2f} mm"
|
||||
|
||||
return "10.0 - 30.0 mm (铝泡沫典型)"
|
||||
return "无法估算 (缺少几何数据)"
|
||||
|
||||
def _calculate_complexity_score(self, analysis: Dict) -> float:
|
||||
"""计算复杂度评分"""
|
||||
@@ -942,7 +943,7 @@ class AluminumFoamMoldGenerator:
|
||||
complexity = min(thickness_ratio / 5.0, 10.0)
|
||||
return round(complexity, 1)
|
||||
|
||||
return 5.0
|
||||
return 0.0
|
||||
|
||||
def _estimate_cycle_time(self, analysis: Dict) -> str:
|
||||
"""估算成型周期"""
|
||||
@@ -963,7 +964,7 @@ class AluminumFoamMoldGenerator:
|
||||
|
||||
def _assess_warpage_risk(self, analysis: Dict) -> str:
|
||||
"""评估翘曲风险"""
|
||||
bbox = analysis.get("bounding_box", {}).get("dimensions", [1, 1, 1])
|
||||
bbox = analysis.get("bounding_box", {}).get("dimensions", [0, 0, 0])
|
||||
aspect_ratio = max(bbox) / min(bbox)
|
||||
|
||||
if aspect_ratio > 5:
|
||||
|
||||
@@ -135,7 +135,7 @@ class GeometryAnalyzer:
|
||||
elif volume > 0:
|
||||
# 如果没有surface_area,基于边界框估算壁厚
|
||||
bbox = geometry_data.get("bounding_box", {})
|
||||
dimensions = bbox.get("dimensions", [100, 100, 100])
|
||||
dimensions = bbox.get("dimensions", [0, 0, 0])
|
||||
bbox_volume = dimensions[0] * dimensions[1] * dimensions[2]
|
||||
if bbox_volume > 0:
|
||||
volume_efficiency = volume / bbox_volume
|
||||
@@ -144,7 +144,7 @@ class GeometryAnalyzer:
|
||||
features.append(create_mold_feature(
|
||||
feature_type="thin_wall",
|
||||
confidence=0.7,
|
||||
location=bbox.get("center", [50, 50, 50]),
|
||||
location=bbox.get("center", [0, 0, 0]),
|
||||
dimensions=[avg_thickness, avg_thickness, avg_thickness],
|
||||
parameters={"average_thickness": avg_thickness, "estimation_method": "bbox_based"},
|
||||
recommendations=[
|
||||
@@ -187,14 +187,14 @@ class GeometryAnalyzer:
|
||||
volume = geometry_data.get("volume", 0)
|
||||
bbox = geometry_data.get("bounding_box", {})
|
||||
|
||||
dimensions = bbox.get("dimensions", [100, 100, 100])
|
||||
volume_efficiency = volume / (dimensions[0] * dimensions[1] * dimensions[2])
|
||||
dimensions = bbox.get("dimensions", [0, 0, 0])
|
||||
volume_efficiency = volume / (dimensions[0] * dimensions[1] * dimensions[2]) if all(d > 0 for d in dimensions) else 0
|
||||
|
||||
if volume_efficiency < 0.3:
|
||||
features.append(create_mold_feature(
|
||||
feature_type="boss_feature",
|
||||
confidence=0.65,
|
||||
location=bbox.get("center", [50, 50, 50]),
|
||||
location=bbox.get("center", [0, 0, 0]),
|
||||
dimensions=[6.0, 12.0, 6.0],
|
||||
parameters={"volume_efficiency": volume_efficiency},
|
||||
recommendations=[
|
||||
@@ -293,7 +293,7 @@ class GeometryAnalyzer:
|
||||
|
||||
# 体积利用率
|
||||
bbox = geometry_data.get("bounding_box", {})
|
||||
dimensions = bbox.get("dimensions", [100, 100, 100])
|
||||
dimensions = bbox.get("dimensions", [0, 0, 0])
|
||||
volume = geometry_data.get("volume", 0)
|
||||
bbox_volume = dimensions[0] * dimensions[1] * dimensions[2]
|
||||
|
||||
|
||||
+17
-15
@@ -471,13 +471,6 @@ class MoldCavityGenerator:
|
||||
"bounds": bounds
|
||||
}
|
||||
|
||||
return {
|
||||
"type": "plane",
|
||||
"normal": [0, 0, 1],
|
||||
"origin": [0, 0, 0],
|
||||
"bounds": bounds
|
||||
}
|
||||
|
||||
def _calculate_mold_size(self, analysis: Dict) -> Dict[str, float]:
|
||||
"""估算模具尺寸"""
|
||||
product_bbox = analysis["bounding_box"]["dimensions"]
|
||||
@@ -531,14 +524,14 @@ class MoldCavityGenerator:
|
||||
return f"{avg_thickness * 0.7:.2f} - {avg_thickness * 1.3:.2f} mm"
|
||||
elif volume > 0:
|
||||
# 如果没有surface_area,基于体积估算
|
||||
bbox_dims = analysis.get("bounding_box", {}).get("dimensions", [1, 1, 1])
|
||||
bbox_dims = analysis.get("bounding_box", {}).get("dimensions", [0, 0, 0])
|
||||
bbox_volume = bbox_dims[0] * bbox_dims[1] * bbox_dims[2]
|
||||
if bbox_volume > 0:
|
||||
efficiency = volume / bbox_volume
|
||||
avg_thickness = (bbox_dims[0] + bbox_dims[1]) / 2 * efficiency
|
||||
return f"{avg_thickness * 0.7:.2f} - {avg_thickness * 1.3:.2f} mm"
|
||||
|
||||
return "2.0 - 4.0 mm (默认)"
|
||||
return "无法估算 (缺少几何数据)"
|
||||
|
||||
def _calculate_complexity_score(self, analysis: Dict) -> float:
|
||||
"""计算复杂度评分(0-10)"""
|
||||
@@ -552,14 +545,14 @@ class MoldCavityGenerator:
|
||||
return round(complexity, 1)
|
||||
elif volume > 0:
|
||||
# 如果没有surface_area,基于拓扑复杂度评分
|
||||
bbox_dims = analysis.get("bounding_box", {}).get("dimensions", [100, 100, 100])
|
||||
bbox_dims = analysis.get("bounding_box", {}).get("dimensions", [0, 0, 0])
|
||||
bbox_volume = bbox_dims[0] * bbox_dims[1] * bbox_dims[2]
|
||||
if bbox_volume > 0:
|
||||
volume_ratio = volume / bbox_volume
|
||||
complexity = (1.0 - volume_ratio) * 10
|
||||
return round(min(max(complexity, 0), 10), 1)
|
||||
|
||||
return 5.0
|
||||
return 0.0
|
||||
|
||||
def _estimate_cycle_time(self, analysis: Dict) -> str:
|
||||
"""估算成型周期"""
|
||||
@@ -594,7 +587,7 @@ class MoldCavityGenerator:
|
||||
|
||||
def _assess_warpage_risk(self, analysis: Dict) -> str:
|
||||
"""评估翘曲风险"""
|
||||
bbox = analysis.get("bounding_box", {}).get("dimensions", [1, 1, 1])
|
||||
bbox = analysis.get("bounding_box", {}).get("dimensions", [0, 0, 0])
|
||||
aspect_ratio = max(bbox) / min(bbox)
|
||||
|
||||
if aspect_ratio > 5:
|
||||
@@ -715,6 +708,15 @@ class MoldCavityGenerator:
|
||||
|
||||
使用 BRepAlgoAPI_Section 进行布尔运算求交
|
||||
"""
|
||||
# 获取实际边界框作为回退
|
||||
try:
|
||||
bbox_fallback = Bnd_Box()
|
||||
brepbndlib.Add(shape, bbox_fallback)
|
||||
xmin, ymin, zmin, xmax, ymax, zmax = bbox_fallback.Get()
|
||||
fallback_bbox = {"bounding_box": {"min": [xmin, ymin, zmin], "max": [xmax, ymax, zmax], "center": [(xmin+xmax)/2, (ymin+ymax)/2, (zmin+zmax)/2]}}
|
||||
except Exception:
|
||||
fallback_bbox = {"bounding_box": {"min": [0, 0, 0], "max": [0, 0, 0], "center": [0, 0, 0]}}
|
||||
|
||||
try:
|
||||
# 创建截面运算
|
||||
section = BRepAlgoAPI_Section(shape, parting_surface)
|
||||
@@ -724,7 +726,7 @@ class MoldCavityGenerator:
|
||||
logger.warning("截面运算未完成,使用简化分型线")
|
||||
return self._simple_parting_line(
|
||||
parting_surface,
|
||||
{"bounding_box": {"min": [-50, -50, 0], "max": [50, 50, 100]}}
|
||||
fallback_bbox
|
||||
)
|
||||
|
||||
# 提取交线(边)
|
||||
@@ -755,7 +757,7 @@ class MoldCavityGenerator:
|
||||
logger.warning("未找到交线,使用简化分型线")
|
||||
return self._simple_parting_line(
|
||||
parting_surface,
|
||||
{"bounding_box": {"min": [-50, -50, 0], "max": [50, 50, 100]}}
|
||||
fallback_bbox
|
||||
)
|
||||
|
||||
logger.info(f"计算得到 {len(edges)} 个分型线点")
|
||||
@@ -765,7 +767,7 @@ class MoldCavityGenerator:
|
||||
logger.error(f"分型线计算失败:{e}")
|
||||
return self._simple_parting_line(
|
||||
parting_surface,
|
||||
{"bounding_box": {"min": [-50, -50, 0], "max": [50, 50, 100]}}
|
||||
fallback_bbox
|
||||
)
|
||||
|
||||
def _simple_parting_surface(self, analysis: Dict) -> Tuple[Any, List]:
|
||||
|
||||
+26
-30
@@ -133,10 +133,13 @@ class STPParser:
|
||||
|
||||
props = GProp_GProps()
|
||||
brepgprop.VolumeProperties(shape, props)
|
||||
return props.Mass()
|
||||
volume = props.Mass()
|
||||
if volume <= 0:
|
||||
raise ValueError("计算得到的体积为0或负数,形状可能无效")
|
||||
return volume
|
||||
except Exception as e:
|
||||
logger.error(f"体积计算失败: {e}")
|
||||
return 1000000.0
|
||||
raise RuntimeError(f"体积计算失败: {e}") from e
|
||||
|
||||
def _compute_surface_area(self, shape) -> float:
|
||||
"""计算表面积"""
|
||||
@@ -147,7 +150,6 @@ class STPParser:
|
||||
props = GProp_GProps()
|
||||
brepgprop.SurfaceProperties(shape, props)
|
||||
area = props.Mass()
|
||||
logger.info(f"表面积计算成功: {area:.2f} mm²")
|
||||
|
||||
# 如果计算结果为0,使用备选估算方法
|
||||
if area <= 0:
|
||||
@@ -155,18 +157,23 @@ class STPParser:
|
||||
raise ValueError("Surface area is zero")
|
||||
|
||||
return area
|
||||
except Exception as e:
|
||||
logger.error(f"表面积计算失败: {e}")
|
||||
except ValueError:
|
||||
# 基于边界框估算表面积
|
||||
try:
|
||||
bbox = self._compute_bounding_box(shape)
|
||||
dims = bbox.get("dimensions", [100, 100, 100])
|
||||
dims = bbox.get("dimensions", [0, 0, 0])
|
||||
if any(d <= 0 for d in dims):
|
||||
raise RuntimeError("边界框尺寸无效,无法估算表面积")
|
||||
# 简化的估算公式:2*(lw + lh + wh)
|
||||
estimated_area = 2 * (dims[0]*dims[1] + dims[0]*dims[2] + dims[1]*dims[2])
|
||||
logger.warning(f"使用边界框估算表面积: {estimated_area:.2f} mm²")
|
||||
return estimated_area
|
||||
except:
|
||||
return 60000.0
|
||||
except Exception as e:
|
||||
logger.error(f"表面积估算失败: {e}")
|
||||
raise RuntimeError(f"表面积计算失败: {e}") from e
|
||||
except Exception as e:
|
||||
logger.error(f"表面积计算失败: {e}")
|
||||
raise RuntimeError(f"表面积计算失败: {e}") from e
|
||||
|
||||
def _compute_center_of_mass(self, shape) -> List[float]:
|
||||
"""计算质心"""
|
||||
@@ -180,7 +187,12 @@ class STPParser:
|
||||
return [float(center.X()), float(center.Y()), float(center.Z())]
|
||||
except Exception as e:
|
||||
logger.error(f"质心计算失败: {e}")
|
||||
return [0.0, 0.0, 0.0]
|
||||
# 回退到边界框中心
|
||||
try:
|
||||
bbox = self._compute_bounding_box(shape)
|
||||
return bbox.get("center", [0.0, 0.0, 0.0])
|
||||
except Exception:
|
||||
raise RuntimeError(f"质心计算失败且边界框回退也失败: {e}") from e
|
||||
|
||||
def _compute_inertia_properties(self, shape) -> Dict[str, Any]:
|
||||
"""计算惯性属性"""
|
||||
@@ -227,30 +239,14 @@ class STPParser:
|
||||
logger.error(f"拓扑分析失败: {e}")
|
||||
raise
|
||||
|
||||
def _create_dummy_shape(self):
|
||||
"""创建虚拟形状"""
|
||||
return "dummy_shape"
|
||||
|
||||
def _simulate_analysis(self) -> Dict[str, Any]:
|
||||
"""模拟分析结果"""
|
||||
logger.info("使用模拟分析数据")
|
||||
return {
|
||||
"bounding_box": self._default_bounding_box(),
|
||||
"volume": 1000000.0,
|
||||
"surface_area": 60000.0,
|
||||
"topology": {"faces": 6, "edges": 12, "vertices": 8},
|
||||
"center_of_mass": [50.0, 50.0, 50.0],
|
||||
"inertia_properties": {},
|
||||
"analysis_method": "simulated"
|
||||
}
|
||||
|
||||
def _default_bounding_box(self) -> Dict[str, Any]:
|
||||
"""默认边界框"""
|
||||
"""默认边界框(边界框计算失败时的回退值,标注为估算)"""
|
||||
return {
|
||||
"min": [0.0, 0.0, 0.0],
|
||||
"max": [100.0, 100.0, 100.0],
|
||||
"dimensions": [100.0, 100.0, 100.0],
|
||||
"center": [50.0, 50.0, 50.0]
|
||||
"max": [0.0, 0.0, 0.0],
|
||||
"dimensions": [0.0, 0.0, 0.0],
|
||||
"center": [0.0, 0.0, 0.0],
|
||||
"estimated": True
|
||||
}
|
||||
|
||||
def export_to_json(self, geometry_data: Dict[str, Any], output_path: Path) -> str:
|
||||
|
||||
Reference in New Issue
Block a user