smartlab-studio-ctrl 0.2.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- smartlab_studio_ctrl/__init__.py +11 -0
- smartlab_studio_ctrl/config.py +37 -0
- smartlab_studio_ctrl/desktop_capture.py +215 -0
- smartlab_studio_ctrl/server_grpc/__init__.py +1 -0
- smartlab_studio_ctrl/server_grpc/client.py +230 -0
- smartlab_studio_ctrl/server_grpc/server.py +282 -0
- smartlab_studio_ctrl/smartlab_config.py +231 -0
- smartlab_studio_ctrl/smartlab_ctrl.py +329 -0
- smartlab_studio_ctrl/smartlab_uia.py +1239 -0
- smartlab_studio_ctrl/snipping_config.py +59 -0
- smartlab_studio_ctrl/snipping_uia.py +325 -0
- smartlab_studio_ctrl-0.2.9.dist-info/METADATA +12 -0
- smartlab_studio_ctrl-0.2.9.dist-info/RECORD +16 -0
- smartlab_studio_ctrl-0.2.9.dist-info/WHEEL +5 -0
- smartlab_studio_ctrl-0.2.9.dist-info/licenses/LICENSE +201 -0
- smartlab_studio_ctrl-0.2.9.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1239 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""SmartLab Studio II の測定画面を Windows UI Automation で操作する。
|
|
3
|
+
|
|
4
|
+
設計方針:
|
|
5
|
+
- GUI操作は SmartLabUIA に集約し、装置通信プロトコルは扱わない。
|
|
6
|
+
- 対象ウィンドウはタイトルと AutomationId の両方で特定する。
|
|
7
|
+
- UIA要素は確認済みの親子階層を Children スコープで順に検索する。
|
|
8
|
+
Descendants 検索や曖昧な代替検索には切り替えない。
|
|
9
|
+
- Z軸は GUI 側で選択済みとし、本クラスから軸の切替は行わない。
|
|
10
|
+
- パラメータ取得には ValuePattern / SelectionItemPattern を使用する。
|
|
11
|
+
設定には SetValue / Select を使用する。
|
|
12
|
+
- 取得できないパラメータは None を返す。
|
|
13
|
+
- 測定状態はダイアログと設定ブロックの有効状態から判定する。
|
|
14
|
+
- 停止・残り時間はダイアログを動的に探索する。
|
|
15
|
+
スキャンボタンの不在・操作失敗は RuntimeError とする。
|
|
16
|
+
|
|
17
|
+
UIAパスは SmartLab Studio II x64 v5.3.200.0 の調査結果に基づく。
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from datetime import datetime, timedelta
|
|
21
|
+
import re
|
|
22
|
+
import time
|
|
23
|
+
from pywinauto import mouse
|
|
24
|
+
import sys
|
|
25
|
+
import traceback
|
|
26
|
+
from typing import Any, Optional, Tuple
|
|
27
|
+
|
|
28
|
+
from smartlab_studio_ctrl.smartlab_config import SmartLabConfig
|
|
29
|
+
|
|
30
|
+
from comtypes.gen.UIAutomationClient import (
|
|
31
|
+
IUIAutomationInvokePattern,
|
|
32
|
+
IUIAutomationSelectionItemPattern,
|
|
33
|
+
IUIAutomationValuePattern,
|
|
34
|
+
)
|
|
35
|
+
from pywinauto import Desktop
|
|
36
|
+
from pywinauto.uia_defines import IUIA
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class SmartLabUIA:
|
|
40
|
+
"""SmartLab Studio II のGUI制御を担当する単一クラス。
|
|
41
|
+
|
|
42
|
+
Attributes:
|
|
43
|
+
running: 測定ダイアログ表示またはスキャン設定ブロック無効による稼働判定。
|
|
44
|
+
remaining_time: 残り時間。取得不能または非測定時はNone。
|
|
45
|
+
estimated_end_time: 残り時間から算出した推定終了日時。
|
|
46
|
+
|
|
47
|
+
Notes:
|
|
48
|
+
画面構造は2026年11月に調査したSmartLab Studio II x64 v5.3.200.0を前提とする。
|
|
49
|
+
Z軸パラメータは確認済みの階層パスから検索する。
|
|
50
|
+
測定状態は装置本体の通信状態ではなくGUIのダイアログと設定ブロック状態による判定である。
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
REMAINING_RE = re.compile(r"残り時間\s*[::]?\s*(\d+):(\d{2}):(\d{2})")
|
|
54
|
+
|
|
55
|
+
def __init__(
|
|
56
|
+
self,
|
|
57
|
+
logger: Optional[Any] = None,
|
|
58
|
+
log_level: Optional[str] = None,
|
|
59
|
+
) -> None:
|
|
60
|
+
"""UIAインターフェースとウィンドウ要素のキャッシュを初期化する。
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
logger: XLogger 等のロガー。省略時は標準 logging を使用する。
|
|
64
|
+
log_level: 標準 logging 使用時のログレベル。省略時は INFO。
|
|
65
|
+
|
|
66
|
+
Raises:
|
|
67
|
+
RuntimeError: Windows 以外で初期化された場合。
|
|
68
|
+
|
|
69
|
+
Notes:
|
|
70
|
+
初期化時には装置の操作も GUI 要素の検索も行わない。
|
|
71
|
+
"""
|
|
72
|
+
if sys.platform != "win32":
|
|
73
|
+
raise RuntimeError("Windowsで実行してください")
|
|
74
|
+
# cobotta_ctrl.py / cobotta_bcap.py と同じ方式でロガーを受け取る。
|
|
75
|
+
# 呼び出し元から渡されなければ標準loggingを使用する。
|
|
76
|
+
if logger is None:
|
|
77
|
+
import logging
|
|
78
|
+
|
|
79
|
+
log_level = log_level or "INFO"
|
|
80
|
+
logging.basicConfig(level=log_level.upper())
|
|
81
|
+
logger = logging.getLogger(__name__)
|
|
82
|
+
|
|
83
|
+
self._logger = logger
|
|
84
|
+
self._uia = IUIA()
|
|
85
|
+
self._config = SmartLabConfig()
|
|
86
|
+
self._root = None
|
|
87
|
+
self._scan_button = None
|
|
88
|
+
self._dialog = None
|
|
89
|
+
self._remaining_element = None
|
|
90
|
+
self._stop_button = None
|
|
91
|
+
self._scan_phase = None
|
|
92
|
+
self._export_active = False
|
|
93
|
+
self._logger.info("SmartLabUIA: 初期化完了")
|
|
94
|
+
|
|
95
|
+
def _clear_dialog_cache(self) -> None:
|
|
96
|
+
"""画面更新で無効化されるダイアログ関連の参照を破棄する。"""
|
|
97
|
+
self._dialog = None
|
|
98
|
+
self._remaining_element = None
|
|
99
|
+
self._stop_button = None
|
|
100
|
+
self._logger.debug("測定ダイアログ関連キャッシュを破棄")
|
|
101
|
+
|
|
102
|
+
def _find_child(self, parent, property_name: str, value: str):
|
|
103
|
+
"""指定した親の直下から一致する要素を1件取得する。
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
parent: UIAの親要素。
|
|
107
|
+
property_name: AutomationIdなどのUIAプロパティ名。
|
|
108
|
+
value: 完全一致で検索する値。
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
見つかったUIA要素。見つからなければNone。
|
|
112
|
+
"""
|
|
113
|
+
property_id_name = "UIA_" + property_name + "PropertyId"
|
|
114
|
+
property_id = getattr(self._uia.UIA_dll, property_id_name)
|
|
115
|
+
condition = self._uia.iuia.CreatePropertyCondition(property_id, value)
|
|
116
|
+
scope = self._uia.tree_scope["children"]
|
|
117
|
+
result = parent.FindFirst(scope, condition)
|
|
118
|
+
# comtypesは未検出時にNoneではなくNULL COMポインタを返す場合がある。
|
|
119
|
+
if not result:
|
|
120
|
+
result = None
|
|
121
|
+
self._logger.debug(
|
|
122
|
+
f"UIA検索: scope=children property={property_name} value={value} found={result is not None}"
|
|
123
|
+
)
|
|
124
|
+
return result
|
|
125
|
+
|
|
126
|
+
def _find_descendant(self, parent, property_name: str, value: str):
|
|
127
|
+
"""子孫から一致する最初の要素を取得する。必要な時だけ使用する。"""
|
|
128
|
+
property_id_name = "UIA_" + property_name + "PropertyId"
|
|
129
|
+
property_id = getattr(self._uia.UIA_dll, property_id_name)
|
|
130
|
+
condition = self._uia.iuia.CreatePropertyCondition(property_id, value)
|
|
131
|
+
scope = self._uia.tree_scope["descendants"]
|
|
132
|
+
result = parent.FindFirst(scope, condition)
|
|
133
|
+
# NULL COMポインタはPythonのNoneと同じ「未検出」として扱う。
|
|
134
|
+
if not result:
|
|
135
|
+
result = None
|
|
136
|
+
self._logger.debug(
|
|
137
|
+
f"UIA検索: scope=descendants property={property_name} value={value} found={result is not None}"
|
|
138
|
+
)
|
|
139
|
+
return result
|
|
140
|
+
|
|
141
|
+
def _resolve_path(self, path):
|
|
142
|
+
"""ウィンドウから既知の階層を1段ずつ直下検索する。
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
path: (UIAプロパティ名, 値) の組を並べた経路。
|
|
146
|
+
None は未定義の経路を表す。
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
最終階層の UIA 要素。経路未定義または途中で不一致なら None。
|
|
150
|
+
|
|
151
|
+
Notes:
|
|
152
|
+
検索スコープは常に Children。子孫検索による補完は行わない。
|
|
153
|
+
"""
|
|
154
|
+
if path is None:
|
|
155
|
+
self._logger.warning("UIA階層パスが未定義です")
|
|
156
|
+
return None
|
|
157
|
+
self._logger.debug(f"UIA階層検索開始: {path}")
|
|
158
|
+
element = self._get_root()
|
|
159
|
+
for property_name, value in path:
|
|
160
|
+
element = self._find_child(element, property_name, value)
|
|
161
|
+
if element is None:
|
|
162
|
+
self._logger.warning(f"UIA階層検索失敗: {property_name}={value}")
|
|
163
|
+
return None
|
|
164
|
+
self._logger.debug(f"UIA階層検索成功: {path[-1]}")
|
|
165
|
+
return element
|
|
166
|
+
|
|
167
|
+
def _get_root(self):
|
|
168
|
+
"""SmartLab Studio II のメインウィンドウを特定する。
|
|
169
|
+
|
|
170
|
+
Returns:
|
|
171
|
+
AutomationId が mainShellWindow の UIA 要素。
|
|
172
|
+
|
|
173
|
+
Raises:
|
|
174
|
+
RuntimeError: 条件に一致するウィンドウが一意でない場合。
|
|
175
|
+
|
|
176
|
+
Notes:
|
|
177
|
+
キャッシュ済み要素は AutomationId を再確認してから使用する。
|
|
178
|
+
不正なキャッシュは破棄してウィンドウ一覧から再取得する。
|
|
179
|
+
"""
|
|
180
|
+
if self._root is not None:
|
|
181
|
+
try:
|
|
182
|
+
if self._root.CurrentAutomationId == self._config.ROOT_ID:
|
|
183
|
+
self._logger.debug("メインウィンドウのキャッシュを再利用")
|
|
184
|
+
return self._root
|
|
185
|
+
except Exception:
|
|
186
|
+
self._root = None
|
|
187
|
+
|
|
188
|
+
# タイトルの部分一致と AutomationId の完全一致を両方満たす候補を集める。
|
|
189
|
+
windows = []
|
|
190
|
+
desktop = Desktop(backend="uia")
|
|
191
|
+
for window in desktop.windows():
|
|
192
|
+
title = window.window_text()
|
|
193
|
+
if self._config.TARGET_TITLE not in title:
|
|
194
|
+
continue
|
|
195
|
+
|
|
196
|
+
automation_id = window.element_info.automation_id
|
|
197
|
+
if automation_id != self._config.ROOT_ID:
|
|
198
|
+
continue
|
|
199
|
+
|
|
200
|
+
windows.append(window)
|
|
201
|
+
|
|
202
|
+
if len(windows) != 1:
|
|
203
|
+
self._logger.error(f"メインウィンドウ候補数が不正: {len(windows)}")
|
|
204
|
+
raise RuntimeError(f"メインウィンドウ候補数: {len(windows)}")
|
|
205
|
+
self._logger.debug(f"メインウィンドウ取得: candidates={len(windows)}")
|
|
206
|
+
self._root = windows[0].element_info.element
|
|
207
|
+
return self._root
|
|
208
|
+
|
|
209
|
+
def _get_scan_button(self):
|
|
210
|
+
"""スキャンボタンを階層から取得し、識別情報を確認する。
|
|
211
|
+
|
|
212
|
+
Returns:
|
|
213
|
+
AutomationId・Name・ControlType が一致するボタン要素。
|
|
214
|
+
|
|
215
|
+
Raises:
|
|
216
|
+
RuntimeError: 対象が取得できないか、識別情報が一致しない場合。
|
|
217
|
+
|
|
218
|
+
Notes:
|
|
219
|
+
キャッシュされたボタンは AutomationId を確認して再利用する。
|
|
220
|
+
"""
|
|
221
|
+
if self._scan_button is not None:
|
|
222
|
+
try:
|
|
223
|
+
if self._scan_button.CurrentAutomationId == self._config.SCAN_ID:
|
|
224
|
+
self._logger.debug("スキャンボタンのキャッシュを再利用")
|
|
225
|
+
return self._scan_button
|
|
226
|
+
except Exception:
|
|
227
|
+
self._scan_button = None
|
|
228
|
+
|
|
229
|
+
element = self._resolve_path(self._config.PATHS["Z"]["scan"]["button"])
|
|
230
|
+
if element is None:
|
|
231
|
+
raise RuntimeError("スキャンボタンの階層取得失敗")
|
|
232
|
+
if element.CurrentAutomationId != self._config.SCAN_ID:
|
|
233
|
+
raise RuntimeError("スキャンボタンのAutomationIdが一致しません")
|
|
234
|
+
if element.CurrentName != self._config.SCAN_NAME:
|
|
235
|
+
raise RuntimeError("スキャンボタンのNameが一致しません")
|
|
236
|
+
button_type = self._uia.UIA_dll.UIA_ButtonControlTypeId
|
|
237
|
+
if element.CurrentControlType != button_type:
|
|
238
|
+
raise RuntimeError("スキャンボタンのControlTypeが一致しません")
|
|
239
|
+
self._scan_button = element
|
|
240
|
+
self._logger.debug("スキャンボタンを特定")
|
|
241
|
+
return element
|
|
242
|
+
|
|
243
|
+
def _invoke(self, element) -> None:
|
|
244
|
+
"""InvokePattern を持つ有効・表示中のボタンを1回実行する。
|
|
245
|
+
|
|
246
|
+
Args:
|
|
247
|
+
element: 対象の UIA 要素。
|
|
248
|
+
|
|
249
|
+
Raises:
|
|
250
|
+
RuntimeError: 無効・非表示・InvokePattern 不在の場合。
|
|
251
|
+
"""
|
|
252
|
+
if not element.CurrentIsEnabled:
|
|
253
|
+
raise RuntimeError("対象ボタンが無効です")
|
|
254
|
+
if element.CurrentIsOffscreen:
|
|
255
|
+
raise RuntimeError("対象ボタンが非表示です")
|
|
256
|
+
pattern = element.GetCurrentPattern(self._uia.UIA_dll.UIA_InvokePatternId)
|
|
257
|
+
if not pattern:
|
|
258
|
+
raise RuntimeError("InvokePatternを取得できません")
|
|
259
|
+
self._logger.debug("InvokePattern取得成功: 実行開始")
|
|
260
|
+
pattern.QueryInterface(IUIAutomationInvokePattern).Invoke()
|
|
261
|
+
self._logger.debug("InvokePattern.Invoke戻り")
|
|
262
|
+
|
|
263
|
+
def _export_data_resolve_dialog_path(self, dialog, key: str):
|
|
264
|
+
"""調査済みのダイアログ階層をChildrenで順番に取得する。"""
|
|
265
|
+
element = dialog
|
|
266
|
+
for property_name, value in self._config.EXPORT_DATA_DIALOG_PATHS[key]:
|
|
267
|
+
element = self._find_child(element, property_name, value)
|
|
268
|
+
if element is None:
|
|
269
|
+
return None
|
|
270
|
+
return element
|
|
271
|
+
|
|
272
|
+
def _export_data_wait_dialog_closed(self, dialog, key: str, timeout: float) -> None:
|
|
273
|
+
"""指定したダイアログが閉じるまで待機する。"""
|
|
274
|
+
deadline = time.monotonic() + timeout
|
|
275
|
+
if key == "export_close":
|
|
276
|
+
# export_close は閉じるボタンのパスであり、ウィンドウの生存判定には使わない。
|
|
277
|
+
# 閉じた後もCOM参照が残ることがあるため、トップレベルウィンドウを再取得する。
|
|
278
|
+
process_id = dialog.CurrentProcessId
|
|
279
|
+
self._logger.info(
|
|
280
|
+
f"export_data終了確認開始: exportDataWindow, PID={process_id}"
|
|
281
|
+
)
|
|
282
|
+
while time.monotonic() < deadline:
|
|
283
|
+
windows = Desktop(backend="uia").windows()
|
|
284
|
+
active = any(
|
|
285
|
+
window.element_info.process_id == process_id
|
|
286
|
+
and window.element_info.automation_id == "exportDataWindow"
|
|
287
|
+
and window.is_visible()
|
|
288
|
+
for window in windows
|
|
289
|
+
)
|
|
290
|
+
if not active:
|
|
291
|
+
self._logger.info("export_data終了確認完了: exportDataWindow非表示")
|
|
292
|
+
return
|
|
293
|
+
time.sleep(0.2)
|
|
294
|
+
raise RuntimeError("SmartLabエクスポート画面が閉じません: exportDataWindow")
|
|
295
|
+
|
|
296
|
+
while time.monotonic() < deadline:
|
|
297
|
+
try:
|
|
298
|
+
if self._export_data_resolve_dialog_path(dialog, key) is None:
|
|
299
|
+
self._logger.info(f"export_dataダイアログ終了確認: {key}")
|
|
300
|
+
return
|
|
301
|
+
except Exception:
|
|
302
|
+
return # 親ウィンドウ自体が閉じた場合
|
|
303
|
+
time.sleep(0.1)
|
|
304
|
+
raise RuntimeError(f"ダイアログが閉じません: {key}")
|
|
305
|
+
|
|
306
|
+
def _export_data_window_visible(self) -> bool:
|
|
307
|
+
"""SmartLab のエクスポート画面の表示を確認する。"""
|
|
308
|
+
root = self._get_root()
|
|
309
|
+
process_id = root.CurrentProcessId
|
|
310
|
+
windows = Desktop(backend="uia").windows()
|
|
311
|
+
for window in windows:
|
|
312
|
+
info = window.element_info
|
|
313
|
+
if info.process_id != process_id:
|
|
314
|
+
continue
|
|
315
|
+
if info.automation_id != "exportDataWindow":
|
|
316
|
+
continue
|
|
317
|
+
if window.is_visible():
|
|
318
|
+
return True
|
|
319
|
+
return False
|
|
320
|
+
|
|
321
|
+
def get_state(self) -> str:
|
|
322
|
+
"""GUIと操作履歴から装置の占有状態を取得する。"""
|
|
323
|
+
if self._export_active:
|
|
324
|
+
return "EXPORTING"
|
|
325
|
+
|
|
326
|
+
if self._export_data_window_visible():
|
|
327
|
+
return "EXPORTING"
|
|
328
|
+
|
|
329
|
+
dialog = self._get_dialog()
|
|
330
|
+
if dialog is not None:
|
|
331
|
+
if self._scan_phase == "SCAN_STOPPING":
|
|
332
|
+
return "SCAN_STOPPING"
|
|
333
|
+
self._scan_phase = "SCANNING"
|
|
334
|
+
return "SCANNING"
|
|
335
|
+
|
|
336
|
+
scan_group = self._resolve_path(self._config.SCAN_GROUP_PATH)
|
|
337
|
+
if scan_group is None:
|
|
338
|
+
raise RuntimeError("スキャン設定ブロックの階層取得失敗")
|
|
339
|
+
if not bool(scan_group.CurrentIsEnabled):
|
|
340
|
+
if self._scan_phase == "SCAN_STOPPING":
|
|
341
|
+
return "SCAN_STOPPING"
|
|
342
|
+
self._scan_phase = "SCAN_STARTING"
|
|
343
|
+
return "SCAN_STARTING"
|
|
344
|
+
|
|
345
|
+
self._scan_phase = None
|
|
346
|
+
return "IDLE"
|
|
347
|
+
|
|
348
|
+
@property
|
|
349
|
+
def state(self) -> str:
|
|
350
|
+
return self.get_state()
|
|
351
|
+
|
|
352
|
+
def is_ready(self) -> bool:
|
|
353
|
+
return self.get_state() == "IDLE"
|
|
354
|
+
|
|
355
|
+
def _require_idle(self, operation: str) -> None:
|
|
356
|
+
state = self.get_state()
|
|
357
|
+
if state != "IDLE":
|
|
358
|
+
self._logger.error(f"{operation}: 操作禁止 state={state}")
|
|
359
|
+
raise RuntimeError(f"{operation} は {state} 中に実行できません")
|
|
360
|
+
|
|
361
|
+
def export_data(
|
|
362
|
+
self,
|
|
363
|
+
filename: Optional[str] = None,
|
|
364
|
+
save: bool = False,
|
|
365
|
+
graph: str = "2theta",
|
|
366
|
+
overwrite: bool = False,
|
|
367
|
+
) -> bool:
|
|
368
|
+
"""指定グラフのタブを選択し、右クリックからデータをエクスポートする。
|
|
369
|
+
|
|
370
|
+
graph: "2theta" または "2theta/theta"(既定 "2theta")。
|
|
371
|
+
filenameを省略すると保存ダイアログの初期値を維持する。
|
|
372
|
+
save=Falseでは保存ダイアログを表示した状態で終了する。
|
|
373
|
+
overwrite=Falseで上書き確認が出た場合は「いいえ」後に保存をキャンセルする。
|
|
374
|
+
"""
|
|
375
|
+
self._require_idle("export_data")
|
|
376
|
+
self._export_active = True
|
|
377
|
+
self._logger.info(
|
|
378
|
+
f"export_data開始: filename={filename!r}, save={save}, graph={graph!r}, "
|
|
379
|
+
f"overwrite={overwrite}"
|
|
380
|
+
)
|
|
381
|
+
try:
|
|
382
|
+
if graph not in self._config.EXPORT_DATA_GRAPH_PATHS:
|
|
383
|
+
raise ValueError(f"未対応のグラフです: {graph!r}")
|
|
384
|
+
if filename is not None and not filename.strip():
|
|
385
|
+
raise ValueError("filenameが空です")
|
|
386
|
+
|
|
387
|
+
# 26番で確認したSelectionItemPatternによる明示的なタブ選択。
|
|
388
|
+
tab = self._resolve_path(self._config.EXPORT_DATA_TAB_PATHS[graph])
|
|
389
|
+
if tab is None:
|
|
390
|
+
raise RuntimeError(f"{graph}タブを取得できません")
|
|
391
|
+
pattern = tab.GetCurrentPattern(
|
|
392
|
+
self._uia.UIA_dll.UIA_SelectionItemPatternId
|
|
393
|
+
)
|
|
394
|
+
if not pattern:
|
|
395
|
+
raise RuntimeError(f"{graph}タブにSelectionItemPatternがありません")
|
|
396
|
+
pattern.QueryInterface(IUIAutomationSelectionItemPattern).Select()
|
|
397
|
+
self._logger.info(f"{graph}タブを選択しました")
|
|
398
|
+
|
|
399
|
+
graph_path = self._config.EXPORT_DATA_GRAPH_PATHS[graph]
|
|
400
|
+
graph_element = None
|
|
401
|
+
deadline = time.monotonic() + 5.0
|
|
402
|
+
while time.monotonic() < deadline:
|
|
403
|
+
graph_element = self._resolve_path(graph_path)
|
|
404
|
+
if graph_element is not None and not graph_element.CurrentIsOffscreen:
|
|
405
|
+
break
|
|
406
|
+
time.sleep(0.1)
|
|
407
|
+
else:
|
|
408
|
+
raise RuntimeError(f"{graph}グラフが表示状態になりません")
|
|
409
|
+
graph = graph_element
|
|
410
|
+
|
|
411
|
+
rect = graph.CurrentBoundingRectangle
|
|
412
|
+
if (rect.right - rect.left) <= 0 or (rect.bottom - rect.top) <= 0:
|
|
413
|
+
raise RuntimeError("2θ/θグラフの座標範囲が不正です")
|
|
414
|
+
graph_xy = (
|
|
415
|
+
int(rect.left + (rect.right - rect.left) / 2),
|
|
416
|
+
int(rect.top + (rect.bottom - rect.top) / 2),
|
|
417
|
+
)
|
|
418
|
+
# メニューが見えなかった場合だけ、次の入力に進む。
|
|
419
|
+
settle_seconds = 0.15
|
|
420
|
+
shifted_xy = (min(graph_xy[0] + 6, rect.right - 1), graph_xy[1])
|
|
421
|
+
buttons = ("right", "right", "left", "right")
|
|
422
|
+
attempts = [(button, graph_xy) for button in buttons]
|
|
423
|
+
if shifted_xy != graph_xy:
|
|
424
|
+
attempts.append(("move", shifted_xy))
|
|
425
|
+
attempts.extend((button, shifted_xy) for button in buttons)
|
|
426
|
+
|
|
427
|
+
root = self._get_root()
|
|
428
|
+
export_item = None
|
|
429
|
+
for button, point in attempts:
|
|
430
|
+
self._logger.info(f"グラフメニュー: {button} {point}")
|
|
431
|
+
if button == "move":
|
|
432
|
+
mouse.move(coords=point)
|
|
433
|
+
else:
|
|
434
|
+
mouse.click(button=button, coords=point)
|
|
435
|
+
time.sleep(settle_seconds)
|
|
436
|
+
deadline = time.monotonic()
|
|
437
|
+
if button == "right":
|
|
438
|
+
deadline += 1.0
|
|
439
|
+
while True:
|
|
440
|
+
element = root
|
|
441
|
+
for property_name, value in self._config.EXPORT_DATA_MENU_PATH:
|
|
442
|
+
element = self._find_child(element, property_name, value)
|
|
443
|
+
if element is None:
|
|
444
|
+
break
|
|
445
|
+
if element is not None and not element.CurrentIsOffscreen:
|
|
446
|
+
export_item = element
|
|
447
|
+
break
|
|
448
|
+
remaining = deadline - time.monotonic()
|
|
449
|
+
if remaining <= 0:
|
|
450
|
+
break
|
|
451
|
+
time.sleep(min(0.05, remaining))
|
|
452
|
+
if export_item is not None:
|
|
453
|
+
break
|
|
454
|
+
if export_item is None:
|
|
455
|
+
raise RuntimeError("右クリックメニューのエクスポートを取得できません")
|
|
456
|
+
rect = export_item.CurrentBoundingRectangle
|
|
457
|
+
menu_xy = (
|
|
458
|
+
int(rect.left + (rect.right - rect.left) / 2),
|
|
459
|
+
int(rect.top + (rect.bottom - rect.top) / 2),
|
|
460
|
+
)
|
|
461
|
+
# UIAで第1段階のメニューを展開する。
|
|
462
|
+
# 右クリック調査でAutomationIdを確認済み。
|
|
463
|
+
# ExpandCollapsePattern対応は未確認のため、取得できなければ
|
|
464
|
+
# 従来のmouse.moveへフォールバックする。
|
|
465
|
+
# 元に戻す場合: このブロックを削除し、直下の旧処理を有効化。
|
|
466
|
+
expanded_by_uia = False
|
|
467
|
+
try:
|
|
468
|
+
from comtypes.gen.UIAutomationClient import IUIAutomationExpandCollapsePattern
|
|
469
|
+
expand_pattern = export_item.GetCurrentPattern(
|
|
470
|
+
self._uia.UIA_dll.UIA_ExpandCollapsePatternId
|
|
471
|
+
)
|
|
472
|
+
if expand_pattern:
|
|
473
|
+
expand_pattern.QueryInterface(
|
|
474
|
+
IUIAutomationExpandCollapsePattern
|
|
475
|
+
).Expand()
|
|
476
|
+
expanded_by_uia = True
|
|
477
|
+
self._logger.info("エクスポート項目をUIA ExpandCollapsePatternで展開")
|
|
478
|
+
except Exception as exc:
|
|
479
|
+
self._logger.info(
|
|
480
|
+
f"エクスポート項目のUIA展開不可、従来方式に切替: "
|
|
481
|
+
f"{type(exc).__name__}: {exc}"
|
|
482
|
+
)
|
|
483
|
+
if not expanded_by_uia:
|
|
484
|
+
self._logger.info("エクスポート項目にマウスを移動してサブメニューを表示")
|
|
485
|
+
mouse.move(coords=menu_xy)
|
|
486
|
+
# 旧処理(復元用):
|
|
487
|
+
# self._logger.info("エクスポート項目にマウスを移動してサブメニューを表示")
|
|
488
|
+
# mouse.move(coords=menu_xy)
|
|
489
|
+
time.sleep(settle_seconds)
|
|
490
|
+
|
|
491
|
+
data_item = None
|
|
492
|
+
deadline = time.monotonic() + 15.0
|
|
493
|
+
while time.monotonic() < deadline:
|
|
494
|
+
popup = self._find_child(root, "ClassName", "Popup")
|
|
495
|
+
if popup is not None:
|
|
496
|
+
submenu = self._find_child(popup, "ClassName", "Popup")
|
|
497
|
+
if submenu is not None:
|
|
498
|
+
data_item = self._find_child(
|
|
499
|
+
submenu, "Name", "データをエクスポート..."
|
|
500
|
+
)
|
|
501
|
+
if data_item is None:
|
|
502
|
+
menu = self._find_child(popup, "AutomationId", "contextMenu")
|
|
503
|
+
if menu is not None:
|
|
504
|
+
item = self._find_child(
|
|
505
|
+
menu, "AutomationId", "contextMenuItem_Export"
|
|
506
|
+
)
|
|
507
|
+
if item is not None:
|
|
508
|
+
data_item = self._find_child(
|
|
509
|
+
item, "Name", "データをエクスポート..."
|
|
510
|
+
)
|
|
511
|
+
if data_item is not None and not data_item.CurrentIsOffscreen:
|
|
512
|
+
break
|
|
513
|
+
time.sleep(0.1)
|
|
514
|
+
else:
|
|
515
|
+
raise RuntimeError("サブメニューのデータをエクスポートを取得できません")
|
|
516
|
+
rect = data_item.CurrentBoundingRectangle
|
|
517
|
+
data_xy = (
|
|
518
|
+
int(rect.left + (rect.right - rect.left) / 2),
|
|
519
|
+
int(rect.top + (rect.bottom - rect.top) / 2),
|
|
520
|
+
)
|
|
521
|
+
self._logger.info("データをエクスポート...をクリック")
|
|
522
|
+
mouse.click(button="left", coords=data_xy)
|
|
523
|
+
time.sleep(settle_seconds)
|
|
524
|
+
|
|
525
|
+
# 20番で確認したトップレベル exportDataWindow。
|
|
526
|
+
process_id = root.CurrentProcessId
|
|
527
|
+
dialog = None
|
|
528
|
+
deadline = time.monotonic() + 15.0
|
|
529
|
+
while time.monotonic() < deadline:
|
|
530
|
+
for window in Desktop(backend="uia").windows():
|
|
531
|
+
info = window.element_info
|
|
532
|
+
if info.process_id != process_id:
|
|
533
|
+
continue
|
|
534
|
+
if info.automation_id == "exportDataWindow":
|
|
535
|
+
dialog = info.element
|
|
536
|
+
break
|
|
537
|
+
if dialog is not None:
|
|
538
|
+
break
|
|
539
|
+
time.sleep(0.1)
|
|
540
|
+
if dialog is None:
|
|
541
|
+
raise RuntimeError("エクスポート画面を取得できません")
|
|
542
|
+
self._logger.info("エクスポート画面取得: データ選択の初期値を維持")
|
|
543
|
+
export_button = self._export_data_resolve_dialog_path(dialog, "export_button")
|
|
544
|
+
if export_button is None:
|
|
545
|
+
raise RuntimeError("エクスポートボタンを取得できません")
|
|
546
|
+
self._invoke(export_button)
|
|
547
|
+
|
|
548
|
+
# 20番の記録ではWindows保存ダイアログはexportDataWindowの子。
|
|
549
|
+
save_dialog = None
|
|
550
|
+
deadline = time.monotonic() + 15.0
|
|
551
|
+
while time.monotonic() < deadline:
|
|
552
|
+
save_dialog = self._export_data_resolve_dialog_path(
|
|
553
|
+
dialog, "save_dialog"
|
|
554
|
+
)
|
|
555
|
+
if save_dialog is not None:
|
|
556
|
+
break
|
|
557
|
+
time.sleep(0.1)
|
|
558
|
+
if save_dialog is None:
|
|
559
|
+
raise RuntimeError("Windows保存ダイアログを取得できません")
|
|
560
|
+
edit = self._export_data_resolve_dialog_path(dialog, "filename_edit")
|
|
561
|
+
if edit is None:
|
|
562
|
+
raise RuntimeError("ファイル名入力欄の固定階層を取得できません")
|
|
563
|
+
pattern = edit.GetCurrentPattern(self._uia.UIA_dll.UIA_ValuePatternId)
|
|
564
|
+
if not pattern:
|
|
565
|
+
raise RuntimeError("ファイル名入力欄にValuePatternがありません")
|
|
566
|
+
value = pattern.QueryInterface(IUIAutomationValuePattern)
|
|
567
|
+
selected_filename = value.CurrentValue
|
|
568
|
+
if filename is not None:
|
|
569
|
+
self._logger.info(f"ファイル名を設定: {filename!r}")
|
|
570
|
+
value.SetValue(filename)
|
|
571
|
+
selected_filename = filename
|
|
572
|
+
else:
|
|
573
|
+
self._logger.info("保存先とファイル名の初期値を維持")
|
|
574
|
+
if not selected_filename:
|
|
575
|
+
raise RuntimeError("保存ファイル名が空です")
|
|
576
|
+
if save:
|
|
577
|
+
save_button = self._export_data_resolve_dialog_path(
|
|
578
|
+
dialog, "save_button"
|
|
579
|
+
)
|
|
580
|
+
if save_button is None:
|
|
581
|
+
raise RuntimeError("保存ボタンを取得できません")
|
|
582
|
+
if save_button.CurrentControlType != self._uia.UIA_dll.UIA_ButtonControlTypeId:
|
|
583
|
+
raise RuntimeError("保存ボタンのControlTypeが不正です")
|
|
584
|
+
self._logger.info("保存ボタンを実行")
|
|
585
|
+
self._invoke(save_button)
|
|
586
|
+
self._logger.info("export_data状態: 保存ボタンInvoke完了、確認ダイアログ待機")
|
|
587
|
+
|
|
588
|
+
deadline = time.monotonic() + 15.0
|
|
589
|
+
confirmation_handled = False
|
|
590
|
+
while time.monotonic() < deadline:
|
|
591
|
+
save_dialog = self._export_data_resolve_dialog_path(
|
|
592
|
+
dialog, "save_dialog"
|
|
593
|
+
)
|
|
594
|
+
if save_dialog is None:
|
|
595
|
+
self._logger.info("Windows保存ダイアログが閉じました")
|
|
596
|
+
break
|
|
597
|
+
confirmation = self._export_data_resolve_dialog_path(
|
|
598
|
+
dialog, "confirmation"
|
|
599
|
+
)
|
|
600
|
+
if confirmation is not None:
|
|
601
|
+
message = self._export_data_resolve_dialog_path(
|
|
602
|
+
dialog, "confirmation_message"
|
|
603
|
+
)
|
|
604
|
+
content = message.CurrentName if message is not None else ""
|
|
605
|
+
if "既に存在します" not in content or "置き換えますか" not in content:
|
|
606
|
+
raise RuntimeError(f"想定外の保存確認ダイアログです: {content!r}")
|
|
607
|
+
key = "confirmation_yes" if overwrite else "confirmation_no"
|
|
608
|
+
button = self._export_data_resolve_dialog_path(dialog, key)
|
|
609
|
+
if button is None:
|
|
610
|
+
raise RuntimeError("上書き確認ボタンを取得できません")
|
|
611
|
+
self._logger.info(f"export_data状態: 上書き確認表示、選択={key}, message={content!r}")
|
|
612
|
+
self._invoke(button)
|
|
613
|
+
confirmation_handled = True
|
|
614
|
+
self._logger.info(f"上書き確認: {'はい' if overwrite else 'いいえ'}")
|
|
615
|
+
if not overwrite:
|
|
616
|
+
self._export_data_wait_dialog_closed(
|
|
617
|
+
dialog, "confirmation", 10.0
|
|
618
|
+
)
|
|
619
|
+
cancel = self._export_data_resolve_dialog_path(
|
|
620
|
+
dialog, "save_cancel"
|
|
621
|
+
)
|
|
622
|
+
if cancel is None:
|
|
623
|
+
raise RuntimeError("保存ダイアログのキャンセルボタンがありません")
|
|
624
|
+
self._logger.info("export_data状態: Windows保存ダイアログのキャンセルをInvoke")
|
|
625
|
+
self._invoke(cancel)
|
|
626
|
+
self._logger.info("Windows保存ダイアログをキャンセル")
|
|
627
|
+
self._export_data_wait_dialog_closed(
|
|
628
|
+
dialog, "save_dialog", 10.0
|
|
629
|
+
)
|
|
630
|
+
close = self._export_data_resolve_dialog_path(
|
|
631
|
+
dialog, "export_close"
|
|
632
|
+
)
|
|
633
|
+
if close is None:
|
|
634
|
+
raise RuntimeError("エクスポート画面の閉じるボタンがありません")
|
|
635
|
+
self._logger.info("export_data状態: SmartLabエクスポート画面の閉じるをInvoke")
|
|
636
|
+
self._invoke(close)
|
|
637
|
+
self._logger.info("エクスポート画面の閉じるInvoke完了。ウィンドウ消滅を確認します")
|
|
638
|
+
self._export_data_wait_dialog_closed(
|
|
639
|
+
dialog, "export_close", 10.0
|
|
640
|
+
)
|
|
641
|
+
self._logger.info("export_data結果: False(上書き拒否・GUIキャンセル完了)")
|
|
642
|
+
return False
|
|
643
|
+
time.sleep(0.1)
|
|
644
|
+
else:
|
|
645
|
+
raise RuntimeError("保存ダイアログの終了を確認できません")
|
|
646
|
+
self._logger.info(
|
|
647
|
+
f"保存処理終了: 上書き確認={'あり' if confirmation_handled else 'なし'}"
|
|
648
|
+
)
|
|
649
|
+
else:
|
|
650
|
+
self._logger.info("save=False: 保存ダイアログを開いたまま終了")
|
|
651
|
+
self._logger.info(f"export_data完了: {selected_filename!r}")
|
|
652
|
+
result = bool(save)
|
|
653
|
+
self._logger.info(f"export_data結果: {result}(save={save})")
|
|
654
|
+
return result
|
|
655
|
+
except Exception as exc:
|
|
656
|
+
self._logger.error(f"export_data失敗: {type(exc).__name__}: {exc}")
|
|
657
|
+
raise
|
|
658
|
+
finally:
|
|
659
|
+
self._export_active = False
|
|
660
|
+
|
|
661
|
+
def scan(self) -> None:
|
|
662
|
+
"""スキャンボタンを確認入力なしで1回押す。
|
|
663
|
+
|
|
664
|
+
Raises:
|
|
665
|
+
RuntimeError: 対象ボタンを取得できない、または操作できない場合。
|
|
666
|
+
"""
|
|
667
|
+
self._require_idle("scan")
|
|
668
|
+
self._logger.info("scan() 呼び出し: スキャン開始ボタンを実行")
|
|
669
|
+
self._invoke(self._get_scan_button())
|
|
670
|
+
self._scan_phase = "SCAN_STARTING"
|
|
671
|
+
self._clear_dialog_cache()
|
|
672
|
+
self._logger.info("scan: スキャン開始ボタンの実行完了")
|
|
673
|
+
|
|
674
|
+
def _get_dialog(self):
|
|
675
|
+
"""測定中ダイアログを取得する。不在ならNoneを返す。
|
|
676
|
+
|
|
677
|
+
09の実測で確認した見出しを探し、RawViewWalkerで親Windowへ戻る。
|
|
678
|
+
ダイアログのキャッシュは見出しの可視性で検証し、終了後の
|
|
679
|
+
古いダイアログを誤って使わないようにする。
|
|
680
|
+
"""
|
|
681
|
+
if self._dialog is not None:
|
|
682
|
+
self._logger.debug("ダイアログキャッシュを確認")
|
|
683
|
+
try:
|
|
684
|
+
heading = self._find_descendant(
|
|
685
|
+
self._dialog, "Name", self._config.DIALOG_NAME
|
|
686
|
+
)
|
|
687
|
+
if heading is not None and not heading.CurrentIsOffscreen:
|
|
688
|
+
self._logger.debug("測定ダイアログのキャッシュを再利用")
|
|
689
|
+
return self._dialog
|
|
690
|
+
except Exception:
|
|
691
|
+
pass
|
|
692
|
+
self._clear_dialog_cache()
|
|
693
|
+
|
|
694
|
+
root = self._get_root()
|
|
695
|
+
self._logger.debug("ダイアログ新規検索")
|
|
696
|
+
heading = self._find_descendant(root, "Name", self._config.DIALOG_NAME)
|
|
697
|
+
if heading is None:
|
|
698
|
+
self._logger.debug("装置制御中の見出しなし: 測定中ダイアログ不在")
|
|
699
|
+
return None
|
|
700
|
+
if heading.CurrentIsOffscreen:
|
|
701
|
+
self._logger.debug("測定ダイアログの見出しは非表示")
|
|
702
|
+
return None
|
|
703
|
+
current = heading
|
|
704
|
+
walker = self._uia.iuia.RawViewWalker
|
|
705
|
+
for _ in range(48):
|
|
706
|
+
if self._uia.iuia.CompareElements(current, root):
|
|
707
|
+
break
|
|
708
|
+
if current.CurrentControlType == self._uia.UIA_dll.UIA_WindowControlTypeId:
|
|
709
|
+
self._dialog = current
|
|
710
|
+
self._logger.debug("測定ダイアログを特定")
|
|
711
|
+
return current
|
|
712
|
+
current = walker.GetParentElement(current)
|
|
713
|
+
if not current:
|
|
714
|
+
break
|
|
715
|
+
raise RuntimeError("装置制御中の親Windowを特定できません")
|
|
716
|
+
|
|
717
|
+
def is_running(self) -> bool:
|
|
718
|
+
"""測定準備・測定・終了処理・エクスポートを含む占有状態。"""
|
|
719
|
+
state = self.get_state()
|
|
720
|
+
return state != "IDLE"
|
|
721
|
+
|
|
722
|
+
@property
|
|
723
|
+
def running(self) -> bool:
|
|
724
|
+
"""測定状態を取得する。is_running() と同じ結果を返す。"""
|
|
725
|
+
return self.is_running()
|
|
726
|
+
|
|
727
|
+
def get_running(self) -> bool:
|
|
728
|
+
"""測定状態を取得する。is_running() と同じ結果を返す。"""
|
|
729
|
+
return self.is_running()
|
|
730
|
+
|
|
731
|
+
def stop(self) -> None:
|
|
732
|
+
"""装置制御中ダイアログの中止ボタンを即時に1回押す。
|
|
733
|
+
|
|
734
|
+
Raises:
|
|
735
|
+
RuntimeError: ダイアログやボタンを確認できない場合。
|
|
736
|
+
"""
|
|
737
|
+
state = self.get_state()
|
|
738
|
+
if state != "SCANNING":
|
|
739
|
+
raise RuntimeError(f"stop は {state} 中に実行できません")
|
|
740
|
+
self._logger.info("stop() 呼び出し: 測定中止ボタンを実行")
|
|
741
|
+
self._logger.debug("stop: ダイアログ確認")
|
|
742
|
+
dialog = self._get_dialog()
|
|
743
|
+
if dialog is None:
|
|
744
|
+
self._logger.error("stop: 装置制御中ダイアログが確認できません")
|
|
745
|
+
raise RuntimeError("装置制御中ダイアログが確認できません")
|
|
746
|
+
button = self._stop_button
|
|
747
|
+
if button is not None:
|
|
748
|
+
try:
|
|
749
|
+
if button.CurrentAutomationId != self._config.STOP_ID:
|
|
750
|
+
button = None
|
|
751
|
+
except Exception:
|
|
752
|
+
button = None
|
|
753
|
+
if button is None:
|
|
754
|
+
button = self._find_descendant(dialog, "AutomationId", self._config.STOP_ID)
|
|
755
|
+
if button is None:
|
|
756
|
+
self._logger.error("stop: 中止ボタンが見つかりません")
|
|
757
|
+
raise RuntimeError("中止ボタンが見つかりません")
|
|
758
|
+
if button.CurrentName != "中止":
|
|
759
|
+
raise RuntimeError("中止ボタンのNameが一致しません")
|
|
760
|
+
button_type = self._uia.UIA_dll.UIA_ButtonControlTypeId
|
|
761
|
+
if button.CurrentControlType != button_type:
|
|
762
|
+
raise RuntimeError("中止ボタンのControlTypeが一致しません")
|
|
763
|
+
self._stop_button = button
|
|
764
|
+
self._logger.debug("stop: 中止ボタンを特定。Invoke実行")
|
|
765
|
+
try:
|
|
766
|
+
self._invoke(button)
|
|
767
|
+
self._scan_phase = "SCAN_STOPPING"
|
|
768
|
+
self._logger.info("stop: 中止ボタンの実行完了")
|
|
769
|
+
finally:
|
|
770
|
+
# 中止操作でダイアログが破棄される可能性がある。
|
|
771
|
+
# 次の状態照会では必ず新規検索する。
|
|
772
|
+
self._clear_dialog_cache()
|
|
773
|
+
self._logger.debug("stop: ダイアログキャッシュ破棄")
|
|
774
|
+
|
|
775
|
+
def _read_remaining(self) -> Optional[Tuple[int, int, int]]:
|
|
776
|
+
"""残り時間表示を取得する。表示がなければNoneを返す。
|
|
777
|
+
|
|
778
|
+
12.pyの失敗原因だったCurrentName=Noneを正規表現に渡さない。
|
|
779
|
+
初回だけダイアログ内部を列挙し、次回以降は該当要素を直接読む。
|
|
780
|
+
"""
|
|
781
|
+
dialog = self._get_dialog()
|
|
782
|
+
if dialog is None:
|
|
783
|
+
self._logger.debug("残り時間: 測定ダイアログなし")
|
|
784
|
+
return None
|
|
785
|
+
if self._remaining_element is not None:
|
|
786
|
+
try:
|
|
787
|
+
name = self._remaining_element.CurrentName
|
|
788
|
+
if isinstance(name, str):
|
|
789
|
+
match = self.REMAINING_RE.search(name)
|
|
790
|
+
if match:
|
|
791
|
+
self._logger.debug("残り時間: キャッシュした要素から取得")
|
|
792
|
+
return self._parse_remaining(match)
|
|
793
|
+
except Exception:
|
|
794
|
+
pass
|
|
795
|
+
self._remaining_element = None
|
|
796
|
+
|
|
797
|
+
scope = self._uia.tree_scope["descendants"]
|
|
798
|
+
self._logger.debug("残り時間: ダイアログ内部を検索")
|
|
799
|
+
elements = dialog.FindAll(scope, self._uia.true_condition)
|
|
800
|
+
for index in range(elements.Length):
|
|
801
|
+
element = elements.GetElement(index)
|
|
802
|
+
if element.CurrentIsOffscreen:
|
|
803
|
+
continue
|
|
804
|
+
name = element.CurrentName
|
|
805
|
+
if not isinstance(name, str):
|
|
806
|
+
continue
|
|
807
|
+
match = self.REMAINING_RE.search(name)
|
|
808
|
+
if match:
|
|
809
|
+
self._remaining_element = element
|
|
810
|
+
self._logger.debug(f"残り時間要素を特定: index={index} name={name}")
|
|
811
|
+
return self._parse_remaining(match)
|
|
812
|
+
self._logger.debug("残り時間: 対象要素が見つかりません")
|
|
813
|
+
return None
|
|
814
|
+
|
|
815
|
+
@staticmethod
|
|
816
|
+
def _parse_remaining(match) -> Optional[Tuple[int, int, int]]:
|
|
817
|
+
"""残り時間の時分秒を検証して返す。"""
|
|
818
|
+
# 3項目を明示的に変換する。短縮表記は使用しない。
|
|
819
|
+
hours = int(match.group(1))
|
|
820
|
+
minutes = int(match.group(2))
|
|
821
|
+
seconds = int(match.group(3))
|
|
822
|
+
if minutes >= 60:
|
|
823
|
+
return None
|
|
824
|
+
if seconds >= 60:
|
|
825
|
+
return None
|
|
826
|
+
return hours, minutes, seconds
|
|
827
|
+
|
|
828
|
+
def get_remaining_time(self) -> Optional[str]:
|
|
829
|
+
"""残り時間を取得する。
|
|
830
|
+
|
|
831
|
+
Returns:
|
|
832
|
+
Optional[str]: HH:MM:SS形式の残り時間。取得できなければNone。
|
|
833
|
+
"""
|
|
834
|
+
self._logger.info("get_remaining_time() 呼び出し: 残り時間を取得")
|
|
835
|
+
value = self._read_remaining()
|
|
836
|
+
if value is None:
|
|
837
|
+
self._logger.info("残り時間の取得結果: None")
|
|
838
|
+
return None
|
|
839
|
+
hours, minutes, seconds = value
|
|
840
|
+
result = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
|
|
841
|
+
self._logger.info(f"残り時間の取得結果: {result}")
|
|
842
|
+
return result
|
|
843
|
+
|
|
844
|
+
@property
|
|
845
|
+
def remaining_time(self) -> Optional[str]:
|
|
846
|
+
"""残り時間を取得する。get_remaining_time() と同じ結果を返す。"""
|
|
847
|
+
return self.get_remaining_time()
|
|
848
|
+
|
|
849
|
+
def get_estimated_end_time(self) -> Optional[datetime]:
|
|
850
|
+
"""残り時間から推定した終了日時を取得する。
|
|
851
|
+
|
|
852
|
+
Returns:
|
|
853
|
+
Optional[datetime]: 推定終了日時。取得できなければNone。
|
|
854
|
+
"""
|
|
855
|
+
self._logger.info("get_estimated_end_time() 呼び出し: 推定終了日時を取得")
|
|
856
|
+
value = self._read_remaining()
|
|
857
|
+
if value is None:
|
|
858
|
+
self._logger.info("推定終了日時の取得結果: None")
|
|
859
|
+
return None
|
|
860
|
+
hours, minutes, seconds = value
|
|
861
|
+
remaining = timedelta(
|
|
862
|
+
hours=hours,
|
|
863
|
+
minutes=minutes,
|
|
864
|
+
seconds=seconds,
|
|
865
|
+
)
|
|
866
|
+
current_time = datetime.now().astimezone()
|
|
867
|
+
result = current_time + remaining
|
|
868
|
+
self._logger.info(f"推定終了日時の取得結果: {result}")
|
|
869
|
+
return result
|
|
870
|
+
|
|
871
|
+
@property
|
|
872
|
+
def estimated_end_time(self) -> Optional[datetime]:
|
|
873
|
+
"""推定終了日時を取得する。get_estimated_end_time() と同じ結果を返す。"""
|
|
874
|
+
return self.get_estimated_end_time()
|
|
875
|
+
|
|
876
|
+
def _get_parameter_element(self, automation_id: str):
|
|
877
|
+
"""AutomationId からZ軸の入力欄を取得する。
|
|
878
|
+
|
|
879
|
+
Args:
|
|
880
|
+
automation_id: PARAMETER_PATH_KEYS に登録された識別子。
|
|
881
|
+
|
|
882
|
+
Returns:
|
|
883
|
+
識別子が一致し、表示中の UIA 要素。取得できなければ None。
|
|
884
|
+
|
|
885
|
+
Notes:
|
|
886
|
+
PATHS から経路を選択し、各階層を Children で検索する。
|
|
887
|
+
GUI 状態によって対象が存在しない場合は None を返す。
|
|
888
|
+
"""
|
|
889
|
+
path_key = self._config.PARAMETER_PATH_KEYS.get(automation_id)
|
|
890
|
+
if path_key is None:
|
|
891
|
+
self._logger.warning(f"未登録のパラメータ: {automation_id}")
|
|
892
|
+
return None
|
|
893
|
+
group, name = path_key
|
|
894
|
+
path = self._config.PATHS["Z"][group][name]
|
|
895
|
+
try:
|
|
896
|
+
element = self._resolve_path(path)
|
|
897
|
+
if element is None:
|
|
898
|
+
self._logger.warning(f"パラメータ要素が見つかりません: {automation_id}")
|
|
899
|
+
return None
|
|
900
|
+
if element.CurrentAutomationId != automation_id:
|
|
901
|
+
self._logger.warning(f"パラメータAutomationId不一致: {automation_id}")
|
|
902
|
+
return None
|
|
903
|
+
if element.CurrentIsOffscreen:
|
|
904
|
+
self._logger.warning(f"パラメータ要素が非表示: {automation_id}")
|
|
905
|
+
return None
|
|
906
|
+
self._logger.debug(f"パラメータ要素取得成功: {automation_id}")
|
|
907
|
+
return element
|
|
908
|
+
except Exception:
|
|
909
|
+
self._logger.error(
|
|
910
|
+
f"パラメータ要素取得不可: {automation_id}: {traceback.format_exc()}"
|
|
911
|
+
)
|
|
912
|
+
return None
|
|
913
|
+
|
|
914
|
+
def _get_parameter(self, automation_id: str, selection: bool = False):
|
|
915
|
+
"""UIAパターンから現在のパラメータ値を読み取る。
|
|
916
|
+
|
|
917
|
+
Args:
|
|
918
|
+
automation_id: 対象フィールドの AutomationId。
|
|
919
|
+
selection: True の場合は SelectionItemPattern の選択状態を読む。
|
|
920
|
+
False の場合は ValuePattern の文字列値を読む。
|
|
921
|
+
|
|
922
|
+
Returns:
|
|
923
|
+
選択状態は bool、数値・選択欄は str。取得不可なら None。
|
|
924
|
+
"""
|
|
925
|
+
self._require_idle(f"get_{automation_id}")
|
|
926
|
+
self._logger.info(f"パラメータ取得開始: {automation_id}")
|
|
927
|
+
element = self._get_parameter_element(automation_id)
|
|
928
|
+
if element is None:
|
|
929
|
+
self._logger.warning(f"パラメータ取得失敗: {automation_id} (要素なし)")
|
|
930
|
+
return None
|
|
931
|
+
try:
|
|
932
|
+
if selection:
|
|
933
|
+
pattern = element.GetCurrentPattern(
|
|
934
|
+
self._uia.UIA_dll.UIA_SelectionItemPatternId
|
|
935
|
+
)
|
|
936
|
+
if not pattern:
|
|
937
|
+
self._logger.warning(f"SelectionItemPatternなし: {automation_id}")
|
|
938
|
+
return None
|
|
939
|
+
result = bool(
|
|
940
|
+
pattern.QueryInterface(
|
|
941
|
+
IUIAutomationSelectionItemPattern
|
|
942
|
+
).CurrentIsSelected
|
|
943
|
+
)
|
|
944
|
+
self._logger.info(f"パラメータ取得結果: {automation_id}={result}")
|
|
945
|
+
return result
|
|
946
|
+
pattern = element.GetCurrentPattern(self._uia.UIA_dll.UIA_ValuePatternId)
|
|
947
|
+
if not pattern:
|
|
948
|
+
self._logger.warning(f"ValuePatternなし: {automation_id}")
|
|
949
|
+
return None
|
|
950
|
+
result = str(pattern.QueryInterface(IUIAutomationValuePattern).CurrentValue)
|
|
951
|
+
self._logger.info(f"パラメータ取得結果: {automation_id}={result}")
|
|
952
|
+
return result
|
|
953
|
+
except Exception:
|
|
954
|
+
self._logger.error(
|
|
955
|
+
f"パラメータ取得不可: {automation_id}: {traceback.format_exc()}"
|
|
956
|
+
)
|
|
957
|
+
return None
|
|
958
|
+
|
|
959
|
+
def _set_parameter(self, automation_id: str, value: str, selection: bool = False):
|
|
960
|
+
"""対象パラメータへ値を設定する。
|
|
961
|
+
|
|
962
|
+
Args:
|
|
963
|
+
automation_id: 対象フィールドの AutomationId。
|
|
964
|
+
value: ValuePattern には文字列化して渡す。
|
|
965
|
+
SelectionItemPattern では True のとき Select を呼び出す。
|
|
966
|
+
selection: True ならラジオボタンの選択、False なら値入力。
|
|
967
|
+
|
|
968
|
+
Returns:
|
|
969
|
+
UIA の設定呼び出しが成功した場合 True。
|
|
970
|
+
要素取得不可・無効・読み取り専用・設定失敗の場合 None。
|
|
971
|
+
|
|
972
|
+
Notes:
|
|
973
|
+
戻り値 True は UIA 呼び出しの成功を表す。
|
|
974
|
+
設定後の値の再読込や装置側の受理確認は行わない。
|
|
975
|
+
"""
|
|
976
|
+
self._require_idle(f"set_{automation_id}")
|
|
977
|
+
self._logger.info(f"パラメータ設定開始: {automation_id}={value}")
|
|
978
|
+
element = self._get_parameter_element(automation_id)
|
|
979
|
+
if element is None:
|
|
980
|
+
self._logger.warning(f"パラメータ設定失敗: {automation_id} (要素なし)")
|
|
981
|
+
return None
|
|
982
|
+
try:
|
|
983
|
+
if not element.CurrentIsEnabled:
|
|
984
|
+
self._logger.warning(f"パラメータ要素が無効: {automation_id}")
|
|
985
|
+
return None
|
|
986
|
+
if selection:
|
|
987
|
+
pattern = element.GetCurrentPattern(
|
|
988
|
+
self._uia.UIA_dll.UIA_SelectionItemPatternId
|
|
989
|
+
)
|
|
990
|
+
if not pattern:
|
|
991
|
+
self._logger.warning(f"SelectionItemPatternなし: {automation_id}")
|
|
992
|
+
return None
|
|
993
|
+
selection_pattern = pattern.QueryInterface(
|
|
994
|
+
IUIAutomationSelectionItemPattern
|
|
995
|
+
)
|
|
996
|
+
if value is True:
|
|
997
|
+
selection_pattern.Select()
|
|
998
|
+
self._logger.info(f"パラメータ設定完了: {automation_id}={value}")
|
|
999
|
+
return True
|
|
1000
|
+
self._logger.warning(
|
|
1001
|
+
f"選択値がTrueではありません: {automation_id}={value}"
|
|
1002
|
+
)
|
|
1003
|
+
return None
|
|
1004
|
+
pattern = element.GetCurrentPattern(self._uia.UIA_dll.UIA_ValuePatternId)
|
|
1005
|
+
if not pattern:
|
|
1006
|
+
self._logger.warning(f"ValuePatternなし: {automation_id}")
|
|
1007
|
+
return None
|
|
1008
|
+
value_pattern = pattern.QueryInterface(IUIAutomationValuePattern)
|
|
1009
|
+
if value_pattern.CurrentIsReadOnly:
|
|
1010
|
+
self._logger.warning(f"パラメータが読み取り専用: {automation_id}")
|
|
1011
|
+
return None
|
|
1012
|
+
value_pattern.SetValue(str(value))
|
|
1013
|
+
self._logger.info(f"パラメータ設定完了: {automation_id}={value}")
|
|
1014
|
+
return True
|
|
1015
|
+
except Exception:
|
|
1016
|
+
self._logger.error(
|
|
1017
|
+
f"パラメータ設定不可: {automation_id}: {traceback.format_exc()}"
|
|
1018
|
+
)
|
|
1019
|
+
return None
|
|
1020
|
+
|
|
1021
|
+
def get_absolute_scan(self):
|
|
1022
|
+
"""スキャン条件の絶対指定の現在値を取得する。"""
|
|
1023
|
+
return self._get_parameter("Manual_RadioButtonAbsoluteScan", selection=True)
|
|
1024
|
+
|
|
1025
|
+
def set_absolute_scan(self, value):
|
|
1026
|
+
"""スキャン条件の絶対指定の値を変更する。"""
|
|
1027
|
+
return self._set_parameter(
|
|
1028
|
+
"Manual_RadioButtonAbsoluteScan", value, selection=True
|
|
1029
|
+
)
|
|
1030
|
+
|
|
1031
|
+
def get_relative_scan(self):
|
|
1032
|
+
"""スキャン条件の相対指定の現在値を取得する。"""
|
|
1033
|
+
return self._get_parameter("Manual_RadioButtonRelativeScan", selection=True)
|
|
1034
|
+
|
|
1035
|
+
def set_relative_scan(self, value):
|
|
1036
|
+
"""スキャン条件の相対指定の値を変更する。"""
|
|
1037
|
+
return self._set_parameter(
|
|
1038
|
+
"Manual_RadioButtonRelativeScan", value, selection=True
|
|
1039
|
+
)
|
|
1040
|
+
|
|
1041
|
+
def get_scan_mode(self):
|
|
1042
|
+
"""スキャン条件のスキャンモードの現在値を取得する。"""
|
|
1043
|
+
return self._get_parameter("Manual_ComboBoxScanMode")
|
|
1044
|
+
|
|
1045
|
+
def set_scan_mode(self, value):
|
|
1046
|
+
"""スキャン条件のスキャンモードの値を変更する。"""
|
|
1047
|
+
return self._set_parameter("Manual_ComboBoxScanMode", value)
|
|
1048
|
+
|
|
1049
|
+
def get_start(self):
|
|
1050
|
+
"""スキャン条件の開始の現在値を取得する。"""
|
|
1051
|
+
return self._get_parameter("Manual_TextBoxStart")
|
|
1052
|
+
|
|
1053
|
+
def set_start(self, value):
|
|
1054
|
+
"""スキャン条件の開始の値を変更する。"""
|
|
1055
|
+
return self._set_parameter("Manual_TextBoxStart", value)
|
|
1056
|
+
|
|
1057
|
+
def get_stop_position(self):
|
|
1058
|
+
"""スキャン条件の終了の現在値を取得する。"""
|
|
1059
|
+
return self._get_parameter("Manual_TextBoxStop")
|
|
1060
|
+
|
|
1061
|
+
def set_stop_position(self, value):
|
|
1062
|
+
"""スキャン条件の終了の値を変更する。"""
|
|
1063
|
+
return self._set_parameter("Manual_TextBoxStop", value)
|
|
1064
|
+
|
|
1065
|
+
def get_step(self):
|
|
1066
|
+
"""スキャン条件のステップの現在値を取得する。"""
|
|
1067
|
+
return self._get_parameter("Manual_TextBoxStep")
|
|
1068
|
+
|
|
1069
|
+
def set_step(self, value):
|
|
1070
|
+
"""スキャン条件のステップの値を変更する。"""
|
|
1071
|
+
return self._set_parameter("Manual_TextBoxStep", value)
|
|
1072
|
+
|
|
1073
|
+
def get_speed(self):
|
|
1074
|
+
"""スキャン条件のスピードの現在値を取得する。"""
|
|
1075
|
+
return self._get_parameter("Manual_TextBoxSpeedTimeCount")
|
|
1076
|
+
|
|
1077
|
+
def set_speed(self, value):
|
|
1078
|
+
"""スキャン条件のスピードの値を変更する。"""
|
|
1079
|
+
return self._set_parameter("Manual_TextBoxSpeedTimeCount", value)
|
|
1080
|
+
|
|
1081
|
+
def get_move_to(self):
|
|
1082
|
+
"""直接移動条件の移動先の現在値を取得する。"""
|
|
1083
|
+
return self._get_parameter("Manual_TextBoxMoveTo")
|
|
1084
|
+
|
|
1085
|
+
def set_move_to(self, value):
|
|
1086
|
+
"""直接移動条件の移動先の値を変更する。"""
|
|
1087
|
+
return self._set_parameter("Manual_TextBoxMoveTo", value)
|
|
1088
|
+
|
|
1089
|
+
def get_move_step(self):
|
|
1090
|
+
"""直接移動条件のステップの現在値を取得する。"""
|
|
1091
|
+
return self._get_parameter("Manual_TextBoxMoveStep")
|
|
1092
|
+
|
|
1093
|
+
def set_move_step(self, value):
|
|
1094
|
+
"""直接移動条件のステップの値を変更する。"""
|
|
1095
|
+
return self._set_parameter("Manual_TextBoxMoveStep", value)
|
|
1096
|
+
|
|
1097
|
+
def get_slit(self):
|
|
1098
|
+
"""スリット条件のスリットの現在値を取得する。"""
|
|
1099
|
+
return self._get_parameter("Manual_ComboBoxSlit")
|
|
1100
|
+
|
|
1101
|
+
def set_slit(self, value):
|
|
1102
|
+
"""スリット条件のスリットの値を変更する。"""
|
|
1103
|
+
return self._set_parameter("Manual_ComboBoxSlit", value)
|
|
1104
|
+
|
|
1105
|
+
def get_slit_list(self):
|
|
1106
|
+
"""スリット条件のリストから選択の現在値を取得する。"""
|
|
1107
|
+
return self._get_parameter("Manual_ComboBoxSlitList")
|
|
1108
|
+
|
|
1109
|
+
def set_slit_list(self, value):
|
|
1110
|
+
"""スリット条件のリストから選択の値を変更する。"""
|
|
1111
|
+
return self._set_parameter("Manual_ComboBoxSlitList", value)
|
|
1112
|
+
|
|
1113
|
+
def get_slit_width(self):
|
|
1114
|
+
"""スリット条件の数値入力の現在値を取得する。"""
|
|
1115
|
+
return self._get_parameter("Manual_TextBoxSlitWidth")
|
|
1116
|
+
|
|
1117
|
+
def set_slit_width(self, value):
|
|
1118
|
+
"""スリット条件の数値入力の値を変更する。"""
|
|
1119
|
+
return self._set_parameter("Manual_TextBoxSlitWidth", value)
|
|
1120
|
+
|
|
1121
|
+
@property
|
|
1122
|
+
def absolute_scan(self):
|
|
1123
|
+
"""「スキャン条件」の「絶対指定」の値を取得する。取得不可ならNone。"""
|
|
1124
|
+
return self.get_absolute_scan()
|
|
1125
|
+
|
|
1126
|
+
@absolute_scan.setter
|
|
1127
|
+
def absolute_scan(self, value):
|
|
1128
|
+
"""「スキャン条件」の「絶対指定」に値を設定する。"""
|
|
1129
|
+
self.set_absolute_scan(value)
|
|
1130
|
+
|
|
1131
|
+
@property
|
|
1132
|
+
def relative_scan(self):
|
|
1133
|
+
"""「スキャン条件」の「相対指定」の値を取得する。取得不可ならNone。"""
|
|
1134
|
+
return self.get_relative_scan()
|
|
1135
|
+
|
|
1136
|
+
@relative_scan.setter
|
|
1137
|
+
def relative_scan(self, value):
|
|
1138
|
+
"""「スキャン条件」の「相対指定」に値を設定する。"""
|
|
1139
|
+
self.set_relative_scan(value)
|
|
1140
|
+
|
|
1141
|
+
@property
|
|
1142
|
+
def scan_mode(self):
|
|
1143
|
+
"""「スキャン条件」の「スキャンモード」の値を取得する。取得不可ならNone。"""
|
|
1144
|
+
return self.get_scan_mode()
|
|
1145
|
+
|
|
1146
|
+
@scan_mode.setter
|
|
1147
|
+
def scan_mode(self, value):
|
|
1148
|
+
"""「スキャン条件」の「スキャンモード」に値を設定する。"""
|
|
1149
|
+
self.set_scan_mode(value)
|
|
1150
|
+
|
|
1151
|
+
@property
|
|
1152
|
+
def start(self):
|
|
1153
|
+
"""「スキャン条件」の「開始」の値を取得する。取得不可ならNone。"""
|
|
1154
|
+
return self.get_start()
|
|
1155
|
+
|
|
1156
|
+
@start.setter
|
|
1157
|
+
def start(self, value):
|
|
1158
|
+
"""「スキャン条件」の「開始」に値を設定する。"""
|
|
1159
|
+
self.set_start(value)
|
|
1160
|
+
|
|
1161
|
+
@property
|
|
1162
|
+
def stop_position(self):
|
|
1163
|
+
"""「スキャン条件」の「終了」の値を取得する。取得不可ならNone。"""
|
|
1164
|
+
return self.get_stop_position()
|
|
1165
|
+
|
|
1166
|
+
@stop_position.setter
|
|
1167
|
+
def stop_position(self, value):
|
|
1168
|
+
"""「スキャン条件」の「終了」に値を設定する。"""
|
|
1169
|
+
self.set_stop_position(value)
|
|
1170
|
+
|
|
1171
|
+
@property
|
|
1172
|
+
def step(self):
|
|
1173
|
+
"""「スキャン条件」の「ステップ」の値を取得する。取得不可ならNone。"""
|
|
1174
|
+
return self.get_step()
|
|
1175
|
+
|
|
1176
|
+
@step.setter
|
|
1177
|
+
def step(self, value):
|
|
1178
|
+
"""「スキャン条件」の「ステップ」に値を設定する。"""
|
|
1179
|
+
self.set_step(value)
|
|
1180
|
+
|
|
1181
|
+
@property
|
|
1182
|
+
def speed(self):
|
|
1183
|
+
"""「スキャン条件」の「スピード」の値を取得する。取得不可ならNone。"""
|
|
1184
|
+
return self.get_speed()
|
|
1185
|
+
|
|
1186
|
+
@speed.setter
|
|
1187
|
+
def speed(self, value):
|
|
1188
|
+
"""「スキャン条件」の「スピード」に値を設定する。"""
|
|
1189
|
+
self.set_speed(value)
|
|
1190
|
+
|
|
1191
|
+
@property
|
|
1192
|
+
def move_to(self):
|
|
1193
|
+
"""「軸移動条件」の「移動先」の値を取得する。取得不可ならNone。"""
|
|
1194
|
+
return self.get_move_to()
|
|
1195
|
+
|
|
1196
|
+
@move_to.setter
|
|
1197
|
+
def move_to(self, value):
|
|
1198
|
+
"""「軸移動条件」の「移動先」に値を設定する。"""
|
|
1199
|
+
self.set_move_to(value)
|
|
1200
|
+
|
|
1201
|
+
@property
|
|
1202
|
+
def move_step(self):
|
|
1203
|
+
"""「軸移動条件」の「ステップ」の値を取得する。取得不可ならNone。"""
|
|
1204
|
+
return self.get_move_step()
|
|
1205
|
+
|
|
1206
|
+
@move_step.setter
|
|
1207
|
+
def move_step(self, value):
|
|
1208
|
+
"""「軸移動条件」の「ステップ」に値を設定する。"""
|
|
1209
|
+
self.set_move_step(value)
|
|
1210
|
+
|
|
1211
|
+
@property
|
|
1212
|
+
def slit(self):
|
|
1213
|
+
"""「スリット条件」の「スリット」の値を取得する。取得不可ならNone。"""
|
|
1214
|
+
return self.get_slit()
|
|
1215
|
+
|
|
1216
|
+
@slit.setter
|
|
1217
|
+
def slit(self, value):
|
|
1218
|
+
"""「スリット条件」の「スリット」に値を設定する。"""
|
|
1219
|
+
self.set_slit(value)
|
|
1220
|
+
|
|
1221
|
+
@property
|
|
1222
|
+
def slit_list(self):
|
|
1223
|
+
"""「スリット条件」の「リストから選択」の値を取得する。取得不可ならNone。"""
|
|
1224
|
+
return self.get_slit_list()
|
|
1225
|
+
|
|
1226
|
+
@slit_list.setter
|
|
1227
|
+
def slit_list(self, value):
|
|
1228
|
+
"""「スリット条件」の「リストから選択」に値を設定する。"""
|
|
1229
|
+
self.set_slit_list(value)
|
|
1230
|
+
|
|
1231
|
+
@property
|
|
1232
|
+
def slit_width(self):
|
|
1233
|
+
"""「スリット条件」の「数値を入力」の値を取得する。取得不可ならNone。"""
|
|
1234
|
+
return self.get_slit_width()
|
|
1235
|
+
|
|
1236
|
+
@slit_width.setter
|
|
1237
|
+
def slit_width(self, value):
|
|
1238
|
+
"""「スリット条件」の「数値を入力」に値を設定する。"""
|
|
1239
|
+
self.set_slit_width(value)
|