File size: 2,500 Bytes
5154c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations

import re
from pathlib import Path

import cadquery as cq


OUTPUT_ROOT = Path(r"C:\Users\User\Desktop")
STEP_NAME = "cli_027_mass_reduced_out.step"
TEXT_NAME = "cli_027_massprops.txt"
HOLES = [(-30.0, -20.0), (30.0, -20.0), (-30.0, 20.0), (30.0, 20.0)]
TOL = 0.08
DENSITY_G_CM3 = 2.70


def load_single_solid(path: Path):
    if not path.exists() or path.stat().st_size <= 0:
        raise FileNotFoundError(path)
    solids = cq.importers.importStep(str(path)).solids().vals()
    if len(solids) != 1 or not solids[0].isValid():
        raise ValueError("expected one valid bracket solid")
    return solids[0]


def inside(solid, x: float, y: float, z: float) -> bool:
    return bool(solid.isInside(cq.Vector(x, y, z), 1e-5))


def check_bbox(solid) -> bool:
    bb = solid.BoundingBox()
    actual = (bb.xmin, bb.xmax, bb.ymin, bb.ymax, bb.zmin, bb.zmax)
    expected = (-50.0, 50.0, -30.0, 30.0, 0.0, 40.0)
    return all(abs(a - e) <= TOL for a, e in zip(actual, expected))


def check_holes_and_rib(solid) -> bool:
    for cx, cy in HOLES:
        if any(inside(solid, x, y, z) for x, y, z in [(cx, cy, 4), (cx + 2.7, cy, 4), (cx, cy + 2.7, 4)]):
            return False
        if not all(inside(solid, x, y, z) for x, y, z in [(cx + 3.5, cy, 4), (cx, cy + 3.5, 4)]):
            return False
    # The centered rib must remain intact and uncut.
    return all(inside(solid, x, y, z) for x, y, z in [(-45, 0, 20), (0, 0, 35), (45, 0, 20)])


def mass_g(volume_mm3: float) -> float:
    return volume_mm3 / 1000.0 * DENSITY_G_CM3


def check_massprops(path: Path, volume: float) -> bool:
    if not path.exists() or path.stat().st_size <= 0:
        return False
    text = path.read_text(encoding="utf-8", errors="ignore").lower()
    numbers = [float(value) for value in re.findall(r"\d+(?:\.\d+)?", text)]
    has_volume = any(abs(value - volume) <= 5.0 for value in numbers)
    has_mass = any(abs(value - mass_g(volume)) <= 0.2 for value in numbers)
    return "mass" in text and "volume" in text and has_volume and has_mass


def evaluate() -> bool:
    solid = load_single_solid(OUTPUT_ROOT / STEP_NAME)
    volume = solid.Volume()
    return (
        check_bbox(solid)
        and check_holes_and_rib(solid)
        and 178.0 <= mass_g(volume) <= 182.0
        and check_massprops(OUTPUT_ROOT / TEXT_NAME, volume)
    )


if __name__ == "__main__":
    try:
        print(True if evaluate() else False)
    except Exception:
        print(False)