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

Modelica图表宏无法显示可替换包中字符串的技术问询

问题解答

显示可替换包中的字符串是可以实现的,你的代码里存在两处问题导致无法正常显示:

1. 宏语法的引用限制

Modelica图表文本宏%{...}仅支持引用模型内的变量/参数,不能直接引用包的属性(比如%{Medium.mediumName}这种写法不符合规范)。你已经通过medname参数存储Medium.mediumName,这是正确的间接引用方式,只要确保参数替换逻辑生效即可。

2. 宏语法书写错误

最后一个Text组件的textString="{%teststringB}"多写了一个百分号,正确写法应为"%{teststringB}"。

修正后的代码

model StringVisual
  replaceable package Medium = Modelica.Media.R134a.R134a_ph;
  parameter String medname=Medium.mediumName;
  parameter String teststringA = "Hello World";
  parameter String teststringB = teststringA;
  parameter Real myreal=3.14;

equation 
  when initial() then
    Modelica.Utilities.Streams.print(medname);
  end when;

  annotation (Diagram(graphics={
        Text(
          extent={{0,-50},{100,-100}},
          textColor={30,100,200},
          textString="%{myreal}"),
        Text(
          extent={{-100,60},{-10,0}},
          textColor={30,100,200},
          textString="%{medname}"),
        Text(
          extent={{0,100},{100,40}},
          textColor={30,100,200},
          textString="%{teststringA}"),
        Text(
          extent={{0,50},{100,0}},
          textColor={30,100,200},
          textString="%{teststringB}")}));
end StringVisual;

关键说明

  • 当替换Medium包时,medname参数会自动更新为新包的mediumName,图表中的%{medname}也会同步显示新值。
  • 必须通过模型内的参数间接引用包的属性,不能直接在宏中写包路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 14:45:06