# Use the existing connected PyFluent variable solver.
# Continue with the complete executable script here.

task_directory = r"C:\Users\13977\AppData\Local\FluentAGI\data\runs\20261020-124717-f33d8b"
surface_name = "agi-section-e3e1d5"
contour_name = "agi-pressure-cut-auto-repair"

print("=== Fluent内部切面压力云图修复 ===")
print("原任务目录:", task_directory)
print("复用现有模型、网格和切面，不初始化、不推进物理时间。")

graphics = solver.settings.results.graphics
contour_collection = graphics.contour
contour_object = None
collection_state = None

# 1. 先检查之前的修改是否留下了可用对象。
try:
    collection_state = contour_collection.get_state()
    if isinstance(collection_state, dict):
        print("现有等值线对象:", list(collection_state.keys()))
except Exception as exc:
    print("等值线集合状态读取失败:", repr(exc))

try:
    contour_object = contour_collection[contour_name]
    print("发现已有独立压力图对象，继续检查，不重复创建。")
except Exception as exc:
    print("直接获取现有对象失败:", repr(exc))

# 只有确认集合状态中没有同名对象，才尝试创建。
if contour_object is None:
    name_exists = (
        isinstance(collection_state, dict)
        and contour_name in collection_state
    )
    if not name_exists and "create" in dir(contour_collection):
        try:
            contour_collection.create(name=contour_name)
            contour_object = contour_collection[contour_name]
            print("已请求创建独立压力图对象。")
        except Exception as exc:
            print("创建或获取对象失败:", repr(exc))

# 2. 修正压力变量、填色、切面列表和自动范围。
if contour_object is not None:
    try:
        initial_state = contour_object.get_state()
        print("修改前状态:", repr(initial_state))

        for property_name, value in [
            ("field", "pressure"),
            ("filled", True),
            ("surfaces_list", [surface_name]),
            (
                "range_option",
                {
                    "option": "auto-range-on",
                    "auto_range_on": {"global_range": True},
                },
            ),
        ]:
            if property_name in dir(contour_object):
                try:
                    setattr(contour_object, property_name, value)
                    print("已提交设置:", property_name, repr(value))
                except Exception as exc:
                    print("设置失败:", property_name, repr(exc))
            else:
                print("当前对象未公开属性:", property_name)

        # 3. 重新获取对象并读取实际状态。
        contour_object = contour_collection[contour_name]
        final_state = contour_object.get_state()
        print("修改后状态:", repr(final_state))

        surfaces_ok = (
            final_state.get("surfaces_list") == [surface_name]
        )
        pressure_ok = final_state.get("field") == "pressure"
        filled_ok = final_state.get("filled") is True
        range_state = final_state.get("range_option", {})
        range_ok = (
            isinstance(range_state, dict)
            and range_state.get("option") == "auto-range-on"
        )

        print("压力变量已核实:", pressure_ok)
        print("填色已核实:", filled_ok)
        print("切面绑定已核实:", surfaces_ok)
        print("自动色标已核实:", range_ok)

        # 只有关键设置回读正确，才请求显示。
        if pressure_ok and filled_ok and surfaces_ok and range_ok:
            try:
                contour_object.display()
                print("已请求显示压力切面等值线。")
            except Exception as exc:
                print("显示请求失败:", repr(exc))
        else:
            print("原生设置尚未全部通过核验，暂不宣称修复成功。")

    except Exception as exc:
        print("压力图对象检查或修复失败:", repr(exc))
else:
    print("未取得可用的压力图对象；保留现有结果，不盲目重复创建。")

# 4. 使用已有切面导出压力场，不请求动画、不推进计算。
print("\n=== 导出切面压力结果 ===")

try:
    export_result = fluentagi.export_results(
        directory=task_directory,
        fields=["pressure"],
        surfaces=[surface_name],
    )
    print("实际导出返回:", repr(export_result))

    if isinstance(export_result, dict):
        print("导出状态:", export_result.get("status", "未提供"))
        print("输出目录:", export_result.get("directory", "未提供"))
        print(
            "收敛验证:",
            export_result.get("convergence_verified", "未提供"),
        )
except Exception as exc:
    print("压力切面导出失败:", repr(exc))

print("\n=== 本轮结束 ===")
print("没有重新初始化、重建网格或推进瞬态时间。")
print("以原生设置回读和实际导出结果判断是否修复成功。")