0

I try to retreive information from catia usig com4j. Some methods require to pass an array in argument to retreive information but the array is never populated. In this example is to get coordinate from a point in catia.

The declaration of the method generated by com4j

public interface Point extends com4j.catia.HybridShape {
     ...   

        void getCoordinates(java.lang.Object[] oCoordinates);
     ...
}

my code to get the coordinate

 public static void testGetPointCoordinates() {
    String catiafile="E:\\test.CATPart";
    Application app =null;
    app = COM4J.createInstance(Application.class, "CATIA.Application");
    Documents documents = app.documents();
    Document oDocument = (Document) documents.open(new Holder<>(catiaFile.getAbsolutePath()));
    PartDocument partDocument =  oDocument.queryInterface(PartDocument.class);
    Part part =  partDocument.part();

    Factory HSFactory = part.hybridShapeFactory();
    HybridShapeFactory HSF = HSFactory.queryInterface(HybridShapeFactory.class);
    HybridBodies hbodies = part.hybridBodies();
    int n = hbodies.count();

    for (int i = 1; i <= n; i++) {
        HybridBody hbody = null;
        hbody = hbodies.item(i);
        int nbitems = hbody.hybridShapes().count();
        for (int j = 1; j <= nbitems; j++) {
            String name = hbody.hybridShapes().item(j).name();
            System.out.println("name=" + name);
            //Object tab[]=new Object[3];
            if (name.startsWith("Point.12")) {//true point
                HybridShape hs = hbody.hybridShapes().item(j);
                Reference reference = part.createReferenceFromObject(hs);
                HybridShapePointCoord p3 = hs.queryInterface(HybridShapePointCoord.class);
                //works
                System.out.println("point name = " + p3.name());
                System.out.println(p3.y().value());//display 50.0

                //doesn't work
                Variant tab[] = new Variant[3];
                for (int k = 0; k < 3; k++) {
                    Variant variant = new Variant();
                    variant.set(k);
                    tab[k] = variant;
                }
                p3.getCoordinates(tab);
                System.out.println(tab[1].getParseableString()); //display 1  (value not modified)
                //doesn't  work
                tab = new Variant[3];
                for (int k = 0; k < 3; k++) {
                    Variant variant = new Variant(Variant.Type.VT_EMPTY);//tested with VT_R4 VT_R8,...
                    tab[k] = variant;
                }
                 System.out.println(tab[1].getJavaCode()); //display null  (Don't know how to print VT_EMPTY as an Java literal)

                 //doesn't  work with this other solution but ok in VBA  
                tab = new Variant[3];
                //Variant v = new Variant(Type.VT_EMPTY);
                tab[0] = new Variant(Variant.Type.VT_EMPTY);
                tab[1] = new Variant(Variant.Type.VT_EMPTY);
                tab[2] = new Variant(Variant.Type.VT_EMPTY);
                HybridShapePointExplicit point = HSF.addNewPointDatum(reference);
                point.getCoordinates(tab);
                System.out.println(tab[1].doubleValue() + " " + tab[2].toString()); //display 0
                 //doesn't  work 
                //crash JVM
               // tab = new Variant[3];
               // p3.getCoordinates(tab);
                break;
            }
        }
    }
}

the code below works in VBA with CATIA

      Dim P1
      Dim coordonnees(2)
      Dim coordonnees2(100, 3)
      coordonnees(0) = 0
      coordonnees(1) = 0
      coordonnees(2) = 0
      Set P1 = HSF.AddNewPointDatum(hbody.HybridShapes.Item(i))
      'fonction to get coordinates
      P1.GetCoordinates coordonnees

      'set name and coordinates
      coordonnees2(Y, 0) = hbody.HybridShapes.Item(i).name
      coordonnees2(Y, 1) = Round(coordonnees(0), 3)
      coordonnees2(Y, 2) = Round(coordonnees(1), 3)
      coordonnees2(Y, 3) = Round(coordonnees(2), 3)
tao
  • 3
  • 3

1 Answers1

0

I'm not familiar with com4j, but Point.GetCoordinates is a restricted method:

Call to a restricted method .

The solution to this problem in VB is you need to create a new variant and set it equal to p3. Then, call GetCoordinates from the variant's context. Intellisense will not work well on the variant, but the call to GetCoordinates will work.

In VB the code would look something like this:

...
Dim p3 As Variant
Dim arrayOfCoord(2) As Variant
Set p3 = hybridShapePointCoord1 'set variant = strongly typed point object
p3.GetCoordinates arrayOfCoord
...

In your case, non-VB, your code might look like this

...    
Object p3Obj; //used in vb:Variant p3Var;
p3Obj = p3;
p3Obj.GetCoordinates(tab);//Intellisense will not work, but the call to this method should
...

Let me know if this works.

GisMofx
  • 982
  • 9
  • 27
  • P3 must be a Point class. The problem is the method GetCoordinates do not modify the array tab – tao Apr 23 '14 at 08:59
  • @tao did you try the code I provided above? Do you get any error messages? Do you get any error messages with your code? I see that you made `tab` and array of variants which is what the method is expecting as a parameter, so that's one thing we can rule out. – GisMofx Apr 23 '14 at 13:43
  • In VBA in catia it is working : ` Dim coordonnees(2) Set P1 = HSF.AddNewPointDatum(hbody.HybridShapes.Item(i)) 'fonction coordonnes P1.GetCoordinates coordonnees 'get name + coordinate coordonnees2(Y, 0) = hbody.HybridShapes.Item(i).name coordonnees2(Y, 1) = Round(coordonnees(0), 3) coordonnees2(Y, 2) = Round(coordonnees(1), 3) coordonnees2(Y, 3) = Round(coordonnees(2), 3)` – tao Apr 23 '14 at 14:10
  • @tao So is your problem resolved? Can you please post that code above in your question so that it's formatted correctly...It's difficult to read. – GisMofx Apr 23 '14 at 14:18
  • Variant is a number object and P3 is a point object (can not be cast) In catia the VBA code upper is working – tao Apr 23 '14 at 14:18
  • Understood. Then, use a generic "Object" for the type of P3Obj. (In VB an object can also be a variant.) I've updated my code above – GisMofx Apr 23 '14 at 14:23
  • I think the problem is inside com4j to pass array to DLL in JAVA and get the array modified. Perhaps I need to use safearray but I don't known how to use properly with com4j – tao Apr 23 '14 at 14:37
  • @tao the API Documentation does specify a SafeArray type for parameter and that might be a good starting point. That said, I'm unfamiliar with com4j. Is there a specific reason you that you can't just use VB for this? – GisMofx Apr 23 '14 at 14:45
  • getCoordinates is an example to get data from CATIA. I need to use others functions to retreive data and drived by a java application (not in VB). I try many solutions with safearray but anyone works. Does someone use safearray with CATIA and COM4J ? – tao May 05 '14 at 12:29