Question

I’ve tried to use com4j to get the PartDocument of a Catia file but when I get the object it was a Document object and not a PartDocument object. By consequent I cannot access to the method “part” of my object. I try to cast Document to PartDocument but the system reaches a cast exception. I ve tried with java 7 x86 or 64 and end up with the same results.

  1. I generate the java code with

java -jar tlbimp.jar -o E:\testcatia\src -p catia InfTypeLib.tlb

java -jar tlbimp.jar -o E:\testcatia\src -p catia SMTypeLib.tlb

java -jar tlbimp.jar -o E:\testcatia\src -p catia PartTypeLib.tlb

java -jar tlbimp.jar -o E:\testcatia\src -p catia CATIAAppTypeLib.tlb

com4j has built classes and we have : Document and PartDocument (PartDocument extends Document)

  1. my program code

    Application app = COM4J.createInstance( catia.Application.class, "CATIA.Application" ); Documents docs=app.documents(); PartDocument partdoc = (PartDocument) docs.open(new Holder<>("E:\\test.CATPart")); Part part= partdoc.part();

exception : com.sun.proxy.$Proxy9 cannot be cast to catia.PartDocument

Has anyone faced this kind of issue before, and if so I would be very thankful for a solution.

I have tried the JACOB API and it is working. The Document has a correct instance of a PartDocument but the code is too difficult to manipulate

`ActiveXComponent app =new ActiveXComponent("CATIA.Application");
app.setProperty("Visible", new Variant(true));
Dispatch oDocuments = app.getProperty("Documents").toDispatch();
Dispatch oDocument = Dispatch.call(oDocuments, "Open", "E:\\test.CATPart").toDispatch();
Dispatch oPart = Dispatch.get(oDocument,"Part").toDispatch();
Dispatch oBody = Dispatch.get(oPart,"MainBody").toDispatch();`
Was it helpful?

Solution

You have to use QueryInterface (https://com4j.java.net/runtime-semantics.html)

Document doc = docs.open(new Holder<>("E:\\test.CATPart"));    
PartDocument part = doc.queryInterface( PartDocument.class);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top