表格

Sub ConnectivityExample(ByVal ihAPsim As Happ.IHapp)
' This example displays a table showing flowsheet connectivity
Dim ihStreamList As Happ.IHNode
Dim ihBlockList As Happ.IHNode
Dim ihDestBlock As Happ.IHNode
Dim ihSourceBlock As Happ.IHNode
Dim ihStream As Happ.IHNode
Dim strHeading As String
Dim strTable As String
Dim strDestBlock As String
Dim strDestPort As String
Dim strSourceBlock As String
Dim strSourcePort As String
Dim strStreamName As String
Dim strStreamType As String

On Error GoTo ErrorHandler
ihStreamList = ihAPsim.Tree.Data.Streams
ihBlockList = ihAPsim.Tree.Data.Blocks
strTable=""
strHeading = "Stream" & Chr(9) & "From" _
  & Chr(9) & Chr(9) & Chr(9) & "To" & Chr(13)

For Each ihStream In ihStreamList.Elements
  strStreamName = ihStream.Name
  strStreamType = ihStream.AttributeValue(Happ.HAPAttributeNumber.HAP_RECORDTYPE)
  ' get the destination connections
  ihDestBlock = ihStream.Elements("Ports").Elements("DEST")
  If (ihDestBlock.Elements.RowCount(0) > 0) Then
    ' there is a destination port
    strDestBlock = ihDestBlock.Elements(0).Value
    strDestPort = ihBlockList.Elements(strDestBlock). _
      Connections.Elements(strStreamName).Value
  Else
    ' it's a flowsheet product
    strDestBlock = ""
    strDestPort = ""
  End If
  ' get the source connections
  ihSourceBlock = ihStream.Elements("Ports").Elements("SOURCE")
  If (ihSourceBlock.Elements.RowCount(0) > 0) Then
    ' there is a source port
    strSourceBlock = ihSourceBlock.Elements(0).Value
    strSourcePort = ihBlockList.Elements(strSourceBlock). _
      Connections.Elements(strStreamName).Value
  Else
    ' it's a flowsheet feed
    strSourceBlock = ""
    strSourcePort = ""
  End If

  strTable = strTable & Chr(13) & strStreamName _
    & Chr(9) & strSourceBlock _
    & Chr(9) & strSourcePort & Chr(9) _
    & Chr(9) & strDestBlock & Chr(9) _
    & strDestPort
Next ihStream
MsgBox(strHeading & strTable, , "ConnectivityExample")
Exit Sub

ErrorHandler:
MsgBox("ConnectivityExample raised error" & Err.Description)
End Sub

Python翻译

import win32com.client as win32
from tkinter import Tk, messagebox

HAP_RECORDTYPE = 11   # 记录类型

def connectivity_example(ih_ap_sim):
    """与 VB ConnectivityExample 功能完全一致"""
    stream_list = ih_ap_sim.Tree.Data.Streams.Elements
    block_list  = ih_ap_sim.Tree.Data.Blocks.Elements

    heading = "Stream\tFrom\t\t\tTo"
    table_lines = [heading]

    for ih_stream in stream_list:
        stream_name = ih_stream.Name
        stream_type = ih_stream.AttributeValue(HAP_RECORDTYPE) if ih_stream.AttributeType(HAP_RECORDTYPE) != 0 else ""

        # --------- 目的连接 ---------
        dest_port = ih_stream.Elements("Ports").Elements("DEST")
        if dest_port.Elements.RowCount(0) > 0:          # 有目的端口
            dest_block = dest_port.Elements(0).Value
            dest_port_name = block_list.Elements(dest_block).Connections.Elements(stream_name).Value
        else:                                           # 产品出料
            dest_block = dest_port_name = ""

        # --------- 源连接 ---------
        src_port = ih_stream.Elements("Ports").Elements("SOURCE")
        if src_port.Elements.RowCount(0) > 0:           # 有源端口
            src_block = src_port.Elements(0).Value
            src_port_name = block_list.Elements(src_block).Connections.Elements(stream_name).Value
        else:                                           # 进料
            src_block = src_port_name = ""

        # --------- 拼表 ---------
        table_lines.append(f"{stream_name}\t{src_block}\t{src_port_name}\t\t{dest_block}\t{dest_port_name}")

    # --------- 弹窗 ---------
    root = Tk(); root.withdraw()
    messagebox.showinfo("ConnectivityExample", "\n".join(table_lines))
    root.destroy()


# ---------------- 调用示例 ----------------
if __name__ == "__main__":
    ap = win32.Dispatch("AspenPlus.Document.34.0")
    ap.InitNew2()                 # 或 GetObject 打开已有文件
    connectivity_example(ap)
    ap.Quit()