API 示例:实体列表的最小值、最大值和平均值

选择一组实体时,可以通过应用程序编程接口 (API) 以实体列表的形式实现选择。如同结果中的单元结果列表一样,此列表未经排序。

以下示例查找当前显示的所选实体结果的最小值、最大值和平均值。

注: 对于大型模型或大量选择,先对列表进行排序可能对于减少匹配所选的实体结果所用的时间有所帮助。

在运行此脚本之前,必须完成以下步骤:

  1. 选择模型中的实体。
  2. 选择图。

以下示例中实施了一些错误检查。脚本会检查是否已经选中实体,以及是否存在有效的激活图。

'%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