Вы находитесь на странице: 1из 16

Enhancing Your Extensions 1

Enhancing Your Extensions

© 1999 AEA Technology plc - All Rights Reserved. 1

Extn 6.pdf
2 Enhancing Your Extensions

Introduction
This module will introduce you to several advanced topics that you will
use to enhance the functionality of the unit operation extension
created in the previous module.

While the extension that was created in the previous module worked
adequately, there are several things that we can do to improve its
operation and enhance its appearance.

In this module, we will change both the EDF and the DLL to improve
the extension. Therefore, this module will allow you to combine your
knowledge on these two areas of extension design.

Learning Objectives
This module will teach you how to enhance your extensions by adding
plots and other options to an extension.

Prerequisites
As this module will build upon material that was covered in two
previous modules (Creating an EDF and Unit Operation Extensions), it
is vital that you understand the material that was presented there
before you begin this module. If you have any questions about the
material presented in those two modules, please ask the instructor to
explain it again.

Also, it is important that the extension that you created in the previous
module be functioning properly before you start to enhance it. If your
extension is not operating properly, ask the instructor to help you
complete the previous module.

2
Enhancing Your Extensions 3

Background
Recall that the extension that was created in the last module modelled a
natural gas wellhead. The extension was fully functional in that it
accepted the P-Q data entered by the user and calculated the unknown
properties in the simulation. This is the minimum that we would expect
from any operation in HYSYS.

Typically, we expect the default operations in HYSYS to work as


efficiently as possible, to have options like worksheets and plots where
applicable. Additional accessories like Delete buttons and Ignore check
boxes are included with most HYSYS default operations.

We are now left with one question. Can our extension have all of these
features? The answer is: Yes. This module will teach you how to add
these features to the extension created in this course. This will allow
you to add similar features to any extension that you create.

For the extension that was created in this course (the wellhead
modeller), one of the most desirable enhancements would be a plot to
display the P-Q data in a graphical format. This will involve changes to
both the EDF and the DLL. As well, the code for the extension will be
modified to improve its efficiency. Both of these improvements will be
done in the Workshop portion of this module.

In the Exercise portion, we will add a Delete button and an Ignore


checkbox to the EDF. We will also add an input checking routine to the
code to ensure that the number that the user enters for the
NumberOfPoints variable is valid.

3
4 Enhancing Your Extensions

Workshop
Writing Efficient Code
One of the principles of creating efficient code is that each command
and calculation be performed only when necessary. This reduces or
eliminates duplicate and repetitive calculation that increase the
running time and the memory required by the extension.

Can you see any part of the code for this extension that
would allow the extension to run more efficiently if it was
placed in another location, yes or no? __________
What improvements do you recommend?
___________________________________________________

Look at the Execute subroutine in this code; each time that this
subroutine runs, the object references for the streams are reset. This is
unnecessary and inefficient. The extension would perform better if this
code was placed in a method that only ran if the streams were changed.

What method will be called only when a stream is


changed? Hint: this method already exists in the code for
this extension. ______________________

The VariableChanged method already exists in the code for the


extension and will be called only when a variable is changed.
Remember that streams are variables, too. This method already has
code that will run if the NumberOfPoints variable changes. It is a
relatively simple matter to add a few lines of code to handle changes in
The Variable.Tag property
corresponds to the tag given attached streams.
for the variable in the EDF.
Add the following lines of code after the Select Case variable.Tag line.
You may cut-and-paste from the Execute method, if desired.

Case "FeedStream"
Set Feed = myContainer.FindVariable("FeedStream").Variable.object

Case "ProductStream"
Set Product = myContainer.FindVariable("ProductStream").Variable.object

4
Enhancing Your Extensions 5

Once these lines are in place, the lines in the Execute method that
performed the same task can be removed, if they have not been already.

If desired, and if time permits, you may want to test the revised code of
the extension, either run the extension in VB or recreate the DLL. You
should be aware that you will probably not be able to notice an
improvement in the performance of the extension. Any improvement
may only be seen in large simulations where multiple extensions of the
same type are used; this allows a small improvement to be multiplied
several times so that it can be seen.

