CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   STAR-CCM+ (http://www.cfd-online.com/Forums/star-ccm/)
-   -   Loop parts with java (http://www.cfd-online.com/Forums/star-ccm/119587-loop-parts-java.html)

chrfri June 20, 2013 05:01

Loop parts with java
 
Hi
I would like to loop over all parts, and made a small test java:

for (PlaneSection p : simulation_0.getPartManager().getObject()) {
something
}

This method has worked before for other commands.
But I get an error saying that getObject cannot use ()

I find the java API difficult to understand and use. Can anyone help me solving the error?

ryancoe June 21, 2013 09:18

I think that a Collection may be helpful for this.

Code:

package macro;

import java.util.*;
import star.common.*;
import star.meshing.*;

public class partCollection extends StarMacro {

    public void execute() {
        Simulation sim = getActiveSimulation();
        Collection<GeometryPart> myParts = sim.get(SimulationPartManager.class).getLeafParts();
        for (Iterator<GeometryPart> it = myParts.iterator(); it.hasNext();) {
            GeometryPart iPart = it.next();
            sim.println(iPart.getPresentationName());
        }
    }
}

I find APIs challenging as well. My best strategy is to use an IDE (such as Netbeans or Eclipse) with auto-complete and experiment.

AdamS June 21, 2013 13:45

I think the reason you are seeing that specific error is that you are using getObject() instead of getObjects(). getObject requires that you pass in a string to retrieve a specific part; getObjects will return a Collection<Part> object of all parts.

You will also need to use an iterator like ryancoe posted above. So it will look something like this:

Code:

for (Iterator<Part> it = sim.getPartManager().getObjects().iterator(); it.hasNext();){
    PlaneSection p = (PlaneSection) it.next();
    //Do something with p
}

The final thing you will need to do is account for when the next part in the collection is not a PlaneSection. A try...catch statement wrapping the casting should take care of that.

chrfri June 24, 2013 08:03

Thanks
 
Hi
Thank you for the suggestions. I will try them out.

chrfri June 24, 2013 08:05

Thanks
 
Hi
Thank you for the suggestions. I will try them out and I definetely learned a few more tricks and commands. Great

Cheers
Christian


All times are GMT -4. The time now is 12:21.