#!/usr/bin/env python3 """Autonomous 2D-sketch loop: drive the Design tab's sketch layer through the app's MCP server and assert the things that decide whether a profile is buildable. WHY THIS EXISTS. Every question about the 2D layer ("is this loop closed?", "did the offset survive?", "is the circle a void or a second body?"), asked by clicking, costs a GUI session and a human. The Design commands make each one a call, and this script is the loop: build a known profile, ask the app what it thinks it has, compare against arithmetic. RUN IT AGAINST A RUNNING APP started with --mcp and the CAD feature on, for example: python3 tools/mcp/orca_mcp.py launch --cad --datadir python3 scripts/CAD/check-mcp-sketch.py [datadir] datadir is the running app's data directory (default: $ORCA_DATADIR, else the platform's own); the script finds the app's MCP server through the port it writes to /mcp/server.json. Exit 0 = every assertion held. Anything else prints the first mismatch and stops. """ import math, os, sys sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "tools", "mcp")) import orca_mcp # noqa: E402 if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help"): print(__doc__) sys.exit(0) DATADIR = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("ORCA_DATADIR", "") APP = orca_mcp.OrcaMcp.from_datadir(DATADIR) def call(method, **params): return APP.call_command(method, **params)["result"] def near(a, b, tol=1e-6): return abs(a - b) < tol def check(cond, what): if not cond: print(f"FAIL: {what}", file=sys.stderr) sys.exit(1) print(f" ok {what}") def areas(rep): return sorted(round(l["area"], 6) for l in rep["closed_loops"]) print("1. a rectangle is one closed loop of exactly its own area") try: call("sketch_cancel") except Exception: pass call("sketch_create", plane="XY") call("sketch_draw", type="rectangle_2_point", p0=[0, 0], p1=[80, 50]) r = call("sketch_describe") check(r["buildable"], "buildable") check(areas(r) == [4000.0], f"one loop of 4000 mm^2 (got {areas(r)})") print("2. a circle inside it is a VOID, not a second profile") call("sketch_draw", type="circle_center", center=[40, 25], radius=10) r = call("sketch_describe") outer = [l for l in r["closed_loops"] if near(l["area"], 4000.0)][0] check(len(outer["holes"]) == 1, "the rectangle encloses exactly one void") hole = r["closed_loops"][outer["holes"][0]] check(near(hole["area"], math.pi * 100), f"the void is pi*r^2 (got {hole['area']})") print("3. offsetting the outer loop inward keeps it CLOSED and exact") call("sketch_select", entities=[0, 1, 2, 3]) call("sketch_offset", distance=5) r = call("sketch_describe") check(r["open_ends"] == [], "no open ends after the offset") check(any(near(l["area"], 70 * 40) for l in r["closed_loops"]), f"the offset loop is 70x40 (got {areas(r)})") print("4. a gap is REPORTED with its coordinates, then healed into a constraint") call("sketch_cancel") call("sketch_create", plane="XY") call("sketch_draw", entities=[ {"type": "line", "p0": [0, 0], "p1": [60, 0]}, {"type": "line", "p0": [60, 0], "p1": [60, 40]}, {"type": "line", "p0": [60, 40], "p1": [0, 40]}, {"type": "line", "p0": [0, 40], "p1": [0.4, 0]}, # 0.4 mm short of closing ]) r = call("sketch_validate", tolerance=1.0) check(not r["buildable"], "a 0.4 mm gap makes the profile unbuildable") check(len(r["open_ends"]) == 2, f"both free ends are named (got {r['open_ends']})") dof_before = r["dof"] r = call("sketch_repair", tolerance=1.0) check(r["welded"] == 1, f"one pair welded (got {r['welded']})") check(r["buildable"] and r["open_ends"] == [], "healed profile is buildable") check(areas(r) == [2400.0], f"healed loop is 60x40 (got {areas(r)})") check(r["dof"] < dof_before, f"the weld recorded a real constraint: DoF {dof_before} -> {r['dof']}") print("5. construction geometry is excluded from the profile") call("sketch_select", entities=[0]) call("sketch_toggle_construction", silent=True) r = call("sketch_describe") check(not r["buildable"], "turning one side into a guide opens the profile again") call("sketch_toggle_construction", silent=True) r = call("sketch_describe") check(r["buildable"], "turning it back closes it again") print("6. re-dimensioning one side keeps the rectangle a single closed loop") call("sketch_cancel") call("sketch_create", plane="XY") call("sketch_draw", type="rectangle_2_point", p0=[0, 0], p1=[60, 40]) r = call("sketch_describe") check(areas(r) == [2400.0], f"one loop of 2400 mm^2 (got {areas(r)})") call("sketch_select", entities=[0]) # the bottom edge, y=0, from x=0 to x=60 r = call("sketch_set_value", value=40) check(r["kind"] == "length", f"dimension kind is length (got {r['kind']})") check(near(r["before"], 60), f"the edge measured 60 before (got {r['before']})") r = call("sketch_describe") check(len(r["closed_loops"]) == 1, "the rectangle is still exactly one closed loop") check(r["open_ends"] == [], "no open ends after re-dimensioning") # The point of the whole section: a rectangle must SURVIVE one side being re-dimensioned. We do # not assert a specific area — only that the topology held — but print it so a topology-preserving # yet geometry-wrong result is visible in the output. print(f" note resulting rectangle area = {areas(r)} mm^2 (topology held; geometry is what it is)") call("sketch_cancel") print("\nall sketch assertions held")