Adding a Plot to the EDF


In this portion of the workshop, the instructor will lead the class
through the addition of a plot to the EDF that was used for the Wellhead
extension.

The plot that will be added will be on the same tab as the table; and a
Visibility controller will be used to determine whether the plot or the
table will be visible.

1. Open the Extension View Editor application, and retrieve the EDF
file that was created for the unit operation extension in a previous
module.

The Visibility Manager


The Visibility manager is used when you want to show or hide certain
parts of your views under certain conditions. You already have some
experience with the Visibility manager. Remember when you designed
the EDF, you used the Visibility manager to switch between the tabs.

In this workshop, we will do something similar, except that here we will


use the Visibility manager to create a Visibility controller to show either
a plot or a table on the P-Q Data page of our EDF.

2. Access the Visibility Manager by right-clicking on the form and


selecting Open Visibility Manager from the menu that appears.

5
6 Enhancing Your Extensions

Visibility controllers are added from this view by pressing the Create
Controller button. Once the controller is created, it can be edited to
match our requirements by pressing the Edit button within the new
controller’s group box. The Visibility controller’s properties page looks
like this:

Fill in the view so that it looks like this:

Adding the Radio Buttons


The next step involves adding Radio buttons to the form that will
control whether the plot or the table will be shown. Add a Radio Button
widget to the form in the area above the Number Of Points Numerical
Input widget. You may have to move the Numerical Input widget down
in order to make room for the Radio Buttons.

6
Enhancing Your Extensions 7

Complete the Property View for the Radio buttons so that it looks like
this:

Note that in order to select State Value as the Target Moniker, you must
first select the PlotVisController as the Target Widget. The State Value
moniker is used as the Target Moniker when the value that controls the
visibility is not needed in the code. If you use the State Value as the
moniker, the correct Target Widget must be selected first.

Make sure that the values for Table and Plot match the values that were
entered in the PlotVisController properties view.

Connecting the Widgets and the Visibility Controllers


After the Radio Button widget and the Visibility controller are properly
set up, you must set the table dependent widgets to the appropriate
state of the PlotVisController. We want the Visibility controller to show
or hide the following widgets:

• The Matrix Widget


• The Static Text Widget
• The Numerical Input Widget

For each of the widgets in the list above, completing the following steps
will set up the Visibility managers correctly.

Access the widget’s Visibility manager by right-clicking on the widget


and selecting Visibility... from that menu, or by clicking on the
Visibility button that is available on the widget properties page.

7
8 Enhancing Your Extensions

The Widget Visibility manager should look like this:

Add another Visibility controller by clicking on the Add button and


selecting the PlotVisController. When the PlotVisController is added
and the focus is on it, the following view will be displayed in the
Selected Controller States window.

Because we want the three widgets listed previously to be displayed


when the PlotVisController is in Table mode, select the check box in the
Table row and the Visible column.

Set up all three widgets so that they are displayed or hidden at


appropriate times.

After all three widgets have been properly set up, they can be tested.
There are two ways to test the function of the Visibility controllers.
Either use the Visibility manager, or select Test from the menu that
appears after right-clicking on the form.

Do the three widgets on the P-Q Data page disappear and


reappear as expected? ______________

8
Enhancing Your Extensions 9

Adding the Plot Widget


Next, add a new variable to the Object Manager page. The name of the
new variable will be PlotName and it will be a Text variable; make sure
that this variable is specified as Persistent.

Return to the view editing environment, and use the Visibility Manager
to select the Plot view of the P-Q Data page. There should only be the
Group Box widget and the Radio Buttons widget visible now, all other
widgets should be hidden.

On this page, add a Plot widget with the following information:

In This Cell... Enter...

Name PQPlotWidget

X-Stretch Yes

Y-Stretch Yes

Plot Description PlotName

Size the widget to occupy most of the available area inside of the Group
Box and below the radio buttons.

Access the Visibility manager for this widget by pressing the Visibility
button. Add the PlotVisController to this widget and set the visibility to
true for the Plot state.

Note that if the plot is going to stretch in both the X and Y directions,
the Group Box that surrounds it will also have to stretch in both
directions. Make the necessary changes now.

