控制模拟问题(运行+++收敛等
Happ 对象的 Engine 属性返回 IHAPEngine 对象,该对象是模拟引擎的接口。 Happ 与 IHAPEngine 提供的方法使自动化客户端能够运行并控制模拟。
以下代码片段演示如何:
-
提示用户输入模拟参数;
-
重新运行模拟;
-
将更新后的结果呈现给用户。
Public Sub RunExample(ByVal ihAPsim As Happ.IHapp)
' 本例修改模拟参数并重新运行模拟
Dim nStages As Object
Dim strPrompt As String
On Error GoTo ErrorHandler
EditSimulation:
' 读取当前塔板数
nStages = ihAPsim.Tree.Data.Blocks.B6.Input.Elements("NSTAGE").Value
strPrompt = "Existing number of stages for column B6 = " & nStages & _
Chr(13) & "Enter new value for number of stages."
nStages = InputBox(strPrompt)
If nStages = "" Then GoTo finish
' 修改模拟
ihAPsim.Tree.Data.Blocks.B6.Input.Elements("NSTAGE").Value = nStages
' 运行模拟
ihAPsim.Run()
' 查看状态与结果
Call ListBlocksExample(ihAPsim)
Call TempProfExample(ihAPsim)
GoTo EditSimulation
finish:
Exit Sub
ErrorHandler:
MsgBox "RunExample failed with error " & Err.Description
End Sub
Python 直译版:循环提示用户→改塔板数→运行→输出结果
import win32com.client as win32
from tkinter import simpledialog, messagebox, Tk
def list_blocks_example(ap):
"""简易版:把模块列表打印到字符串"""
blocks = ap.Tree.Data.Blocks.Elements
lines = [f"{blk.Name}\t{blk.AttributeValue(11)}" for blk in blocks]
return "\n".join(lines)
def temp_prof_example(ap):
"""简易版:返回 B6 温度分布字符串"""
b_temp = ap.Tree.Data.Blocks.B6.Output.B_TEMP
rows = [f"{stage.Name}\t{stage.Value:.2f}\t{stage.UnitString}"
for stage in b_temp.Elements]
return "\n".join(rows)
def run_example(ap):
"""与 VB RunExample 完全一致:交互→改参数→运行→看结果"""
root = Tk()
root.withdraw() # 隐藏主窗口
while True:
n_stages_node = ap.Tree.Data.Blocks.B6.Input.Elements("NSTAGE")
old_val = n_stages_node.Value
new_val = simpledialog.askinteger(
"修改塔板数",
f"当前 B6 塔板数 = {old_val}\n请输入新值:",
minvalue=2, maxvalue=200)
if new_val is None: # 用户取消
break
# 修改模拟
n_stages_node.Value = new_val
# 运行模拟
ap.Engine.Run2()
# 查看结果
blocks_txt = list_blocks_example(ap)
temp_txt = temp_prof_example(ap)
messagebox.showinfo("运行结果",
f"=== 模块状态