CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   STAR-CCM+ (https://www.cfd-online.com/Forums/star-ccm/)
-   -   Run Java macros in Star Ccm+ (https://www.cfd-online.com/Forums/star-ccm/162144-run-java-macros-star-ccm.html)

peter parkour November 4, 2015 10:32

Run Java macros in Star Ccm+
 
Hi!
I recently began programming in Java in order to do some pre-process into Star Ccm+.

The problem I have is when a play in Star Ccm+ a java macro wich calls another macro, Star throws a Instantiation Exception, like if the class was abstract or empty or something.

This is a silly example of what I'm trying with two simple macros (the classpath is correct). The second macro "crearprueba.java" calls the first macro "prueba.java" and set its data member "a" as value for BaseSize.

First class:

public class prueba
{
int a;
public prueba (int b)
{
a=b;
}
}




Second class:

import java.util.*;
import star.resurfacer.*;
import star.common.*;
import star.base.neo.*;
import star.meshing.*;

public class crearprueba extends StarMacro
{
public prueba Pr;
public crearprueba (prueba p)
{
Pr=p;
}

int metodo()
{
return Pr.a;
}

public void execute()
{
prueba paul = new prueba (3);

crearprueba hewson = new crearprueba (paul);

Simulation simulation_0 = getActiveSimulation();

MeshContinuum meshContinuum_0 = simulation_0.getContinuumManager().createContinuum (MeshContinuum.class);

meshContinuum_0.enable(ResurfacerMeshingModel.clas s);

meshContinuum_0.getReferenceValues().get(BaseSize. class).setValue(hewson.metodo());
}
}

Either if I play "prueba.java" or "crearprueba.java" Star Ccm+ throws "java.lan.InstantiationException: prueba/crearprueba"

Any ideas?

Thanks for all the solutions you can provide.

olesen November 5, 2015 07:57

You have to consider that your crearpreuba class is derived from StarMacro, so you can't use your constructer, since this doesn't have any means of creating its super class (StarMacro).

Instead, just do it directly:
Code:


public class crearprueba extends StarMacro
{
    private Simulation sim_;
    private prueba Pr;

    public void execute()
    {
        sim_ = getActiveSimulationMethod();
        Pr = new prueba (3);
    }
}

To handle this type of situation, I actually find it easier just to pass through differently. For example, we'll call your main class MyMeshVals:
Code:


public class MyMeshVals
{
    private int val_;

    public void MyMeshVals(int val)
    {
        val_ = val;
    }
 
    public void setSomething(Simulation sim)
    {
        MeshContinuum meshCont = sim.getContinuumManager().createContinuum (MeshContinuum.class);

        meshCont.enable(ResurfacerMeshingModel.clas s);
        meshCont.getReferenceValues().get(BaseSize. class).setValue(val_);
    }
}

And then your caller looks a bit clearer:

Code:


public class crearprueba extends StarMacro
{
    public void execute()
    {
        Simulation sim = getActiveSimulationMethod();
        MyMeshVals paul = new MyMeshVals(3);
 
        paul.setSomething(sim);
    }
}


peter parkour November 6, 2015 02:48

Hey olesen!
Thanks for your reply and your advices.

I tried your code in Star Ccm+ but Star still does not rcognize "MyMeshVals".
Throws a "cannot find symbol (class MyMeshVals)" message and
I don't know why.

Is it maybe due to not having a .jar file made of the classes in the classpath of Star Ccm+?

The thing is I can't make a .jar file because I can't compile those classes since NetBeans doesn't recognize the lines such as "import star.common.*;"
"import star.base.neo.*;" neither the own methods of Star like " Simulation sim = getActiveSimulation"...


All times are GMT -4. The time now is 14:38.