For now, the plot widget will just look like a blank box. The plot will be
defined in the VB code for the extension.

At this time, save your EDF with the filename Wellhead_Plot.EDF.

Save your file!

9
10 Enhancing Your Extensions

Defining the Plot in the Code


For this portion of the workshop, we will go through each step in adding
the required code to set up the plot in the DLL.

1. The first step is to add some variable declarations to the code.


Add the following two lines to the other variable declarations
already at the top of the class module.

Dim myPlotName As InternalTextVariable


Dim myPlot As TwoDimensionalPlot

2. Add the following line of code to the Initialize function with the
other Set ... lines.

Set myPlotName = myContainer.FindVariable("PlotName").Variable

3. At the end of all the code, i.e. after the Interpolate and Sort
methods, create a new subroutine called CreatePlot. There are no
arguments for this subroutine. Type the following line of code
and the End Sub line should be automatically entered.

Sub CreatePlot

4. Return to the Initialize method and add the following line of code
at the end of this function, but make sure that it is before the error
handler.

Call CreatePlot

5. Go back to the newly created subroutine and add the following


code to the CreatePlot subroutine.

If Not myPlot Is Nothing Then


myContainer.DeletePlot "PQPlot"
Set myPlot = Nothing
End If

myContainer.BuildPlot2 "PQPlot", myPlot, hptTwoDimensionalPlot


myPlotName.Value = "PQPlot"

The first four lines in this code will check to see if a plot already exists in
the EDF, and if there is a preexisting plot, the plot and its reference will
be deleted. The fifth line uses the BuildPlot2 method of the container
to create the plot in the EDF. The first argument in this method

10
Enhancing Your Extensions 11

("PQPlot") is the label of the plot. The second argument (myPlot) is the
object in HYSYS that will hold the reference to the plot, and the third
argument (hptTwoDimensionalPlot) is the type of plot that we are
creating. More information on the BuildPlot2 method can be found
using the VB Object Browser.

Finally, the last line in this code block will connect the plot widget in the
EDF to the newly created plot. This is done by setting the value of the
myPlotName variable (which is tied to the PlotName variable in the
EDF) to the name of the plot that was entered as the first argument in
the BuildPlot2 method.

6. With the plot now created, we can define its attributes. Add the
following lines of code to the CreatePlot subroutine immediately
after the lines that were added in step #5.

With myPlot

.TitleData = "Wellhead PQ Relationship"

.SetAxisLabelData hatXAxis, "Flow"


.SetAxisLabelData hatYAxis, "Pressure"
.SetAxisLabelVisible hatXAxis, True
.SetAxisLabelVisible hatYAxis, True

.LegendVisible = True

.CreateXYDataSet 1, "PQData"
.SetDataSetXData 1, flowRFV
.SetDataSetYData 1, pressureRFV
.SetDataSetColour 1, "Red"

End With

The purpose of each of these lines is quite self-explanatory; however, if


you have any questions, do not hesitate to ask the instructor. After
these lines are added, the CreatePlot subroutine will be completed; the
plot is now fully defined.

11
12 Enhancing Your Extensions

7. As a final step, add the following lines of code to the Terminate


method. This will delete the plot if the extension is deleted, and
will set the variables to "nothing". It is good programming
practice to set all object variables to "nothing" if the extension is
deleted.

myContainer.DeletePlot "PQPlot"
Set myPlot = Nothing
Set myPlotName = Nothing

The VB code can now be tested using one of the two methods presented
previously in this course. Either put the code into the Run mode, and
register the EDF, or compile the code into a DLL and register the DLL
and the EDF together. If you choose to make the DLL at this time
remember to use the same filename as was used when the EDF was
saved.

Remember to save the VBP and CLS files before you run the program.
Save the VBP file as Wellhead_Plot.VBP.

Save your file!

Did the plotting feature in the EDF work as expected?


__________
Can you think of any other features that could be added to
this extension in order to increase its functionality, or to
better its appearance? _____________

12
Enhancing Your Extensions 13

