2 * Project Info: http://jcae.sourceforge.net
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 * (C) Copyright 2008,2009, by EADS France
20 package org.jcae.opencascade;
22 import java.io.PrintStream;
23 import java.util.Arrays;
24 import org.jcae.opencascade.jni.*;
28 * Note that this methods are not Open CASCADE binding and should probably
29 * not be used outside of the jCAE project, as compatibility between versions
32 public class Utilities
34 private static final int TAB=2;
36 /** Dump the topology of a shape (for debugging) */
37 public static void dumpTopology(TopoDS_Shape shape)
39 dumpTopology(shape, System.out);
42 /** Dump the topology of a shape (for debugging) */
43 public static void dumpTopology(TopoDS_Shape shape, PrintStream out)
45 dumpTopology(shape, out, 0);
48 private static void dumpTopology(TopoDS_Shape shape, PrintStream out, int level)
50 TopoDS_Iterator it=new TopoDS_Iterator(shape);
51 char[] dots=new char[level*TAB];
52 Arrays.fill(dots, '-');
53 String dotss=new String(dots);
54 String label=shape.toString().substring("org.jcae.opencascade.jni.TopoDS_".length());
55 System.out.print("+"+dotss+label);
56 switch(shape.shapeType())
59 out.print(" "+BRep_Tool.tolerance((TopoDS_Face)shape));
62 out.print(" "+BRep_Tool.tolerance((TopoDS_Edge)shape));
65 out.print(" "+BRep_Tool.tolerance((TopoDS_Vertex)shape));
69 out.print(" "+shape.orientation());
73 dumpTopology(it.value(), out, level+1);
78 /** Return the number of shapes in one shape */
79 public static int numberOfShape(TopoDS_Shape shape, TopAbs_ShapeEnum type)
82 for(TopExp_Explorer exp=new TopExp_Explorer(shape, type); exp.more(); exp.next())
87 /** Test if a shape is part of another one */
88 public static boolean isShapeInShape(TopoDS_Shape parent, TopoDS_Shape child)
90 for(TopExp_Explorer exp=new TopExp_Explorer(parent, child.shapeType()); exp.more(); exp.next())
92 if(exp.current().equals(child))
99 * Read a file guessing the format with the file extension
100 * Only .step, .igs and .brep are supported.
102 public static TopoDS_Shape readFile(String fileName)
104 if (fileName.endsWith(".stp") || fileName.endsWith(".STP") ||
105 fileName.endsWith(".step") || fileName.endsWith(".STEP"))
107 STEPControl_Reader aReader = new STEPControl_Reader();
108 aReader.readFile(fileName.getBytes());
109 aReader.nbRootsForTransfer();
110 aReader.transferRoots();
111 return aReader.oneShape();
114 if (fileName.endsWith(".igs") || fileName.endsWith(".IGS") ||
115 fileName.endsWith(".iges") || fileName.endsWith(".IGES"))
117 IGESControl_Reader aReader = new IGESControl_Reader();
118 aReader.readFile(fileName.getBytes());
119 aReader.nbRootsForTransfer();
120 aReader.transferRoots();
121 return aReader.oneShape();
124 return BRepTools.read(fileName, new BRep_Builder());
128 * Return the face whose order is id in the given shape
130 public static TopoDS_Face getFace(TopoDS_Shape shape, int id)
132 TopExp_Explorer exp=new TopExp_Explorer(shape, TopAbs_ShapeEnum.FACE);
137 return (TopoDS_Face) exp.current();
141 throw new IndexOutOfBoundsException("Face "+id+" not found");
145 * Return the vertex whose order is id in the given shape
147 public static TopoDS_Vertex getVertex(TopoDS_Shape shape, int id)
149 TopExp_Explorer exp=new TopExp_Explorer(shape, TopAbs_ShapeEnum.VERTEX);
154 return (TopoDS_Vertex) exp.current();
158 throw new IndexOutOfBoundsException("Vertex "+id+" not found");
162 * Compute the tolerance of shapes by selecting the highest
163 * tolerance of the give child shapes
165 public static double tolerance(TopoDS_Shape shape)
168 if(shape instanceof TopoDS_Face)
170 toReturn=BRep_Tool.tolerance((TopoDS_Face)shape);
172 else if(shape instanceof TopoDS_Edge)
174 toReturn=BRep_Tool.tolerance((TopoDS_Edge)shape);
176 else if(shape instanceof TopoDS_Vertex)
177 return BRep_Tool.tolerance((TopoDS_Vertex)shape);
179 TopoDS_Iterator it=new TopoDS_Iterator(shape);
182 TopoDS_Shape s=it.value();
183 double t=tolerance(s);