Files
autocad-mcp-pro/tests/test_arch_draw.py
T
Umut CANandClaude Opus 5.5 cd1ae3529c feat(arch): SECTION 25 opens - walls, openings, stairs and exterior chains as tools
engineering/arch/draw.py is the only module of the track that touches a
backend. It draws through draw_prims(role_layer=ARCH_ROLE_LAYER) and the
generic contract members, so it runs on both engines as written, and it writes
the plan onto the drawing: each element's ACADMCP_ARCH record on one of its own
entities (a wall's first face line, a door's leaf, a window's frame, a stair's
first riser) and, beside it under ACADMCP_ARCH_H, the handles that element
owns. read_plan reads the records back and never modifies the drawing.

arch_wall computes the whole network before and after the new walls and
redraws only the drawn walls whose outline changes - erasing exactly the
entities they own - and says which under redrawn. arch_opening hosts a door or
a window in a drawn wall found by id and redraws that wall cut; its tag
defaults to the next D1/W1 (K1/P1 in Turkish). A new wall that would leave a
drawn opening uncuttable, and an opening that runs into a junction, are refused
before anything is erased. arch_stair reports the Blondel value; it does not
enforce it.

exterior_chains gives each side up to three rows, nearest first: every jamb in
that facade, both faces of every wall meeting it, and the overall - from the
wall engine's own faces. A row that would only restate a farther one is not
emitted (ISO 129-1: each measurement once). The 10 mm / 8 mm row spacing is a
declared drafting default times the plot scale, not a standard value.

The arch tag is ranked after mech so the four tools file under a new
architecture group; alias records carry WALLADD/MLINE, DOORADD/WINDOWADD and
STAIRADD with English and Turkish shop words. Inventory regenerated; README
snapshot 233 tools (228 advertised) in 26 groups; registry snapshot
architecture: 4.

No new ActiveX member is called. Measured on AutoCAD 2026 in a scratch
document through ComBackend: shell + teed partition, D1 + W1, a straight 17-riser stair
and the bottom chains gave the same 20 wall LINEs (identical endpoints),
9 poche HATCHes, 1+1 door, 4 window lines, 11 dimensions and a read_plan
round trip as the headless engine.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FKFStjEFzpGyLq5bMpWxTd
2026-09-23 23:00:05 +03:00

208 lines
8.4 KiB
Python

