feat(review): 截屏前最大化绘图区 + 空瓦片过滤覆盖回退路径
新增 MCP 工具 set_drawing_area(mode="max"|"restore"):最大化主窗口 + CLEANSCREENON (隐藏功能区/命令行/面板)。实测绘图区 2110x462 -> 3310x1084(面积 3.68x), restore 精确回位(窗口矩形/最大化态/绘图区尺寸全部还原)。 坑:CLEANSCREENOFF 是异步命令,AutoCAD 会按「进入干净屏幕时的布局」重设窗口几何, 把同步的 SetWindowPlacement 覆盖掉(表现为窗口停在最大化)。解法是插件内 SetTimer 延迟 700ms 再压回;实测窗口在 0.5~1.0s 之间收敛,故调用方等待取 1.5s。 取证层: - 新增 maximized_drawing_area(ctx) 上下文管理器,__main__ 用它包住整个取证循环 (多图框只进出一次);受 DRAWING_AREA_MAXIMIZE 控制,工具缺失/调用失败静默降级, restore 失败不掩盖业务异常。 - 内容过滤统一到规划与回退两条路径,共用同一谓词;过滤后为空则退回原集合。 - 判定改用「实体 bbox」而非「占用格」:格子量化会把跨界内容算进相邻格,反而更粗 (实测实体 bbox 正好压象限边界时 4 个象限全判有内容)。heatmap.occupied_boxes 随之删除。 实测(真实图纸 A0/A1/A2 三图框):截图张数 12/8/4 -> 8/5/4;pytest 296 passed。 新增 verify_drawing_area.py 作为该工具的验收脚本。
This commit is contained in:
1 parent
9c4ab62dc1
commit
1e5bff268a
15 files changed
+657
-50
No files matched your search
@@ -101,6 +101,19 @@ def test_tiling_defaults_are_sane():
|
||||
assert config.HEATMAP_CELL_DIV > 0
|
||||
|
||||
|
||||
def test_drawing_area_constants_have_expected_types():
|
||||
assert isinstance(config.DRAWING_AREA_MAXIMIZE, bool)
|
||||
assert isinstance(config.DRAWING_AREA_ENTER_WAIT, float)
|
||||
assert isinstance(config.DRAWING_AREA_RESTORE_WAIT, float)
|
||||
|
||||
|
||||
def test_drawing_area_defaults_are_sane():
|
||||
# 默认开启绘图区最大化;还原等待须盖过插件内 700ms 的延迟还原(实测 1.0s 才收敛)
|
||||
assert config.DRAWING_AREA_MAXIMIZE is True
|
||||
assert config.DRAWING_AREA_ENTER_WAIT >= 0
|
||||
assert config.DRAWING_AREA_RESTORE_WAIT >= 1.2
|
||||
|
||||
|
||||
def test_credibility_defaults_are_on():
|
||||
# 默认开启,保证默认拿到可信度收益
|
||||
assert config.DETERMINISTIC_CHECKS is True
|
||||
|
||||
+138
-3
@@ -6,13 +6,25 @@ import io
|
||||
import pytest
|
||||
|
||||
from dwg_review.phases.evidence import (
|
||||
_apply_keep,
|
||||
_content_predicate,
|
||||
_is_truncated,
|
||||
_plan_tiles,
|
||||
_text_boxes,
|
||||
bbox_to_window,
|
||||
collect_frame,
|
||||
maximized_drawing_area,
|
||||
)
|
||||
from tests.fakes import (
|
||||
STANDARD_TOOLS,
|
||||
FakeMcpClient,
|
||||
err,
|
||||
frame,
|
||||
make_ctx,
|
||||
ok_image,
|
||||
ok_text,
|
||||
tool_schema,
|
||||
)
|
||||
from tests.fakes import FakeMcpClient, frame, make_ctx, ok_image, ok_text
|
||||
|
||||
|
||||
def test_bbox_to_window_mapping():
|
||||
@@ -267,9 +279,25 @@ def test_capture_images_tiles_text_cluster_is_finer(tiling_env):
|
||||
|
||||
|
||||
def test_capture_images_falls_back_without_scale(tiling_env):
|
||||
"""缺 sheet_size/sheet_scale → 退回固定 2x2 象限。"""
|
||||
"""缺 sheet_size/sheet_scale → 退回固定 2x2 象限,并按内容过滤空白象限。"""
|
||||
pytest.importorskip("PIL")
|
||||
ctx = make_ctx(client=_tiling_client(_png_b64()))
|
||||
client = _tiling_client(_png_b64())
|
||||
# 实体完全落在**左下**象限内(不贴边界)
|
||||
client.responses["query_entities"] = ok_text(
|
||||
{"total": 1, "items": [{"handle": "A", "type": "LINE",
|
||||
"bbox": [0, 0, 50000, 15000]}]}
|
||||
)
|
||||
ctx = make_ctx(client=client)
|
||||
ev = collect_frame(ctx, 1, frame([0, 0, 120000, 40000], sheet_size="ZZ"))
|
||||
assert [i["label"] for i in ev["images"]] == ["overall", "tile-1"]
|
||||
|
||||
|
||||
def test_capture_images_fallback_keeps_all_when_no_boxes(tiling_env):
|
||||
"""拿不到实体 bbox(无法判断内容)→ 回退路径不过滤,四象限全拍。"""
|
||||
pytest.importorskip("PIL")
|
||||
client = _tiling_client(_png_b64())
|
||||
client.responses["query_entities"] = ok_text({"total": 0, "items": []})
|
||||
ctx = make_ctx(client=client)
|
||||
ev = collect_frame(ctx, 1, frame([0, 0, 120000, 40000], sheet_size="ZZ"))
|
||||
assert [i["label"] for i in ev["images"]] == [
|
||||
"overall", "tile-1", "tile-2", "tile-3", "tile-4",
|
||||
@@ -291,3 +319,110 @@ def test_capture_images_falls_back_when_truncated(tiling_env):
|
||||
assert [i["label"] for i in ev["images"]] == [
|
||||
"overall", "tile-1", "tile-2", "tile-3", "tile-4",
|
||||
]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 内容过滤 / 绘图区最大化
|
||||
# ---------------------------------------------------------------------------
|
||||
def test_apply_keep_filters_and_falls_back():
|
||||
regions = [
|
||||
{"min_x": 0.0, "min_y": 0.0, "max_x": 1.0, "max_y": 1.0},
|
||||
{"min_x": 1.0, "min_y": 1.0, "max_x": 2.0, "max_y": 2.0},
|
||||
]
|
||||
keep_first = lambda w: w["min_x"] == 0.0 # noqa: E731
|
||||
assert _apply_keep(regions, keep_first) == regions[:1]
|
||||
assert _apply_keep(regions, None) == regions # 无谓词 → 原样
|
||||
assert _apply_keep(regions, lambda w: False) == regions # 全被滤掉 → 退回原集合
|
||||
assert _apply_keep([], keep_first) == []
|
||||
|
||||
|
||||
def test_content_predicate_none_without_boxes():
|
||||
assert _content_predicate([]) is None
|
||||
assert _content_predicate(None) is None
|
||||
|
||||
|
||||
def test_content_predicate_detects_intersection():
|
||||
keep = _content_predicate([(0.0, 0.0, 5.0, 5.0)])
|
||||
assert keep({"min_x": 4.0, "min_y": 4.0, "max_x": 9.0, "max_y": 9.0}) is True
|
||||
assert keep({"min_x": 6.0, "min_y": 6.0, "max_x": 9.0, "max_y": 9.0}) is False
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def drawing_area_env(monkeypatch):
|
||||
"""打开绘图区最大化,并把等待时间归零(用例不真的 sleep)。"""
|
||||
from dwg_review import config
|
||||
|
||||
monkeypatch.setattr(config, "DRAWING_AREA_MAXIMIZE", True)
|
||||
monkeypatch.setattr(config, "DRAWING_AREA_ENTER_WAIT", 0.0)
|
||||
monkeypatch.setattr(config, "DRAWING_AREA_RESTORE_WAIT", 0.0)
|
||||
return config
|
||||
|
||||
|
||||
def _area_client(responses=None):
|
||||
"""带 set_drawing_area 工具的假客户端。"""
|
||||
return FakeMcpClient(
|
||||
tools=STANDARD_TOOLS + [tool_schema("set_drawing_area", "绘图区最大化")],
|
||||
responses=responses,
|
||||
)
|
||||
|
||||
|
||||
def test_maximized_drawing_area_calls_max_then_restore(drawing_area_env):
|
||||
client = _area_client()
|
||||
ctx = make_ctx(client=client)
|
||||
with maximized_drawing_area(ctx):
|
||||
assert client.called_names() == ["set_drawing_area"] # 进入时只 max
|
||||
assert [c["arguments"] for c in client.called("set_drawing_area")] == [
|
||||
{"mode": "max"}, {"mode": "restore"},
|
||||
]
|
||||
|
||||
|
||||
def test_maximized_drawing_area_degrades_without_tool(drawing_area_env):
|
||||
"""旧版插件没有该工具 → 静默降级,一次调用都不发。"""
|
||||
client = FakeMcpClient()
|
||||
ctx = make_ctx(client=client)
|
||||
with maximized_drawing_area(ctx):
|
||||
pass
|
||||
assert client.calls == []
|
||||
|
||||
|
||||
def test_maximized_drawing_area_disabled_by_config(drawing_area_env):
|
||||
drawing_area_env.DRAWING_AREA_MAXIMIZE = False
|
||||
client = _area_client()
|
||||
ctx = make_ctx(client=client)
|
||||
with maximized_drawing_area(ctx):
|
||||
pass
|
||||
assert client.calls == []
|
||||
|
||||
|
||||
def test_maximized_drawing_area_restores_even_if_body_raises(drawing_area_env):
|
||||
client = _area_client()
|
||||
ctx = make_ctx(client=client)
|
||||
with pytest.raises(RuntimeError):
|
||||
with maximized_drawing_area(ctx):
|
||||
raise RuntimeError("boom")
|
||||
assert client.arguments_of("set_drawing_area") == {"mode": "restore"}
|
||||
|
||||
|
||||
def test_maximized_drawing_area_degrades_when_max_fails(drawing_area_env):
|
||||
"""max 调用失败 → 视为没进入最大化,不再发 restore,也不抛异常。"""
|
||||
client = _area_client(responses={"set_drawing_area": err("no window")})
|
||||
ctx = make_ctx(client=client)
|
||||
with maximized_drawing_area(ctx):
|
||||
pass
|
||||
assert [c["arguments"] for c in client.called("set_drawing_area")] == [{"mode": "max"}]
|
||||
|
||||
|
||||
def test_maximized_drawing_area_restore_failure_does_not_mask_body(drawing_area_env):
|
||||
"""restore 失败不应掩盖业务异常。"""
|
||||
client = _area_client()
|
||||
ctx = make_ctx(client=client)
|
||||
|
||||
def boom(args):
|
||||
if args.get("mode") == "restore":
|
||||
raise RuntimeError("restore failed")
|
||||
return ok_text({})
|
||||
|
||||
client.responses["set_drawing_area"] = boom
|
||||
with pytest.raises(ValueError):
|
||||
with maximized_drawing_area(ctx):
|
||||
raise ValueError("business error")
|
||||
@@ -171,12 +171,3 @@ def test_grid_stats_without_clusters():
|
||||
st = hm.grid_stats(_grid([]))
|
||||
assert st["occupied"] == 0
|
||||
assert "clusters" not in st
|
||||
|
||||
|
||||
def test_occupied_boxes_returns_cell_world_boxes():
|
||||
# 左下角格(row 1 / col 0)→ 世界 (0,0)-(50,50)
|
||||
assert hm.occupied_boxes(_grid([(0, 0, 50, 50)])) == [(0.0, 0.0, 50.0, 50.0)]
|
||||
|
||||
|
||||
def test_occupied_boxes_empty():
|
||||
assert hm.occupied_boxes(_grid([])) == []
|
||||
Reference in new issue
Block a user