SIMUL8's API (OLE Automation/COM/ActiveX interface).

This is a standard Windows COM interface that allows any application that can use COM to drive SIMUL8 without user intervention.

There are two different levels of COM functionality available with SIMUL8:

1. SIMUL8 Basic Edition

A COM interface that allows basic operation of SIMUL8 including opening and running simulations, running trials, extracting results etc. Both .S8 and .XML simulation files are supported

2. SIMUL8 Professional Edition COM Interface

All of 1 plus the functionality to change simulation programmatically (for example by creating and linking simulation objects in the same way as can be done from SIMUL8's own internal Visual Logic.

(Note that SIMUL8 also supports an older style DDE/DLL interface that continues to work for back compatibility with old implementations. SIMUL8 V7 also supports a COM interface created for a few specific applications. This older COM interface is now not visible in most applications, but continues to work for back compatibility. It should not be used for new applications. Only the COM interface documented here should be used for most applications)

How to use SIMUL8 COM

The remainder of this document is a step by step guide to using SIMUL8 Basic's COM interface.

It assumes no previous knowledge of COM or VBA but does assume some understanding of the use of Excel (note: Any COM-compatible language can be used, VBA is used here for example purposes because it is widely available (inside Excel). No recommendation should be implied from its use here and SIMUL8 Corporation's support team is not available to handle Visual Basic questions! - but we will help you with this example and the COM interface, and various languages use of the COM interface).

The example used here is included with this build (examples\api\com\example1.xls) but we recommend you build it yourself because there are key steps to learn in checking boxes in VBA.

In this example we use a standard S8 file and we assume you have installed SIMUL8 in the default directory. However XML files can be used with the advantage that their content can be changed under program control (by reading and writing the file to, for example, change values of variables).

Step by Step getting started guide

  • Open Excel with a blank spreadsheet
  • Open the CONTROL TOOLBOX toolbar (click RIGHT on any toolbar and check the CONTROL TOOLBOX option)
  • From this toolbar select a button and drag a rectangle on the spreadsheet to create a button (don't cover column A because we will use it later).
  • Double click the new button and you will see VBA open and place some empty code on the screen
  • In the menu on the VBA window click Tools/References
  • Find and check SIMUL8 Library
  • Click OK. Excel's VBA can now help and prompt you to get the syntax right when using SIMUL8's COM/ActiveX interface.
  • In the code window move the cursor up above the Private Sub line and type:
      ''Private WithEvents MYSIMUL8 As SIMUL8.S8Simulation''

We are now going to make the button open SIMUL8

  • Inside the code for the button click type:
      ''Set MYSIMUL8 = GetObject("", "SIMUL8.S8Simulation")''

Note that the Set keyword is NOT optional here (in most VBA it is optional - but NOT here!)

  • ALT-TAB to the main part of EXCEL.
  • Save the file.
  • Close the file
  • Reopen the file, and tell Excel to Enable Macros (otherwise your button will not work)
  • Click the button and SIMUL8 will start. DO NOT attempt to close SIMUL8 from the SIMUL8 Screen (Because it is not in control, Excel is)
  • ALT-TAB back to Excel
  • Use Tools/Macros/Visual Basic Editor to redisplay the VBA screen and click the RESET button. This stops your macros and closes SIMUL8 (we will next create a button to do this neatly.)
  • Use the CONTROL TOOLBOX toolbar again to put a second button on the screen, double click it and enter this code:
  Private Sub CommandButton2_Click()
     MYSIMUL8.Open "c:\program files\simul8\examples\others\demo2.s8"
     MYSIMUL8.RunSim 2400
  End Sub 
  • Put a 3rd button on the screen with this code:

Private Sub CommandButton3_Click()

Set MYSIMUL8 = Nothing

End Sub so we can close SIMUL8, when we want to, from Excel (again, the SET keyword is important)

  • Switch out of Design Mode so you can use the buttons
  • Click each button in turn to Open SIMUL8, Open a simulation and run it, Close SIMUL8.
  • Next we will add an 'Event' to your VBA so you know when the simulation run has finished and you can display some results
  • Go back to the VBA screen and select MYSIMUL8 in the left had drop down box.
  • In the right drop down box select S8SimulationEndRun and this code will appear:

Private Sub MYSIMUL8_S8SimulationEndRun() End Sub

  • Add these 3 extra lines:

Private Sub MYSIMUL8_S8SimulationEndRun()

For n = 1 To MYSIMUL8.ResultsCount
  Worksheets("Sheet1").Cells(n, 1).Value = MYSIMUL8.Results(n).Value
Next n

End Sub At the end of the run SIMUL8 will tell Excel the run has finished. Excel them loops through all the results on the KPI Summary and adds the results into the cells in column 1 on sheet 1

  • Go back to Excel, click button 1, then 2, then wait for SIMUL8 to finish the run. The results will appear once the run is complete.
  • Now click button 3 to close SIMUL8.
  • Try out the other methods and properties listed in the SIMUL8 COM Object simulation reference over the page.

Finally, one helpful hint

If you don't want to see SIMUL8 running put the line: MYSIMUL8.Visible = False after the Set MYSIMUL8 = GetObject(””, “SIMUL8.S8Simulation”) line