Using Gen Automation from Java utilising Eclipse SWT Standard Widget Toolkit and OLE.

Document ID : KB000053179
Last Modified Date : 14/02/2018
Show Technical Document Details

Description:

This knowledge document explains how to access the CA Gen Toolset from a Java application using Eclipse SWT Win32 OLE interface. Eclipse SWT stands for the Standard Widget Toolkit and is a part of the Eclipse framework. This interface allows an easy way to access OLE controls from the Java environment.

Solution:

Introduction

This knowledge document explains how to access the CA Gen Toolset from a Java application using Eclipse SWT Win32 OLE interface. Eclipse SWT stands for the Standard Widget Toolkit and is a part of the Eclipse framework. This interface allows an easy way to access OLE controls from the Java environment.

One of the methods to access CA Gen Toolset and models opened by the toolset is to use the CA Gen Toolset Automation. The CA Gen Toolset Automation has been developed for the Microsoft Windows environment and it is used to develop plug-ins for the CA Gen Toolset.

The Eclipse SWT Win32 OLE Java together with the CA Gen Toolset Automation opens the possibility of developing plug-ins and various utilities using Java technology.

The Eclipse runtime instantiates a pointer to the currently opened CA Gen Toolset using the Toolset OLE automation interface. This gives the Java application access to the functionality provided by the interface. In particular, it will allow the application to read/modify the currently opened model using the meta-model API functions added to the Toolset OLE automation interface by this project as well as other functions already provided by the interface.

Eclipse SWT Win32 OLE

The package org.eclipse.swt.ole.win32 contains all implementation classes for SWT Win32 OLE. The package is delivered as a part of any Eclipse distribution package and can be downloaded from the http://www.eclipse.org/downloads/ website.

You will find the package in the org.eclipse.swt.win32.win32.x86_3.4.0.v3448f.jar file located in the /plugins subdirectory of the Eclipse installation. (the specific name will vary depending on the version of eclipse installed e.g. in 3.4.2 the file name is org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar)

This package contains the classes which provide the public API to the Microsoft Win32 Object Linking and Embedding mechanism that the win32 variant of SWT is capable of using. Two classes OleControlSite and OleAutomation are essential and you need to know how to use them.

The first, OleControlSite, provides a site to manage an embedded ActiveX Control within a container. The second, OleAutomation, provides a generic mechanism for accessing functionality that is specific to a particular ActiveX Control or OLE Document. Both, ActiveX Control or OLE Document must support the IDispatch interface in order to provide OleAutomation. Arguments are passed around in the form of Variant objects.

A Variant class is a generic OLE mechanism for passing data of different types via a common interface. It is used within the OLEAutomation object for getting or setting properties of invoking methods on an OLE Controls or OLE Documents.

Sample code

The following sample code below shows a very basic application accessing model opened in the CA Gen Toolset.

package my.auto.test;
   
import org.eclipse.swt.SWT;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Shell;
   
public class SampleCode {
 	 private OleControlSite site;
  	 private OleAutomation toolset;
  	 private OleAutomation models;
  	 private OleAutomation model;
  	 private OleAutomation autoOpenAPI;
 
  	 public static void main(String[] args) {
 
  		System.out.println("Starting...");
   		SampleCode sampleCode = new SampleCode();
   		sampleCode .process();
   		System.out.println("Ending...");
 	}
 
   	private void process() {
 
    		// Note 1. Connect to the toolset
    		Shell shell = new Shell();
    		OleFrame frame = new OleFrame(shell, SWT.NONE);
    		site = new OleControlSite(frame, SWT.NONE, "COOLgen.Toolset");
    		toolset = new OleAutomation(site);
   	{
    		// Note 2. Find opened models object
    		int[] ids = toolset.getIDsOfNames(new String[] { "Models" });
    		Variant variant = toolset.getProperty(ids[0]);
    		models = variant.getAutomation();
   	}
   	{
    		// Note 3. Get model object
    		int ids[] = models.getIDsOfNames(new String[] { "Item" });
    		Variant[] rgvarg = new Variant[1];
    		rgvarg[0] = new Variant(1);
    		Variant variant = models.invoke(ids[0], rgvarg);
    		model = variant.getAutomation();
   	}
  	{
    		// Note 4. Extract OpenAPI object
    		int ids[] = model.getIDsOfNames(new String[] { "OpenAPI" });
    		Variant variant = model.getProperty(ids[0]);
    		autoOpenAPI = variant.getAutomation();
   	}
   	{
    		// Note 5. Count all objects in the model
    		int ids[] = autoOpenAPI
          			.getIDsOfNames(new String[] { "CountModelObjs" });
    		Variant variant = autoOpenAPI.invoke(ids[0]);
    		long numberOfObjects = variant.getInt();
    		System.out.println("Number of objects in the model is " + numberOfObjects);
   	}
     }
 }

This sample code shows a sequence of Java statements establishing connection with the site COOLgen.Toolset, retrieving information about opened models on the workstation, selecting the first model and counting the number of objects in the selected model. The sample demonstrates the use of all essential classes. CA Gen Toolset Automation documentation explains which objects can be retried and how they are related. Good understanding of how the Gen Toolset Automation works will be very helpful in developing any complex application accessing and updating Gen Models.

  • This is a typical opening sequence and the top object representing the toolset is retrieved before navigating further by selecting a specific model and retrieving objects constituting model itself.

  • The Models object is collection of opened models.

  • Select the first model from the collection.

  • Retrieve OpenAPI object. An OpenAPI object provides functions similar to the Encyclopedia API functions.

  • Invoke CountModelObjs function to count all objects in the model.

You need to be sure that all runtime classes are on the classpath when running this application. Additionally you need to place somewhere on the classpath the swt-win32-3448.dll (or name as appropriate to the Eclipse version) library supporting the runtime classes. You can extract the DLL file from the same jar file as the runtime classes. You need to do so only when running the application from the outside Eclipse. You need to launch Toolset and open any model before running this application. When running the application you will see similar output to that below:

Starting... 
Number of objects in the model is 1008 
Ending... 

Resources

More information about used technology can be found at the following location: