Option Explicit

'How long between updates of the progress graphs
Const PROGRESS_GRAPH_UPDATE_INTERVAL% = 25

'A Windows API function, used to rename progress graphs.
Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long

'This variable allows use to receive events from the RDK library:
Dim WithEvents RDKApp As RDKApplication

'Variables that control the real-time progress graph:
Dim m_ShowProgressGraph As Boolean
Dim m_RealJPGFile As String
Dim m_TempJPGFile As String

'This function is used to "connect" an instance of this class module to the
'RDKX library, so it can receive RDKX events:
Public Sub Attach(x As RDKApplication)
   Set RDKApp = x
End Sub

'On shutdown of your application, call this function to "disconnect" the
'class module from the RDKX library:
Public Sub Detach()
   Set RDKApp = Nothing
End Sub

'This function initializes this module for the display of the live updating progress graph:
Public Sub InitializeProgressGraph(showProgressGraph As Boolean, outputDir As String)
   m_ShowProgressGraph = showProgressGraph
      
   If showProgressGraph Then
      'save the names of the "temp" and "real" .jpg file destinations.  See the iteration
      'event comments for more information...
      m_TempJPGFile = outputDir & "\temp.jpg"
      m_RealJPGFile = outputDir & "\prog.jpg"
      
      With RDKApp.GraphDefaults
         .RevertToDefaultSettings
         .Destination = RDKJPGFile
         .DestinationFile = m_TempJPGFile
         .PictureHeight = 5000
         .PictureWidth = 6000
         .DisplayMean = True
         .DisplayLegend = False
      End With
   End If
End Sub

'This is an RDKX event which is called each iteration of an RDK simulation.
'Every PROGRESS_GRAPH_UPDATE_INTERVAL iterations, a progress graph is generated.
Private Sub RDKApp_Iteration(whichSim As Long, whichIter As Long, cancel As Boolean)
   With RDKApp
      'Calculate the output.
      'For this simple model, the output is simply equal to the input:
      .Outputs(1) = .Inputs(1)
      
      'If necessary, update the progress graph:
      If m_ShowProgressGraph And (whichIter Mod PROGRESS_GRAPH_UPDATE_INTERVAL = 0) Then
         .GraphDefaults.MainTitle = "Simple Simulation" & vbLf & whichIter & " Iterations"
         .Outputs(1).Results(whichSim).Graph RDKResultCurveTypeHistogram
         
         'To make sure the progress graph update smoothly on the web, the graph is written to
         'a "temp" .jpg file and then renamed to the "real" .jpg file that a web browser can read.
         'This keeps us from accidentally writing to a file which the web server is in the
         'middle of delivering to a client.  We make use of the Windows API function "MoveFile"
         'to do this.  If the web server is in the middle of serving up the \prog.jpg file,
         'the MoveFile call will fail, and will not interrupt the web server.
         On Error Resume Next
         Kill m_RealJPGFile
         MoveFile m_TempJPGFile, m_RealJPGFile
         On Error GoTo 0
      End If
   End With
End Sub


VB Source Code Publisher R2

© Palisade Corporation, eDecisionTools.com, 2001