jCAE
 

Scripting with OccJava

Scripting with OccJava

Introduction

OccJava is a Java binding to Opencascade. Java libraries are scriptable throw many languages. It allow to make scripts with Opencascade. We will explain how to do it with the Groovy language but it's also possible with Jython (Java Python), Beanshell and propably many others which are listed here:

http://java-source.net/open-source/scripting-languages

Download groovy

The Groovy home page is http://groovy.codehaus.org/. Here is a shortcut to last (on 2007-12-07) version archives:

Just unzip the archives.

Environment and running

Here are examples on how running a Groovy/OccJava script. They need to be adapted to your configuration. You may find easier to set JAVA_HOME and CLASSPATH in your .bashrc file (Linux) or in the Environment Variable panel (Windows). You may also run the groovy command line from you own script (.sh or .bat).

On Linux

export JAVA_HOME=/a/path/jcae-0.15.0/jre-6
export CLASSPATH=/a/path/jcae-0.15.0/jcae/modules/ext/occjava.jar:$CLASSPATH
export LD_LIBRARY_PATH=/a/path/jcae-0.15.0/jcae/modules/lib/:$LD_LIBRARY_PATH
groovy test.groovy

On Windows

set JAVA_HOME=c:\jcae-0.15.0\jre-6
set CLASSPATH=c:\jcae-0.15.0\jcae\modules\ext\occjava.jar;%CLASSPATH%
set PATH=c:\jcae-0.15.0\jcae\modules\ext;%PATH%
groovy.bat test.groovy

Testing

Create a cone

//An example which create a cone
import org.jcae.opencascade.jni.*;
double[] axis=[0,0,0,1,1,1]
b=new BRepPrimAPI_MakeCone(axis, 0, 2, 3, 5)
BRepTools.write(b.shape(), "/tmp/cone.brep")

Create a square with an attached free edge

//Create a square with an attached free edge

import org.jcae.opencascade.jni.*;

// The plate
double[] p1=[0, 0, 0];
double[] p2=[0, 1, 0];
double[] p3=[1, 1, 0];
double[] p4=[1, 0, 0];
double[] p5=[0.5, 0.5, 0];
double[] p6=[0.5, 0.5, 1];

TopoDS_Edge edge1=new BRepBuilderAPI_MakeEdge(p1,p2).shape();
TopoDS_Edge edge2=new BRepBuilderAPI_MakeEdge(p2,p3).shape();
TopoDS_Edge edge3=new BRepBuilderAPI_MakeEdge(p3,p4).shape();
TopoDS_Edge edge4=new BRepBuilderAPI_MakeEdge(p4,p1).shape();

TopoDS_Wire wirePlate=
    new BRepBuilderAPI_MakeWire(edge1, edge2, edge3, edge4).shape();
    
TopoDS_Face face=new BRepBuilderAPI_MakeFace(wirePlate, true).shape();

// The wire
TopoDS_Vertex vertex1=new BRepBuilderAPI_MakeVertex(p5).shape();
TopoDS_Vertex vertex2=new BRepBuilderAPI_MakeVertex(p6).shape();
TopoDS_Edge freeEdge=new BRepBuilderAPI_MakeEdge(vertex1,vertex2).shape();
    
//Connect the wire to the plate
BRep_Builder bb=new BRep_Builder();
bb.add(face, vertex1);
    
//Put everything in a compound
TopoDS_Compound compound=new TopoDS_Compound();
bb.makeCompound(compound);
bb.add(compound, freeEdge);
bb.add(compound, face);

//Write to to a file
BRepTools.write(compound, "/tmp/plate.brep");

API Documentation

The list of available API is here. The semantic of each object and method is available in the Opencascade documentation.