Quantcast
Channel: OSIsoft Users Community
Viewing all articles
Browse latest Browse all 1120

Let's Share Useful Code Snippets!

$
0
0

Hi Community,

 

A lot of us have been looking for a good place to share and discuss different functions that we find helpful in day-to-day PI activity, and that's exactly what we are going to do here and now.  Please feel free to provide different functions that you often use below so we can discuss and learn from them.  Any coding language and applications are welcome; just provide us with the language the code is written in and a brief description of what it does/what you use it for.

 

As a quick note, please do note request assistance troubleshooting code here as that is best handled by creating a new separate thread on the forum.

 

I'll start by providing a few of my favorite ProcessBook VBA macros:

 

Here is a short script that I use to iterate through every symbol in a ProcessBook display.  Usually I'll add an if statement so that I can sort by type.  I find this extremely helpful for passing along symbol changes.  For example, changing the time range on one trend automatically updating the time range on all other trends:

 

Dim Sym as PbObjLib.Symbol 'create a variable for a generic symbol'

For Each Sym in Application.ActiveDisplay.Symbols 'iterate through all symbols in display'

   If Sym.Type = <desired type> Then 'for the desired type use pbSYMBOLTYPE.pbSymbol<your desired symbol> for example a trend is pbSYMBOLTYPE.pbSymbolTrend'

      'insert code here'
 
   End If

Next Sym

 

This next one's a bit more complicated but I find it to be extremely valuable.  I have a trend symbol that I keep in the corner of my displays.  When I double-click on any bar or value symbol in my display the corner trend updates with an 8-hour trace of the bar or value symbol.  One of the very cool things about this code is that, because of how we resolve dataset names, we do not need any special handling to deal with symbols that use datasets.  This trend will update correctly no matter what we use:

 

Private Sub Display_BeforeDoubleClick(bCancelDefault As Boolean, ByVal lvarX As Long, ByVal lvarY As Long)

If ThisDisplay.Application.RunMode = True Then

    If SelectedSymbols.Count > 0 Then 'make sure we have actually double-clicked a symbol'
    
        If SelectedSymbols.Item(1).Type = pbSYMBOLTYPE.pbSymbolBar Or SelectedSymbols.Item(1).Type = pbSYMBOLTYPE.pbSymbolValue Then 'update trend for a bar or value symbol'
        
            bCancelDefault = True
            
            Trend1.RemoveTrace 1 'remove the existing trace'
            
            Trend1.AddTrace SelectedSymbols.Item(1).GetTagName(1) 'add a trace using the tag or dataset from the selected symbol'
            
        End If
        
    End If
    
End If

End Sub

 

I hope you find these scripts helpful and let me know if you'd like me to elaborate on any of the information provided here.

 

Please comment on and like any particularly helpful functions you find here.  We are looking forward to seeing the awesome scripts you all have created!


Viewing all articles
Browse latest Browse all 1120

Trending Articles