Hysys Excel Automation Training

Wednesday, May 15, 2013

Pipe Segment of Aspen Hysys Through Excel VBA



Now, we're trying to access pipesegment on Aspen Hysys 7.3 through VBA. This post is an answer of previous post comment.

Here is the example

Create a table as shown figure



Then add following VBA code

Option Explicit

Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Public pStream As ProcessStream
Public psegment As PipeSegment


Public Sub StartHYSYS()
Dim filename As String
Dim ITEM As Integer
Dim lengthVar As Variant
Dim p_length, p_elev, p_rough, p_idiam, p_odiam, p_cond As Variant
    
    ' HYSYS CUSTOMATION THROUGH EXCEL TO ACCESS PIPE SEGMENT
    
    ' LOADING HYSYS SIMULATION FILE
    Set hyApp = CreateObject("HYSYS.Application")
    
    hyApp.Visible = True
    Set simCase = hyApp.ActiveDocument
    If simCase Is Nothing Then
        filename = Worksheets("HYSYS").Range("C4").Text
        
        If filename <> "False" And simCase Is Nothing Then
            Set simCase = GetObject(filename, "HYSYS.SimulationCase")
            simCase.Visible = True
        End If
    End If
  
    ' define operation
    Set psegment = simCase.Flowsheet.Operations("pipeseg").ITEM("pipe-100")
        
    ' define pipesegment's parameters
    p_length = psegment.SegmentLengthValue
    p_elev = psegment.SegmentElevationChangeValue
    p_rough = psegment.RoughnessValue
    p_idiam = psegment.SegmentInnerDiamValue
    p_odiam = psegment.SegmentOuterDiamValue
    p_cond = psegment.PipeConductValue
    
    

    ' collecting data as variant

    For ITEM = 1 To 4
        p_length(ITEM - 1) = Cells(6, ITEM + 2)
        p_elev(ITEM - 1) = Cells(7, ITEM + 2)
        p_odiam(ITEM - 1) = Cells(8, ITEM + 2)
        p_idiam(ITEM - 1) = Cells(9, ITEM + 2)
        p_rough(ITEM - 1) = Cells(10, ITEM + 2)
        p_cond(ITEM - 1) = Cells(11, ITEM + 2)
            
    Next ITEM
    
    ' inputting data to hysys
    psegment.SegmentLength.Values = p_length
    psegment.SegmentElevationChange.Values = p_elev
    psegment.Roughness.Values = p_rough
    psegment.SegmentInnerDiam.Values = p_idiam
    psegment.SegmentOuterDiam.Values = p_odiam
    psegment.PipeConduct.Values = p_cond
      
    ' FINISH
End Sub

As you can see, Hysys model is just as simple as following figure.








And now, pipe segment is on your hand :D

5 comments:

  1. Hi Alex,

    Can you post an example for changing the split ratio value from excel?
    Thanks a lot for posting this interesting code.

    Mayembe

    ReplyDelete
  2. Mayembe,

    I've generate VBA code to access splitter on hysys, and I'll post it soon.

    Alex

    ReplyDelete
  3. Have you got any idea how can I set elbows(fittings) instead of pipe?

    ReplyDelete
  4. Vba code for pipesys?

    ReplyDelete