Professional Documents
Culture Documents
vCampus+Library White+Papers+and+Tutorials PI+ProcessBook White+Paper+-+A+Few+PI+ProcessBook+VBA+Tips
vCampus+Library White+Papers+and+Tutorials PI+ProcessBook White+Paper+-+A+Few+PI+ProcessBook+VBA+Tips
Tips
WWW.OSISOFT.COM
OSIsoft, LLC is the owner of the following trademarks and registered trademarks: PI System, PI
ProcessBook, Sequencia, Sigmafine, gRecipe, sRecipe, and RLINK. All terms mentioned in this book
that are known to be trademarks or service marks have been appropriately capitalized. Any trademark
that appears in this book that is not owned by OSIsoft, LLC is the property of its owner and use herein
in no way indicates an endorsement, recommendation, or warranty of such party’s products or any
affiliation with such party of any kind.
Unpublished – rights reserved under the copyright laws of the United States.
ii
The Display_BeforeClose Event ........................................................................................................... 13
Pass the Context from Parent to Child (Module Relative Display) ...................................................... 14
Introduction .......................................................................................................................................... 14
iii
OVERVIEW
ABOUT THIS DOCUMENT
This document is exclusive to the OSIsoft Virtual Campus (vCampus) and is available on its
online Library, located at http://vCampus.osisoft.com/Library/library.aspx. As such, it is
provided 'as is' and is not supported by OSIsoft's regular Technical Support.
Any question or comment related to this document should be posted in the appropriate
vCampus discussion forum (http://vCampus.osisoft.com/forums) or sent to the vCampus
Team at vCampus@osisoft.com.
VBA allows a user to build displays that modify themselves based on an event. This event
can be a mouse click as well as the receiving of a new value in a PI Point.
PI ProcessBook 3.2
1
"ONE-CLICK VALUE TREND" VBA SCRIPT
INTRODUCTION
The "One-Click Value Trend" VBA script places a displayed value into a PI ProcessBook trend
when it is selected. You can access a trend of any value in your display.This VBA script
references a Trend object named PointTrend on the display. In your own displays, you could either
modify the VBA script to reference a trend with a different name or rename your Trend object to
PointTrend:
2
THE DISPLAY_CLICK EVENT
1. We select the Display_Click event:
3. Now we get a reference of the selected symbols collection and check if there any are
selected :
' Set selected symbols object to the selected symbols collection.
Set MySymbols = ThisDisplay.SelectedSymbols
4. As we have the collection of symbols, we loop through it and search for a value
symbol and modify the trend object accordingly:
For Each MySymbol In MySymbols
' If symbol object is type pbSymbolValue, it is a value object.
If MySymbol.Type = pbSymbolValue Then
' Get the tagname and trend it.
strPITagname = MySymbol.GetTagName(1)
' Pick up the current trend time range.
strTrendStartTime = ThisDisplay.PointTrend.StartTime
strTrendEndTime = ThisDisplay.PointTrend.EndTime
' If selected tag is not the current one in trend, put it there.
If Not (PointTrend.GetTagName(1) = strPITagname) Then
' Remove old point trace from trend.
PointTrend.RemoveTrace(1)
' Add trace for this tag to trend ( using format \\severname\tagname )
PointTrend.AddTrace(strPITagname)
' Refresh time range, setting
Call PointTrend.SetStartAndEndTime(strTrendStartTime, strTrendEndTime)
End If
5. In addition, we can use the PI SDK to add some PI Point information to the trend
title. In order to do this, we have to reference the PI SDK:
3
6. Then we can use PI SDK objects:
' If you plan to use this script to enhance displaysviewed
' in PI ProcessBook, you can use the PI SDK.
'
' NOTE: You will need to add a reference to the PI SDK libraries
' to your PI ProcessBook VBA
' project.
7. And finally we set the trace scale to "Autorange" and finish the loop:
' Refresh trend Y-axis scale.
Success = PointTrend.SetTraceScale("Autorange", "Autorange")
Exit Sub
End If
Next MySymbol
4
"ONE-CLICK VALUE AD HOC TREND" VBA SCRIPT
INTRODUCTION
This VBA script automates the "ad hoc trending" functionality of PI ProcessBook. When you
click any value in the display, an ad hoc trend appears.
3. Now we get the selected symbols object and check if there are selected symbols at
all:
5
' Set selected symbols collection object to the
' selected symbols currently on ThisDisplay.
Set pbSymbols = ThisDisplay.SelectedSymbols
6
CREATING XYPLOTS PROGRAMMATICALLY
INTRODUCTION
The following VBA script creates several PI ProcessBook objects and populates them with tags
retrieved by a simple PI SDK call. The purpose of this example is to show you how to create display
objects programmatically instead of drawing them manually. The advantage of the programmatic
approach gets obvious if the number of objects increases.
This VBA script was inspired by a customer in training who asked for an option to create XYPlots
where ten tags compare to each other. If the number of tags to trend is n, you can simply calculate
that the number of XYPlot is n(n-1)/2 so for 10 tags this will be 45 XYPlots to draw.
7
3. We define some constants. These constants define the size of the XYPlot symbol, the
time range of the trend, the tag mask and the PI Server.
' Heigth of symbol
Const cHeigth = 600
' Width of symbol
Const cWidth = 600
' The timerangefor our symbols
Const strStartTime = "Y"
Const strEndTime = "T"
' The taglist we use to build our symbols
Const strWhereClause = "Tag='sin*'"
' The PI Server. make sureit is already in the known server list!
Const strServer = "ANDREASPC"
8
2. First we disable the redrawing to reduce the load:
' Improve the performance by turning off
' redrawing until we have drawn all our symbols
Application.Redraw = False
5. Create the PointList and initialize it with the tags based on the tag mask:
' The point list
Dim MyPointList As pisdk.PointList
6. Get the count, limit it to 15 (to reduce the number of symbols) and calculate how to
get them best arranged as a square of symbols:
' Get the count
TagMaxNumber1 = MyPointList.Count
9
TagMaxNumber2 = Sqr(iConnections)
7. Now we iterate through all the tags and create an XYPlot symbol:
' Get an XY plot for each tag with each tag;-)
' from the first to the last tag
For a = 1 To TagMaxNumber1
' With each other tag
For b = (a + 1) To TagMaxNumber1
' Add an XY plot
Set MyXYPlot = ThisDisplay.Symbols.Add(pbSymbolXYPlot)
8. We cannot simply add a trace to a XYPlot object directly. You have to add tags to the
collection that is part of the XYDefinition object:
' The XY definition
Dim MyDefinition As XYDefinition
' Initialize the XY definition
Set MyDefinition = MyXYPlot.GetDefinition
9. So let us move the XYPlot to the appropriate position and adjust its size:
' Move it to the right place and the right size
MyXYPlot.Left = -14950 + (((i Mod TagMaxNumber2) + 1) * (cWidth + 5))
MyXYPlot.Top = 14950 - (((i \ TagMaxNumber2)) * (cHeigth + 5))
MyXYPlot.Height = cHeigth
MyXYPlot.Width = cWidth
10. Adjust the time range and don’t forget to count the XYPlot (in favor of some complex
mathematics I simply count the XYPlot to arrange them in a square ;-)):
' Set the time
Call MyXYPlot.SetTimeRange(strStartTime, strEndTime)
' Count!
i = i + 1
ThisDisplay.Zoom = “FitAll”
10
1. Let's use the Display_DataUpdate event to analyze all of our XYPlots when they
update (You may wonder why we don’t do that when we create them – well, the
statistics are not updated after you set the definition, so you have to wait for the
data update) and declare some variables:
' We want to access the XY statistic
' this should be done after the DataUpdate
11
“SETVIEWPORT” VBA CODE SNIPPET
INTRODUCTION
PI ProcessBook displays can be huge and the amount of information overwhelming. With the help of
VBA we can zoom the view to a certain area to catch the eye of the observer. In this example we will
use a digital tag (CDM158) to drive the visible area.
12
“MODIFIED” PROPERTY
INTRODUCTION
The PI ProcessBook Display class exposes a property called Modified. Simply catch the
BeforeClose event of the Display and set the Modified property to False. PI ProcessBook
will not ask you to save the changes.
This can be handy if your users are allowed to change the display but you neither want them to safe
the changes nor bother them with the question.
13
PASS THE CONTEXT FROM PARENT TO CHILD
(MODULE RELATIVE DISPLAY)
INTRODUCTION
In this example we will play with the concept of a Module-Relative Display (MRD), which consists of
a unique display that can represent data from a list of similar Modules (as opposed to creating one
display per Module). Looking at assets in a "top-down" approach is not uncommon and therefore
one might be interested in opening a child display from another display (i.e. the parent). In this
scenario, passing the "module context" (i.e. the Module you were looking at) from the parent display
to the child display might prove useful, such that the user does not have to re-select the desired
Module from the list.
To illustrate the functionality we have built two displays: ParentDisplay.PDI with a Microsoft Forms
2.0 CommandButton and a Module Relative Trend showing an Alias and ChildDisplay.PDI with a
Module Relative Trend showing an Alias. Both displays use the same modules and we will use the
Microsoft Forms 2.0 CommandButton named cmdOpenChildDisplay to open the child display.
End Sub
1. First we get the path to the current display and remove the filename:
' This will hold the absolute path to the display,
' exluding fileame
Dim myPath As String
14
' Get the path from this display, excluding filename
myPath = ThisDisplay.Path
myPath = Left(myPath, InStrRev(myPath, "\", -1, vbTextCompare))
AliasContextHandler.CurrentContext(myChildDisplay) = _
AliasContextHandler.CurrentContext(myParentDisplay)
PropContextHandler.CurrentContext(myChildDisplay) = _
PropContextHandler.CurrentContext(myParentDisplay)
15
PASS THE CONTEXT FROM PARENT TO CHILD
(ELEMENT RELATIVE DISPLAY)
INTRODUCTION
In this example we will play with the concept of an Element-Relative Display (ERD), which consists of
a unique display that can represent data from a list of similar AF Elements (as opposed to creating
one display per AF Element). Looking at assets in a "top-down" approach is not uncommon and
therefore one might be interested in opening a child display from another display (i.e. the parent). In
this scenario, passing the "element context" (i.e. the AF Element you were looking at) from the
parent display to the child display might prove useful, such that the user does not have to re-select
the desired element from the list.
To illustrate the functionality we have built two displays: ParentDisplayERD.PDI with a Microsoft
Forms 2.0 CommandButton and an Element Relative Trend showing an Attribut and
ChildDisplayERD.PDI with an Element Relative Trend showing an Attribute. Both displays use the
same AF Elements and we will use the Microsoft Forms 2.0 CommandButton named
cmdOpenChildDisplay to open the child display.
End Sub
6. First we get the path to the current display and remove the filename:
16
' This will hold the absolute path to the display,
' exluding fileame
Dim myPath As String
Note: The code is almost identical to the module context, except that you have only one context
(not the one for alias and property), and its name is “E”.
17
REVISION HISTORY
2006 Initial DevNet version by Curt Hertler (Click Value AdHocTrend) and Tom Hosea
(Click Value Trend)
18