+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.occ.geometry;\r
+\r
+import java.io.IOException;\r
+import java.util.Collection;\r
+\r
+import javax.vecmath.AxisAngle4d;\r
+import javax.vecmath.Tuple3d;\r
+import javax.vecmath.Vector2d;\r
+\r
+import org.jcae.opencascade.jni.BRepBuilderAPI_MakeEdge;\r
+import org.jcae.opencascade.jni.BRepBuilderAPI_MakeFace;\r
+import org.jcae.opencascade.jni.BRepBuilderAPI_MakeWire;\r
+import org.jcae.opencascade.jni.BRepBuilderAPI_Transform;\r
+import org.jcae.opencascade.jni.BRepOffsetAPI_ThruSections;\r
+import org.jcae.opencascade.jni.BRepPrimAPI_MakePrism;\r
+import org.jcae.opencascade.jni.BRepPrimAPI_MakeRevol;\r
+import org.jcae.opencascade.jni.BRepPrimAPI_MakeTorus;\r
+import org.jcae.opencascade.jni.BRep_Builder;\r
+import org.jcae.opencascade.jni.GC_MakeArcOfCircle;\r
+import org.jcae.opencascade.jni.GC_MakeSegment;\r
+import org.jcae.opencascade.jni.GP_Elips;\r
+import org.jcae.opencascade.jni.GP_Trsf;\r
+import org.jcae.opencascade.jni.TopoDS_Edge;\r
+import org.jcae.opencascade.jni.TopoDS_Face;\r
+import org.jcae.opencascade.jni.TopoDS_Shape;\r
+import org.jcae.opencascade.jni.TopoDS_Wire;\r
+import org.simantics.proconf.g3d.csg.stubs.Barrel;\r
+import org.simantics.proconf.g3d.csg.stubs.BooleanOperation;\r
+import org.simantics.proconf.g3d.csg.stubs.Box;\r
+import org.simantics.proconf.g3d.csg.stubs.CSGShape;\r
+import org.simantics.proconf.g3d.csg.stubs.Cone;\r
+import org.simantics.proconf.g3d.csg.stubs.Cylinder;\r
+import org.simantics.proconf.g3d.csg.stubs.EllipticCylinder;\r
+import org.simantics.proconf.g3d.csg.stubs.RectangularSolid;\r
+import org.simantics.proconf.g3d.csg.stubs.RegularPrism;\r
+import org.simantics.proconf.g3d.csg.stubs.Sphere;\r
+import org.simantics.proconf.g3d.csg.stubs.Torus;\r
+import org.simantics.layer0.utils.IEntity;\r
+import org.simantics.proconf.g3d.base.G3DTools;\r
+import org.simantics.proconf.g3d.base.GeometryProvider;\r
+import org.simantics.proconf.g3d.occ.OccResources;\r
+import org.simantics.utils.ErrorLogger;\r
+\r
+import com.jme.eclipse.test.ply.PLY_Loader;\r
+import com.jme.scene.Geometry;\r
+import com.jme.scene.Line;\r
+import com.jme.scene.TriMesh;\r
+import com.jme.util.geom.BufferUtils;\r
+\r
+public class OccTriangulator implements GeometryProvider{\r
+\r
+ \r
+ public static final double MIN_VALUE = 0.001;\r
+ \r
+ public OccTriangulator() {\r
+ \r
+ }\r
+ \r
+ \r
+ \r
+ public boolean canHandle(IEntity instance) {\r
+ if (instance.isInstanceOf(OccResources.csgResource.CSGShape)) {\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ public Geometry[] getGeometryFromResource(IEntity resource, boolean transform) {\r
+ if (resource.isInstanceOf(OccResources.csgResource.CSGShape)) {\r
+ TopoDS_Shape shape = OccTriangulator.getShapeFromResource(resource, transform);\r
+ Geometry[] g = OccTriangulator.getGeometry(shape);\r
+ shape.delete();\r
+ return g;\r
+ } \r
+ return null;\r
+ }\r
+ \r
+ public boolean reconstructGeometry(IEntity instance, boolean transform, Geometry[] geometry) {\r
+ if (instance.isInstanceOf(OccResources.csgResource.CSGShape)) {\r
+ TopoDS_Shape shape = OccTriangulator.getShapeFromResource(instance, transform);\r
+ boolean b = OccTriangulator.getGeometry(shape,geometry);\r
+ shape.delete();\r
+ return b;\r
+ } \r
+ return false;\r
+ \r
+ }\r
+\r
+ public static Geometry[] getGeometry(TopoDS_Shape tds_shape) {\r
+ Geometry g[] = new Geometry[]{new TriMesh(),new Line()};\r
+ if (getGeometry(tds_shape, g))\r
+ return g;\r
+ return null;\r
+ \r
+ }\r
+ \r
+ public static boolean getGeometry(TopoDS_Shape tds_shape, Geometry[] geometry) {\r
+ ViewableShapeImpl shape = new ViewableShapeImpl(tds_shape);\r
+ //System.out.println("Geometries in shape " + shape.numGeometries());\r
+ if (shape.numGeometries() > 0) {\r
+ int totalIndicesCount = 0;\r
+ int totalVerticesCount = 0;\r
+ boolean hasTCoords = true;\r
+ for (int geometryIndex = 0; geometryIndex < shape.numGeometries(); geometryIndex++) {\r
+ IndexedGeometry geom = shape.getGeometry(geometryIndex);\r
+ totalIndicesCount += geom.getIndices().length;\r
+ totalVerticesCount += (geom.getCoordinates().length / 3);\r
+ if (geom.getTCoordinates() == null || geom.getTCoordinates().length == 0)\r
+ hasTCoords = false;\r
+ }\r
+ int currentVertex = 0;\r
+ int currentIndex = 0;\r
+ int indices[] = new int[totalIndicesCount];\r
+ \r
+ \r
+ \r
+ //TriMesh mesh = new TriMesh();\r
+ TriMesh mesh = (TriMesh)geometry[0];\r
+ float data[] = new float[totalVerticesCount*3];\r
+ float normals[] = new float[totalVerticesCount*3];\r
+ float tcoords[] = null;\r
+ if (hasTCoords)\r
+ tcoords = new float[totalVerticesCount*2];\r
+ for (int geometryIndex = 0; geometryIndex < shape.numGeometries(); geometryIndex++) {\r
+ IndexedGeometry geom = shape.getGeometry(geometryIndex); \r
+ System.arraycopy(geom.getCoordinates(), 0, data, currentVertex*3, geom.getCoordinates().length);\r
+ System.arraycopy(geom.getNormals(), 0, normals, currentVertex*3, geom.getCoordinates().length);\r
+ if (hasTCoords)\r
+ System.arraycopy(geom.getTCoordinates(), 0, tcoords, currentVertex*2, geom.getTCoordinates().length - 1);\r
+ \r
+ for (int i = 0; i < geom.getIndices().length; i++) {\r
+ indices[currentIndex + i] = geom.getIndices()[i] + currentVertex;\r
+ }\r
+ \r
+ currentVertex += geom.getCoordinates().length/3;\r
+ currentIndex += geom.getIndices().length;\r
+ \r
+ }\r
+ mesh.reconstruct(BufferUtils.createFloatBuffer(data),BufferUtils.createFloatBuffer(normals) , null, hasTCoords ? BufferUtils.createFloatBuffer(tcoords) : null, BufferUtils.createIntBuffer(indices));\r
+ \r
+ int numEdgeVertices = 0;\r
+ int currentEdgeVertex = 0;\r
+ for (int i = 0; i < shape.getNumEdges(); i++) {\r
+ numEdgeVertices += shape.getEdge(i).length/3;\r
+ }\r
+\r
+ Line lines = (Line)geometry[1];\r
+ data = new float[numEdgeVertices*3];\r
+ for (int i = 0; i < shape.getNumEdges(); i++) {\r
+ int index = currentEdgeVertex *3;\r
+ System.arraycopy(shape.getEdge(i), 0,data, index, shape.getEdge(i).length);\r
+ currentEdgeVertex+= shape.getEdge(i).length/3;\r
+ }\r
+ lines.reconstruct(BufferUtils.createFloatBuffer(data), null, null, null);\r
+ \r
+ tds_shape.delete();\r
+ return true;\r
+ } else {\r
+ tds_shape.delete();\r
+ return false;\r
+ }\r
+ }\r
+ /*\r
+ public static Geometry[] getGeometry(TopoDS_Shape tds_shape) {\r
+ return getGeometry(tds_shape,true);\r
+ }\r
+ */\r
+ /*\r
+ public static boolean getGeometry(TopoDS_Shape tds_shape, Geometry[] geometry) {\r
+ return getGeometry(tds_shape,true, geometry);\r
+ }\r
+ */\r
+ \r
+ private static TopoDS_Shape getPrimitiveFromResource(IEntity thing) {\r
+ if (!thing.isInstanceOf(OccResources.csgResource.Primitive)) \r
+ throw new IllegalArgumentException("Resource is not a primitive");\r
+ TopoDS_Shape shape = null;\r
+ if (thing.isInstanceOf(OccResources.csgResource.Box)) {\r
+ Box box = new Box(thing);\r
+ double sx = box.getXAxisSize()[0];\r
+ double sy = box.getYAxisSize()[0];\r
+ double sz = box.getZAxisSize()[0];\r
+ if (sx <= MIN_VALUE)\r
+ sx = MIN_VALUE;\r
+ if (sy <= MIN_VALUE)\r
+ sy = MIN_VALUE;\r
+ if (sz <= MIN_VALUE)\r
+ sz = MIN_VALUE;\r
+ \r
+ shape = makeBox(-sx * 0.5, -sy * 0.5, -sz * 0.5, sx * 0.5, sy * 0.5, sz * 0.5);\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Cone)) {\r
+ Cone cone = new Cone(thing);\r
+ double h = cone.getHeight()[0];\r
+ double r1 = cone.getBottomRadius()[0];\r
+ double r2 = cone.getTopRadius()[0];\r
+ if (Math.abs(r1 - r2) > MIN_VALUE) { // OpenCASCADE won't work,\r
+ // if r1 == r2\r
+ shape = makeCone(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r1,\r
+ r2, h);\r
+ } else {\r
+ shape = makeCylinder(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 },\r
+ r1, h);\r
+ }\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Sphere)) {\r
+ Sphere sphere = new Sphere(thing);\r
+ double r = sphere.getRadius()[0];\r
+ if (r <= MIN_VALUE)\r
+ r = MIN_VALUE;\r
+ shape = makeSphere(0.0, 0.0, 0.0, r);\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Torus)) {\r
+ Torus torus = new Torus(thing);\r
+ double r1 = torus.getMajorRadius()[0];\r
+ double r2 = torus.getMinorRadius()[0];\r
+ if (r1 <= MIN_VALUE)\r
+ r1 = MIN_VALUE;\r
+ if (r2 <= MIN_VALUE)\r
+ r2 = MIN_VALUE;\r
+ shape = makeTorus(new double[] { 0.0, 0.0, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r1, r2);\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Cylinder)) {\r
+ Cylinder cylinder = new Cylinder(thing);\r
+ double h = cylinder.getHeight()[0];\r
+ double r = cylinder.getRadius()[0];\r
+ if (r <= MIN_VALUE)\r
+ r = MIN_VALUE;\r
+ if (h <= MIN_VALUE)\r
+ h = MIN_VALUE;\r
+ shape = makeCylinder(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r, h);\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Barrel)) {\r
+ Barrel barrel = new Barrel(thing);\r
+ double h = barrel.getHeight()[0];\r
+ if (h <= 0.0)\r
+ h = 0.01;\r
+ double r1 = barrel.getMinorRadius()[0];\r
+ if (r1 <= MIN_VALUE)\r
+ r1 = MIN_VALUE;\r
+ double r2 = barrel.getMajorRadius()[0];\r
+ if (r2 <= MIN_VALUE)\r
+ r2 = MIN_VALUE;\r
+ if (Math.abs(r1 -r2)< MIN_VALUE)\r
+ r2 = r1 + MIN_VALUE;\r
+ double p0[] = new double[]{0.0,-h*0.5,0.0};\r
+ double p1[] = new double[]{0.0,-h*0.5,r1};\r
+ double p2[] = new double[]{0.0, 0.0 ,r2};\r
+ double p3[] = new double[]{0.0, h*0.5,r1};\r
+ double p4[] = new double[]{0.0, h*0.5,0.0};\r
+ GC_MakeArcOfCircle m = new GC_MakeArcOfCircle(p1,p2,p3);\r
+ GC_MakeSegment s1 = new GC_MakeSegment(p0,p1);\r
+ GC_MakeSegment s2 = new GC_MakeSegment(p3,p4);\r
+ TopoDS_Edge e1 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(s1.value()).shape();\r
+ TopoDS_Edge e2 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(m.value()).shape();\r
+ TopoDS_Edge e3 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(s2.value()).shape();\r
+ TopoDS_Wire w = (TopoDS_Wire) new BRepBuilderAPI_MakeWire(e1,e2,e3).shape();\r
+ TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape();\r
+ shape = new BRepPrimAPI_MakeRevol(F,new double[]{0.0,0.0,0.0,0.0,1.0,0.0}).shape();\r
+ m.delete();\r
+ s1.delete();\r
+ s2.delete();\r
+ e1.delete();\r
+ e2.delete();\r
+ e3.delete();\r
+ w.delete();\r
+ F.delete();\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.EllipticCylinder)) {\r
+ EllipticCylinder cylinder = new EllipticCylinder(thing);\r
+ double h = cylinder.getHeight()[0];\r
+ if (h < MIN_VALUE)\r
+ h = MIN_VALUE;\r
+ double r2 = cylinder.getMinorRadius()[0];\r
+ if (r2 < MIN_VALUE)\r
+ r2 = MIN_VALUE;\r
+ double r1 = cylinder.getMajorRadius()[0];\r
+ if (r1 < MIN_VALUE)\r
+ r1 = MIN_VALUE;\r
+ GP_Elips ellipse;\r
+ if (r1 < r2) {\r
+ // FIXME : ellipse should be rotated, but current JNI won't allow it since Ax2 is not separate object\r
+ ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r2,r1);\r
+ } else {\r
+ ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r1,r2);\r
+ }\r
+ TopoDS_Edge ed = (TopoDS_Edge) new BRepBuilderAPI_MakeEdge(ellipse).shape();\r
+ TopoDS_Wire w = (TopoDS_Wire) new BRepBuilderAPI_MakeWire(ed).shape();\r
+ TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape();\r
+ shape = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 }).shape();\r
+ ellipse.delete();\r
+ ed.delete();\r
+ w.delete();\r
+ F.delete();\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.RegularPrism)) {\r
+ RegularPrism prism = new RegularPrism(thing);\r
+ int n = prism.getCorners()[0];\r
+ if (n < 3)\r
+ n = 3;\r
+ double h = prism.getHeight()[0];\r
+ if (h < MIN_VALUE)\r
+ h = MIN_VALUE;\r
+ double r = prism.getRadius()[0];\r
+ if (r < MIN_VALUE)\r
+ r = MIN_VALUE;\r
+ Vector2d vertices[] = new Vector2d[n];\r
+ for (int i = 0; i < n; i++) {\r
+ vertices[i] = new Vector2d(Math.sin(Math.PI * 2.0 * i / n)*r,Math.cos(Math.PI * 2.0 * i / n)*r);\r
+ }\r
+ BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire();\r
+ for (int i = 0; i < n; i++) {\r
+ Vector2d v1 = vertices[i];\r
+ Vector2d v2 = vertices[(i+1)%n];\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{v1.x,-h*0.5,v1.y},new double[]{v2.x,-h*0.5,v2.y}).shape());\r
+ }\r
+ TopoDS_Wire w = (TopoDS_Wire)wire.shape();\r
+ TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape();\r
+ shape = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 }).shape(); \r
+ wire.delete();\r
+ w.delete();\r
+ F.delete();\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.RectangularSolid)) {\r
+ RectangularSolid solid = new RectangularSolid(thing);\r
+ double x1 = solid.getXAxisMinimumSize()[0];\r
+ double x2 = solid.getXAxisMaximumSize()[0];\r
+ double y = solid.getYAxisSize()[0];\r
+ double z1 = solid.getZAxisMinimumSize()[0];\r
+ double z2 = solid.getZAxisMaximumSize()[0];\r
+ if (x1 < MIN_VALUE)\r
+ x1 = MIN_VALUE;\r
+ if (x2 < MIN_VALUE)\r
+ x2 = MIN_VALUE;\r
+ if (y < MIN_VALUE)\r
+ y = MIN_VALUE;\r
+ if (z1 < MIN_VALUE)\r
+ z1 = MIN_VALUE;\r
+ if (z2 < MIN_VALUE)\r
+ z2 = MIN_VALUE;\r
+ x1 *= 0.5;\r
+ x2 *= 0.5;\r
+ y *= 0.5;\r
+ z1 *= 0.5;\r
+ z2 *= 0.5;\r
+ \r
+ BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire();\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x1,-y,z1},new double[]{x1,-y,-z1}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x1,-y,-z1},new double[]{-x1,-y,-z1}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x1,-y,-z1},new double[]{-x1,-y,z1}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x1,-y,z1},new double[]{x1,-y,z1}).shape());\r
+ TopoDS_Wire w1 = (TopoDS_Wire)wire.shape();\r
+ wire.delete();\r
+ wire = new BRepBuilderAPI_MakeWire();\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x2, y,z2},new double[]{x2, y,-z2}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x2, y,-z2},new double[]{-x2, y,-z2}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x2, y,-z2},new double[]{-x2, y,z2}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x2, y,z2},new double[]{x2, y,z2}).shape());\r
+ TopoDS_Wire w2 = (TopoDS_Wire)wire.shape();\r
+ BRepOffsetAPI_ThruSections generatorb = new BRepOffsetAPI_ThruSections(true, true);\r
+ generatorb.addWire(w1);\r
+ generatorb.addWire(w2);\r
+ generatorb.build();\r
+ shape = generatorb.shape();\r
+ wire.delete();\r
+ w1.delete();\r
+ w2.delete();\r
+ } else {\r
+ throw new UnsupportedOperationException("Unsupported primitive");\r
+\r
+ }\r
+ return shape;\r
+ }\r
+ \r
+ private static TopoDS_Shape getBooleanOp(IEntity thing, TopoDS_Shape topoDSshape1, TopoDS_Shape topoDSshape2) {\r
+ if (!thing.isInstanceOf(OccResources.csgResource.BooleanOperation))\r
+ throw new IllegalArgumentException("Resource is not a boolean operation");\r
+ TopoDS_Shape shape = null;\r
+ int type;\r
+ if (thing.isInstanceOf(OccResources.csgResource.Difference)) {\r
+ type = 0;\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Union)) {\r
+ type = 1;\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Intersection)) {\r
+ type = 2;\r
+ } else {\r
+ throw new UnsupportedOperationException("Unsupported boolean operation");\r
+ }\r
+ \r
+ switch (type) {\r
+ case 0:\r
+ shape = makeCut(topoDSshape1, topoDSshape2);\r
+ break;\r
+ case 1:\r
+ shape = makeFuse(topoDSshape1, topoDSshape2);\r
+ break;\r
+ case 2:\r
+ shape = makeCommon(topoDSshape1, topoDSshape2);\r
+ break;\r
+ }\r
+ topoDSshape1.delete();\r
+ topoDSshape2.delete();\r
+ return shape;\r
+ }\r
+ \r
+ private static TopoDS_Shape getBooleanOpFromResource(IEntity thing) {\r
+ if (!thing.isInstanceOf(OccResources.csgResource.BooleanOperation))\r
+ throw new IllegalArgumentException("Resource is not a boolean operation");\r
+ \r
+ TopoDS_Shape shape = null;\r
+ TopoDS_Shape topoDSshape1 = null;\r
+ TopoDS_Shape topoDSshape2 = null;\r
+ BooleanOperation op = new BooleanOperation(thing);\r
+ CSGShape mainShape = op.getMainShape();\r
+ Collection<CSGShape> secondaryShapes = op.getSecondaryShape();\r
+ if (mainShape == null || secondaryShapes.size() < 1) {\r
+ throw new RuntimeException("Cannot find requested Shapes for boolean operation");\r
+ }\r
+ topoDSshape1 = getShapeFromResource(mainShape);\r
+ for (CSGShape shape2 : secondaryShapes) {\r
+ topoDSshape2 = getShapeFromResource(shape2);\r
+ shape = getBooleanOp(thing,topoDSshape1,topoDSshape2);\r
+ topoDSshape1.delete();\r
+ topoDSshape1 = shape;\r
+ }\r
+ \r
+ topoDSshape2.delete();\r
+ return shape;\r
+ }\r
+\r
+ private static TopoDS_Shape getShapeFromResource(IEntity thing) {\r
+ return getShapeFromResource(thing, true);\r
+ }\r
+ \r
+ private static TopoDS_Shape getShapeFromResource(IEntity thing, boolean transform) {\r
+ if (thing.isInstanceOf(OccResources.csgResource.CSGShape)) {\r
+ TopoDS_Shape shape = null;\r
+ \r
+ if (thing.isInstanceOf(OccResources.csgResource.Primitive)) {\r
+ shape = getPrimitiveFromResource(thing);\r
+\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.BooleanOperation)) {\r
+ shape = getBooleanOpFromResource(thing);\r
+\r
+ } else {\r
+ throw new UnsupportedOperationException("Shape must be a primitive or a boolean operation");\r
+ }\r
+ Tuple3d c = null;\r
+ CSGShape shapeType = new CSGShape(thing);\r
+ \r
+ if (shapeType.getCenter() != null)\r
+ c = G3DTools.getVector(shapeType.getCenter());\r
+ \r
+ TopoDS_Shape tShape = null;\r
+ if(c != null) {\r
+ tShape = makeTranslation(shape, c.x, c.y, c.z);\r
+ shape.delete();\r
+ shape = tShape;\r
+ }\r
+ if (transform) {\r
+ //CSGShape shapeType = CSGShapeFactory.create(resource);\r
+ Tuple3d p = G3DTools.getVector(shapeType.getLocalPosition());\r
+ AxisAngle4d r = G3DTools.getOrientation(shapeType.getLocalOrientation());\r
+ \r
+ if (Math.abs(r.angle) > 0.01) {\r
+ tShape = makeRotation(shape, new double[] { 0.0, 0.0, 0.0, r.x, r.y, r.z }, r.angle);\r
+ shape.delete();\r
+ shape = tShape;\r
+ }\r
+ tShape = makeTranslation(shape, p.x, p.y, p.z);\r
+ shape.delete();\r
+ shape = tShape;\r
+ }\r
+\r
+ return shape;\r
+\r
+ }\r
+ throw new UnsupportedOperationException("Resource is not a shape");\r
+ }\r
+\r
+ private static TopoDS_Shape getShapeFromFile(String filename) {\r
+ assert (filename != null);\r
+ if (filename.endsWith("stp")) {\r
+ TopoDS_Shape shape = importSTEP(filename);\r
+ return shape;\r
+ } else if (filename.endsWith("step")) {\r
+ TopoDS_Shape shape = importSTEP(filename);\r
+ return shape;\r
+ } else if (filename.endsWith("iges")) {\r
+ TopoDS_Shape shape = importIGES(filename);\r
+ return shape;\r
+ } else if (filename.endsWith("brep")) {\r
+ TopoDS_Shape shape = importBREP(filename);\r
+ return shape;\r
+ } else {\r
+ throw new UnsupportedOperationException("Unsupported format " + filename);\r
+ }\r
+\r
+ }\r
+\r
+ \r
+\r
+ public static Geometry[] getGeometryFromFile(String filename) {\r
+ if (filename.endsWith("ply")) {\r
+ try {\r
+ return new Geometry[]{PLY_Loader.loadPLY(filename)};\r
+ } catch (IOException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ return null;\r
+ }\r
+ }\r
+ TopoDS_Shape shape = OccTriangulator.getShapeFromFile(filename);\r
+ Geometry g[] = OccTriangulator.getGeometry(shape);\r
+ shape.delete();\r
+ return g;\r
+ }\r
+ \r
+ public static TopoDS_Shape importBREP(String filename) {\r
+ org.jcae.opencascade.jni.BRep_Builder aBuilder = new org.jcae.opencascade.jni.BRep_Builder();\r
+ org.jcae.opencascade.jni.TopoDS_Shape myShape = org.jcae.opencascade.jni.BRepTools.read(filename, aBuilder);\r
+ aBuilder.delete();\r
+ return myShape;\r
+ }\r
+\r
+ public static TopoDS_Shape importIGES(String filename) {\r
+ org.jcae.opencascade.jni.IGESControl_Reader aReader = new org.jcae.opencascade.jni.IGESControl_Reader();\r
+ aReader.readFile(filename);\r
+ aReader.clearShapes();\r
+ aReader.transferRoots();\r
+ TopoDS_Shape result = aReader.oneShape();\r
+ aReader.delete();\r
+ return result;\r
+ }\r
+ \r
+ public static TopoDS_Shape importSTEP(String filename) {\r
+ org.jcae.opencascade.jni.STEPControl_Reader aReader = new org.jcae.opencascade.jni.STEPControl_Reader();\r
+ aReader.readFile(filename);\r
+ aReader.clearShapes();\r
+ aReader.transferRoots();\r
+ TopoDS_Shape result = aReader.oneShape();\r
+ aReader.delete();\r
+ return result;\r
+ }\r
+\r
+ public static TopoDS_Shape archimede(TopoDS_Shape topoDS_Shape, double param, double param2, double param3) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape suppressHolesInFaceOrShell(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape suppressHole(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1,\r
+ TopoDS_Shape[] topoDS_Shape2, TopoDS_Shape[] topoDS_Shape3) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape[] suppressFaces(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape[] subShapeAll(TopoDS_Shape topoDS_Shape, short param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape subShape(TopoDS_Shape topoDS_Shape, short param, TopoDS_Shape[] topoDS_Shape2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static boolean setBlock(TopoDS_Shape topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape partitionKeepFaces(TopoDS_Shape[] topoDS_Shape, TopoDS_Shape topoDS_Shape1, boolean param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape orientationChange(TopoDS_Shape topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static short nbLabels() {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeWire(TopoDS_Shape[] topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeVertex(double param, double param1, double param2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeVector(double[] pointStruct, double[] pointStruct1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeTranslation(TopoDS_Shape aShape, double x, double y, double z) {\r
+ GP_Trsf theTransformation = new GP_Trsf();\r
+ theTransformation.setTranslation(new double[] { x, y, z });\r
+ BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);\r
+\r
+ TopoDS_Shape shape = bt.shape();\r
+ bt.delete();\r
+ theTransformation.delete();\r
+ return shape;\r
+ }\r
+\r
+ public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double r1, double r2) {\r
+ double[] axe = new double[6];\r
+ System.arraycopy(pointStruct, 0, axe, 0, 3);\r
+ System.arraycopy(dirStruct, 0, axe, 3, 3);\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new BRepPrimAPI_MakeTorus(axe, r1, r2).shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeSphere(double x, double y, double z, double radius) {\r
+ double[] c = new double[] { x, y, z };\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeSphere(c, radius)\r
+ .shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeSewingShape(TopoDS_Shape topoDS_Shape, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeSewing(TopoDS_Shape[] topoDS_Shape, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeScaleTransform(TopoDS_Shape topoDS_Shape, double[] pointStruct, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeRotation(TopoDS_Shape aShape, double[] axisStruct, double angle) {\r
+\r
+ GP_Trsf theTransformation = new GP_Trsf();\r
+ theTransformation.setRotation(axisStruct, angle);\r
+ BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);\r
+ TopoDS_Shape shape = bt.shape();\r
+ bt.delete();\r
+ theTransformation.delete();\r
+ return shape;\r
+ }\r
+\r
+ public static TopoDS_Shape makeRevolution(TopoDS_Shape topoDS_Shape, double[] axisStruct, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePrism(TopoDS_Shape topoDS_Shape, double[] pointStruct, double[] pointStruct2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePlane(double[] pointStruct, double[] dirStruct, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePlacedBox(double param, double param1, double param2, double param3, double param4,\r
+ double param5) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePipe(TopoDS_Shape topoDS_Shape, TopoDS_Shape topoDS_Shape1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePanel(TopoDS_Shape topoDS_Shape, short param, double param2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMultiTranslation2D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double param,\r
+ short param3, double[] dirStruct4, double param5, short param6) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMultiTranslation1D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double param, short param3) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMultiRotation2D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double[] pointStruct,\r
+ double param, short param4, double param5, short param6) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMultiRotation1D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double[] pointStruct,\r
+ short param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMirrorByPlane(TopoDS_Shape topoDS_Shape, TopoDS_Shape topoDS_Shape1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeLine(double[] pointStruct, double[] dirStruct) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeFilling(TopoDS_Shape topoDS_Shape, short param, short param2, double param3, double param4,\r
+ short param5) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeFillet(TopoDS_Shape topoDS_Shape, double param, short param2, TopoDS_Shape[] topoDS_Shape3) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeFace(TopoDS_Shape topoDS_Shape, boolean param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeEdge(double[] pointStruct, double[] pointStruct1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeCylinder(double[] pointStruct, double[] dirStruct, double radius, double height) {\r
+ double[] axe = new double[6];\r
+ System.arraycopy(pointStruct, 0, axe, 0, 3);\r
+ System.arraycopy(dirStruct, 0, axe, 3, 3);\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder(axe, radius,\r
+ height, 2 * Math.PI).shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeCopy(TopoDS_Shape topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeCone(double[] pointStruct, double[] dirStruct, double radius1, double radius2, double height) {\r
+ double[] axe = new double[6];\r
+ System.arraycopy(pointStruct, 0, axe, 0, 3);\r
+ System.arraycopy(dirStruct, 0, axe, 3, 3);\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCone(axe, radius1,\r
+ radius2, height, 2 * Math.PI).shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeCompound(TopoDS_Shape[] shapes) {\r
+ BRep_Builder builder = new BRep_Builder();\r
+ org.jcae.opencascade.jni.TopoDS_Compound comp = new org.jcae.opencascade.jni.TopoDS_Compound();\r
+ builder.makeCompound(comp);\r
+ for (int i = 0; i < shapes.length; i++) {\r
+\r
+ builder.add(comp, shapes[i]);\r
+ }\r
+ builder.delete();\r
+ return comp;\r
+ }\r
+\r
+ public static TopoDS_Shape makeCircle(double[] pointStruct, double[] dirStruct, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeChamfer(TopoDS_Shape topoDS_Shape, double param, double param2, short param3,\r
+ TopoDS_Shape[] topoDS_Shape4) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeCDG(TopoDS_Shape topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeBox(double x1, double y1, double z1, double x2, double y2, double z2) {\r
+ double[] p1 = new double[] { x1, y1, z1 };\r
+ double[] p2 = new double[] { x2, y2, z2 };\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeBox(p1, p2).shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeCut(TopoDS_Shape shape1, TopoDS_Shape shape2) {\r
+\r
+ org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Cut(shape1, shape2).shape();\r
+ return s;\r
+ }\r
+ \r
+ public static TopoDS_Shape makeCommon(TopoDS_Shape shape1, TopoDS_Shape shape2) {\r
+\r
+ org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Common(shape1, shape2).shape();\r
+ return s;\r
+ }\r
+ \r
+ public static TopoDS_Shape makeFuse(TopoDS_Shape shape1, TopoDS_Shape shape2) {\r
+\r
+ org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Fuse(shape1, shape2).shape();\r
+ return s;\r
+ }\r
+\r
+ public static TopoDS_Shape makeArc(double[] pointStruct, double[] pointStruct1, double[] pointStruct2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ \r
+ public static TopoDS_Shape makeWedge(double[] pointStruct, double[] dirStruct,double dx, double dy, double dz, double xmin, double zmin, double xmax, double zmax) {\r
+ double[] axe = new double[6];\r
+ System.arraycopy(pointStruct, 0, axe, 0, 3);\r
+ System.arraycopy(dirStruct, 0, axe, 3, 3);\r
+ org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepPrimAPI_MakeWedge(axe, dx, dy, dz, xmin, zmin, xmax, zmax).shape();\r
+ return s;\r
+ }\r
+\r
+ \r
+\r
+ public static void exportBREP(TopoDS_Shape shape, String filename) {\r
+ org.jcae.opencascade.jni.BRepTools.write(shape, filename);\r
+ }\r
+\r
+ public static void exportIGES(TopoDS_Shape shape, String filename) {\r
+ throw new UnsupportedOperationException();\r
+ \r
+// IGESControl_Writer writer=new IGESControl_Writer();\r
+// writer.addShape(shape); writer.write(filename,false);\r
+ \r
+ }\r
+\r
+\r
+\r
+\r
+ \r
+}\r