# 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("现有切面:", surface_name)
print("保留当前模型、网格和物理时间。")
print("不重新初始化，不推进瞬态计算。")

# 1. 检查原生图形设置接口。
graphics = None

try:
    graphics = solver.settings.results.graphics
    graphics_names = dir(graphics)
    print("\n图形接口读取成功。")
    print("图形接口类型:", type(graphics).__name__)
    print(
        "相关接口:",
        [
            name for name in graphics_names
            if any(
                word in name.lower()
                for word in [
                    "contour", "stream", "pathline",
                    "vector", "views", "mesh", "scene"
                ]
            )
        ]
    )
except Exception as exc:
    print("图形接口读取失败:", repr(exc))

# 2. 查看视图和网格显示接口，不修改其状态。
if graphics is not None:
    for name in ["views", "mesh"]:
        if name in dir(graphics):
            try:
                node = getattr(graphics, name)
                print("\n--- graphics." + name + " ---")
                print("公开接口:", [
                    item for item in dir(node)
                    if not item.startswith("_")
                ][:100])

                try:
                    print("当前状态:", repr(node.get_state())[:3000])
                except Exception as exc:
                    print("状态读取失败:", repr(exc))
            except Exception as exc:
                print("接口检查失败:", name, repr(exc))

# 3. 仅在原生图形接口真实存在时，尝试建立独立压力等值线。
contour_collection = None
contour_object = None

if graphics is not None and "contour" in dir(graphics):
    try:
        contour_collection = getattr(graphics, "contour")
        print("\n检测到原生 contour 接口。")
        print("接口类型:", type(contour_collection).__name__)
        print("公开接口:", [
            item for item in dir(contour_collection)
            if not item.startswith("_")
        ][:100])

        try:
            print(
                "现有等值线状态:",
                repr(contour_collection.get_state())[:4000]
            )
        except Exception as exc:
            print("现有等值线状态读取失败:", repr(exc))

    except Exception as exc:
        print("读取 contour 接口失败:", repr(exc))
else:
    print("\n当前图形接口没有公开 contour 节点。")

# 4. 优先使用独立名称，避免覆盖之前的图形对象。
if contour_collection is not None:
    try:
        contour_object = contour_collection[contour_name]
        print("找到已有修复等值线对象，尝试检查并继续配置。")
    except Exception:
        contour_object = None

    if contour_object is None 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))

# 5. 只设置实际公开的属性；每项单独捕获错误。
if contour_object is not None:
    properties = dir(contour_object)
    print("\n等值线对象属性:", [
        item for item in properties
        if not item.startswith("_")
    ][:120])

    changes = [
        ("field", "pressure"),
        ("surfaces", [surface_name]),
        ("filled", True),
        ("auto_range", True),
        ("range_option", "auto"),
    ]

    for property_name, value in changes:
        if property_name in properties:
            try:
                if property_name == "field":
                    contour_object.field = value
                elif property_name == "surfaces":
                    contour_object.surfaces = value
                elif property_name == "filled":
                    contour_object.filled = value
                elif property_name == "auto_range":
                    contour_object.auto_range = value
                elif property_name == "range_option":
                    contour_object.range_option = value

                print("属性设置已提交:", property_name, repr(value))
            except Exception as exc:
                print("属性设置未成功:", property_name, repr(exc))
        else:
            print("当前对象未公开属性:", property_name)

    # 仅在对象确实公开 display 接口时尝试显示。
    if "display" in dir(contour_object):
        try:
            contour_object.display()
            print("已调用等值线显示接口。")
        except Exception as exc:
            print("等值线显示失败:", repr(exc))

    try:
        print(
            "\n配置后原生状态:",
            repr(contour_object.get_state())[:5000]
        )
    except Exception as exc:
        print("配置后状态读取失败:", repr(exc))
else:
    print("未能取得可配置的原生压力等值线对象。")
    print("没有调用未经验证的图形接口。")

# 6. 结束汇总。
print("\n=== 本轮检查结束 ===")
print("目标: 现有内部切面压力的彩色填色显示。")
print("本轮不重新导出已有动画，不推进物理时间。")
print("只有原生设置返回确认后，才能认定相应配置成功。")
print("即使图形配置成功，也不能据此认定流场已经收敛。")