Most of JMP can be driven through OLE automation. Please see the Automation Reference.pdf in JMP/12/Documentation for details about automating JMP. This document introduces how to automate JMP through Visual Basic and using Visual C++ with MFC. It also contains details for the methods and properties that JMP exposes to automation clients like Visual Basic and Visual C++
The JMP Samples/Automation folder contains several example Visual Basic .Net, Visual C# .Net, and Visual C++ .Net programs that automate features in JMP.
1.
Select Project > References in VB. A list of applications that are known to VB appears. If JMP is not in that list, select Browse. A file window asks you to locate a .tlb (Type library) file. Find the icon for the JMP type library in the JMP directory. Select this library and click OK.
2.
Open the object browser by selecting View > Object Browser in VB. Select JMP from the drop down list box.
Now that you have access to the type library information, write the necessary code to instantiate JMP. This is done with CreateObject. In global declarations for the VB project, create a variable of type JMP.Application. This is done as:
Now dimension some other variables. Good examples are DataTable, Distrib, Oneway, and JMPDoc. These are specified with JMP.DataTable, JMP.Distribution, JMP.Oneway, and JMP.Document respectively.
Set JMPDoc = MyJMP.OpenDocument("C:\Program Files\SAS\JMP\<version number>\Samples\Data\Big Class.jmp")
The Dim statement indicates the type of variable. This declaration should go in the general declarations section of your VB project, though. If you do not do this, the JMP objects are destroyed when the variable goes out of scope at the end of the procedure.
The first step is to create the analysis object, which is done by calling the CreateOneway method of the document class. Next, X and Y columns are selected, and then Launch is called to create the actual One-way analysis. Each analysis platform has a distinct creation method, which you can view under the Document object in the object browser. In many cases, it is possible to specify options before the Launch of the object, so the analysis output uses the options that are already set. In this example, most option processing is done after the launch of the analysis, which shows the options popup in the display. As you can see, most methods are a simple setting of options, like you might do from a menu. SetAlpha takes an argument, since you do not want to open a window for interaction during automation. CompareMeans takes two arguments, one for the type of comparison and one for the toggle to indicate on or off. The Save method takes a predefined constant (viewable in the object browser) that tells the Oneway analysis what to save.
Here, the FitLine method produces an object of type Fit. This object has methods and properties of its own, which can be manipulated. Remember, the new object created by FitLine can be manipulated only while its variable is in scope.
If a method produces an object that can also be automated, the object browser indicates this. For FitLine, the object browser specifies that the return type is As Fit.
Since this is not a predefined type like short or BSTR, you can probably guess that this is an object. If you look farther down the object browser, you see Fit as an object type. This confirms that an object is produced, and also gives you the methods that Fit supports.
New data tables can be created with the (appropriately named) NewDataTable method of the Application object. A filename is assigned at creation time. This method returns a column object, which must be retained as long as you want to add rows. By default, 20 rows are created. The SetCellVal method can be used to populate individual cells, and AddRows can be used to add rows as needed. Here is an example:
The JMP Samples\Automation folder contains several example Visual Basic .Net, Visual C# .Net, and Visual C++ .Net programs that automate features in JMP. The Visual Basic programs require Visual Studio 2005 or later.
The sample code for all five example programs assumes the data files reside in the JMP Samples/Data directory. If you move your sample data files, you need to change the path information in the VB samples.
Begin by opening Microsoft Excel. To create a Visual Basic script for an Excel workbook, select Visual Basic from the Developer ribbon. The Visual Basic editor opens in a separate window. On the left side of the Visual basic editor, there is a pane entitled VBA Project. This pane shows the sheets that might have Visual Basic code associated with them, as well as the workbook itself.
VBA Project for Excel
There are three sections involved in the coding for this example. First, there are some variables that are global in scope that are declared in the module1.bas file. This allows these variables to be referenced in other code modules. A module can be inserted into the Visual Basic project by context-clicking on the VBA project icon and selecting Insert > Module. Type the following code into the module. The code declares instances of a JMP application, a JMP data table, and a flag to keep track of whether a document is open or not.
The main module is associated with the workbook. In the VBA Project Browser, the workbook code area is typically assigned the name ThisWorkbook, but this name can be easily changed. The following code goes into this area.
The Workbook_Open subroutine is called when the Excel table is initially loaded. It initializes some variables, starts JMP, and tells JMP to open (through ODBC) the same Excel file that is currently loaded into Excel 2007. Note that JMP opens the Excel file as a database object rather than opening it as a file. This is necessary because JMP does not open a file that is already open in another application.
The Workbook_Change event is generated every time a user changes the data in any cell in any worksheet in the workbook. This sample assumes that there is only one active worksheet in the workbook. The first time the user changes a cell value in the worksheet, the Workbook_Change subroutine creates a Control Chart in JMP using the current data table.
In this sample, the Workbook_change subroutine also creates a PNG graphic file of the Control Chart output and updates it on the disk every fifth time a change is made to the workbook. This just gives some ideas on how Excel events and JMP automation can be used together to create output.
Finally, the Workbook_BeforeClose subroutine is invoked when the Excel workbook is closed, but before the window goes away. The code within this subroutine instructs JMP to close down as well.
Note that there are some limitations in this method. This example is good if the only activities that occur with the data are additions or changes. The Excel Worksheet_Change event is very limited in the reporting that it provides. In particular, cell-by-cell updating of a JMP data table can be difficult in instances where deletion, drag and drop, or block replication needs support.
Using C or C++ to create an automation client can be a long, tedious task. However, if you use the support provided by MFC in Microsoft Visual C++, the task is considerably easier. There are several steps that must be performed in order to get to a state where you can launch the automation server application (JMP in this case). The AutoClient application that is included in the JMP Samples/Automation/Visual C++ Sample directory contains some code that provides ideas on how to get started. The Microsoft sample application CALCDRIV also shows a MFC-based automation client. CALCDRIV is typically included with Visual C++, and on MSDN CDs.
AutoClient shows how to start up JMP and drive a Bivariate analysis and the data table. The sample is much smaller than any of the Visual Basic samples. However, the mechanics behind all the automation calls that you might want to use are the same as the examples with Bivariate and the data table. The following steps are based on the Visual C++ Version 5.0 UI.
2.
Bring up the Class Wizard and select the Automation tab. Select the Add Class drop down list and then the From a Type Library option. Navigate to the JMP install directory until you find JMP.TLB. Select this type library.
3.
You are prompted to confirm the classes that you want to use in your project. If you are unsure what objects (and interfaces) that you want, select them all by Shift-clicking. Select the names for the files where the class wizard generates interface stubs and header information. Class Wizard is generating wrapper classes based on the MFC ColeDispatchDriver class. This gives you easy access to the OLE Invoke automation function without having to know a lot of the technical details. Select OK. Class Wizard generates the two files (.h and .cpp). You should include the .h file in whatever .cpp files use the JMP automation objects. For example, your View class implementation file.
5.
To start JMP, define a variable of type IJMPAutoApp that persist for the length of the automation session. Call CreateDispatch on this variable, passing in the JMP ProgID (“JMP.Application”) as the lone argument. At this point, when the code executes JMP starts.
6.
Call SetVisible(TRUE) on the JMP object created in step 5. If you do not want to see JMP execute, do not do this step. However, for debugging it is necessary.
7.
Now you can use the JMP application object to spawn further objects, which themselves can spawn more objects. The first thing you probably want to do is load a Data table. To load an existing JMP data table, call the OpenDocument method on the JMP object created in step 5. If successful, this method returns a dispatch pointer that can be attached to an object of type IJMPDoc using the AttachDispatch method.
8.
The IJMPDoc object provides the methods to launch the analysis and graphing platforms. Once you create an analysis and attach the dispatch pointer, you can specify the data table columns to use in the analysis and then you can launch it. Once the analysis is launched, you can manipulate it using the properties and methods specific to that particular type of analysis. Code that is taken from the sample application that describes steps 5–8 is shown below: