File size: 2,785 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | from __future__ import annotations
from pathlib import Path
import cadquery as cq
OUTPUT_ROOT = Path(r"C:\Users\User\Desktop")
OUTPUT_NAME = "cli_037_replaced_motor_out.step"
TOL = 0.08
HOLES = [(25.0, -20.0), (25.0, 20.0), (55.0, -20.0), (55.0, 20.0)]
def load_solids(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) != 3 or any(not s.isValid() for s in solids):
raise ValueError("expected frame, new_motor, and coupler")
return solids
def bbox_tuple(solid):
bb = solid.BoundingBox()
return (bb.xmin, bb.xmax, bb.ymin, bb.ymax, bb.zmin, bb.zmax)
def dims(solid):
bb = solid.BoundingBox()
return (bb.xlen, bb.ylen, bb.zlen)
def close_tuple(actual, expected) -> bool:
return all(abs(float(a) - float(e)) <= TOL for a, e in zip(actual, expected))
def inside(solid, x: float, y: float, z: float) -> bool:
return bool(solid.isInside(cq.Vector(x, y, z), 1e-5))
def classify(solids):
frame = motor = coupler = None
for solid in solids:
if close_tuple(bbox_tuple(solid), (-30.0, 70.0, -35.0, 35.0, 0.0, 8.0)):
frame = solid
elif close_tuple(bbox_tuple(solid), (10.0, 85.0, -22.5, 22.5, 8.0, 48.0)):
motor = solid
elif close_tuple(bbox_tuple(solid), (85.0, 115.0, -9.0, 9.0, 21.0, 39.0)):
coupler = solid
if frame is None or motor is None or coupler is None:
raise ValueError("replacement assembly parts not found")
return frame, motor, coupler
def check_mounting_holes(frame, motor) -> bool:
for cx, cy in HOLES:
if inside(frame, cx, cy, 4.0) or inside(motor, cx, cy, 12.0):
return False
if not inside(frame, cx + 3.6, cy, 4.0):
return False
return True
def check_axis_alignment(motor, coupler) -> bool:
return (
inside(motor, 75.0, 0.0, 30.0)
and inside(motor, 75.0, 4.7, 30.0)
and not inside(motor, 75.0, 5.8, 30.0)
and inside(coupler, 100.0, 0.0, 30.0)
and inside(coupler, 100.0, 8.5, 30.0)
and not inside(coupler, 100.0, 9.8, 30.0)
and abs(motor.BoundingBox().xmax - coupler.BoundingBox().xmin) <= TOL
)
def evaluate() -> bool:
frame, motor, coupler = classify(load_solids(OUTPUT_ROOT / OUTPUT_NAME))
return (
check_mounting_holes(frame, motor)
and check_axis_alignment(motor, coupler)
and abs(frame.Volume() - 55095.2) <= 80.0
and abs(motor.Volume() - 104834.3) <= 120.0
and abs(coupler.Volume() - 7634.1) <= 50.0
)
if __name__ == "__main__":
try:
print(True if evaluate() else False)
except Exception:
print(False)
|