39 lines
1.5 KiB
Python
39 lines
1.5 KiB
Python
"""Shared execution validation for batch IR and stateful tools."""
|
|
import math
|
|
import re
|
|
|
|
|
|
def finite(value):
|
|
if isinstance(value, float):
|
|
return math.isfinite(value)
|
|
if isinstance(value, dict):
|
|
return all(finite(v) for v in value.values())
|
|
if isinstance(value, (list, tuple)):
|
|
return all(finite(v) for v in value)
|
|
return True
|
|
|
|
|
|
def safe_name(value):
|
|
if not isinstance(value, str) or not re.fullmatch(r"[\w-]{1,100}", value):
|
|
raise ValueError("零件名只能包含字母、数字、下划线或连字符,长度 1–100")
|
|
return value
|
|
|
|
|
|
def solid_errors(shape, expected_count=None):
|
|
try:
|
|
solids = list(shape.solids())
|
|
if not solids or not math.isfinite(shape.volume) or shape.volume <= 0:
|
|
return ["EMPTY_SOLID: 最终结果必须包含正体积实体"]
|
|
if not shape.is_valid or any(not s.is_valid for s in solids):
|
|
return ["INVALID_GEOMETRY: 实体几何无效"]
|
|
if expected_count is not None:
|
|
if isinstance(expected_count, str) and expected_count.isdigit():
|
|
expected_count = int(expected_count)
|
|
if type(expected_count) is not int or expected_count < 1:
|
|
return ["INVALID_SOLID_COUNT: 预期实体数必须是正整数"]
|
|
if len(solids) != expected_count:
|
|
return [f"SOLID_COUNT_MISMATCH: 预期 {expected_count} 个实体,实际 {len(solids)} 个"]
|
|
except Exception as exc:
|
|
return [f"INVALID_GEOMETRY: 无法读取最终实体 ({type(exc).__name__})"]
|
|
return []
|