"""Emit deterministic current-browser references; never call a generation model. Generated inputs and artifacts live in a new external experiment directory. Existing files are opened exclusively, so historical failures and test jobs cannot be replaced. """ import argparse import copy import json import math from pathlib import Path BUILD = "f3d62ce93f72db42f311a46e" def cmd(command, **params): return {"command": command, "params": params} def params(definitions): return [cmd("parameters.add", name=n, expression=e, unit=u) for n, e, u in definitions] def mm(name, value): return name, f"{value} mm", "mm" def batch(commands): return {"method": "engine.batch", "params": {"commands": commands, "rollback": True}} def ref(type_id, definitions, phases, expected, facts, edit): return {"type_id": type_id, "engine_build": BUILD, "source": "deterministic_reference_not_model_generation", "parameters": definitions, "facts": facts, "expected": expected, "steps": [batch(params(definitions))] + [batch(p) for p in phases], "parameter_edit": edit} def plate(alternate=False): length, width, thick = (120, 70, 10) if alternate else (100, 60, 8) holes = [(-39, -16, 5), (36, -17, 7), (-38, 18, 6), (40, 20, 4)] if alternate else [ (-35, -15, 6), (35, -15, 6), (-35, 15, 6), (35, 15, 6)] definitions = [mm("plateLength", length), mm("plateWidth", width), mm("plateThickness", thick)] operations = [cmd("solid.box", corner=["-plateLength/2", "-plateWidth/2", 0], length="plateLength", width="plateWidth", height="plateThickness", operation="new", body_name="Plate", name="PlateBlank")] cylinders = [] for i, (x, y, d) in enumerate(holes, 1): definitions += [mm(f"hole{i}X", x), mm(f"hole{i}Y", y), mm(f"hole{i}Diameter", d)] operations += [cmd("solid.cylinder", diameter=f"hole{i}Diameter", height="plateThickness+2 mm", base=[f"hole{i}X", f"hole{i}Y", -1], operation="cut", targets=["Plate"], name=f"Hole{i}")] cylinders.append(cylinder(d / 2, [x, y, 0], [0, 0, 1], [0, thick], f"hole{i}")) expected = {"bodies": [{"bbox": [[-length/2, -width/2, 0], [length/2, width/2, thick]], "volume": (length*width-sum(math.pi*d*d/4 for _, _, d in holes))*thick, "cylinders": cylinders}], "holes": holes} return ref("mounting-plate", definitions, [operations], expected, {"geometry_representation": "rectangular-plate-z-through-holes-v1", "hole_count": len(holes)}, {"name": "plateThickness", "expression": f"{thick+2} mm"}) def cylinder(radius, origin, axis, interval, role): return {"radius": radius, "origin": origin, "axis": axis, "interval": interval, "role": role} def shaft(alternate=False): segments = [(24, 25), (16, 30), (10, 15)] if alternate else [(20, 40), (12, 20)] definitions, operations, cylinders = [], [], [] z, volume = 0, 0 for i, (d, length) in enumerate(segments, 1): start = "0 mm" if i == 1 else f"segment{i-1}Start+segment{i-1}Length" definitions += [mm(f"segment{i}Diameter", d), mm(f"segment{i}Length", length), (f"segment{i}Start", start, "mm")] operation = {"operation": "new", "body_name": "Shaft"} if i == 1 else {"operation": "join", "targets": ["Shaft"]} operations.append(cmd("solid.cylinder", diameter=f"segment{i}Diameter", height=f"segment{i}Length", base=[0, 0, f"segment{i}Start"], name=f"Segment{i}", **operation)) cylinders.append(cylinder(d/2, [0, 0, 0], [0, 0, 1], [z, z+length], f"segment{i}")) z += length volume += math.pi*d*d*length/4 radius = max(d for d, _ in segments)/2 expected = {"bodies": [{"bbox": [[-radius, -radius, 0], [radius, radius, z]], "volume": volume, "cylinders": cylinders}], "segments": segments} return ref("stepped-shaft", definitions, [operations], expected, {"geometry_representation": "coaxial-solid-stepped-shaft-v1", "segment_count": len(segments)}, {"name": "segment1Length", "expression": f"{segments[0][1]+10} mm"}) def bolt(alternate=False): d, length, af, head, thread, pitch = (10, 40, 17, 7, 25, 1.5) if alternate else (8, 30, 14, 5, 20, 1.25) definitions = [mm("nominalDiameter", d), mm("underHeadLength", length), mm("acrossFlats", af), mm("headHeight", head), mm("threadLength", thread), mm("threadPitch", pitch), ("headRadius", "acrossFlats/sqrt(3)", "mm")] points = [[f"headRadius*cos({i*60} deg)", f"headRadius*sin({i*60} deg)"] for i in range(6)] phases = [[cmd("solid.cylinder", diameter="nominalDiameter", height="underHeadLength", operation="new", body_name="Bolt", name="SimplifiedShank")], [cmd("sketch.create", name="HexHead", plane="XY", offset="underHeadLength"), cmd("sketch.line", points=points, closed=True, infer=False), cmd("sketch.solve"), cmd("sketch.finish")], [cmd("solid.extrude", sketch="HexHead", distance="headHeight", operation="join", targets=["Bolt"], name="HexHeadExtrude")], [cmd("parameters.comment", name="threadLength", comment="简化螺纹区:杆端Z=0至threadLength;圆柱实体,无真实牙型"), cmd("parameters.comment", name="threadPitch", comment="仅螺距语义标注,不切削真实螺旋牙型")]] radius = af/math.sqrt(3) expected = {"bodies": [{"bbox": [[-radius, -af/2, 0], [radius, af/2, length+head]], "volume": math.pi*d*d*length/4+math.sqrt(3)*af*af*head/2, "cylinders": [cylinder(d/2, [0, 0, 0], [0, 0, 1], [0, length], "shank")]}], "across_flats": af, "head_z": [length, length+head], "thread_interval": [0, thread]} facts = {"geometry_representation": "hex-bolt-simplified-thread-v1", "nominal_diameter_mm": d, "under_head_length_mm": length, "across_flats_mm": af, "head_height_mm": head, "thread_length_mm": thread} return ref("hex-head-bolt", definitions, phases, expected, facts, {"name": "underHeadLength", "expression": f"{length+10} mm"}) def adaptive_simpson(f, a, b, eps=1e-9, depth=24): def go(a, b, fa, fm, fb, whole, eps, depth): mid = (a+b)/2 left_mid, right_mid = (a+mid)/2, (mid+b)/2 fl, fr = f(left_mid), f(right_mid) left, right = (mid-a)*(fa+4*fl+fm)/6, (b-mid)*(fm+4*fr+fb)/6 if depth == 0 or abs(left+right-whole) <= 15*eps: return left+right+(left+right-whole)/15 return go(a, mid, fa, fl, fm, left, eps/2, depth-1)+go(mid, b, fm, fr, fb, right, eps/2, depth-1) fa, fm, fb = f(a), f((a+b)/2), f(b) return go(a, b, fa, fm, fb, (b-a)*(fa+4*fm+fb)/6, eps, depth) def housing(alternate=False): L, W, T, D, bore, C, B, P, rt, reach, rh, cb, depth = (140, 80, 14, 64, 30, 50, 46, 46, 7, 20, 20, 38, 8) if alternate else (120, 70, 12, 60, 32, 45, 40, 44, 6, 18, 18, 40, 6) hx, hy, hd = (56, 28, 9) if alternate else (48, 23, 8) values = {"baseLength": L, "baseWidth": W, "baseThickness": T, "bossDiameter": D, "boreDiameter": bore, "centerHeight": C, "bossWidth": B, "pedestalWidth": P, "ribThickness": rt, "ribReach": reach, "ribHeight": rh, "counterboreDiameter": cb, "counterboreDepth": depth, "mountX": hx, "mountY": hy, "mountDiameter": hd} definitions = [mm(n, v) for n, v in values.items()] main = [cmd("solid.box", corner=["-baseLength/2", "-baseWidth/2", 0], length="baseLength", width="baseWidth", height="baseThickness", operation="new", body_name="Housing", name="Base"), cmd("solid.box", corner=["-pedestalWidth/2", "-bossWidth/2", "baseThickness"], length="pedestalWidth", width="bossWidth", height="centerHeight-baseThickness", operation="join", targets=["Housing"], name="Pedestal"), cmd("solid.cylinder", diameter="bossDiameter", height="bossWidth", axis=[0, 1, 0], base=[0, "-bossWidth/2", "centerHeight"], operation="join", targets=["Housing"], name="BearingBoss")] phases = [main] for side, sign in (("Left", -1), ("Right", 1)): a = "-pedestalWidth/2" if sign < 0 else "pedestalWidth/2" outside = f"{a}{'-' if sign<0 else '+'}ribReach" phases.append([cmd("sketch.create", name=f"{side}Rib", plane="XZ", offset="-ribThickness/2"), # Native XZ uses local (X,-Z), with normal +Y. cmd("sketch.line", points=[[a, "-baseThickness"], [outside, "-baseThickness"], [a, "-baseThickness-ribHeight"]], closed=True, infer=False), cmd("sketch.solve"), cmd("sketch.finish"), cmd("solid.extrude", sketch=f"{side}Rib", distance="ribThickness", operation="join", targets=["Housing"], name=f"{side}RibExtrude")]) cut = [cmd("solid.cylinder", diameter="boreDiameter", height="bossWidth+2 mm", base=[0, "-bossWidth/2-1 mm", "centerHeight"], axis=[0, 1, 0], operation="cut", targets=["Housing"], name="BearingBore"), cmd("solid.cylinder", diameter="counterboreDiameter", height="counterboreDepth", base=[0, "-bossWidth/2", "centerHeight"], axis=[0, 1, 0], operation="cut", targets=["Housing"], name="NegativeYCounterbore")] cylinders = [cylinder(bore/2, [0, 0, C], [0, 1, 0], [-B/2+depth, B/2], "bearing-bore"), cylinder(cb/2, [0, 0, C], [0, 1, 0], [-B/2, -B/2+depth], "negative-y-counterbore")] for i, (sx, sy) in enumerate(((-1, -1), (1, -1), (-1, 1), (1, 1)), 1): cut.append(cmd("solid.cylinder", diameter="mountDiameter", height="baseThickness+2 mm", base=[f"{sx}*mountX", f"{sy}*mountY", -1], operation="cut", targets=["Housing"], name=f"MountHole{i}")) cylinders.append(cylinder(hd/2, [sx*hx, sy*hy, 0], [0, 0, 1], [0, T], f"mount{i}")) phases.append(cut) R, a = D/2, P/2 overlap = a*math.sqrt(R*R-a*a)+R*R*math.asin(a/R) def rib_overlap(x): if x >= R: return 0 low, high = C-math.sqrt(R*R-x*x), C+math.sqrt(R*R-x*x) top = T+rh*(1-(x-a)/reach) return max(0, min(top, high)-max(T, low)) extra_ribs = 2*rt*(reach*rh/2-adaptive_simpson(rib_overlap, a, a+reach)) volume = L*W*T+(P*(C-T)+math.pi*R*R-overlap)*B+extra_ribs-math.pi*(bore/2)**2*B-math.pi*((cb/2)**2-(bore/2)**2)*depth-4*math.pi*(hd/2)**2*T expected = {"bodies": [{"bbox": [[-L/2, -W/2, 0], [L/2, W/2, C+R]], "volume": volume, "cylinders": cylinders}], "rib_sections": {"thickness": rt, "reach": reach, "height": rh, "base_z": T, "side_x": a}, "volume_method": "independent circle/rectangle union and adaptive Simpson rib overlap; no mesh or fitted volume"} facts = {"geometry_representation": "ribbed-housing-y-bore-z-mount-v1", "center_height_mm": C, "base_thickness_mm": T, "boss_outer_diameter_mm": D, "bore_diameter_mm": bore, "boss_width_mm": B, "pedestal_width_mm": P, "rib_height_mm": rh, "min_wall_mm": 3} return ref("ribbed-bearing-housing", definitions, phases, expected, facts, {"name": "centerHeight", "expression": f"{C+5} mm"}) def enclosure(alternate=False): L, W, H, t, bottom, lid, gap, inset, D, pilot, cover, bh, pd, rt, rh = (140, 90, 45, 3, 4, 5, 0.5, 12, 12, 4, 5, 41, 14, 2.5, 14) if alternate else (120, 80, 40, 3, 3, 4, 0.3, 10, 10, 3, 4, 37, 12, 2, 12) values = {"caseLength": L, "caseWidth": W, "caseHeight": H, "wallThickness": t, "bottomThickness": bottom, "lidThickness": lid, "assemblyGap": gap, "fastenerInset": inset, "bossDiameter": D, "pilotDiameter": pilot, "coverHoleDiameter": cover, "bossHeight": bh, "pilotDepth": pd, "ribThickness": rt, "ribHeight": rh} definitions = [mm(n, v) for n, v in values.items()]+[("fastenerX", "caseLength/2-fastenerInset", "mm"), ("fastenerY", "caseWidth/2-fastenerInset", "mm")] main = [cmd("solid.box", corner=["-caseLength/2", "-caseWidth/2", 0], length="caseLength", width="caseWidth", height="caseHeight", operation="new", body_name="Case", name="CaseBlank"), cmd("solid.box", corner=["-caseLength/2+wallThickness", "-caseWidth/2+wallThickness", "bottomThickness"], length="caseLength-2*wallThickness", width="caseWidth-2*wallThickness", height="caseHeight-bottomThickness+1 mm", operation="cut", targets=["Case"], name="OpenCavity")] additions, cuts, cover_cuts, cylinders = [], [], [], [] cover_cylinders = [] for i, (sx, sy) in enumerate(((-1, -1), (1, -1), (-1, 1), (1, 1)), 1): x, y = f"{sx}*fastenerX", f"{sy}*fastenerY" # Each rib overlaps the boss and terminates at the inner Y wall. rib_y = "fastenerY" if sy > 0 else "-caseWidth/2+wallThickness" additions += [cmd("solid.cylinder", diameter="bossDiameter", height="bossHeight", base=[x, y, "bottomThickness"], operation="join", targets=["Case"], name=f"Boss{i}"), cmd("solid.box", corner=[f"{x}-ribThickness/2", rib_y, "bottomThickness"], length="ribThickness", width="fastenerInset-wallThickness", height="ribHeight", operation="join", targets=["Case"], name=f"BossRib{i}")] cuts += [cmd("solid.cylinder", diameter="pilotDiameter", height="pilotDepth", base=[x, y, "bottomThickness+bossHeight"], axis=[0, 0, -1], operation="cut", targets=["Case"], name=f"BlindPilot{i}")] cover_cuts += [cmd("solid.cylinder", diameter="coverHoleDiameter", height="lidThickness+2 mm", base=[x, y, "caseHeight+assemblyGap-1 mm"], operation="cut", targets=["Lid"], name=f"CoverHole{i}")] cylinders.append(cylinder(pilot/2, [sx*(L/2-inset), sy*(W/2-inset), 0], [0, 0, 1], [bottom+bh-pd, bottom+bh], f"blind-pilot{i}")) cover_cylinders.append(cylinder(cover/2, [sx*(L/2-inset), sy*(W/2-inset), 0], [0, 0, 1], [H+gap, H+gap+lid], f"cover-hole{i}")) cover_main = [cmd("solid.box", corner=["-caseLength/2", "-caseWidth/2", "caseHeight+assemblyGap"], length="caseLength", width="caseWidth", height="lidThickness", operation="new", body_name="Lid", name="IndependentLid")] R, a = D/2, rt/2 rib_overlap = a*math.sqrt(R*R-a*a)+R*R*math.asin(a/R) volume = L*W*H-(L-2*t)*(W-2*t)*(H-bottom)+4*math.pi*R*R*bh+4*(rt*(inset-t)-rib_overlap)*rh-4*math.pi*(pilot/2)**2*pd expected = {"bodies": [{"bbox": [[-L/2, -W/2, 0], [L/2, W/2, H]], "volume": volume, "cylinders": cylinders}, {"bbox": [[-L/2, -W/2, H+gap], [L/2, W/2, H+gap+lid]], "volume": (L*W-4*math.pi*(cover/2)**2)*lid, "cylinders": cover_cylinders}], "gap": gap, "inner_cavity": [L-2*t, W-2*t, H-bottom], "shared_fastener_xy": [L/2-inset, W/2-inset]} facts = {"geometry_representation": "open-case-four-bosses-independent-flat-lid-v1", "length_mm": L, "width_mm": W, "height_mm": H, "wall_thickness_mm": t, "bottom_thickness_mm": bottom, "lid_thickness_mm": lid, "assembly_gap_mm": gap, "fastener_inset_mm": inset, "boss_outer_diameter_mm": D, "boss_height_mm": bh, "pilot_depth_mm": pd, "rib_height_mm": rh, "min_wall_mm": 1.5} return ref("covered-thin-wall-enclosure", definitions, [main, additions, cuts, cover_main+cover_cuts], expected, facts, {"name": "fastenerInset", "expression": f"{inset+2} mm"}) def herringbone(grooved=False, alternate=False): mn, teeth, beta = (1.5, 20, -15) if alternate else (2, 24, 20) width, gap = ((18, 2) if alternate else (20, 4)) if grooved else ((18, 0) if alternate else (16, 0)) bore = 6 if alternate else 8 definitions = [mm("normalModule", mn), ("gearTeeth", str(teeth), ""), ("normalPressure", "20 deg", "deg"), ("helixAngle", f"{beta} deg", "deg"), mm("gearWidth", width), mm("centralGap", gap), mm("boreDiameter", bore), ("halfWidth", "(gearWidth-centralGap)/2", "mm"), ("transverseModule", "normalModule/cos(helixAngle)", "mm"), ("gearPressure", "atan(tan(normalPressure)/cos(helixAngle))", "deg"), ("pitchRadius", "transverseModule*gearTeeth/2", "mm"), ("baseRadius", "pitchRadius*cos(gearPressure)", "mm"), ("tipRadius", "pitchRadius+normalModule", "mm"), ("rootRadius", "pitchRadius-1.25*normalModule", "mm"), ("toothBaseAngle", "90 deg/gearTeeth+(tan(gearPressure)-gearPressure/(1 rad))*(1 rad)", "deg"), ("tipRoll", "sqrt((tipRadius/baseRadius)^2-1)", ""), ("tipAngle", "toothBaseAngle-(tipRoll-atan(tipRoll)/(1 rad))*(1 rad)", "deg"), ("halfTwist", "halfWidth*tan(helixAngle)/pitchRadius*(1 rad)", "deg")] phases = [] for prefix, offset, phase, twist, path_start in (("Lower", 0, "0 deg", "halfTwist", 0), ("Upper", "halfWidth+centralGap", "halfTwist", "-halfTwist", "halfWidth+centralGap")): def point(radius, angle): return [f"{radius}*cos(({phase})+({angle}))", f"{radius}*sin(({phase})+({angle}))"] a = "toothBaseAngle-(t-atan(t)/(1 rad))*(1 rad)" profile = [cmd("sketch.create", plane="XY", offset=offset, name=prefix+"Section"), cmd("sketch.line", points=[point("rootRadius", "-toothBaseAngle"), point("baseRadius", "-toothBaseAngle")], infer=False), cmd("sketch.equation_curve", x=f"baseRadius*sqrt(1+t^2)*cos(({phase})-({a}))", y=f"baseRadius*sqrt(1+t^2)*sin(({phase})-({a}))", start=0, end="tipRoll", tolerance=0.002), cmd("sketch.arc.three_point", start=point("tipRadius", "-tipAngle"), through=point("tipRadius", "0 deg"), end=point("tipRadius", "tipAngle")), cmd("sketch.equation_curve", x=f"baseRadius*sqrt(1+t^2)*cos(({phase})+({a}))", y=f"baseRadius*sqrt(1+t^2)*sin(({phase})+({a}))", start=0, end="tipRoll", tolerance=0.002), cmd("sketch.line", points=[point("baseRadius", "toothBaseAngle"), point("rootRadius", "toothBaseAngle")], infer=False), cmd("sketch.arc.three_point", start=point("rootRadius", "toothBaseAngle"), through=point("rootRadius", "180 deg/gearTeeth"), end=point("rootRadius", "360 deg/gearTeeth-toothBaseAngle")), cmd("sketch.pattern.circular", entities=["l1", "s1", "a1", "s2", "l2", "a2"], center=[0, 0], count="gearTeeth"), cmd("sketch.solve"), cmd("sketch.finish")] path = [cmd("sketch.create", plane="YZ", name=prefix+"Path"), cmd("sketch.line", points=[[0, path_start], [0, "halfWidth" if prefix=="Lower" else "gearWidth"]], infer=False), cmd("sketch.finish")] if prefix == "Upper": continue phases += [profile, path, [cmd("solid.sweep", sketch=prefix+"Section", path_sketch=prefix+"Path", path=["l1"], orientation="parallel", twist=twist, operation="new", body_name="Gear", name=prefix+"Teeth")]] if grooved: # Extend only into the groove, avoiding coincident swept cylindrical faces. phases += [[cmd("solid.cylinder", radius="rootRadius", height="centralGap", base=[0, 0, "halfWidth"], operation="join", targets=["Gear"], name="CentralRootBridge")]] # Mirror the completed half+half-gap, so the bridge overlaps in the groove. # Remove the excess half of the bridge before mirroring by using G/2. phases[-1][0]["params"]["height"] = "centralGap/2" phases += [[cmd("solid.mirror", bodies=["Gear"], plane={"origin": [0, 0, "gearWidth/2"], "normal": [0, 0, 1]}, combine=True, name="OppositeHandHalf")]] phases += [[cmd("solid.cylinder", diameter="boreDiameter", height="gearWidth+2 mm", base=[0, 0, -1], operation="cut", targets=["Gear"], name="Bore")]] beta_r, alpha_n = math.radians(beta), math.radians(20) pitch = mn/math.cos(beta_r)*teeth/2 alpha = math.atan(math.tan(alpha_n)/math.cos(beta_r)) rb, ra, rf = pitch*math.cos(alpha), pitch+mn, pitch-1.25*mn base_angle = math.pi/(2*teeth)+math.tan(alpha)-alpha roll = math.sqrt((ra/rb)**2-1) area = math.pi*rf*rf+teeth*(rb*rb*roll**3/3+ra*ra*(base_angle-roll+math.atan(roll))-rf*rf*base_angle) expected = {"bodies": [{"bbox": [[-ra, -ra, 0], [ra, ra, width]], "volume": (area-math.pi*(bore/2)**2)*(width-gap)+(math.pi*rf*rf-math.pi*(bore/2)**2)*gap, "cylinders": [cylinder(bore/2, [0, 0, 0], [0, 0, 1], [0, width], "bore")]}], "gear": {"normal_module": mn, "teeth": teeth, "beta_deg": beta, "width": width, "gap": gap, "pitch_radius": pitch, "base_radius": rb, "tip_radius": ra, "root_radius": rf, "base_angle_rad": base_angle, "half_width": (width-gap)/2, "half_twist_rad": (width-gap)/2*math.tan(beta_r)/pitch}} type_id = "double-helical-gear" if grooved else "herringbone-gear" facts = {"normal_module_mm": mn, "teeth": teeth, "normal_pressure_angle_deg": 20, "helix_angle_deg": beta, "face_width_mm": width, "central_gap_mm": gap, "profile_shift": 0, "backlash_mm": 0, "addendum_coefficient": 1, "clearance_coefficient": 0.25, "root_transition": "radial-simplified", "geometry_representation": "double-helical-root-core-gap-v1" if grooved else "herringbone-continuous-midplane-v1"} return ref(type_id, definitions, phases, expected, facts, {"name": "gearWidth", "expression": f"{width+4} mm"}) BUILDERS = {"mounting-plate": plate, "stepped-shaft": shaft, "hex-head-bolt": bolt, "ribbed-bearing-housing": housing, "covered-thin-wall-enclosure": enclosure, "herringbone-gear": lambda alternate=False: herringbone(False, alternate), "double-helical-gear": lambda alternate=False: herringbone(True, alternate)} def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("output", type=Path) args = parser.parse_args() args.output.mkdir(parents=True, exist_ok=True) for type_id, builder in BUILDERS.items(): for alternate in (False, True): data = builder(alternate) path = args.output/f"reference-{type_id}-{'transfer' if alternate else 'standard'}.json" with path.open("x") as stream: json.dump(data, stream, ensure_ascii=False, indent=2) stream.write("\n") print(json.dumps({"references": 14, "engine_build": BUILD, "model_requests": 0})) if __name__ == "__main__": main()