API example: The minimum, maximum, average of an entity list

When you select a set of entities, the selection is made available through the Application Programming Interface (API) as an entity list. This list is unsorted, like the list of elemental results in a result.

The following example finds the minimum, maximum and average values of the result that is currently displayed, for the selected entities.

Note: For large models or large selections, it may be beneficial to sort the lists first to reduce the time spent matching the selected entities to results.

Before running this script, you must complete the following steps:

  1. Select entities in the model.
  2. Select a plot.

In the following example, some error checking has been implemented. The script checks that entities have been selected, and that there is a valid, active plot.

'%RunPerInstance
'@
'@ DESCRIPTION
'@ This command will calculate the result Minimum Maximum and Average Value   
'@ of the entities selected 
'@
'@ SYNTAX
'@ MinimumMaximumAverage
'@
'@ PARAMETERS
'@ none 
'@
'@ DEPENDENCIES/LIMITATIONS
'@  1. User must select a group of entities before using this command
'@  2. User must select a result before using this command
'@  3. Only works with nodal/elemental plots  ie no vector/tensor plots
'@
'@ History
'@ Created DRA 9/8/2006
'@@ 
Option Explicit
SetLocale("en-us")
Dim SynergyGetter, Synergy
On Error Resume Next
Set SynergyGetter = GetObject(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%SAInstance%"))
On Error GoTo 0
If (Not IsEmpty(SynergyGetter)) Then
  Set Synergy = SynergyGetter.GetSASynergy
Else
  Set Synergy = CreateObject("synergy.Synergy")
End If
Synergy.SetUnits "METRIC"

Dim StudyDoc, Viewer, Plot, PlotManager
Dim EntList, ResultID, ResultType, ResultName, ResultData
Dim IndpValues, IndpValues1
Dim EntityIndex, Result, ElementNumber, NodeNumber
Dim Min, Max, Ave, Count, Value, I, J, Ent, Name

' Check that the user has selected some Entities
Set StudyDoc = Synergy.StudyDoc
Set EntList = StudyDoc.Selection
If EntList.Size <= 0 Then
   MsgBox "No Entities Selected",,"Error"
   WScript.Quit
End If

' Read/Check the Plot Information is correct
' Get Plot
Set Viewer = Synergy.Viewer
Set Plot = Viewer.ActivePlot()
If Plot Is Nothing Then
   MsgBox "Please Select a result",,"Error"
   WScript.Quit
End If

' Read Result ID Type
ResultID=Plot.GetDataID

' Read the Result Name
ResultName = Plot.GetName
 
' Check for Invalid Plot Data
ResultData = Plot.GetDataType
If ResultData <> "ELDT" and ResultData <> "NDDT"  Then
   MsgBox "Data Type Not Supported",,"Error"
   WScript.Quit
End If

' Check for Invalid Plot Types
ResultType = Plot.GetPlotType
If ResultType <> "Contour Plot"  Then
   MsgBox "Plot Type Not Supported",,"Error"
   WScript.Quit
End If

' Read the Result Data set
' Ensure we read the last data set
Set PlotManager = Synergy.PlotManager
Set IndpValues = Synergy.CreateDoubleArray()
PlotManager.GetIndpValues ResultID, IndpValues
Set IndpValues1 = Synergy.CreateDoubleArray()
IndpValues1.AddDouble(IndpValues.Val(IndpValues.Size()-1))
' Read Result Data from last set

Set EntityIndex = Synergy.CreateIntegerArray()
Set Result = Synergy.CreateDoubleArray()
PlotManager.GetScalarData ResultID, IndpValues1, EntityIndex, Result


' Calcuate the required Values
Min = 1.0E20   ' Set extremely Large Value
Max = -1.0E20  ' Set extremely Small Value
Ave = 0.0
Count= 0

' If Result in Elemental then loop through selected elements
If ResultData = "ELDT" Then
  For I = 0 To EntList.Size()-1
    Set Ent = EntList.Entity(I)
    Name = Ent.ConvertToString
    If (Left(Name,1) = "T" and Left(Name,2) <> "TE" ) Then
          ElementNumber = StudyDoc.GetEntityID(Ent)
      For J = 0 To EntityIndex.Size()-1
        If EntityIndex.Val(J) = ElementNumber Then
          Count = Count + 1
          Value = Result.Val(J)
          If (Value > Max) Then 
            Max = Value
          End If
          If (Value < Min) Then 
            Min = Value
          End If
          Ave = Ave + Value
        End If
      Next
    End If
  Next
' If Result in Nodal then loop through selected  nodes
ElseIf ResultData = "NDDT" Then
  For I = 0 To EntList.Size()-1
    Set Ent = EntList.Entity(I)
    Name = Ent.ConvertToString
    If (Left(Name,1) = "N" ) Then
          NodeNumber = StudyDoc.GetEntityID(Ent)
      For J = 0 To EntityIndex.Size()-1
        If EntityIndex.Val(J) = NodeNumber Then
          Count = Count + 1
          Value = Result.Val(J)
          If (Value > Max) Then
            Max = Value
          End If
          If (Value < Min) Then
            Min = Value
          End If
          Ave = Ave + Value
        End If
      Next
    End If
  Next
End If

' Display Results
If Count > 0 Then
        Ave = Ave / CDbl(Count)
  MsgBox "Analysing result :" & ResultName & vbcrLf & _
         "Number of Selected Entities is: " & Cstr(Count) & vbcrLF & _
         "Minimum is: " & Cstr(Min)  & vbcrLF & _
         "Maximum is: " & Cstr(Max)  & vbcrLF & _
         "Average is: " & Cstr(Ave)
Else
  MsgBox "Analysing result :" & ResultName & vbcrLf & _
         "Number of Selected Entities is: " & Cstr(Count) & vbcrLF
End If        
 
 
MsgBox "Script Completed"
WScript.Quit