javacomcom4jcatia

How to bridge Java to CATIA COM API with com4j


Here is the C++ code:

#import "path\to\catia\intel_a\code\bin\InfTypeLib.tlb" no_namespace

// ...

ApplicationPtr catia;

catia.CreateInstance("CATIA.Application");

catia->PutVisible(VARIANT_TRUE);

I want to call the same CATIA API in Java with com4j. According to com4j tutorial page, the Java definitions were generated by following command:

java -jar ..\..\tlbimp.jar -o catia -p catia InfTypeLib.tlb

But the result ClassFactory has no static methods to create COM objects.

import com4j.*;

/**
 * Defines methods to create COM objects
 */
public abstract class ClassFactory {
  private ClassFactory() {} // instanciation is not allowed
}

Then I tried another typelib 'CATIAAppTypeLib.tlb'.

import com4j.*;

/**
 * Defines methods to create COM objects
 */
public abstract class ClassFactory {
  private ClassFactory() {} // instanciation is not allowed


  public static org.huys.catia.AppType._CATIAApp createCATIAAppObj() {
    return COM4J.createInstance( org.huys.catia.AppType._CATIAApp.class, "{5A29422A-F2B0-11D4-A3AA-00D0B756AD5D}" );
  }

  public static org.huys.catia.AppType.CATIAVBAHostingApplication createCATIAVBAHostingApplicationImpl() {
    return COM4J.createInstance( org.huys.catia.AppType.CATIAVBAHostingApplication.class, "{0663D095-471D-11D2-9AA3-00A024941EF0}" );
  }
}

When calling these methods in Java, I got

com4j.ExecutionException: com4j.ComException: 80040154 CoCreateInstance failed : Class not registered : .\com4j.cpp:153

I cannot find the method to create an instance for the Application interface.

Can anyone help me for this? Thanks.


Solution

  • The generated ClassFactory for InfTypeLib.tlb doesn't contain the static method for creating instances of Application.

    This can be worked around by add the method manually.

    public static catia.Application createApplication() {
        return COM4J.createInstance( org.huys.catia.Application.class, "{87fd6f40-e252-11d5-8040-0010b5fa1031}" );
    }
    

    The GUID for "CATIA.Application" can be find in the registry. Or just replace it with the string.