193 lines
12 KiB
Python
193 lines
12 KiB
Python
"""Analytic checks for the six declared pilot profiles, never CAD validation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
|
|
from jsonschema import Draft202012Validator
|
|
|
|
|
|
def _path(parts):
|
|
return "$" + "".join(f"[{p}]" if isinstance(p, int) else f".{p}" for p in parts)
|
|
|
|
|
|
def _nonfinite(value, path=()):
|
|
if isinstance(value, float) and not math.isfinite(value):
|
|
yield _path(path)
|
|
elif isinstance(value, dict):
|
|
for key, item in value.items():
|
|
yield from _nonfinite(item, (*path, key))
|
|
elif isinstance(value, list):
|
|
for index, item in enumerate(value):
|
|
yield from _nonfinite(item, (*path, index))
|
|
|
|
|
|
def validate_parameters(family_id, schema, params):
|
|
result = {"family_id": family_id, "valid": False, "missing": [], "conflicts": [],
|
|
"unsupported": [], "derived": {}, "checks": [], "geometry_validated": False}
|
|
|
|
def conflict(path, message, code="relationship"):
|
|
result["conflicts"].append({"path": path, "code": code, "message": message})
|
|
|
|
for path in _nonfinite(params):
|
|
conflict(path, "Numbers must be finite", "nonfinite")
|
|
if result["conflicts"]:
|
|
return result
|
|
for error in sorted(Draft202012Validator(schema).iter_errors(params), key=lambda e: str(list(e.path))):
|
|
group = "missing" if error.validator == "required" else "unsupported" if error.validator in {"enum", "const", "additionalProperties"} else "conflicts"
|
|
result[group].append({"path": _path(error.path), "code": error.validator, "message": error.message})
|
|
if any(result[key] for key in ("missing", "conflicts", "unsupported")):
|
|
return result
|
|
|
|
def holes_in_plate(holes, length, width, margin=0, path="$.holes"):
|
|
for index, hole in enumerate(holes):
|
|
edge_clearance = min(length / 2 - abs(hole["x"]) - hole["diameter"] / 2,
|
|
width / 2 - abs(hole["y"]) - hole["diameter"] / 2)
|
|
if edge_clearance <= 0 or edge_clearance < margin:
|
|
conflict(f"{path}[{index}]", "Hole exceeds the plate or required edge margin")
|
|
for previous in holes[:index]:
|
|
distance = math.hypot(hole["x"] - previous["x"], hole["y"] - previous["y"])
|
|
if distance <= (hole["diameter"] + previous["diameter"]) / 2 + margin:
|
|
conflict(f"{path}[{index}]", "Holes touch/overlap or violate the required ligament")
|
|
|
|
p, d = params, result["derived"]
|
|
if family_id == "mounting-plate":
|
|
if ("holes" in p) == ("hole_pattern" in p):
|
|
result["missing" if "holes" not in p else "conflicts"].append(
|
|
{"path": "$", "code": "hole_layout", "message": "Provide exactly one of holes or hole_pattern"})
|
|
return result
|
|
if "holes" in p:
|
|
holes = p["holes"]
|
|
else:
|
|
pattern = p["hole_pattern"]
|
|
if pattern["rows"] * pattern["columns"] > 256:
|
|
conflict("$.hole_pattern", "At most 256 holes are supported", "capacity")
|
|
return result
|
|
holes = [{"x": pattern["origin_x"] + col * pattern["pitch_x"],
|
|
"y": pattern["origin_y"] + row * pattern["pitch_y"],
|
|
"diameter": pattern["diameter"]}
|
|
for row in range(pattern["rows"]) for col in range(pattern["columns"])]
|
|
holes_in_plate(holes, p["length"], p["width"])
|
|
d.update(holes=holes, hole_count=len(holes),
|
|
volume=p["length"] * p["width"] * p["thickness"] -
|
|
sum(math.pi / 4 * h["diameter"] ** 2 * p["thickness"] for h in holes))
|
|
result["checks"] = ["hole_edge_clearance", "hole_pair_separation", "analytic_volume"]
|
|
elif family_id == "stepped-shaft":
|
|
offset, positions = 0, []
|
|
for segment in p["segments"]:
|
|
positions.append({"start_z": offset, "end_z": offset + segment["length"]})
|
|
offset += segment["length"]
|
|
d.update(total_length=offset, max_diameter=max(s["diameter"] for s in p["segments"]),
|
|
segment_positions=positions,
|
|
volume=sum(math.pi / 4 * s["diameter"] ** 2 * s["length"] for s in p["segments"]))
|
|
if len({s["diameter"] for s in p["segments"]}) == 1:
|
|
conflict("$.segments", "A stepped shaft needs at least two different diameters")
|
|
result["checks"] = ["continuous_axial_chain", "distinct_step", "analytic_volume"]
|
|
elif family_id == "hex-head-bolt":
|
|
if p["across_flats"] <= p["nominal_diameter"]:
|
|
conflict("$.across_flats", "Head across-flats must exceed shank diameter in this pilot")
|
|
if p["thread_length"] > p["under_head_length"]:
|
|
conflict("$.thread_length", "Thread length exceeds under-head length")
|
|
d.update(total_length=p["under_head_length"] + p["head_height"],
|
|
head_circumdiameter=2 * p["across_flats"] / math.sqrt(3),
|
|
volume=math.pi / 4 * p["nominal_diameter"] ** 2 * p["under_head_length"] +
|
|
math.sqrt(3) / 2 * p["across_flats"] ** 2 * p["head_height"])
|
|
result["checks"] = ["head_shank_relation", "thread_extent", "simplified_volume"]
|
|
elif family_id == "ribbed-bearing-housing":
|
|
wall = p["min_wall"]
|
|
if p["base_thickness"] < wall:
|
|
conflict("$.base_thickness", "Base thickness is below requested minimum wall")
|
|
if p["boss_outer_diameter"] - p["bore_diameter"] < 2 * wall:
|
|
conflict("$.bore_diameter", "Insufficient radial wall around bearing bore")
|
|
if p["center_height"] - p["bore_diameter"] / 2 < p["base_thickness"] + wall:
|
|
conflict("$.center_height", "Bore breaks into base or violates its required ligament")
|
|
if not (p["base_thickness"] < p["center_height"] and 0 < p["pedestal_width"] < p["boss_outer_diameter"]):
|
|
conflict("$.pedestal_width", "Pedestal must rise from base to bore centre and intersect boss")
|
|
if p["boss_outer_diameter"] > p["base_length"] or p["boss_width"] > p["base_width"]:
|
|
conflict("$.boss_outer_diameter", "Boss footprint exceeds pilot base envelope")
|
|
if p["pedestal_width"] / 2 + p["rib_reach"] > p["base_length"] / 2 - wall:
|
|
conflict("$.rib_reach", "Rib reaches beyond base margin")
|
|
if p["rib_thickness"] > p["boss_width"] or p["rib_thickness"] < wall:
|
|
conflict("$.rib_thickness", "Rib thickness is outside the declared profile")
|
|
if p["base_thickness"] + p["rib_height"] > p["center_height"]:
|
|
conflict("$.rib_height", "Rib attachment extends above pedestal")
|
|
if p["pedestal_width"] / 2 < p["bore_diameter"] / 2 + wall:
|
|
conflict("$.pedestal_width", "Rib attachment lies too close to bore")
|
|
holes_in_plate(p["mount_holes"], p["base_length"], p["base_width"], wall, "$.mount_holes")
|
|
for i, hole in enumerate(p["mount_holes"]):
|
|
# Conservative plan envelope of pedestal and the two symmetric ribs.
|
|
rectangles = [(p["pedestal_width"] / 2, p["boss_width"] / 2),
|
|
(p["pedestal_width"] / 2 + p["rib_reach"], p["rib_thickness"] / 2)]
|
|
if any(math.hypot(max(abs(hole["x"]) - rx, 0), max(abs(hole["y"]) - ry, 0)) < hole["diameter"] / 2 + wall
|
|
for rx, ry in rectangles):
|
|
conflict(f"$.mount_holes[{i}]", "Mounting hole conflicts with support/rib footprint")
|
|
if "counterbore" in p:
|
|
c = p["counterbore"]
|
|
if not p["bore_diameter"] < c["diameter"] <= p["boss_outer_diameter"] - 2 * wall:
|
|
conflict("$.counterbore.diameter", "Counterbore must enlarge bore while retaining radial wall")
|
|
if c["depth"] >= p["boss_width"]:
|
|
conflict("$.counterbore.depth", "Counterbore eliminates the locating shoulder")
|
|
if p["center_height"] - c["diameter"] / 2 < p["base_thickness"] + wall:
|
|
conflict("$.counterbore.diameter", "Counterbore violates the base ligament")
|
|
d.update(total_height=max(p["base_thickness"], p["center_height"] + p["boss_outer_diameter"] / 2),
|
|
radial_wall=(p["boss_outer_diameter"] - p["bore_diameter"]) / 2,
|
|
bore_bottom=p["center_height"] - p["bore_diameter"] / 2,
|
|
rib_count=2, mounting_hole_count=len(p["mount_holes"]))
|
|
result["checks"] = ["declared_axes", "boss_wall", "pedestal_rib_attachment", "mounting_access"]
|
|
elif family_id == "involute-spur-gear":
|
|
module, teeth = p["module"], p["tooth_count"]
|
|
pitch = module * teeth
|
|
tip, root = module * (teeth + 2), module * (teeth - 2.5)
|
|
base = pitch * math.cos(math.radians(p["pressure_angle"]))
|
|
if p["hub_diameter"] > root:
|
|
conflict("$.hub_diameter", "Hub extends beyond root circle in this pilot")
|
|
if p["keyway_width"] >= p["bore_diameter"]:
|
|
conflict("$.keyway_width", "Keyway must open into the bore in this pilot")
|
|
corner_radius = math.hypot(p["bore_diameter"] / 2 + p["keyway_depth"], p["keyway_width"] / 2)
|
|
if corner_radius + p["min_wall"] >= min(p["hub_diameter"], root) / 2:
|
|
conflict("$.keyway_depth", "Bore/keyway breaks through hub or root ligament")
|
|
d.update(pitch_diameter=pitch, addendum_diameter=tip, root_diameter=root, base_diameter=base,
|
|
angular_pitch_deg=360 / teeth, tooth_thickness_at_pitch=math.pi * module / 2,
|
|
involute_start_radius=max(base, root) / 2,
|
|
involute_tip_parameter=math.sqrt((tip / base) ** 2 - 1),
|
|
total_length=p["face_width"] + p["hub_length"])
|
|
result["checks"] = ["reference_tooth_system", "circle_dependencies", "bore_keyway_ligament", "declared_root_simplification"]
|
|
elif family_id == "covered-thin-wall-enclosure":
|
|
inner_l = p["outer_length"] - 2 * p["wall_thickness"]
|
|
inner_w = p["outer_width"] - 2 * p["wall_thickness"]
|
|
inner_h = p["outer_height"] - p["bottom_thickness"]
|
|
if min(inner_l, inner_w, inner_h) <= 0:
|
|
conflict("$.wall_thickness", "Wall/bottom thickness eliminates the cavity")
|
|
radius = p["boss_outer_diameter"] / 2
|
|
x = p["outer_length"] / 2 - p["fastener_inset"]
|
|
y = p["outer_width"] / 2 - p["fastener_inset"]
|
|
if x <= radius or y <= radius:
|
|
conflict("$.fastener_inset", "Four corner bosses overlap or collapse to one axis")
|
|
if x + radius >= inner_l / 2 or y + radius >= inner_w / 2:
|
|
conflict("$.boss_outer_diameter", "Bosses must fit inside cavity; ribs connect them to walls")
|
|
if (p["boss_outer_diameter"] - p["boss_pilot_diameter"]) / 2 < p["min_wall"]:
|
|
conflict("$.boss_pilot_diameter", "Insufficient boss wall")
|
|
if not p["boss_pilot_diameter"] < p["cover_hole_diameter"] < p["boss_outer_diameter"]:
|
|
conflict("$.cover_hole_diameter", "Cover clearance hole must exceed pilot and fit over boss")
|
|
if p["fastener_inset"] - p["cover_hole_diameter"] / 2 < p["min_wall"]:
|
|
conflict("$.cover_hole_diameter", "Insufficient cover edge margin")
|
|
if p["boss_height"] > inner_h:
|
|
conflict("$.boss_height", "Boss protrudes above shell rim")
|
|
if p["pilot_depth"] > p["boss_height"] - p["min_wall"]:
|
|
conflict("$.pilot_depth", "Blind pilot removes the required bottom ligament")
|
|
if p["rib_height"] > p["boss_height"] or p["rib_thickness"] >= p["boss_outer_diameter"]:
|
|
conflict("$.rib_height", "Ribs must attach within boss height and diameter")
|
|
if min(p["wall_thickness"], p["bottom_thickness"], p["lid_thickness"], p["rib_thickness"]) < p["min_wall"]:
|
|
conflict("$.min_wall", "One of the declared walls is below the requested minimum")
|
|
centres = [{"x": sx * x, "y": sy * y} for sy in (-1, 1) for sx in (-1, 1)]
|
|
d.update(inner_length=inner_l, inner_width=inner_w, inner_height=inner_h,
|
|
fastener_centers=centres, cover_bottom_z=p["outer_height"] + p["assembly_gap"],
|
|
total_height=p["outer_height"] + p["assembly_gap"] + p["lid_thickness"], part_count=2)
|
|
result["checks"] = ["positive_cavity", "four_boss_spacing", "shared_interface_centers", "blind_hole_ligament", "two_separate_parts"]
|
|
else:
|
|
raise ValueError(f"No pilot validator registered: {family_id}")
|
|
for path in _nonfinite(d):
|
|
conflict(path, "Derived value is non-finite", "nonfinite_derived")
|
|
result["valid"] = not any(result[key] for key in ("missing", "conflicts", "unsupported"))
|
|
return result
|