# Measurement data export SmartLab Studio II does not always render menus immediately after input. v0.2.7 adds short waits and visibility checks to the existing mouse/UIA sequence. It retries graph input only while the initial context menu remains unavailable. ## Usage This example saves the data already shown in the 2θ graph. It does not start a measurement. Run it when `get_state()` reports `IDLE`. ```python from smartlab_studio_ctrl import SmartLabUIA smartlab = SmartLabUIA() saved = smartlab.export_data(filename="measurement.txt", save=True, graph="2theta") print(saved) ``` ## GUI operation sequence 1. **Select the graph through UIA.** Use `SelectionItemPattern.Select()` for `graph="2theta"` (the default) or `graph="2theta/theta"`. Resolve the visible `chartControl` using `SmartLabConfig.EXPORT_DATA_GRAPH_PATHS` and compute its center from `CurrentBoundingRectangle`. 2. **Right-click the graph.** Use `pywinauto.mouse.click(button="right", coords=...)`. After the input, wait **150 ms** and look up `mainShellWindow → Popup → contextMenu → contextMenuItem_Export` through fixed UIA child paths. After each right-click, poll for visibility for up to **one second**, about every **50 ms**. Stop sending retry input once the item is visible. 3. **Retry only if the menu has not opened.** Right-click again at the same point. If it is still absent, left-click the graph and then right-click. If the menu still does not open, use `pywinauto.mouse.move()` to shift up to **six pixels** to the right within the graph and repeat the same sequence. The move and the graph's left click also have a **150 ms** settle time and a visibility check. Exhausting these attempts raises an error. 4. **Expand through UIA and select with the mouse.** Call `ExpandCollapsePattern.Expand()` on Export. If the pattern is unavailable or the call raises an exception, move the mouse to the item's center. Wait **150 ms**, find the visible `データをエクスポート...` item in the known candidate paths, and left-click its center with `pywinauto.mouse.click(button="left", coords=...)`. Wait **150 ms** before looking for `exportDataWindow` in the same process. 5. **Save through UIA.** Keep the dialog's default data selection. Use `InvokePattern.Invoke()` on `exportBtn` and resolve the Windows Save dialog by its known child path. Use `ValuePattern.SetValue()` when `filename` is supplied, and invoke Save when `save=True`. The existing `overwrite` option controls overwrite confirmation. The local UIA API returns `True` after completing the save operation and `False` for a no-save operation or overwrite refusal. The right click on the graph and the left click on the data-export item require mouse input on the logged-in Windows desktop. Matching the session ID alone does not grant mouse input rights. On the test host, an SSH/PsExec-launched process could read the UIA tree but received an access-denied error from `SetCursorPos`. Timing retries cannot resolve that error. ## Arguments, results, and file verification - Use `graph="2theta"` for 2θ or `graph="2theta/theta"` for 2θ/θ. The v0.2.7 repeat test covered 2θ. - `filename=None` preserves the Save dialog's default. `save=False` leaves the Save dialog open and returns `False`. - `save=True` waits for the Save dialog to close. Declining an overwrite cancels the save, closes the export window, and returns `False`. Operation failures raise an exception. - `True` means the GUI save operation completed. The local API does not validate the file contents. The gRPC server waits for a newly created or updated file before returning its bytes. [gRPC and client-side saving](server_grpc.md) ## Instrument verification On SMARTLAB, the logged-in Windows user exported the existing 2θ data three times consecutively and twice with a two-second pause. All five menus opened on the first right click, and the 115,366-byte output files had identical SHA-256 checksums. The repeat test left measurement and its settings untouched. Simulated tests covered retry order and early exit. Recovery via retry on the instrument and live gRPC export using this update remain unverified. ## Reproducing the investigation Opening the context menu and operating it after it opens can be tested independently. See the [UIA studies](uia_study.md) for the role and scope of each script.