# 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"

print("=== Fluent切面压力与彩色云图修复 ===")
print("原任务目录:", task_directory)
print("复用当前模型和已有切面，不重新初始化或推进时间。")

# 1. 先检查当前模型状态。
active = None

try:
    active = is_active()
    print("is_active =", active)
except Exception as exc:
    print("无法直接读取活动状态:", repr(exc))
    print("运行上下文此前报告 model_active=True。")

if active is False:
    raise RuntimeError(
        "当前没有有效活动模型，停止操作，不自动重载或初始化。"
    )


def show_node(label, node):
    print("\n---", label, "---")
    print("节点类型:", type(node).__name__)

    try:
        names = sorted(
            name for name in dir(node)
            if not name.startswith("_")
        )
        keywords = [
            "contour", "stream", "pathline", "vector",
            "color", "range", "legend", "surface",
            "view", "mesh", "scene"
        ]
        matches = [
            name for name in names
            if any(word in name.lower() for word in keywords)
        ]
        print("相关公开接口:", matches[:100])
    except Exception as exc:
        print("接口枚举失败:", repr(exc))

    try:
        state = node.get_state()
        print("原生状态:", repr(state)[:3000])
    except Exception as exc:
        print("未能读取该节点状态:", repr(exc))


# 2. 检查 Fluent 原生图形接口，不猜测未知 API。
graphics = None

try:
    graphics = solver.settings.results.graphics
    show_node("graphics 根节点", graphics)
except Exception as exc:
    print("读取 graphics 失败:", repr(exc))

if graphics is not None:
    for name in ["views", "mesh"]:
        try:
            node = getattr(graphics, name)
            show_node("graphics." + name, node)
        except Exception as exc:
            print("读取 graphics." + name + " 失败:", repr(exc))

    try:
        names = [
            name for name in dir(graphics)
            if not name.startswith("_")
        ]
        candidates = [
            name for name in names
            if any(
                word in name.lower()
                for word in [
                    "contour", "stream", "pathline",
                    "vector", "legend", "surface"
                ]
            )
        ]
        print("\n流线、等值线及色标候选接口:", candidates)
    except Exception as exc:
        print("候选接口检查失败:", repr(exc))

# 3. 在现有内部切面导出压力和速度大小。
# 这一步用于观察场变量变化，不假定导出适配器能设置色标。
print("\n=== 导出内部切面数据 ===")
print("切面名称:", surface_name)

result = None

try:
    result = fluentagi.export_results(
        directory=task_directory,
        fields=["pressure", "velocity-magnitude"],
        surfaces=[surface_name]
    )

    print("实际导出返回:", repr(result)[:8000])

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

# 4. 说明实际执行范围。
print("\n=== 检查结束 ===")
print("保留原有模型、网格和计算结果。")
print("没有重新创建切面，没有推进瞬态计算。")
print("本轮没有伪造色标范围或宣称已生成流线动画。")
print("根据实际图形接口与导出结果决定下一步色标调整。")