]> gerrit.simantics Code Review - simantics/3d.git/blob - org.jcae.opencascade/src-java/org/jcae/opencascade/Utilities.java
Avoid extra write transactions when opening Plant3D editor
[simantics/3d.git] / org.jcae.opencascade / src-java / org / jcae / opencascade / Utilities.java
1 /*
2  * Project Info:  http://jcae.sourceforge.net
3  * 
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)
7  * any later version.
8  *
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
12  * details.
13  *
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.
17  *
18  * (C) Copyright 2008,2009, by EADS France
19  */
20 package org.jcae.opencascade;
21
22 import java.io.PrintStream;
23 import java.util.Arrays;
24 import org.jcae.opencascade.jni.*;
25
26 /**
27  * Useful toolbox.
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
30  * won't be warranted.
31  */
32 public class Utilities
33 {
34         private static final int TAB=2;
35         
36         /** Dump the topology of a shape (for debugging) */
37         public static void dumpTopology(TopoDS_Shape shape)
38         {
39                 dumpTopology(shape, System.out);
40         }
41         
42         /** Dump the topology of a shape (for debugging) */
43         public static void dumpTopology(TopoDS_Shape shape, PrintStream out)
44         {
45                 dumpTopology(shape, out, 0);
46         }
47
48         private static void dumpTopology(TopoDS_Shape shape, PrintStream out, int level)
49         {
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())
57                 {
58                         case FACE:
59                                 out.print(" "+BRep_Tool.tolerance((TopoDS_Face)shape));
60                                 break;
61                         case EDGE:
62                                 out.print(" "+BRep_Tool.tolerance((TopoDS_Edge)shape));
63                                 break;
64                         case VERTEX:
65                                 out.print(" "+BRep_Tool.tolerance((TopoDS_Vertex)shape));
66                                 break;
67                         default:
68                 }
69                 out.print(" "+shape.orientation());
70                 out.println();
71                 while(it.more())
72                 {
73                         dumpTopology(it.value(), out, level+1);
74                         it.next();
75                 }
76         }
77         
78         /** Return the number of shapes in one shape */
79         public static int numberOfShape(TopoDS_Shape shape, TopAbs_ShapeEnum type)
80         {
81                 int n=0;
82                 for(TopExp_Explorer exp=new TopExp_Explorer(shape, type); exp.more(); exp.next())
83                         n++;
84                 return n;
85         }
86         
87         /** Test if a shape is part of another one  */
88         public static boolean isShapeInShape(TopoDS_Shape parent, TopoDS_Shape child)
89         {
90                 for(TopExp_Explorer exp=new TopExp_Explorer(parent, child.shapeType()); exp.more(); exp.next())
91                 {
92                         if(exp.current().equals(child))
93                                 return true;
94                 }
95                 return false;
96         }
97         
98         /**
99          * Read a file guessing the format with the file extension
100          * Only .step, .igs and .brep are supported.
101          */
102         public static TopoDS_Shape readFile(String fileName)
103         {
104                 if (fileName.endsWith(".stp") || fileName.endsWith(".STP") ||
105                         fileName.endsWith(".step") || fileName.endsWith(".STEP"))
106                 {
107                         STEPControl_Reader aReader = new STEPControl_Reader();
108                         aReader.readFile(fileName.getBytes());
109                         aReader.nbRootsForTransfer();
110                         aReader.transferRoots();
111                         return aReader.oneShape();
112                 }
113
114                 if (fileName.endsWith(".igs") || fileName.endsWith(".IGS") ||
115                         fileName.endsWith(".iges") || fileName.endsWith(".IGES"))
116                 {
117                         IGESControl_Reader aReader = new IGESControl_Reader();
118                         aReader.readFile(fileName.getBytes());
119                         aReader.nbRootsForTransfer();
120                         aReader.transferRoots();
121                         return aReader.oneShape();
122                 }
123
124                 return BRepTools.read(fileName, new BRep_Builder());
125         }
126         
127         /**
128          * Return the face whose order is id in the given shape
129          */
130         public static TopoDS_Face getFace(TopoDS_Shape shape, int id)
131         {
132                 TopExp_Explorer exp=new TopExp_Explorer(shape, TopAbs_ShapeEnum.FACE);
133                 int i=0;
134                 while(exp.more())
135                 {
136                         if(id==i)
137                                 return (TopoDS_Face) exp.current();
138                         exp.next();
139                         i++;
140                 }
141                 throw new IndexOutOfBoundsException("Face "+id+" not found");
142         }
143
144         /**
145          * Return the vertex whose order is id in the given shape
146          */
147         public static TopoDS_Vertex getVertex(TopoDS_Shape shape, int id)
148         {
149                 TopExp_Explorer exp=new TopExp_Explorer(shape, TopAbs_ShapeEnum.VERTEX);
150                 int i=0;
151                 while(exp.more())
152                 {
153                         if(id==i)
154                                 return (TopoDS_Vertex) exp.current();
155                         exp.next();
156                         i++;
157                 }
158                 throw new IndexOutOfBoundsException("Vertex "+id+" not found");
159         }
160         
161         /**
162          * Compute the tolerance of shapes by selecting the highest
163          * tolerance of the give child shapes
164          */
165         public static double tolerance(TopoDS_Shape shape)
166         {
167                 double toReturn=0;
168                 if(shape instanceof TopoDS_Face)
169                 {
170                         toReturn=BRep_Tool.tolerance((TopoDS_Face)shape);
171                 }
172                 else if(shape instanceof TopoDS_Edge)
173                 {
174                         toReturn=BRep_Tool.tolerance((TopoDS_Edge)shape);
175                 }
176                 else if(shape instanceof TopoDS_Vertex)
177                         return BRep_Tool.tolerance((TopoDS_Vertex)shape);
178                 
179                 TopoDS_Iterator it=new TopoDS_Iterator(shape);
180                 while(it.more())
181                 {
182                         TopoDS_Shape s=it.value();
183                         double t=tolerance(s);
184                         if(t>toReturn)
185                                 toReturn=t;
186                         it.next();
187                 }
188                 return toReturn;
189         }
190 }