Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / mfc / occtdemo / Glue / Glue_Presentation.cpp
1 // Glue_Presentation.cpp: implementation of the Glue_Presentation class.
2 // Glue two solid shapes with contiguous faces
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "Glue_Presentation.h"
7 #include <OCCDemoDoc.h>
8
9 #include <TopoDS_Solid.hxx>
10 #include <BRepPrimAPI_MakeBox.hxx>
11 #include <BRepFeat_Gluer.hxx>
12 #include <TopExp.hxx>
13 #include <TopTools_IndexedMapOfShape.hxx>
14 #include <TopoDS.hxx>
15 #include <BRepBuilderAPI_MakeEdge.hxx>
16 #include <Geom_Circle.hxx>
17 #include <Geom_TrimmedCurve.hxx>
18 #include <BRepBuilderAPI_MakeWire.hxx>
19 #include <BRepPrimAPI_MakePrism.hxx>
20 #include <BRepBuilderAPI_MakeFace.hxx>
21 #include <BRepPrimAPI_MakeCylinder.hxx>
22 #include <BRepAlgoAPI_Cut.hxx>
23 #include <TopTools_ListOfShape.hxx>
24 #include <TopTools_ListIteratorOfListOfShape.hxx>
25
26
27 // Initialization of global variable with an instance of this class
28 OCCDemo_Presentation* OCCDemo_Presentation::Current = new Glue_Presentation;
29
30 // Initialization of array of samples
31 const Glue_Presentation::PSampleFuncType Glue_Presentation::SampleFuncs[] =
32 {
33   //&Glue_Presentation::sampleBoxes1,
34   &Glue_Presentation::sampleBoxes,
35   &Glue_Presentation::sampleCylinder,
36   //&Glue_Presentation::sampleMoreBoxes
37 };
38
39 #ifdef WNT
40  #define EOL "\r\n"
41 #else
42  #define EOL "\n"
43 #endif
44
45 #define RESULT_SHAPE_COLOR Quantity_NOC_ORANGE
46
47 //////////////////////////////////////////////////////////////////////
48 // Construction/Destruction
49 //////////////////////////////////////////////////////////////////////
50
51 Glue_Presentation::Glue_Presentation()
52 {
53   myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
54   setName ("Glue shapes");
55 }
56
57 //////////////////////////////////////////////////////////////////////
58 // Sample execution
59 //////////////////////////////////////////////////////////////////////
60
61 void Glue_Presentation::DoSample()
62 {
63   getAISContext()->EraseAll();
64   if (myIndex >=0 && myIndex < myNbSamples)
65     (this->*SampleFuncs[myIndex])();
66 }
67
68 //////////////////////////////////////////////////////////////////////
69 // Sample functions
70 //////////////////////////////////////////////////////////////////////
71 //================================================================
72 // Function : Glue_Presentation::sampleBoxes
73 // Purpose  : 
74 //================================================================
75 void Glue_Presentation::sampleBoxes()
76 {
77   setResultTitle("Glue two solids");
78   setResultText(
79     "  // Create shapes" EOL
80     "  TopoDS_Solid aShape1;" EOL
81     "  TopoDS_Solid aShape2; " EOL
82     "  //aShape1 = ..." EOL
83     "  //aShape2 = ..." EOL
84     "" EOL
85     "  // Define same domain faces on aShape1 and aShape2" EOL
86     "  TopoDS_Face aFace1;" EOL
87     "  TopoDS_Face aFace2;" EOL
88     "  //aFace1 = ..." EOL
89     "  //aFace2 = ..." EOL
90     "" EOL
91     "  // Define same domain edges on aShape1 and aShape2" EOL
92     "  TopoDS_Edge aEdge1;" EOL
93     "  TopoDS_Edge aEdge2;" EOL
94     "  //aEdge1 = ..." EOL
95     "  //aEdge2 = ..." EOL
96     "" EOL
97     "  BRepFeat_Gluer aGluer(aShape2, aShape1);" EOL
98     "  aGluer.Bind(aFace2, aFace1);" EOL
99     "  aGluer.Bind(aEdge2, aEdge1);" EOL
100     "  aGluer.Build();" EOL
101     "" EOL
102     "  TopoDS_Shape aGluedShape;" EOL
103     "  if (aGluer.IsDone())" EOL
104     "    aGluedShape = aGluer;" EOL);
105
106   Standard_Real a = 10.0;
107   TopoDS_Solid aShape1 = BRepPrimAPI_MakeBox(a,a,a);
108   TopoDS_Solid aShape2 = BRepPrimAPI_MakeBox(gp_Pnt(a,a/2,a/4),a/2,a/2,a/2);
109
110   TopTools_IndexedMapOfShape aShape1Faces, aShape2Faces;
111   TopTools_IndexedMapOfShape aShape1Edges, aShape2Edges;
112   TopExp::MapShapes(aShape1, TopAbs_FACE, aShape1Faces);
113   TopExp::MapShapes(aShape2, TopAbs_FACE, aShape2Faces);
114   TopExp::MapShapes(aShape1, TopAbs_EDGE, aShape1Edges);
115   TopExp::MapShapes(aShape2, TopAbs_EDGE, aShape2Edges);
116   Standard_Integer fi1 = 2, fi2 = 1, ei1 = 7, ei2 = 3;
117   TopoDS_Face aFace1 = TopoDS::Face(aShape1Faces(fi1));
118   TopoDS_Face aFace2 = TopoDS::Face(aShape2Faces(fi2));
119   TopoDS_Edge aEdge1 = TopoDS::Edge(aShape1Edges(ei1));
120   TopoDS_Edge aEdge2 = TopoDS::Edge(aShape2Edges(ei2));
121
122   BRepFeat_Gluer aGluer(aShape2, aShape1);
123   aGluer.Bind(aFace2, aFace1);
124   aGluer.Bind(aEdge2, aEdge1);
125   aGluer.Build();
126   
127   TopoDS_Shape aGluedShape;
128   if (aGluer.IsDone())
129     aGluedShape = aGluer;
130
131   TopTools_ListOfShape aEdges1,aEdges2,aFaces1,aFaces2;
132   aEdges1.Append(aEdge1);  aEdges2.Append(aEdge2);
133   aFaces1.Append(aFace1);  aFaces2.Append(aFace2);
134
135   displayShapesFaces(aShape1,aShape2,aGluedShape,aFaces1,aFaces2,aEdges1,aEdges2);
136 }
137
138 //================================================================
139 // Function : Glue_Presentation::sampleCylinder
140 // Purpose  : 
141 //================================================================
142 void Glue_Presentation::sampleCylinder()
143 {
144   setResultTitle("Glue two solids");
145   setResultText(
146     "  // Create shapes" EOL
147     "  TopoDS_Solid aShape1;" EOL
148     "  TopoDS_Solid aShape2; " EOL
149     "  //aShape1 = ..." EOL
150     "  //aShape2 = ..." EOL
151     "" EOL
152     "  // Define same domain faces on aShape1 and aShape2" EOL
153     "  TopoDS_Face aFace1;" EOL
154     "  TopoDS_Face aFace2;" EOL
155     "  //aFace1 = ..." EOL
156     "  //aFace2 = ..." EOL
157     "" EOL
158     "  BRepFeat_Gluer aGluer(aShape2, aShape1);" EOL
159     "  aGluer.Bind(aFace2, aFace1);" EOL
160     "  aGluer.Build();" EOL
161     "" EOL
162     "  TopoDS_Shape aGluedShape;" EOL
163     "  if (aGluer.IsDone())" EOL
164     "    aGluedShape = aGluer;" EOL);
165
166   TopoDS_Edge e1 = BRepBuilderAPI_MakeEdge(gp_Pnt(0,0,0),gp_Pnt(5,0,0));
167   TopoDS_Edge e3 = BRepBuilderAPI_MakeEdge(gp_Pnt(19,0,0),gp_Pnt(24,0,0));
168   TopoDS_Edge e4 = BRepBuilderAPI_MakeEdge(gp_Pnt(24,0,0),gp_Pnt(24,15,0));
169   TopoDS_Edge e5 = BRepBuilderAPI_MakeEdge(gp_Pnt(24,15,0),gp_Pnt(0,15,0));
170   TopoDS_Edge e6 = BRepBuilderAPI_MakeEdge(gp_Pnt(0,15,0),gp_Pnt(0,0,0));
171   Handle_Geom_Circle e2C = new Geom_Circle(gp_Ax2(gp_Pnt(12,0,0), gp_Dir(0,0,1)), 7);
172   e2C->Rotate(gp_Ax1(gp_Pnt(12,0,0),gp_Dir(0,0,1)),PI);
173   Handle_Geom_TrimmedCurve e2A = new Geom_TrimmedCurve(e2C, PI, 2*PI);
174   TopoDS_Edge e2 = BRepBuilderAPI_MakeEdge(e2A);
175
176   BRepBuilderAPI_MakeWire wireMaker(e1,e2,e3,e4);
177   wireMaker.Add(e5);
178   wireMaker.Add(e6);
179
180   TopoDS_Face aFace = BRepBuilderAPI_MakeFace(wireMaker);
181
182   TopoDS_Shape aShape1 = BRepPrimAPI_MakePrism(aFace, gp_Vec(gp_Pnt(0,0,0),gp_Pnt(0,0,30)));
183
184   TopoDS_Shape aShape2 = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(12,0,-10),gp_Dir(0,0,1), gp_Dir(-0.5,-0.5,0)), 7, 50);
185
186   TopTools_IndexedMapOfShape aShape1Faces, aShape2Faces;
187   TopExp::MapShapes(aShape1, TopAbs_FACE, aShape1Faces);
188   TopExp::MapShapes(aShape2, TopAbs_FACE, aShape2Faces);
189   Standard_Integer fi1 = 2;
190   Standard_Integer fi2 = 1;
191   TopoDS_Face aFace1 = TopoDS::Face(aShape1Faces(fi1));
192   TopoDS_Face aFace2 = TopoDS::Face(aShape2Faces(fi2));
193
194   BRepFeat_Gluer aGluer(aShape1, aShape2);
195   aGluer.Bind(aFace1, aFace2);
196   aGluer.Build();
197
198   TopoDS_Shape aGluedShape;
199   if (aGluer.IsDone())
200     aGluedShape = aGluer;
201
202   TopTools_ListOfShape aEdges1,aEdges2,aFaces1,aFaces2;
203   aFaces1.Append(aFace1);  aFaces2.Append(aFace2);
204
205   displayShapesFaces(aShape1, aShape2, aGluedShape, aFaces1, aFaces2, aEdges1, aEdges2);
206 }
207
208 //================================================================
209 // Function : Glue_Presentation::sampleMoreBoxes
210 // Purpose  : 
211 //================================================================
212 void Glue_Presentation::sampleMoreBoxes()
213 {
214   setResultTitle("Glue two solids");
215   setResultText(
216     "  // Create shapes" EOL
217     "  TopoDS_Solid aShape1;" EOL
218     "  TopoDS_Solid aShape2; " EOL
219     "  //aShape1 = ..." EOL
220     "  //aShape2 = ..." EOL
221     "" EOL
222     "  // Define same domain faces on aShape1 and aShape2" EOL
223     "  TopoDS_Face aFace11,aFace12,aFace13;" EOL
224     "  TopoDS_Face aFace21,aFace22,aFace23;" EOL
225     "  //aFace11 = ..." EOL
226     "  //aFace21 = ..." EOL
227     "" EOL
228     "  // Define same domain edges on aShape1 and aShape2" EOL
229     "  TopoDS_Edge aEdge11,aEdge12,aEdge13;" EOL
230     "  TopoDS_Edge aEdge21,aEdge22,aEdge23;" EOL
231     "  //aEdge11 = ..." EOL
232     "  //aEdge21 = ..." EOL
233     "" EOL
234     "  BRepFeat_Gluer aGluer(aShape2, aShape1);" EOL
235     "  aGluer.Bind(aFace21, aFace11);" EOL
236     "  aGluer.Bind(aFace22, aFace12);" EOL
237     "  aGluer.Bind(aFace23, aFace13);" EOL
238     "  aGluer.Bind(aEdge21, aEdge11);" EOL
239     "  aGluer.Bind(aEdge22, aEdge12);" EOL
240     "  aGluer.Bind(aEdge23, aEdge13);" EOL
241     "  aGluer.Build();" EOL
242     "" EOL
243     "  TopoDS_Shape aGluedShape;" EOL
244     "  if (aGluer.IsDone())" EOL
245     "    aGluedShape = aGluer;" EOL);
246
247   TopoDS_Shape aShape1;
248   TopoDS_Shape aShape2;
249
250   Standard_Real a = 10.0;
251   TopoDS_Solid aBox1 = BRepPrimAPI_MakeBox(a,a,a);
252   TopoDS_Solid aBox2 = BRepPrimAPI_MakeBox(gp_Pnt(a/2,a/2,a/2),a,a,a);
253
254   BRepAlgoAPI_Cut aCutter(aBox1,aBox2);
255   aCutter.Build();
256   if (aCutter.IsDone())
257     aShape1 = aCutter.Shape();
258
259   aShape2 = aBox2;
260
261   TopTools_IndexedMapOfShape aShape1Faces, aShape2Faces;
262   TopTools_IndexedMapOfShape aShape1Edges, aShape2Edges;
263   TopExp::MapShapes(aShape1, TopAbs_FACE, aShape1Faces);
264   TopExp::MapShapes(aShape2, TopAbs_FACE, aShape2Faces);
265   TopExp::MapShapes(aShape1, TopAbs_EDGE, aShape1Edges);
266   TopExp::MapShapes(aShape2, TopAbs_EDGE, aShape2Edges);
267   Standard_Integer ei11 = 15, ei12 = 14, ei13 = 18;
268   Standard_Integer ei21 = 1, ei22 = 9, ei23 = 4;
269   Standard_Integer fi11 = 9, fi12 = 4, fi13 = 6;
270   Standard_Integer fi21 = 1, fi22 = 3, fi23 = 5;
271   TopoDS_Face f11 = TopoDS::Face(aShape1Faces(fi11));
272   TopoDS_Face f12 = TopoDS::Face(aShape1Faces(fi12));
273   TopoDS_Face f13 = TopoDS::Face(aShape1Faces(fi13));
274   TopoDS_Edge e11 = TopoDS::Edge(aShape1Edges(ei11));
275   TopoDS_Edge e12 = TopoDS::Edge(aShape1Edges(ei12));
276   TopoDS_Edge e13 = TopoDS::Edge(aShape1Edges(ei13));
277   TopoDS_Face f21 = TopoDS::Face(aShape2Faces(fi21));
278   TopoDS_Face f22 = TopoDS::Face(aShape2Faces(fi22));
279   TopoDS_Face f23 = TopoDS::Face(aShape2Faces(fi23));
280   TopoDS_Edge e21 = TopoDS::Edge(aShape2Edges(ei21));
281   TopoDS_Edge e22 = TopoDS::Edge(aShape2Edges(ei22));
282   TopoDS_Edge e23 = TopoDS::Edge(aShape2Edges(ei23));
283
284   BRepFeat_Gluer aGluer(aShape1, aShape2);
285   aGluer.Bind(e11, e21);
286   aGluer.Bind(e12, e22);
287   aGluer.Bind(e13, e23);
288   aGluer.Bind(f11, f21);
289   aGluer.Bind(f12, f22);
290   aGluer.Bind(f13, f23);
291   aGluer.Build();
292   
293   TopoDS_Shape aGluedShape;
294   if (aGluer.IsDone())
295     aGluedShape = aGluer;
296
297   TopTools_ListOfShape aEdges1,aEdges2,aFaces1,aFaces2;
298   aEdges1.Append(e11);  aEdges1.Append(e12);
299   aEdges1.Append(e13);  aEdges2.Append(e21);
300   aEdges2.Append(e22);  aEdges2.Append(e23);
301   aFaces1.Append(f11);  aFaces1.Append(f12);
302   aFaces1.Append(f13);  aFaces2.Append(f21);
303   aFaces2.Append(f22);  aFaces2.Append(f23);
304
305   displayShapesFaces(aShape1,aShape2,aGluedShape,aFaces1,aFaces2,aEdges1,aEdges2);
306 }
307
308
309 //================================================================
310 // Function : Glue_Presentation::drawShapeWf
311 // Purpose  : display a shape in wire frame mode
312 //================================================================
313 Standard_Boolean Glue_Presentation::drawShapeWf(TopoDS_Shape& aShape, 
314                                                 Handle_AIS_InteractiveObject& io)
315 {
316   io = drawShape(aShape, Graphic3d_NOM_BRASS, Standard_False);
317   getAISContext()->SetDisplayMode(io, AIS_WireFrame, Standard_False);
318   getAISContext()->Display(io);
319   return !WAIT_A_LITTLE;
320 }
321
322
323 //================================================================
324 // Function : Glue_Presentation::drawShapeSh
325 // Purpose  : display a shape in shaded mode
326 //================================================================
327 Standard_Boolean Glue_Presentation::drawShapeSh(TopoDS_Shape& aShape, 
328                                                 Handle_AIS_InteractiveObject& io)
329 {
330   io = drawShape(aShape, Graphic3d_NOM_BRASS, Standard_False);
331   getAISContext()->SetDisplayMode(io, AIS_Shaded, Standard_False);
332   getAISContext()->Display(io, Standard_False);
333   COCCDemoDoc::Fit();
334   return !WAIT_A_LITTLE;
335 }
336
337 //================================================================
338 // Function : Glue_Presentation::displayShapesFaces
339 // Purpose  : displays shapes, same domain faces and edges if given
340 //================================================================
341 Standard_Boolean Glue_Presentation::displayShapesFaces(TopoDS_Shape& aShape1,
342                                            TopoDS_Shape& aShape2,
343                                            TopoDS_Shape& aGluedShape,
344                                            TopTools_ListOfShape& aFaces1,
345                                            TopTools_ListOfShape& aFaces2,
346                                            TopTools_ListOfShape& aEdges1,
347                                            TopTools_ListOfShape& aEdges2)
348 {
349   Handle_AIS_Shape io1,io2,io3,io4,io5,io6;
350   if (!drawShapeSh(aShape1, io1)) return Standard_False;
351   if (!drawShapeSh(aShape2, io2)) return Standard_False;
352
353   getAISContext()->SetDisplayMode(io1, AIS_WireFrame, Standard_False);
354   getAISContext()->SetDisplayMode(io2, AIS_WireFrame);
355   if (WAIT_A_LITTLE) return Standard_False;
356
357   TopTools_ListIteratorOfListOfShape it1,it2;
358   for (it1.Initialize(aEdges1),it2.Initialize(aEdges2); 
359        it1.More() && it2.More(); 
360        it1.Next(),it2.Next())
361   {
362     if (!drawShapeWf(it2.Value(), io6)) return Standard_False;
363     getAISContext()->Erase(io6);
364     if (WAIT_A_LITTLE) return Standard_False;
365
366     if (!drawShapeWf(it1.Value(), io6)) return Standard_False;
367     getAISContext()->Erase(io6);
368     if (WAIT_A_LITTLE) return Standard_False;
369   }
370
371   for (it1.Initialize(aFaces1),it2.Initialize(aFaces2); 
372        it1.More() && it2.More(); 
373        it1.Next(),it2.Next())
374   {
375     if (!drawShapeSh(it2.Value(), io6)) return Standard_False;
376     getAISContext()->Erase(io6);
377     if (WAIT_A_LITTLE) return Standard_False;
378
379     if (!drawShapeSh(it1.Value(), io6)) return Standard_False;
380     getAISContext()->Erase(io6);
381     if (WAIT_A_LITTLE) return Standard_False;
382   }
383
384   getAISContext()->Erase(io1, Standard_False);
385   getAISContext()->Erase(io2, Standard_False);  
386
387   io1 = drawShape(aGluedShape, RESULT_SHAPE_COLOR, Standard_False);
388   getAISContext()->SetDisplayMode(io1, AIS_WireFrame, Standard_False);
389   getAISContext()->Display(io1);
390   if (WAIT_A_LITTLE) return Standard_False;
391
392   getAISContext()->SetDisplayMode(io1, AIS_Shaded);
393
394   return Standard_True;
395 }