"""The async layer: walls, openings, stairs and chains drawn through the ezdxf backend.
The same code runs on the live engine: `engineering/arch/draw.py` composes only
generic contract members (entity_create_*, dimension_linear, entity_set_xdata,
entity_list, entity_delete), like the mechanical track's draw layer.
"""
from __future__ import annotations
import pytest
from engineering.arch.draw import (
HANDLES_APP,
add_opening,
add_stair,
add_walls,
assign_tags,
draw_chains,
draw_stair,
draw_walls,
read_plan,
)
from engineering.arch.layers import ARCH_ROLE_LAYER
from engineering.arch.model import APP_ID, Opening, Stair, Wall
pytestmark = pytest.mark.asyncio
WALL_A = {"id": "a", "axis": [[0.0, 0.0], [5000.0, 0.0]], "thickness": 200.0}
WALL_B = {"id": "b", "axis": [[2500.0, 0.0], [2500.0, 3000.0]], "thickness": 100.0}
async def on(backend, role, type_filter=None):
return await backend.entity_list(
type_filter=type_filter, layer_filter=ARCH_ROLE_LAYER[role], limit=10000
)
def ends(info) -> tuple:
a = tuple(round(v, 6) + 0.0 for v in info.properties["start"][:2])
b = tuple(round(v, 6) + 0.0 for v in info.properties["end"][:2])
return tuple(sorted((a, b)))
async def test_draw_walls_puts_faces_jambs_and_poche_on_their_layers(backend):
wall = Wall(id="a", axis=((0.0, 0.0), (5000.0, 0.0)), thickness=200.0)
door = Opening(id="d1", wall="a", kind="door", offset=1000.0, width=900.0, tag="D1")
result = await draw_walls(backend, [wall], [door])
lines = await on(backend, "wall", "LINE")
assert {ends(e) for e in lines} == {
((0.0, 100.0), (1000.0, 100.0)),
((1900.0, 100.0), (5000.0, 100.0)),
((0.0, -100.0), (1000.0, -100.0)),
((1900.0, -100.0), (5000.0, -100.0)),
((0.0, -100.0), (0.0, 100.0)),
((5000.0, -100.0), (5000.0, 100.0)),
((1000.0, -100.0), (1000.0, 100.0)),
((1900.0, -100.0), (1900.0, 100.0)),
}
assert len(await on(backend, "wall_hatch", "HATCH")) == 2
assert len(await on(backend, "door", "LINE")) == 1
assert len(await on(backend, "door", "ARC")) == 1
assert [w["id"] for w in result["walls"]] == ["a"]
assert [o["id"] for o in result["openings"]] == ["d1"]
assert result["omitted"] == []
async def test_the_records_round_trip_through_xdata(backend):
wall = Wall(id="a", axis=((0.0, 0.0), (5000.0, 0.0)), thickness=200.0, material="aac")
window = Opening(id="w1", wall="a", kind="window", offset=2000.0, width=1200.0, tag="W1")
drawn = await draw_walls(backend, [wall], [window])
plan = await read_plan(backend)
assert plan["walls"] == [wall]
assert plan["openings"] == [window]
assert plan["carriers"]["wall:a"] == drawn["walls"][0]["record"]
carrier = drawn["openings"][0]["record"]
assert plan["carriers"]["opening:w1"] == carrier
# the window's record sits on its left-face frame line, with its four lines owned
raw = await backend.entity_get_xdata(carrier, HANDLES_APP)
assert len(raw["xdata"][HANDLES_APP]) == 4
assert (await backend.entity_get_xdata(carrier, APP_ID))["xdata"][APP_ID]
async def test_add_walls_redraws_only_the_wall_the_new_one_joins(backend):
await add_walls(
backend,
[WALL_A, {"id": "c", "axis": [[0.0, 2000.0], [5000.0, 2000.0]], "thickness": 200.0}],
)
result = await add_walls(
backend, [{"id": "b", "axis": [[2500.0, 0.0], [2500.0, 1000.0]], "thickness": 100.0}]
)
assert result["added"] == ["b"]
assert result["redrawn"] == ["a"] # c is not touched by b
faces = {ends(e) for e in await on(backend, "wall", "LINE")}
# a's near face is now cut where b meets it; its far face is still one line
assert ((0.0, 100.0), (2450.0, 100.0)) in faces
assert ((2550.0, 100.0), (5000.0, 100.0)) in faces
assert ((0.0, -100.0), (5000.0, -100.0)) in faces
assert ((0.0, 100.0), (5000.0, 100.0)) not in faces # the old, uncut face was erased
plan = await read_plan(backend)
assert sorted(w.id for w in plan["walls"]) == ["a", "b", "c"]
async def test_an_opening_added_later_cuts_its_wall(backend):
await add_walls(backend, [WALL_A])
before = {ends(e) for e in await on(backend, "wall", "LINE")}
assert ((0.0, 100.0), (5000.0, 100.0)) in before
result = await add_opening(
backend, {"wall": "a", "kind": "door", "offset": 1000.0, "width": 900.0}
)
assert result["opening"] == "D1" and result["tag"] == "D1"
assert result["redrawn"] == ["a"]
after = {ends(e) for e in await on(backend, "wall", "LINE")}
assert ((0.0, 100.0), (5000.0, 100.0)) not in after
assert {((0.0, 100.0), (1000.0, 100.0)), ((1900.0, 100.0), (5000.0, 100.0))} <= after
assert len(await on(backend, "door", "ARC")) == 1
second = await add_opening(
backend, {"wall": "a", "kind": "door", "offset": 3000.0, "width": 800.0}
)
assert second["tag"] == "D2"
assert len(await on(backend, "door", "ARC")) == 2 # the first door was redrawn, not lost
plan = await read_plan(backend)
assert sorted(op.tag for op in plan["openings"]) == ["D1", "D2"]
async def test_an_opening_in_an_unknown_wall_or_overlapping_is_refused_untouched(backend):
await add_walls(backend, [WALL_A])
count = len(await backend.entity_list(limit=10000))
with pytest.raises(ValueError, match="'zz' is not a wall"):
await add_opening(backend, {"wall": "zz", "kind": "door", "offset": 0.0, "width": 900.0})
await add_opening(backend, {"wall": "a", "kind": "window", "offset": 1000.0, "width": 1200.0})
count = len(await backend.entity_list(limit=10000))
with pytest.raises(ValueError):
await add_opening(backend, {"wall": "a", "kind": "door", "offset": 1500.0, "width": 900.0})
assert len(await backend.entity_list(limit=10000)) == count
async def test_a_wall_id_already_drawn_is_refused(backend):
await add_walls(backend, [WALL_A])
with pytest.raises(ValueError, match=r"walls\[0\]\.id: 'a' is already drawn"):
await add_walls(backend, [WALL_A])
async def test_a_new_wall_that_would_swallow_an_opening_is_refused(backend):
await add_walls(backend, [WALL_A])
await add_opening(backend, {"wall": "a", "kind": "door", "offset": 2200.0, "width": 900.0})
count = len(await backend.entity_list(limit=10000))
with pytest.raises(ValueError, match="uncuttable"):
await add_walls(backend, [WALL_B])
assert len(await backend.entity_list(limit=10000)) == count
async def test_draw_stair_writes_its_record_and_reports_blondel(backend):
stair = Stair(
id="s1",
start=(0.0, 0.0),
direction_deg=0.0,
width=1000.0,
risers=17,
riser_height=170.0,
going=290.0,
)
result = await draw_stair(backend, stair)
assert result["blondel"] == {"value": 630.0, "ok": True, "range": [600.0, 650.0]}
assert result["treads"] == 16
assert len(await on(backend, "stair", "LINE")) == 17 + 2 + 2
assert (await read_plan(backend))["stairs"] == [stair]
with pytest.raises(ValueError, match="'s1' is already drawn"):
await add_stair(
backend,
{
"id": "s1",
"start": [0, 0],
"direction_deg": 0.0, # required by Task 1's stair_from_dict
"width": 1000.0,
"risers": 17,
"riser_height": 170.0,
"going": 290.0,
},
)
async def test_draw_chains_puts_real_dimensions_on_the_dimension_layer(backend):
shell = Wall(
id="shell",
axis=((0.0, 0.0), (8200.0, 0.0), (8200.0, 6200.0), (0.0, 6200.0)),
thickness=200.0,
closed=True,
)
result = await draw_chains(backend, [shell], [], sides=("bottom", "left"), scale=50)
dims = await on(backend, "dim", "DIMENSION")
assert len(dims) == result["count"] == 8 # per side: walls (3) + overall (1)
assert {row["row"] for row in result["dimensions"]} == {
"bottom:walls",
"bottom:overall",
"left:walls",
"left:overall",
}
async def test_assign_tags_numbers_each_kind_in_the_chosen_language():
ops = [
Opening(id="1", wall="a", kind="door", offset=0.0, width=900.0),
Opening(id="2", wall="a", kind="window", offset=2000.0, width=900.0),
Opening(id="3", wall="a", kind="door", offset=4000.0, width=900.0, tag="D1"),
]
assert [op.tag for op in assign_tags(ops)] == ["D2", "W1", "D1"]
assert [op.tag for op in assign_tags(ops[:2], lang="tr")] == ["K1", "P1"]