Exercise
In this exercise, you will continue to enhance the functionality of the
extension and the appearance of the EDF by adding new controls to the
EDF and a new subroutine to the extension’s code.

Adding New Controls to the EDF


Those of you who are familiar with the standard operations in HYSYS
will be aware of the Delete button and Ignore check box that many of
those operations have. In the first part of this exercise, you will add
these controls to the EDF.

Adding a Delete Button


A Delete button will be added in this section of the exercise. This is a
relatively simple procedure that involves changes to the EDF only.
Therefore, the extension’s code will not have to be changed in this part
of the exercise.

Recall that in the EDF module, the Status Bar was located in the middle
of the lower portion of the view. There was about 2 cm (1 inch) of room
on both the left and the right sides of the status bar. We will be adding
the Delete button to the left side of the bar. If there is not enough room
for this button on your EDF, either move the status bar over, or reduce
its size. You will need about 2 cm (1 inch) of room in order to add the
widget.

In order to add a Delete button to the EDF we must first add a Button
widget to the form. On the properties page for this widget, change the
name of the widget to DeleteButton, change the Label to Delete, and
select the message as Delete Object. The message can be selected by
pressing the [...] button in the message cell and then clicking the Insert
button on the view that appears. Select Delete Object from the list and
press the OK button.

This widget should not stretch in either direction, and it should be tied
to the bottom left corner of the view.

This will complete the installation procedure for the Delete button.

13
14 Enhancing Your Extensions

Adding the Ignore Check Box


The Ignore Check Box will occupy about the same amount of space that
the Delete button occupies. We want this widget to be located to the
right of the Status bar, so, if there is not enough room, make the space
available.

Add a Check Box widget to the view to the right of the Status Bar. On the
properties page for this widget, specify the following information.

In This Cell... Enter...

Name IgnoredCB

Label Ignored

Target Moniker Object is Ignored In Calculations

Tie To Corner Bottom Right

This widget should not be stretchable in either the X or Y directions.

When both the Delete button and the Ignore check box are added to the
form, the bottom area of the form should look something like this:

14
Enhancing Your Extensions 15

Adding the VariableChanging


Subroutine
Before we begin this portion of the exercise, it is helpful to ask ourselves
some questions.

What will happen if the user enters -10 for the number of
data points? ______________
What about 1 000 000 ? _____________

In these cases, the response of the program is undefined. However,


because we have installed error handlers throughout our program, a
non-descriptive error message will probably appear if a negative value
is entered.

However, if a very large number (say, 1 000 000) is entered, the program
will accept this and continue, it will probably take several minutes to
set up the flowRFV and pressureRFV variables to take in a million
points. Obviously, it is unlikely that any user will have more than 100
data points to define the P-Q relationship. Therefore, it is a good idea
for the program to check the value that the user will input, and display a
descriptive error message if the number is too large, or if it just does not
make sense.

In order for the program to do this, we need to implement a new


function called VariableChanging. This function is called every time
that an attempt is made to change any EDF variable. It will look very
similar to the VariableChanged subroutine that already exists in the
code for this extension.

15
16 Enhancing Your Extensions

The Procedure
1. Add a new function to the code for the extension. The name of
the function will be VariableChanging; there is one argument for
this function. The argument will be Variable and it will be of the
type: InternalVariableWrapper. The function type is Boolean.
Check the Object Browser to determine the format of this
method.
2. Use a Select Case command to determine if the variable that is
changing is the "NumberOfPoints" variable. Hint: see the
VariableChanged subroutine for help in setting up the Select Case
command.
3. If the variable that is changing is the "NumberOfPoints" variable,
use an If ... Then statement to check if the inputted value is
between 2 and 100, inclusive. Any number less than 2, or greater
than 100 is not valid.
4. If the inputted number is not valid, a message box should be
displayed explaining the error, and the function’s value should be
set to False.
5. Otherwise, the function’s value should be set to True.

This will complete this function.

Test the extension by adding it to the HYSYS case that was used in the
previous module.

Optional Exercise
Add the necessary code to the VariableChanging function to check if a
specified feed or product stream exists, and if it does not, to create a
stream with the entered name.

16

Вам также может понравиться