]> gerrit.simantics Code Review - simantics/3d.git/blob - org.jcae.opencascade/src-java-test/org/jcae/opencascade/jni/CircleExtrudeQuilt.java
Include old 64-bit versions of org.jcae.opencascade and vtk bundles
[simantics/3d.git] / org.jcae.opencascade / src-java-test / org / jcae / opencascade / jni / CircleExtrudeQuilt.java
1 package org.jcae.opencascade.jni;
2
3 import static org.junit.Assert.*;
4 import org.junit.Test;
5
6 import java.io.File;
7 import java.util.ArrayList;
8
9 /*import org.jcae.viewer3d.View;
10 import org.jcae.viewer3d.cad.ViewableCAD;
11 import org.jcae.viewer3d.cad.occ.OCCProvider;*/
12
13 /** Test circles, extrude, BRepTools_Quilt */
14 public class CircleExtrudeQuilt
15 {
16         /** Easy creation of faces */
17         private static TopoDS_Face createFace(TopoDS_Edge e1)
18         {
19                 TopoDS_Wire wirePlate=
20                         (TopoDS_Wire) new BRepBuilderAPI_MakeWire(e1).shape();
21                 return (TopoDS_Face) new BRepBuilderAPI_MakeFace(wirePlate, true).shape();
22         }
23
24         private static TopoDS_Edge getLastEdge(TopoDS_Shape shape)
25         {
26                 TopExp_Explorer exp=new TopExp_Explorer(shape, TopAbs_ShapeEnum.EDGE);
27                 TopoDS_Edge lastEdge=null;
28                 while(exp.more())
29                 {                       
30                         lastEdge=(TopoDS_Edge) exp.current();
31                         exp.next();
32                 }
33                 return lastEdge;
34         }
35         
36         private static TopoDS_Edge createCircle(double cx, double cy, double cz, double dx, double dy, double dz, double radius)
37         {
38                 GP_Circ circleB=new GP_Circ(new double[]{cx, cy, cz, dx, dy, dz}, radius);
39                 return (TopoDS_Edge) new BRepBuilderAPI_MakeEdge(circleB).shape();
40         }
41
42         private static TopoDS_Face createFace(TopoDS_Wire wire1, TopoDS_Wire wire2)
43         {
44                 TopoDS_Face face=(TopoDS_Face) new BRepBuilderAPI_MakeFace(wire1, true).shape();
45                 return (TopoDS_Face) new BRepBuilderAPI_MakeFace(face, wire2).shape();          
46         }
47         
48         private static TopoDS_Shell[] createShell(TopoDS_Face[] faces)
49         {
50                 BRepTools_Quilt quil=new BRepTools_Quilt();
51                 for(int i=0; i<faces.length; i++)
52                 {
53                         quil.add(faces[i]);
54                 }
55                 TopExp_Explorer exp=new TopExp_Explorer(quil.shells(), TopAbs_ShapeEnum.SHELL);
56                 ArrayList list=new ArrayList();
57                 while(exp.more())
58                 {
59                         list.add(exp.current());
60                         exp.next();
61                 }
62                 return (TopoDS_Shell[]) list.toArray(new TopoDS_Shell[0]);
63         }
64         
65         private static TopoDS_Face extrude(TopoDS_Shape shape, double vx, double vy, double vz)
66         {
67                 return (TopoDS_Face) new BRepPrimAPI_MakePrism(
68                         shape, new double[]{vx, vy, vz}).shape();
69         }
70         
71         private static TopoDS_Face[] getFaces(TopoDS_Shape shape)
72         {
73                 TopExp_Explorer exp=new TopExp_Explorer(shape, TopAbs_ShapeEnum.FACE);
74                 ArrayList list=new ArrayList();
75                 while(exp.more())
76                 {
77                         list.add(exp.current());
78                         exp.next();
79                 }
80                 return (TopoDS_Face[]) list.toArray(new TopoDS_Face[0]);
81         }
82         
83         private static TopoDS_Wire[] getWires(TopoDS_Shape shape)
84         {
85                 TopExp_Explorer exp=new TopExp_Explorer(shape, TopAbs_ShapeEnum.WIRE);
86                 ArrayList list=new ArrayList();
87                 while(exp.more())
88                 {
89                         System.out.println(exp.current());
90                         list.add(exp.current());
91                         exp.next();
92                 }
93                 return (TopoDS_Wire[]) list.toArray(new TopoDS_Wire[0]);
94         }
95         
96         private static TopoDS_Wire createWire(TopoDS_Edge edge)
97         {
98                 return (TopoDS_Wire) new BRepBuilderAPI_MakeWire(edge).shape();
99         }
100         
101         @Test public void sample()
102         {
103                 try
104                 {
105                         //Create a holed cylinder
106                         TopoDS_Edge circleB=createCircle(0, 0, -1E-3, 0, 0, 1, 4E-3);
107                         TopoDS_Edge circleSP1=createCircle(0, 0, 0, 0, 0, 1, 1E-3);
108                         TopoDS_Face cylinderS=extrude(circleSP1, 0, 0, -0.016);         
109                         TopoDS_Face cylinderBzM=(TopoDS_Face) extrude(circleB, 0, 0, -0.015).reversed();                
110                         TopoDS_Face cylinderBzP=extrude(circleB, 0, 0, 1E-3);
111                         
112                         TopoDS_Face hdSurface=(TopoDS_Face) createFace(
113                                 createWire(getLastEdge(cylinderBzM)),
114                                 createWire(getLastEdge(cylinderS))).reversed();         
115                         
116                         //Create a cylinder with a large disc sewed to it's base
117                         
118                         TopoDS_Face cylinderDiscP2=createFace(
119                                 createWire(getLastEdge(cylinderBzP)), createWire(circleSP1));
120                         TopoDS_Face cylinderSzP=extrude(circleSP1, 0, 0, 7.5E-3);
121                         TopoDS_Face cylinderDiscSzP=createFace(getLastEdge(cylinderSzP));
122                         
123                         //Create a box
124                         TopoDS_Solid box=(TopoDS_Solid) new BRepPrimAPI_MakeBox(
125                                 new double[]{4.5E-2,0.03,-0.001},
126                                 new double[]{-4.5E-2,-0.03,0}).shape();
127                         //Make a hole in the box
128                         TopoDS_Face[] boxFaces=getFaces(box);
129                         TopoDS_Face squareZm=boxFaces[4];
130                         TopoDS_Face squareZp=boxFaces[5];
131                         
132                         squareZm=createFace(
133                                 getWires(squareZm)[0],
134                                 (TopoDS_Wire) createWire(circleB).reversed());
135                         
136                         squareZp=createFace(getWires(squareZp)[0], getWires(cylinderDiscP2)[0]);
137                         TopoDS_Face[] holedBoxFaces=new TopoDS_Face[]{
138                                 boxFaces[0],
139                                 boxFaces[1],
140                                 boxFaces[2],
141                                 boxFaces[3],
142                                 squareZm,
143                                 squareZp,
144                                 cylinderBzP};           
145                         TopoDS_Solid holedBox=(TopoDS_Solid) new BRepBuilderAPI_MakeSolid(
146                                 createShell(holedBoxFaces)[0]).shape();
147                         
148                         //Put it all together
149                         BRep_Builder bb=new BRep_Builder();
150                         TopoDS_Compound compound=new TopoDS_Compound();
151                         bb.makeCompound(compound);
152                         bb.add(compound, holedBox);
153                         bb.add(compound, cylinderBzM);
154                         bb.add(compound, cylinderS);
155                         bb.add(compound, hdSurface);
156                         bb.add(compound, cylinderDiscP2);
157                         bb.add(compound, cylinderSzP);
158                         bb.add(compound, cylinderDiscSzP);
159                         bb.add(compound, cylinderBzP);          
160                         
161                         //save it               
162                         BRepTools.write(compound, File.createTempFile("circleExtrude", ".brep").getPath());
163         
164                         //display(file);
165                         //display("/tmp/Solid_1.brep");
166                 }
167                 catch(Exception ex)
168                 {
169                         ex.printStackTrace();
170                 }
171         }
172         
173         /**
174          * Display the geometry (viewer3d required)
175          */
176         /*public static void display(String file)
177         {
178                 JFrame cadFrame=new JFrame("jcae-viewer3d-cad demo");                   
179                 cadFrame.setSize(800,600);
180                 cadFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
181                 final View cadView=new View(cadFrame);                                  
182                 
183                 ViewableCAD fcad=new ViewableCAD(new OCCProvider(file));
184                 cadView.add(fcad);
185                 cadView.fitAll();
186                 cadFrame.getContentPane().add(cadView);
187                 cadFrame.getContentPane().add(new JButton(new AbstractAction(){
188
189                         public void actionPerformed(ActionEvent e)
190                         {
191                                 cadView.fitAll();
192                         }}), BorderLayout.NORTH);
193                 
194                 cadFrame.setVisible(true);
195                 cadView.setOriginAxisVisible(true);
196         }*/
197 }