fix(task-22): live named views read and restore the display (VIEWCTR/VIEWSIZE, ZOOM Window), UCS sysvars come from the document, and a frozen current layer is thawed before restore makes it current

Four review findings, all measured by the reviewer on AutoCAD 2026:

1. ucs_list and the ucs_restore('world') CMDACTIVE guard read
   app.GetVariable, but GetVariable is an AcadDocument member — the tool
   was dead live and the guard's swallowed read forced cmd_active to 0,
   sending `_.UCS _W` into any open prompt. Both read the document now,
   and a failed CMDACTIVE read refuses instead of proceeding. The fake
   app in tests/test_views_ucs.py has no GetVariable any more, so the
   old code cannot pass.

2. view_named_save defaulted from doc.ActiveViewport.Center/Height/Width,
   which does not track the display (it answered the initial view after
   `_.ZOOM _C 5,6 20`). The current view is VIEWCTR / VIEWSIZE at the
   SCREENSIZE aspect — what `-VIEW _S` records (width 40.267 for height
   20). The fake viewport is deliberately stale so a read of it fails.

3. view_named_restore wrote Center/Height/Width into the AcadViewport;
   AutoCAD anchors the lower-left corner and widens to the display
   aspect, landing a 10x20 view at (5, 6) on VIEWCTR (20.134, 6). It now
   zooms the saved rectangle (app.ZoomWindow), which centres and fits
   like `-VIEW _R` (VIEWCTR = saved centre, VIEWSIZE = max(h, w/aspect)),
   reports `applied: "zoom_window"` and reads VIEWCTR / VIEWSIZE back.
   Headless the same reconciliation moved the centre whenever the
   *Active VPORT's aspect was rewritten from w/h (even the 1.34 default
   opened at x = 11.734): the aspect the file carries is left alone —
   it is the display's, written by AutoCAD at save — and the window is
   fitted into it; the payload adds `vport_height` / `aspect_ratio`.

4. layer_state_restore set ActiveLayer before the loop and outside the
   try; AutoCAD refuses to make a frozen layer current, so the spec's
   own save → freeze → restore path raised an opaque COM error with
   nothing applied. When `frozen` is being restored the state's current
   layer is thawed first; the write is wrapped and a refusal lands in
   `warnings`. The fake document and layer now model both refusals.

Live AutoCAD was not running in this round (GetActiveObject failed, no
acad process), so the fix is measured against the reviewer's numbers,
not re-measured.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FKFStjEFzpGyLq5bMpWxTd
This commit is contained in:
Umut CAN
2026-09-21 16:48:19 +03:00
co-authored by Claude Opus 5
parent bbc9aeeb63
commit c7dd72410a
6 changed files with 318 additions and 62 deletions
+15 -3
View File
@@ -2650,10 +2650,20 @@ class EzdxfBackend(AutoCADBackend):
cx, cy = float(view.dxf.center.x), float(view.dxf.center.y)
h, w = float(view.dxf.height), float(view.dxf.width)
vport = self._active_vport(doc)
# The *Active VPORT's aspect ratio is the DISPLAY's, written by
# AutoCAD at its last save, and AutoCAD reconciles the stored
# window against the real display by its lower-left corner: a
# stored aspect that differs from the display's shifts the centre
# by (display − stored) · height / 2. Measured (AutoCAD 2026,
# display aspect 2.013): a 10x20 view at (5, 6) written with
# aspect 0.5 opened at VIEWCTR (20.134, 6). So the aspect is left
# as the file carries it and the window is FITTED into it —
# height = max(h, w / aspect), the same rule `-VIEW _R` applies —
# which is exactly what AutoCAD's own restore-then-save writes.
aspect = float(vport.dxf.get("aspect_ratio", 1.34)) or 1.34
fit_height = max(h, w / aspect)
vport.dxf.center = (cx, cy)
vport.dxf.height = h
if h > 0:
vport.dxf.aspect_ratio = w / h
vport.dxf.height = fit_height
# The orientation travels with the window: a foreign drawing's
# VIEW may be oblique or twisted, and flattening it to a plan view
# of the same center would be a silent lie. The attribute read
@@ -2674,6 +2684,8 @@ class EzdxfBackend(AutoCADBackend):
# initial view when it opens the file. No live display moves.
"applied": "header_only",
"store": "vport_active",
"vport_height": fit_height,
"aspect_ratio": aspect,
"backend": "ezdxf",
}