You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python获取DigSilent Powerfactory参数的描述与名称?

解决PowerFactory中自动提取参数及其描述的问题

要自动获取参数的名称/描述并提取对应值,关键是利用PowerFactory API的GetAttributeInfo()方法获取属性元数据,结合对象的参数组信息实现遍历。以下是具体实现方案:

核心思路

  • PowerFactory的每个对象属性都附带元数据(包含名称、描述、所属参数组等信息)
  • 通过GetAttributeInfo(attr_name)可获取对应属性的界面显示描述
  • 先筛选出属于params组的所有属性,再批量提取参数描述与对应值

修改后的代码

import sys

def find_index(target_name, obj_list):
    # 补充原有的查找逻辑,按对象名称匹配
    for idx, obj in enumerate(obj_list):
        if obj.loc_name == target_name:
            return idx
    return -1

def get_signal_measurements_data(app):
    """
    从PowerFactory自动提取Signal Measurements的参数描述与对应值
    Args:
        app: 初始化后的PowerFactory应用对象
    Returns:
        dict: 包含参数描述和对应值的字典
    """
    try:
        oGen = app.GetCalcRelevantObjects('*.ElmSym')[0]
        oPlant = oGen.c_pmod
        sModels = oPlant.GetContents('*.ElmComp*', 1)

        # 定位UNITROL 1000模型
        unitrol_index = find_index("UNITROL 1000", sModels)
        if unitrol_index == -1:
            raise ValueError("未找到UNITROL 1000模型")
        oUnitrol = sModels[unitrol_index]
        sModels = oUnitrol.GetContents('*.ElmDsl', 1)

        # 定位Signal Measurements模块
        sm_index = find_index("01_Signal Measurements", sModels)
        if sm_index == -1:
            raise ValueError("未找到01_Signal Measurements模块")
        oSignalM = sModels[sm_index]

        # 自动提取params组下的所有参数及其描述
        signal_measurements = {"Description": [], "Values": []}
        
        # 获取对象的所有属性定义
        attr_defs = oSignalM.GetAttributeDefs()
        for attr_name in attr_defs:
            # 获取属性元数据
            attr_info = oSignalM.GetAttributeInfo(attr_name)
            # 筛选属于params组的参数
            if attr_info.group == "params":
                # 优先取界面显示的描述,无描述则用属性内部名称
                param_desc = attr_info.desc if attr_info.desc else attr_name
                param_value = oSignalM.GetAttribute(attr_name)
                
                signal_measurements["Description"].append(param_desc)
                signal_measurements["Values"].append(param_value)

        return signal_measurements

    except Exception as e:
        print(f"提取Signal Measurements数据出错: {e}")
        sys.exit(1)

关键说明

  • GetAttributeDefs():返回对象的所有属性名称列表
  • GetAttributeInfo(attr_name):返回属性元数据对象,核心字段包括:
    • desc:PowerFactory界面上显示的参数描述文本
    • name:属性的内部标识名(如你之前手动调用的T6、TRe)
    • group:属性所属的参数组,这里我们只筛选params组的内容
  • 若需要保留属性内部名称而非界面描述,可将param_desc = attr_info.desc改为param_desc = attr_info.name

内容的提问来源于stack exchange,提问作者Marco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 15:18:20