1 package org.simantics.opencascade;
3 /*******************************************************************************
4 * Copyright (c) 2007, 2013- VTT Technical Research Centre of Finland.
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
11 * VTT Technical Research Centre of Finland - initial API and implementation
12 *******************************************************************************/
14 import javax.vecmath.Vector2d;
16 import org.jcae.opencascade.jni.BRepBuilderAPI_MakeEdge;
17 import org.jcae.opencascade.jni.BRepBuilderAPI_MakeFace;
18 import org.jcae.opencascade.jni.BRepBuilderAPI_MakeWire;
19 import org.jcae.opencascade.jni.BRepBuilderAPI_Transform;
20 import org.jcae.opencascade.jni.BRepPrimAPI_MakePrism;
21 import org.jcae.opencascade.jni.BRepPrimAPI_MakeTorus;
22 import org.jcae.opencascade.jni.BRep_Builder;
23 import org.jcae.opencascade.jni.GP_Elips;
24 import org.jcae.opencascade.jni.GP_Trsf;
25 import org.jcae.opencascade.jni.TopoDS_Edge;
26 import org.jcae.opencascade.jni.TopoDS_Face;
27 import org.jcae.opencascade.jni.TopoDS_Shape;
28 import org.jcae.opencascade.jni.TopoDS_Wire;
31 public class OccTriangulator {
34 public static final double MIN_VALUE = 0.001;
36 public OccTriangulator() {
41 public static TopoDS_Shape getShapeFromFile(String filename) {
42 assert (filename != null);
43 String lowerFileName = filename.toLowerCase();
44 if (lowerFileName.endsWith(".stp") || lowerFileName.endsWith(".step")) {
45 TopoDS_Shape shape = importSTEP(filename);
47 } else if (lowerFileName.endsWith(".iges")) {
48 TopoDS_Shape shape = importIGES(filename);
50 } else if (lowerFileName.endsWith(".brep")) {
51 TopoDS_Shape shape = importBREP(filename);
54 throw new UnsupportedOperationException("Unsupported format " + filename);
58 public static TopoDS_Shape importBREP(String filename) {
59 return importBREP(filename,1.0); // convert to meters.
61 public static TopoDS_Shape importBREP(String filename, double scale) {
62 org.jcae.opencascade.jni.BRep_Builder aBuilder = new org.jcae.opencascade.jni.BRep_Builder();
63 org.jcae.opencascade.jni.TopoDS_Shape myShape = org.jcae.opencascade.jni.BRepTools.read(filename, aBuilder);
65 if (Math.abs(scale-1.0) < 0.001)
67 TopoDS_Shape scaled = makeScale(myShape, scale);
72 public static TopoDS_Shape importIGES(String filename) {
73 org.jcae.opencascade.jni.IGESControl_Reader aReader = new org.jcae.opencascade.jni.IGESControl_Reader();
74 aReader.setReadUnitM();
75 aReader.readFile(filename.getBytes());
76 aReader.clearShapes();
77 aReader.transferRoots();
78 TopoDS_Shape result = aReader.oneShape();
83 public static TopoDS_Shape importSTEP(String filename) {
84 org.jcae.opencascade.jni.STEPControl_Reader aReader = new org.jcae.opencascade.jni.STEPControl_Reader();
85 aReader.setReadUnitM();
86 aReader.readFile(filename.getBytes());
87 aReader.clearShapes();
88 aReader.transferRoots();
89 TopoDS_Shape result = aReader.oneShape();
96 public static TopoDS_Shape makeTranslation(TopoDS_Shape aShape, double x, double y, double z) {
97 GP_Trsf theTransformation = new GP_Trsf();
98 theTransformation.setTranslation(new double[] { x, y, z });
99 BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);
101 TopoDS_Shape shape = bt.shape();
103 theTransformation.delete();
107 public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double r1, double r2) {
108 double[] axe = new double[6];
109 System.arraycopy(pointStruct, 0, axe, 0, 3);
110 System.arraycopy(dirStruct, 0, axe, 3, 3);
111 BRepPrimAPI_MakeTorus torus = new BRepPrimAPI_MakeTorus(axe, r1, r2);
112 org.jcae.opencascade.jni.TopoDS_Shape tds = torus.shape();
118 public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double[] dirStruct2, double r1, double r2) {
119 double[] axe = new double[9];
120 System.arraycopy(pointStruct, 0, axe, 0, 3);
121 System.arraycopy(dirStruct, 0, axe, 3, 3);
122 System.arraycopy(dirStruct2, 0, axe, 6, 3);
123 BRepPrimAPI_MakeTorus torus = new BRepPrimAPI_MakeTorus(axe, r1, r2);
124 org.jcae.opencascade.jni.TopoDS_Shape tds = torus.shape();
129 public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double r1, double r2, double angle1, double angle2, double angle) {
130 double[] axe = new double[6];
131 System.arraycopy(pointStruct, 0, axe, 0, 3);
132 System.arraycopy(dirStruct, 0, axe, 3, 3);
133 BRepPrimAPI_MakeTorus torus = new BRepPrimAPI_MakeTorus(axe, r1, r2,angle1,angle2,angle);
134 org.jcae.opencascade.jni.TopoDS_Shape tds = torus.shape();
139 public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double[] dirStruct2, double r1, double r2, double angle1, double angle2, double angle) {
140 double[] axe = new double[9];
141 System.arraycopy(pointStruct, 0, axe, 0, 3);
142 System.arraycopy(dirStruct, 0, axe, 3, 3);
143 System.arraycopy(dirStruct2, 0, axe, 6, 3);
144 BRepPrimAPI_MakeTorus torus = new BRepPrimAPI_MakeTorus(axe, r1, r2,angle1,angle2,angle);
145 org.jcae.opencascade.jni.TopoDS_Shape tds = torus.shape();
150 public static TopoDS_Shape makeSphere(double x, double y, double z, double radius) {
151 double[] c = new double[] { x, y, z };
152 org.jcae.opencascade.jni.BRepPrimAPI_MakeSphere sphere = new org.jcae.opencascade.jni.BRepPrimAPI_MakeSphere(c, radius);
153 org.jcae.opencascade.jni.TopoDS_Shape tds = sphere.shape();
159 public static TopoDS_Shape makeRotation(TopoDS_Shape aShape, double[] axisStruct, double angle) {
161 GP_Trsf theTransformation = new GP_Trsf();
162 theTransformation.setRotation(axisStruct, angle);
163 BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);
164 TopoDS_Shape shape = bt.shape();
166 theTransformation.delete();
170 public static TopoDS_Shape makeScale(TopoDS_Shape aShape, double s) {
172 GP_Trsf theTransformation = new GP_Trsf();
173 theTransformation.setValues(s, 0, 0, 0,
176 BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);
177 TopoDS_Shape shape = bt.shape();
179 theTransformation.delete();
184 public static TopoDS_Shape makeCylinder(double[] pointStruct, double[] dirStruct, double radius, double height) {
185 double[] axe = new double[6];
186 System.arraycopy(pointStruct, 0, axe, 0, 3);
187 System.arraycopy(dirStruct, 0, axe, 3, 3);
188 org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder cyl = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder(axe, radius, height, 2 * Math.PI);
189 org.jcae.opencascade.jni.TopoDS_Shape tds = cyl.shape();
194 public static TopoDS_Shape makeCopy(TopoDS_Shape topoDS_Shape) {
195 throw new UnsupportedOperationException();
198 public static TopoDS_Shape makeCone(double[] pointStruct, double[] dirStruct, double radius1, double radius2, double height) {
199 double[] axe = new double[6];
200 System.arraycopy(pointStruct, 0, axe, 0, 3);
201 System.arraycopy(dirStruct, 0, axe, 3, 3);
202 org.jcae.opencascade.jni.BRepPrimAPI_MakeCone cone = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCone(axe, radius1, radius2, height, 2 * Math.PI);
203 org.jcae.opencascade.jni.TopoDS_Shape tds = cone.shape();
208 public static TopoDS_Shape makeCompound(TopoDS_Shape[] shapes) {
209 BRep_Builder builder = new BRep_Builder();
210 org.jcae.opencascade.jni.TopoDS_Compound comp = new org.jcae.opencascade.jni.TopoDS_Compound();
211 builder.makeCompound(comp);
212 for (int i = 0; i < shapes.length; i++) {
214 builder.add(comp, shapes[i]);
221 public static TopoDS_Shape makeBox(double x1, double y1, double z1, double x2, double y2, double z2) {
222 double[] p1 = new double[] { x1, y1, z1 };
223 double[] p2 = new double[] { x2, y2, z2 };
224 org.jcae.opencascade.jni.BRepPrimAPI_MakeBox box = new org.jcae.opencascade.jni.BRepPrimAPI_MakeBox(p1, p2);
225 org.jcae.opencascade.jni.TopoDS_Shape tds = box.shape();
230 public static TopoDS_Shape makeCut(TopoDS_Shape shape1, TopoDS_Shape shape2) {
231 org.jcae.opencascade.jni.BRepAlgoAPI_Cut cut = new org.jcae.opencascade.jni.BRepAlgoAPI_Cut(shape1, shape2);
232 org.jcae.opencascade.jni.TopoDS_Shape s = cut.shape();
237 public static TopoDS_Shape makeCommon(TopoDS_Shape shape1, TopoDS_Shape shape2) {
238 org.jcae.opencascade.jni.BRepAlgoAPI_Common common = new org.jcae.opencascade.jni.BRepAlgoAPI_Common(shape1, shape2);
239 org.jcae.opencascade.jni.TopoDS_Shape s = common.shape();
244 public static TopoDS_Shape makeFuse(TopoDS_Shape shape1, TopoDS_Shape shape2) {
245 org.jcae.opencascade.jni.BRepAlgoAPI_Fuse fuse = new org.jcae.opencascade.jni.BRepAlgoAPI_Fuse(shape1, shape2);
246 org.jcae.opencascade.jni.TopoDS_Shape s = fuse.shape();
252 public static TopoDS_Shape makeWedge(double[] pointStruct, double[] dirStruct,double dx, double dy, double dz, double xmin, double zmin, double xmax, double zmax) {
253 double[] axe = new double[6];
254 System.arraycopy(pointStruct, 0, axe, 0, 3);
255 System.arraycopy(dirStruct, 0, axe, 3, 3);
257 org.jcae.opencascade.jni.BRepPrimAPI_MakeWedge wedge = new org.jcae.opencascade.jni.BRepPrimAPI_MakeWedge(axe, dx, dy, dz, xmin, zmin, xmax, zmax);
258 org.jcae.opencascade.jni.TopoDS_Shape s = wedge.shape();
263 public static TopoDS_Shape makeEllipticCylinder(double h, double r1, double r2) {
266 // FIXME : ellipse should be rotated, but current JNI won't allow it since Ax2 is not separate object
267 ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r2,r1);
269 ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r1,r2);
271 BRepBuilderAPI_MakeEdge edge = new BRepBuilderAPI_MakeEdge(ellipse);
272 TopoDS_Edge ed = (TopoDS_Edge) edge.shape();
273 BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire(ed);
274 TopoDS_Wire w = (TopoDS_Wire) wire.shape();
275 BRepBuilderAPI_MakeFace face = new BRepBuilderAPI_MakeFace(w);
276 TopoDS_Face F = (TopoDS_Face) face.shape();
277 BRepPrimAPI_MakePrism prism = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 });
278 TopoDS_Shape shape = prism.shape();
290 public static TopoDS_Shape makeReqularPrism(double h, double r, int n) {
293 Vector2d vertices[] = new Vector2d[n];
294 for (int i = 0; i < n; i++) {
295 vertices[i] = new Vector2d(Math.sin(Math.PI * 2.0 * i / n)*r,Math.cos(Math.PI * 2.0 * i / n)*r);
297 BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire();
298 for (int i = 0; i < n; i++) {
299 Vector2d v1 = vertices[i];
300 Vector2d v2 = vertices[(i+1)%n];
301 BRepBuilderAPI_MakeEdge edge = new BRepBuilderAPI_MakeEdge(new double[]{v1.x,-h*0.5,v1.y},new double[]{v2.x,-h*0.5,v2.y});
302 wire.add((TopoDS_Edge)edge.shape());
305 TopoDS_Wire w = (TopoDS_Wire)wire.shape();
307 BRepBuilderAPI_MakeFace face = new BRepBuilderAPI_MakeFace(w);
308 TopoDS_Face F = (TopoDS_Face) face.shape();
311 BRepPrimAPI_MakePrism prism = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 });
312 TopoDS_Shape shape = prism.shape();
322 public static void exportBREP(TopoDS_Shape shape, String filename) {
323 org.jcae.opencascade.jni.BRepTools.write(shape, filename);