Salome HOME
71987a990e9ed8aa34fe0496304fd7a2cc41251a
[modules/geom.git] / src / GEOMImpl / GEOMImpl_Block6Explorer.cxx
1 using namespace std;
2
3 #include <BRepOffsetAPI_MakeFilling.hxx>
4
5 #include "GEOMImpl_Block6Explorer.hxx"
6
7 #include "utilities.h"
8
9 #include <BRep_Tool.hxx>
10 #include <BRep_TFace.hxx>
11 #include <BRepTools.hxx>
12 #include <BRepTools_WireExplorer.hxx>
13 #include <BRepOffsetAPI_ThruSections.hxx>
14 #include <BRepBuilderAPI_MakeEdge.hxx>
15 #include <BRepBuilderAPI_MakeWire.hxx>
16 #include <BRepBuilderAPI_MakeFace.hxx>
17 #include <BRepBuilderAPI_Transform.hxx>
18
19 #include <TopAbs.hxx>
20 #include <TopoDS.hxx>
21 #include <TopoDS_Shape.hxx>
22 #include <TopoDS_Edge.hxx>
23 #include <TopoDS_Wire.hxx>
24 #include <TopoDS_Solid.hxx>
25 #include <TopExp.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopTools_MapOfShape.hxx>
28 #include <TopTools_ListOfShape.hxx>
29 #include <TopTools_ListIteratorOfListOfShape.hxx>
30 #include <TopTools_IndexedMapOfShape.hxx>
31 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
32
33 #include <Geom_Curve.hxx>
34 #include <Geom_TrimmedCurve.hxx>
35 #include <GeomFill_Generator.hxx>
36
37 #include <Precision.hxx>
38 #include <gp_Pnt.hxx>
39 #include <TColgp_Array1OfPnt.hxx>
40
41 #include <StdFail_NotDone.hxx>
42 #include <Standard_NullObject.hxx>
43 #include <Standard_TypeMismatch.hxx>
44 #include <Standard_ConstructionError.hxx>
45 #include <Standard_NoSuchObject.hxx>
46
47 #define NBFACES 6
48 #define NBEDGES 12
49 #define NBVERTS 8
50
51 static Standard_Integer mod4 (Standard_Integer nb)
52 {
53   if (nb <= 0) return nb + 4;
54   if (nb > 4)  return nb - 4;
55   return nb;
56 }
57
58 static Standard_Integer edge_id (const Standard_Integer theFaceID,
59                                  const Standard_Integer theEdgeNB)
60 {
61   static Standard_Integer edge_ids[NBFACES][4] = {
62     {  1,  2,  3,  4 },   // face 1
63     {  5,  6,  7,  8 },   // face 2
64     {  9,  5, 10,  1 },   // face 3
65     { 12,  7, 11,  3 },   // face 4
66     {  4, 12,  8,  9 },   // face 5
67     {  2, 11,  6, 10 } }; // face 6
68
69   return edge_ids[theFaceID - 1][theEdgeNB - 1];
70 }
71
72 static Standard_Integer side_edge_id (const Standard_Integer theEdgeNB)
73 {
74   static Standard_Integer side_edge_ids[4] = {9, 10, 11, 12};
75
76   return side_edge_ids[theEdgeNB - 1];
77 }
78
79 static Standard_Integer vertex_id (const Standard_Integer theFaceID,
80                                    const Standard_Integer theVertexNB)
81 {
82   static Standard_Integer vertex_ids[NBFACES][4] = {
83     { 1, 2, 3, 4 },   // face 1
84     { 5, 6, 7, 8 },   // face 2
85     { 1, 5, 6, 2 },   // face 3
86     { 4, 8, 7, 3 },   // face 4
87     { 1, 4, 8, 5 },   // face 5
88     { 2, 3, 7, 6 } }; // face 6
89
90   return vertex_ids[theFaceID - 1][theVertexNB - 1];
91 }
92
93 static Standard_Integer vertex_id_edge (const Standard_Integer theEdgeID, // [1,12]
94                                         const Standard_Integer theVertexNB) // [1,2]
95 {
96   static Standard_Integer vertex_ids_edge[NBEDGES][2] = {
97     {1, 2},   // edge 1
98     {2, 3},   // edge 2
99     {3, 4},   // edge 3
100     {4, 1},   // edge 4
101     {5, 6},   // edge 5
102     {6, 7},   // edge 6
103     {7, 8},   // edge 7
104     {8, 5},   // edge 8
105     {1, 5},   // edge 9
106     {2, 6},   // edge 10
107     {3, 7},   // edge 11
108     {4, 8} }; // edge 12
109
110   return vertex_ids_edge[theEdgeID - 1][theVertexNB - 1];
111 }
112
113 static Standard_Integer face_id_edges (const Standard_Integer theEdge1ID, // [1,12]
114                                        const Standard_Integer theEdge2ID) // [1,12]
115 {
116   static Standard_Integer face_ids_edges[NBEDGES][NBEDGES] = {
117     // 1  2  3  4  5  6  7  8  9  10 11 12
118     {  0, 1, 1, 1, 3, 0, 0, 0, 3, 3, 0, 0  },   // edge 1
119     {  1, 0, 1, 1, 0, 6, 0, 0, 0, 6, 6, 0  },   // edge 2
120     {  1, 1, 0, 1, 0, 0, 4, 0, 0, 0, 4, 4  },   // edge 3
121     {  1, 1, 1, 0, 0, 0, 0, 5, 5, 0, 0, 5  },   // edge 4
122     {  3, 0, 0, 0, 0, 2, 2, 2, 3, 3, 0, 0  },   // edge 5
123     {  0, 6, 0, 0, 2, 0, 2, 2, 0, 6, 6, 0  },   // edge 6
124     {  0, 0, 4, 0, 2, 2, 0, 2, 0, 0, 4, 4  },   // edge 7
125     {  0, 0, 0, 5, 2, 2, 2, 0, 5, 0, 0, 5  },   // edge 8
126     {  3, 0, 0, 5, 3, 0, 0, 5, 0, 3, 0, 5  },   // edge 9
127     {  3, 6, 0, 0, 3, 6, 0, 0, 3, 0, 6, 0  },   // edge 10
128     {  0, 6, 4, 0, 0, 6, 4, 0, 0, 6, 0, 4  },   // edge 11
129     {  0, 0, 4, 5, 0, 0, 4, 5, 5, 0, 4, 0  } }; // edge 12
130
131   return face_ids_edges[theEdge1ID - 1][theEdge2ID - 1];
132 }
133
134 static Standard_Integer edge_id_vertices (const Standard_Integer theVertex1ID, // [1,8]
135                                           const Standard_Integer theVertex2ID) // [1,8]
136 {
137   static Standard_Integer edge_ids_vertices[NBVERTS][NBVERTS] = {
138     // 1   2   3   4   5   6   7   8
139     {  0,  1,  0,  4,  9,  0,  0,  0},   // vertex 1
140     {  1,  0,  2,  0,  0, 10,  0,  0},   // vertex 2
141     {  0,  2,  0,  3,  0,  0, 11,  0},   // vertex 3
142     {  4,  0,  3,  0,  0,  0,  0, 12},   // vertex 4
143     {  9,  0,  0,  0,  0,  5,  0,  8},   // vertex 5
144     {  0, 10,  0,  0,  5,  0,  6,  0},   // vertex 6
145     {  0,  0, 11,  0,  0,  6,  0,  7},   // vertex 7
146     {  0,  0,  0, 12,  8,  0,  7,  0} }; // vertex 8
147
148   return edge_ids_vertices[theVertex1ID - 1][theVertex2ID - 1];
149 }
150
151 static Standard_Integer edge_id_faces (const Standard_Integer theFace1ID, // [1,6]
152                                        const Standard_Integer theFace2ID) // [1,6]
153 {
154   static Standard_Integer edge_ids_faces[NBFACES][NBFACES] = {
155     // 1   2   3   4   5   6
156     {  0,  0,  1,  3,  4,  2  },   // face 1
157     {  0,  0,  5,  7,  8,  6  },   // face 2
158     {  1,  5,  0,  0,  9, 10  },   // face 3
159     {  3,  7,  0,  0, 12, 11  },   // face 4
160     {  4,  8,  9, 12,  0,  0  },   // face 5
161     {  2,  6, 10, 11,  0,  0  } }; // face 6
162
163   return edge_ids_faces[theFace1ID - 1][theFace2ID - 1];
164 }
165
166 //=======================================================================
167 //function : GEOMImpl_Block6Explorer
168 //purpose  : Constructor
169 //=======================================================================
170 GEOMImpl_Block6Explorer::GEOMImpl_Block6Explorer ()
171      : myFaces(1,NBFACES), myEdges(1,NBEDGES), myVertices(1,NBVERTS)
172 {
173 }
174
175 //=======================================================================
176 //function : GetVertex
177 //purpose  :
178 //=======================================================================
179 TopoDS_Shape GEOMImpl_Block6Explorer::GetVertex (const Standard_Integer theVertexID)
180 {
181   TopoDS_Shape aNullShape;
182   if (theVertexID < 1 || theVertexID > NBVERTS) return aNullShape;
183   return myVertices(theVertexID);
184 }
185
186 //=======================================================================
187 //function : GetVertexID
188 //purpose  :
189 //=======================================================================
190 Standard_Integer GEOMImpl_Block6Explorer::GetVertexID (const TopoDS_Shape& theVertex)
191 {
192   for (Standard_Integer id = 1; id <= NBVERTS; id++) {
193     if (theVertex.IsSame(myVertices(id))) return id;
194   }
195   Standard_NoSuchObject::Raise("The Vertex does not belong to the Block");
196   return 0;
197 }
198
199 //=======================================================================
200 //function : GetVertexID
201 //purpose  :
202 //=======================================================================
203 Standard_Integer GEOMImpl_Block6Explorer::GetVertexID (const Standard_Integer theFaceID,
204                                                        const Standard_Integer theVertexNB)
205 {
206   return vertex_id(theFaceID, theVertexNB);
207 }
208
209 //=======================================================================
210 //function : GetVertexOnEdgeID
211 //purpose  :
212 //=======================================================================
213 Standard_Integer GEOMImpl_Block6Explorer::GetVertexOnEdgeID
214                                      (const Standard_Integer theEdgeID,
215                                       const Standard_Integer theVertexNB)
216 {
217   return vertex_id_edge(theEdgeID, theVertexNB);
218 }
219
220 //=======================================================================
221 //function : GetEdge
222 //purpose  :
223 //=======================================================================
224 TopoDS_Shape GEOMImpl_Block6Explorer::GetEdge (const Standard_Integer theEdgeID,
225                                                const Standard_Boolean doMake)
226 {
227   TopoDS_Shape aNullShape;
228   if (theEdgeID < 1 || theEdgeID > NBEDGES) return aNullShape;
229   if (myEdges(theEdgeID).IsNull() && doMake) {
230     // Create the required edge as a linear segment between
231     // corresponding vertices and put it in the Block's edges
232     BRepBuilderAPI_MakeEdge ME (TopoDS::Vertex(myVertices(vertex_id_edge(theEdgeID, 1))),
233                                 TopoDS::Vertex(myVertices(vertex_id_edge(theEdgeID, 2))));
234     if (!ME.IsDone()) {
235       Standard_ConstructionError::Raise("Edge construction failed");
236     }
237     myEdges(theEdgeID) = ME.Shape();
238   }
239
240   return myEdges(theEdgeID);
241 }
242
243 //=======================================================================
244 //function : GetEdgeID
245 //purpose  :
246 //=======================================================================
247 Standard_Integer GEOMImpl_Block6Explorer::GetEdgeID (const TopoDS_Shape& theEdge)
248 {
249   for (Standard_Integer id = 1; id <= NBEDGES; id++) {
250     if (theEdge.IsSame(myEdges(id))) return id;
251   }
252   Standard_NoSuchObject::Raise("The Edge does not belong to the Block");
253   return 0;
254 }
255
256 //=======================================================================
257 //function : GetEdgeID
258 //purpose  :
259 //=======================================================================
260 Standard_Integer GEOMImpl_Block6Explorer::GetEdgeID (const Standard_Integer theFaceID,
261                                                      const Standard_Integer theEdgeNB)
262 {
263   return edge_id(theFaceID, theEdgeNB);
264 }
265
266 //=======================================================================
267 //function : FindEdgeID
268 //purpose  :
269 //=======================================================================
270 Standard_Integer GEOMImpl_Block6Explorer::FindEdgeID (const Standard_Integer theVertex1ID,
271                                                       const Standard_Integer theVertex2ID)
272 {
273   return edge_id_vertices(theVertex1ID, theVertex2ID);
274 }
275
276 //=======================================================================
277 //function : FindCommonEdgeID
278 //purpose  :
279 //=======================================================================
280 Standard_Integer GEOMImpl_Block6Explorer::FindCommonEdgeID
281                                       (const Standard_Integer theFace1ID,
282                                        const Standard_Integer theFace2ID)
283 {
284   return edge_id_faces(theFace1ID, theFace2ID);
285 }
286
287 //=======================================================================
288 //function : GetFace
289 //purpose  :
290 //=======================================================================
291 TopoDS_Shape GEOMImpl_Block6Explorer::GetFace (const Standard_Integer theFaceID,
292                                                const Standard_Boolean doMake)
293 {
294   TopoDS_Shape aNullShape;
295   if (theFaceID < 1 || theFaceID > NBFACES) return aNullShape;
296
297   if (myFaces(theFaceID).IsNull() && doMake) {
298
299     // Create the required face between
300     // corresponding edges and put it in the Block's faces
301
302     TopoDS_Shape E1 = GetEdge(edge_id(theFaceID, 1), doMake);
303     TopoDS_Shape E2 = GetEdge(edge_id(theFaceID, 2), doMake);
304     TopoDS_Shape E3 = GetEdge(edge_id(theFaceID, 3), doMake);
305     TopoDS_Shape E4 = GetEdge(edge_id(theFaceID, 4), doMake);
306
307     BRepBuilderAPI_MakeWire MW (TopoDS::Edge(E1),
308                                 TopoDS::Edge(E2),
309                                 TopoDS::Edge(E3),
310                                 TopoDS::Edge(E4));
311     if (!MW.IsDone()) {
312       Standard_ConstructionError::Raise("Wire construction failed");
313     }
314     TopoDS_Shape aFace;
315     MakeFace(MW, Standard_False, aFace);
316     if (aFace.IsNull()) {
317       Standard_ConstructionError::Raise("Face construction failed");
318     }
319     myFaces(theFaceID) = aFace;
320   }
321
322   return myFaces(theFaceID);
323 }
324
325 //=======================================================================
326 //function : GetFaceID
327 //purpose  :
328 //=======================================================================
329 Standard_Integer GEOMImpl_Block6Explorer::GetFaceID (const TopoDS_Shape& theFace)
330 {
331   for (Standard_Integer id = 1; id <= NBFACES; id++) {
332     if (theFace.IsSame(myFaces(id))) return id;
333   }
334   Standard_NoSuchObject::Raise("The Face does not belong to the Block");
335   return 0;
336 }
337
338 //=======================================================================
339 //function : FindFaceID
340 //purpose  :
341 //=======================================================================
342 Standard_Integer GEOMImpl_Block6Explorer::FindFaceID (const Standard_Integer theEdge1ID,
343                                                       const Standard_Integer theEdge2ID)
344 {
345   return face_id_edges(theEdge1ID, theEdge2ID);
346 }
347
348 //=======================================================================
349 //function : GetOppositeFaceID
350 //purpose  :
351 //=======================================================================
352 Standard_Integer GEOMImpl_Block6Explorer::GetOppositeFaceID (const Standard_Integer theFaceID)
353 {
354   Standard_Integer opp_face_id[NBFACES + 1] = {
355     0,
356     2,  // to 1 face
357     1,  // to 2 face
358     4,  // to 3 face
359     3,  // to 4 face
360     6,  // to 5 face
361     5}; // to 6 face
362
363   return opp_face_id[theFaceID];
364 }
365
366 //=======================================================================
367 //function : IsSimilarFaces
368 //purpose  :
369 //=======================================================================
370 Standard_Boolean GEOMImpl_Block6Explorer::IsSimilarFaces (const Standard_Integer theFace1ID,
371                                                           const Standard_Integer theFace2ID,
372                                                           const gp_Trsf          theTransformation)
373 {
374   Standard_Integer common_edge_id = FindCommonEdgeID(theFace1ID, theFace2ID);
375
376   if (common_edge_id == 0) { // opposite faces
377     for (Standard_Integer id = 1; id <= 4; id++) {
378       TopoDS_Shape E1 = GetEdge(edge_id(theFace1ID, id));
379       TopoDS_Shape E2 = GetEdge(edge_id(theFace2ID, id));
380
381       BRepBuilderAPI_Transform aTrsf (E1, theTransformation, Standard_False);
382       if (!IsSimilarEdges(aTrsf.Shape(), E2))
383         return Standard_False;
384     }
385   } else { // the faces have common edge
386     TopTools_Array1OfShape aVerts1 (1,4);
387     TopTools_Array1OfShape aVerts2 (1,4);
388
389     Standard_Integer common_vertex1 = GetVertexOnEdgeID(common_edge_id, 1);
390     Standard_Integer common_vertex2 = GetVertexOnEdgeID(common_edge_id, 2);
391     aVerts1(1) = myVertices(common_vertex1);
392     aVerts1(2) = myVertices(common_vertex2);
393     aVerts2(1) = myVertices(common_vertex1);
394     aVerts2(2) = myVertices(common_vertex2);
395
396     Standard_Integer not_common_v11 = 0, not_common_v12 = 0;
397     Standard_Integer vnb, vid;
398     for (vnb = 1; vnb <= 4; vnb++) {
399       vid = GetVertexID(theFace1ID, vnb);
400       if (vid != common_vertex1 && FindEdgeID(vid, common_vertex1) == 0) {
401         not_common_v12 = vid;
402       } else {
403         if (vid != common_vertex2 && FindEdgeID(vid, common_vertex2) == 0) {
404           not_common_v11 = vid;
405         }
406       }
407     }
408
409     Standard_Integer not_common_v21 = 0, not_common_v22 = 0;
410     for (vnb = 1; vnb <= 4; vnb++) {
411       vid = GetVertexID(theFace2ID, vnb);
412       if (vid != common_vertex1 && FindEdgeID(vid, common_vertex1) == 0) {
413         not_common_v22 = vid;
414       } else {
415         if (vid != common_vertex2 && FindEdgeID(vid, common_vertex2) == 0) {
416           not_common_v21 = vid;
417         }
418       }
419     }
420     aVerts1(3) = myVertices(not_common_v11);
421     aVerts1(4) = myVertices(not_common_v12);
422     aVerts2(3) = myVertices(not_common_v21);
423     aVerts2(4) = myVertices(not_common_v22);
424
425     for (Standard_Integer id = 1; id <= 4; id++) {
426       BRepBuilderAPI_Transform aTrsf (aVerts1(id), theTransformation, Standard_False);
427       TopoDS_Vertex V1 = TopoDS::Vertex(aTrsf.Shape());
428       TopoDS_Vertex V2 = TopoDS::Vertex(aVerts2(id));
429       if (!BRepTools::Compare(V1, V2)) {
430         return Standard_False;
431       }
432     }
433   }
434
435   return Standard_True;
436 }
437
438 //============ Initialization methods ===================================
439
440 //=======================================================================
441 //function : InitByBlock
442 //purpose  :
443 //=======================================================================
444 void GEOMImpl_Block6Explorer::InitByBlock (const TopoDS_Shape& theBlock)
445 {
446   // 1. Find any one face of the block
447   TopExp_Explorer faces (theBlock, TopAbs_FACE);
448   if (!faces.More()) {
449     Standard_ConstructionError::Raise("The block has no faces");
450   }
451   TopoDS_Shape aFirstFace = faces.Current();
452
453   // 2. Store all elements of the block relatively aFirstFace
454   InitByBlockAndFace(theBlock, aFirstFace);
455 }
456
457 //=======================================================================
458 //function : InitByBlockAndFace
459 //purpose  :
460 //=======================================================================
461 void GEOMImpl_Block6Explorer::InitByBlockAndFace (const TopoDS_Shape& theBlock,
462                                                   const TopoDS_Shape& theFace)
463 {
464   myFaces(1) = theFace;
465
466   // 2. Get wire of the first face
467   TopExp_Explorer wires (myFaces(1), TopAbs_WIRE);
468   if (!wires.More()) {
469     Standard_ConstructionError::Raise("A face of the block has no wires");
470   }
471   TopoDS_Shape aWire = wires.Current();
472   wires.Next();
473   if (wires.More()) {
474     Standard_ConstructionError::Raise("A face of the block has more than one wires");
475   }
476
477   // 3. Explore wire to init edges and vertices of the first face
478   BRepTools_WireExplorer aWE (TopoDS::Wire(aWire), TopoDS::Face(myFaces(1)));
479   Standard_Integer nb = 1;
480   for (; aWE.More(); aWE.Next(), nb++) {
481     if (nb > 4) {
482       Standard_ConstructionError::Raise("A face of the block has more than four edges");
483     }
484     myEdges(edge_id(1, nb)) = aWE.Current();
485     myVertices(vertex_id(1, nb)) = aWE.CurrentVertex();
486   }
487   if (nb < 5) {
488     Standard_ConstructionError::Raise("A face of the block has less than four edges");
489   }
490
491   // 2. Store all other elements of the block
492   InitByBlockAndVertices (theBlock,
493                           myVertices(vertex_id(1,1)),
494                           myVertices(vertex_id(1,2)),
495                           myVertices(vertex_id(1,3)));
496 }
497
498 //=======================================================================
499 //function : InitByBlockAndEdges
500 //purpose  :
501 //=======================================================================
502 void GEOMImpl_Block6Explorer::InitByBlockAndEdges (const TopoDS_Shape& theBlock,
503                                                    const TopoDS_Shape& theEdge1,
504                                                    const TopoDS_Shape& theEdge3)
505 {
506   // 1. Store vertices and edges of the first face
507
508   // 1.1. Store two given edges
509   myEdges(edge_id(1, 1)) = theEdge1;
510   myEdges(edge_id(1, 3)) = theEdge3;
511
512   // 1.2. Find and store the first face
513   TopTools_IndexedDataMapOfShapeListOfShape MEF;
514   MapShapesAndAncestors(theBlock, TopAbs_EDGE, TopAbs_FACE, MEF);
515   if (MEF.Extent() != NBEDGES) {
516     Standard_TypeMismatch::Raise("Block has wrong number of edges");
517   }
518   const TopTools_ListOfShape& aFacesOfE1 = MEF.FindFromKey(theEdge1);
519   const TopTools_ListOfShape& aFacesOfE3 = MEF.FindFromKey(theEdge3);
520
521   Standard_Boolean isFound = Standard_False;
522   TopTools_ListIteratorOfListOfShape anIterF1 (aFacesOfE1);
523   for (; anIterF1.More() && !isFound; anIterF1.Next()) {
524
525     TopTools_ListIteratorOfListOfShape anIterF3 (aFacesOfE3);
526     for (; anIterF3.More() && !isFound; anIterF3.Next()) {
527
528       if (anIterF1.Value().IsSame(anIterF3.Value())) {
529         isFound = Standard_True;
530
531         // Store the face, defined by two opposite edges
532         myFaces(1) = anIterF1.Value();
533       }
534     }
535   }
536   if (!isFound) {
537     Standard_ConstructionError::Raise
538       ("Edges 1 and 2 do not belong to one face of the block");
539   }
540
541   // 1.3. Make vertices of the first edge the first and the
542   //      second vertices of the first face. Order is free.
543   TopoDS_Edge E = TopoDS::Edge(theEdge1);
544   TopoDS_Vertex V1, V2;
545   TopExp::Vertices(E, V1, V2, Standard_True);
546   myVertices(vertex_id(1,1)) = V1;
547   myVertices(vertex_id(1,2)) = V2;
548
549   // Init maps vertex->list_of_edges for the face
550   TopTools_IndexedDataMapOfShapeListOfShape M1;
551   MapShapesAndAncestors(myFaces(1), TopAbs_VERTEX, TopAbs_EDGE, M1);
552   if (M1.Extent() != 4) {
553     Standard_TypeMismatch::Raise("The first face of block has wrong number of vertices");
554   }
555
556   // 1.4. Find and store others elements of the first face
557
558   // edges of the first vertex
559   TopoDS_Shape E1_f = M1.FindFromKey(V1).First();
560   TopoDS_Shape E1_l = M1.FindFromKey(V1).Last();
561
562   if (E1_f.IsSame(theEdge1)) {
563     myEdges(edge_id(1, 4)) = E1_l;
564   } else {
565     myEdges(edge_id(1, 4)) = E1_f;
566   }
567
568   // fourth vertex
569   TopoDS_Edge E4 = TopoDS::Edge(myEdges(edge_id(1, 4)));
570   TopoDS_Vertex V41, V42;
571   TopExp::Vertices(E4, V41, V42, Standard_True);
572   if (V41.IsSame(V1)) {
573     myVertices(vertex_id(1,4)) = V42;
574   } else {
575     myVertices(vertex_id(1,4)) = V41;
576   }
577
578   // edges of the second vertex
579   TopoDS_Shape E2_f = M1.FindFromKey(V2).First();
580   TopoDS_Shape E2_l = M1.FindFromKey(V2).Last();
581
582   if (E2_f.IsSame(theEdge1)) {
583     myEdges(edge_id(1, 2)) = E2_l;
584   } else {
585     myEdges(edge_id(1, 2)) = E2_f;
586   }
587
588   // fird vertex
589   TopoDS_Edge E2 = TopoDS::Edge(myEdges(edge_id(1, 2)));
590   TopoDS_Vertex V21, V22;
591   TopExp::Vertices(E2, V21, V22, Standard_True);
592   if (V21.IsSame(V2)) {
593     myVertices(vertex_id(1,3)) = V22;
594   } else {
595     myVertices(vertex_id(1,3)) = V21;
596   }
597
598   // 2. Store all other elements of the block
599   InitByBlockAndVertices (theBlock,
600                           myVertices(vertex_id(1,1)),
601                           myVertices(vertex_id(1,2)),
602                           myVertices(vertex_id(1,3)));
603 }
604
605 //=======================================================================
606 //function : InitByBlockAndVertices
607 //purpose  :
608 //=======================================================================
609 void GEOMImpl_Block6Explorer::InitByBlockAndVertices (const TopoDS_Shape& theBlock,
610                                                       const TopoDS_Shape& theVertex1,
611                                                       const TopoDS_Shape& theVertex2,
612                                                       const TopoDS_Shape& theVertex3)
613 {
614   // Here we suppose, that vertices are ordered, i.e. exists edge between
615   // theVertex1 and theVertex2 and edge between theVertex2 and theVertex3
616
617   // 1. Store vertices and edges of the first face.
618   //    If the first face is initialized, it means, that this
619   //    method is called from another initialization method, and all
620   //    vertices and edges of the first face are also initialized
621   if (myFaces(1).IsNull()) {
622
623     // 1.1. Store first three vertices
624     myVertices(vertex_id(1, 1)) = theVertex1;
625     myVertices(vertex_id(1, 2)) = theVertex2;
626     myVertices(vertex_id(1, 3)) = theVertex3;
627
628     // 1.2. Find and store the first face
629     TopTools_IndexedDataMapOfShapeListOfShape MVF;
630     MapShapesAndAncestors(theBlock, TopAbs_VERTEX, TopAbs_FACE, MVF);
631     if (MVF.Extent() != NBVERTS) {
632       Standard_TypeMismatch::Raise("Block has wrong number of vertices");
633     }
634     const TopTools_ListOfShape& aFacesOfV1 = MVF.FindFromKey(theVertex1);
635     const TopTools_ListOfShape& aFacesOfV3 = MVF.FindFromKey(theVertex3);
636
637     Standard_Boolean isFound = Standard_False;
638     TopTools_ListIteratorOfListOfShape anIterF1 (aFacesOfV1);
639     for (; anIterF1.More() && !isFound; anIterF1.Next()) {
640
641       TopTools_ListIteratorOfListOfShape anIterF3 (aFacesOfV3);
642       for (; anIterF3.More() && !isFound; anIterF3.Next()) {
643
644         if (anIterF1.Value().IsSame(anIterF3.Value())) {
645           isFound = Standard_True;
646
647           // Store the face, defined by two opposite vertices
648           myFaces(1) = anIterF1.Value();
649         }
650       }
651     }
652     if (!isFound) {
653       Standard_ConstructionError::Raise
654         ("Vertices 1 and 3 do not belong to one face of the block");
655     }
656
657     // Init maps vertex->list_of_edges for the face
658     TopTools_IndexedDataMapOfShapeListOfShape M1;
659     MapShapesAndAncestors(myFaces(1), TopAbs_VERTEX, TopAbs_EDGE, M1);
660     if (M1.Extent() != 4) {
661       Standard_TypeMismatch::Raise("The first face of block has wrong number of vertices");
662     }
663
664     // 1.3. Find and store edges and last vertex of the first face
665     const TopTools_ListOfShape& anEdgesOfV1 = M1.FindFromKey(theVertex1);
666     const TopTools_ListOfShape& anEdgesOfV2 = M1.FindFromKey(theVertex2);
667     const TopTools_ListOfShape& anEdgesOfV3 = M1.FindFromKey(theVertex3);
668
669     TopTools_ListIteratorOfListOfShape anIterE2 (anEdgesOfV2);
670     for (; anIterE2.More(); anIterE2.Next()) {
671
672       TopTools_ListIteratorOfListOfShape anIterE1 (anEdgesOfV1);
673       for (; anIterE1.More(); anIterE1.Next()) {
674
675         if (anIterE1.Value().IsSame(anIterE2.Value())) {
676           // Store the first edge, defined by two vertices
677           myEdges(edge_id(1,1)) = anIterE1.Value();
678
679         } else {
680           // Store the last edge
681           myEdges(edge_id(1,4)) = anIterE1.Value();
682
683           // Find and store the last vertex
684           TopoDS_Edge E = TopoDS::Edge(myEdges(4));
685           TopoDS_Vertex V1, V2;
686           TopExp::Vertices(E, V1, V2, Standard_True);
687
688           if (V1.IsSame(theVertex1)) {
689             myVertices(vertex_id(1,4)) = V2;
690           } else {
691             myVertices(vertex_id(1,4)) = V1;
692           }
693         }
694       }
695
696       TopTools_ListIteratorOfListOfShape anIterE3 (anEdgesOfV3);
697       for (; anIterE3.More(); anIterE3.Next()) {
698
699         if (anIterE3.Value().IsSame(anIterE2.Value())) {
700           // Store the second edge, defined by two vertices
701           myEdges(edge_id(1,2)) = anIterE3.Value();
702
703         } else {
704           // Store the fird edge
705           myEdges(edge_id(1,3)) = anIterE3.Value();
706         }
707       }
708     }
709   }
710
711   // Init map vertex->list_of_edges for the block
712   TopTools_IndexedDataMapOfShapeListOfShape MB;
713   MapShapesAndAncestors(theBlock, TopAbs_VERTEX, TopAbs_EDGE, MB);
714   if (MB.Extent() != NBVERTS) {
715     Standard_TypeMismatch::Raise("Block has wrong number of vertices");
716   }
717
718   // 2. Store edges, linking the first face with the second one
719   //    and vertices of the second face
720   TopTools_IndexedMapOfShape aFaceEdges;
721   TopExp::MapShapes(myFaces(1), TopAbs_EDGE, aFaceEdges);
722
723   for (Standard_Integer i = 1; i <= 4; i++) {
724     // Get i-th vertex of the face 1
725     TopoDS_Shape Vi = myVertices(vertex_id(1, i));
726     if (!MB.Contains(Vi)) {
727       Standard_ConstructionError::Raise("Face does not belong to the block");
728     }
729
730     // Get list of block's edges, sharing this Vertex
731     const TopTools_ListOfShape& anEdgesOfVi = MB.FindFromKey(Vi);
732     TopTools_ListIteratorOfListOfShape anEdgesIter (anEdgesOfVi);
733
734     // Get Edge (from the List), not belonging to the face 1
735     Standard_Boolean isFound = Standard_False;
736     for (; anEdgesIter.More() && !isFound; anEdgesIter.Next()) {
737       if (!aFaceEdges.Contains(anEdgesIter.Value())) {
738         isFound = Standard_True;
739
740         // Store the linking edge
741         TopoDS_Shape aLinkEdge = anEdgesIter.Value();
742         myEdges(side_edge_id(i)) = aLinkEdge;
743
744         // Get another vertex of the linking edge
745         TopoDS_Edge E = TopoDS::Edge(aLinkEdge);
746         TopoDS_Vertex V1, V2;
747         TopExp::Vertices(E, V1, V2, Standard_True);
748
749         // Store the i-th vertex of the second (opposite to the first) face
750         if (V1.IsSame(Vi)) {
751           myVertices(vertex_id(2, i)) = V2;
752         } else {
753           myVertices(vertex_id(2, i)) = V1;
754         }
755       }
756     }
757   }
758
759   // 3. Store edges of the second (opposite to the first) face
760   for (Standard_Integer i = 1; i <= 4; i++) {
761     // Get i-th and (i+1)-th vertices of the face 2
762     TopoDS_Shape Vi = myVertices(vertex_id(2, i));
763     TopoDS_Shape Vj = myVertices(vertex_id(2, mod4(i + 1)));
764
765     // Get list of block's edges, sharing Vi
766     const TopTools_ListOfShape& anEdgesOfVi = MB.FindFromKey(Vi);
767     // Get list of block's edges, sharing Vj
768     const TopTools_ListOfShape& anEdgesOfVj = MB.FindFromKey(Vj);
769
770     // Get Edge (from the List), linking this vertex with the next one
771     Standard_Boolean isFound = Standard_False;
772     TopTools_ListIteratorOfListOfShape anEdgesIteri (anEdgesOfVi);
773     for (; anEdgesIteri.More() && !isFound; anEdgesIteri.Next()) {
774
775       TopTools_ListIteratorOfListOfShape anEdgesIterj (anEdgesOfVj);
776       for (; anEdgesIterj.More() && !isFound; anEdgesIterj.Next()) {
777
778         if (anEdgesIteri.Value().IsSame(anEdgesIterj.Value())) {
779           isFound = Standard_True;
780
781           // Store the linking edge
782           myEdges(edge_id(2, i)) = anEdgesIteri.Value();
783         }
784       }
785     }
786   }
787
788   // 4. Store faces of the block
789   TopTools_IndexedDataMapOfShapeListOfShape MBE;
790   MapShapesAndAncestors(theBlock, TopAbs_EDGE, TopAbs_FACE, MBE);
791   if (MBE.Extent() != NBEDGES) {
792     Standard_TypeMismatch::Raise("Block has wrong number of edges");
793   }
794
795   for (Standard_Integer i = 2; i <= NBFACES; i++) {
796     TopoDS_Shape Ei1 = myEdges(edge_id(i, 1));
797     TopoDS_Shape Ei2 = myEdges(edge_id(i, 2));
798     const TopTools_ListOfShape& aFacesOfEi1 = MBE.FindFromKey(Ei1);
799     const TopTools_ListOfShape& aFacesOfEi2 = MBE.FindFromKey(Ei2);
800
801     Standard_Boolean isFound = Standard_False;
802     TopTools_ListIteratorOfListOfShape anIterEi1 (aFacesOfEi1);
803     for (; anIterEi1.More() && !isFound; anIterEi1.Next()) {
804
805       TopTools_ListIteratorOfListOfShape anIterEi2 (aFacesOfEi2);
806       for (; anIterEi2.More() && !isFound; anIterEi2.Next()) {
807
808         if (anIterEi1.Value().IsSame(anIterEi2.Value())) {
809           isFound = Standard_True;
810
811           // Store the face, defined by two edges
812           myFaces(i) = anIterEi1.Value();
813         }
814       }
815     }
816   }
817 }
818
819 //=======================================================================
820 //function : InitByTwoFaces
821 //purpose  :
822 //=======================================================================
823 void GEOMImpl_Block6Explorer::InitByTwoFaces (const TopoDS_Shape& theFace1,
824                                               const TopoDS_Shape& theFace2)
825 {
826   if (theFace1.IsSame(theFace2)) {
827     Standard_ConstructionError::Raise("The faces must be different");
828   }
829
830   // Add two given faces in the structure
831   myFaces(1) = theFace1;
832   myFaces(2) = theFace2;
833
834   // Step 1. Order vertices (and edges)
835
836   // 1.1. Ordered vertices and edges of the first face we put in <myVertices>
837
838   // Get wire of the first face
839   TopExp_Explorer wires1 (myFaces(1), TopAbs_WIRE);
840   if (!wires1.More()) {
841     Standard_ConstructionError::Raise("A face for the block has no wires");
842   }
843   TopoDS_Shape aWire1 = wires1.Current();
844   wires1.Next();
845   if (wires1.More()) {
846     Standard_ConstructionError::Raise("A face for the block has more than one wire");
847   }
848
849   BRepTools_WireExplorer aWE1 (TopoDS::Wire(aWire1), TopoDS::Face(myFaces(1)));
850   Standard_Integer nb;
851   for (nb = 1; aWE1.More(); aWE1.Next(), nb++) {
852     if (nb > 4) {
853       Standard_ConstructionError::Raise("A face for the block has more than four edges");
854     }
855     myEdges(edge_id(1, nb)) = aWE1.Current();
856     myVertices(vertex_id(1, nb)) = aWE1.CurrentVertex();
857   }
858   if (nb < 5) {
859     Standard_ConstructionError::Raise("A face for the block has less than four edges");
860   }
861
862   // 1.2. Ordered vertices and edges of the second face we temporarily store
863   // in arrays, to find for them rigth location in <myVertices> on the Step 2.
864
865   // declare arrays
866   TopTools_Array1OfShape aVertis2(1,4); // ordered vertices of the second face
867   TopTools_Array1OfShape anEdges2(1,4); // anEdges2(i) links aVertis2(i) and aVertis2(i+1)
868
869   // Get wire of the second face
870   TopExp_Explorer wires2 (myFaces(2), TopAbs_WIRE);
871   if (!wires2.More()) {
872     Standard_ConstructionError::Raise("A face for the block has no wires");
873   }
874   TopoDS_Shape aWire2 = wires2.Current();
875   wires2.Next();
876   if (wires2.More()) {
877     Standard_ConstructionError::Raise("A face for the block has more than one wire");
878   }
879
880   BRepTools_WireExplorer aWE2 (TopoDS::Wire(aWire2), TopoDS::Face(myFaces(2)));
881   for (nb = 1; aWE2.More(); aWE2.Next(), nb++) {
882     if (nb > 4) {
883       Standard_ConstructionError::Raise("A face for the block has more than four edges");
884     }
885     anEdges2(nb) = aWE2.Current();
886     aVertis2(nb) = aWE2.CurrentVertex();
887   }
888   if (nb < 5) {
889     Standard_ConstructionError::Raise("A face for the block has less than four edges");
890   }
891
892   // Step 2. Find right place in <myVertices> for the <aVertis2>,
893   //         so as to minimize common length of linking edges
894   //         between face 1 and face 2.
895   //         Each linking edge (of four) will link vertices of the
896   //         faces 1 and 2 with equal local numbers.
897   // The right place is defined by:
898   //  - vertex <aVertis2(i_min)>, which will become the first vertex
899   //         of the second face <myVertices(vertex_id(2,1))>
900   //  - orientation of <aVertis2> relatively their future location
901   //         in <myVertices> (s_min = 1 if direct, s_min = -1 if reversed)
902   Standard_Integer i_min = 0, s_min = 0;
903
904   TColgp_Array1OfPnt aPnts1 (1,4); // points of the first face
905   aPnts1(1) = BRep_Tool::Pnt(TopoDS::Vertex(myVertices(vertex_id(1, 1))));
906   aPnts1(2) = BRep_Tool::Pnt(TopoDS::Vertex(myVertices(vertex_id(1, 2))));
907   aPnts1(3) = BRep_Tool::Pnt(TopoDS::Vertex(myVertices(vertex_id(1, 3))));
908   aPnts1(4) = BRep_Tool::Pnt(TopoDS::Vertex(myVertices(vertex_id(1, 4))));
909
910   TColgp_Array1OfPnt aPnts2 (1,4); // points of the second face
911   aPnts2(1) = BRep_Tool::Pnt(TopoDS::Vertex(aVertis2(1)));
912   aPnts2(2) = BRep_Tool::Pnt(TopoDS::Vertex(aVertis2(2)));
913   aPnts2(3) = BRep_Tool::Pnt(TopoDS::Vertex(aVertis2(3)));
914   aPnts2(4) = BRep_Tool::Pnt(TopoDS::Vertex(aVertis2(4)));
915
916   Standard_Real Dist_min = RealLast();
917   // try all possible locations to find the best (with minimum sum distance)
918   for (Standard_Integer i = 1; i <= 4; i++) {
919     // try direct orientation
920     Standard_Real Dist_plus = aPnts1(1).Distance(aPnts2(i)) +
921                               aPnts1(2).Distance(aPnts2(mod4(i + 1))) +
922                               aPnts1(3).Distance(aPnts2(mod4(i + 2))) +
923                               aPnts1(4).Distance(aPnts2(mod4(i + 3)));
924     if (Dist_plus < Dist_min) {
925       Dist_min = Dist_plus;
926       i_min = i;
927       s_min = 1;
928     }
929
930     // try reversed orientation
931     Standard_Real Dist_minus = aPnts1(1).Distance(aPnts2(i)) +
932                                aPnts1(2).Distance(aPnts2(mod4(i - 1))) +
933                                aPnts1(3).Distance(aPnts2(mod4(i - 2))) +
934                                aPnts1(4).Distance(aPnts2(mod4(i - 3)));
935     if (Dist_minus < Dist_min) {
936       Dist_min = Dist_minus;
937       i_min = i;
938       s_min = - 1;
939     }
940   }
941
942   // 3. Put vertices and edges of the second face to they
943   //    permanent location in <myVertices> and <myEdges>
944   for (Standard_Integer i = 1; i <= 4; i++) {
945     Standard_Integer nb = mod4(i_min + s_min*(i - 1));
946
947     if (aPnts1(i).Distance(aPnts2(nb)) < Precision::Confusion()) {
948       Standard_ConstructionError::Raise("The faces are too close");
949     }
950
951     myVertices(vertex_id(2, i)) = aVertis2(nb);
952
953     if (s_min == -1) nb = mod4(nb - 1);
954     myEdges(edge_id(2, i)) = anEdges2(nb);
955   }
956
957   // 4. Generate side surface
958   if (!aWire1.Closed() || !aWire2.Closed()) {
959     // BRepOffsetAPI_ThruSections is not applicable on not closed wires
960     GetFace(3, Standard_True);
961     GetFace(4, Standard_True);
962     GetFace(5, Standard_True);
963     GetFace(6, Standard_True);
964   } else {
965     // try to build faces on native surfaces of edges or planar
966     Standard_Boolean tryThru = Standard_False;
967     for (Standard_Integer i = 3; i <= 6 && !tryThru; i++) {
968       Standard_Boolean doMake = Standard_True;
969       TopoDS_Shape E1 = GetEdge(edge_id(i, 1), doMake);
970       TopoDS_Shape E2 = GetEdge(edge_id(i, 2), doMake);
971       TopoDS_Shape E3 = GetEdge(edge_id(i, 3), doMake);
972       TopoDS_Shape E4 = GetEdge(edge_id(i, 4), doMake);
973
974       BRepBuilderAPI_MakeWire MW (TopoDS::Edge(E1),
975                                   TopoDS::Edge(E2),
976                                   TopoDS::Edge(E3),
977                                   TopoDS::Edge(E4));
978       if (!MW.IsDone()) {
979         Standard_ConstructionError::Raise("Wire construction failed");
980       }
981
982       BRepBuilderAPI_MakeFace MF (MW, Standard_False);
983       if (MF.IsDone()) {
984         myFaces(i) = MF.Shape();
985       } else {
986         tryThru = Standard_True;
987       }
988     }
989
990     // Build side surface by ThruSections algorithm
991     if (tryThru) {
992       BRepOffsetAPI_ThruSections THS;
993       THS.AddWire(TopoDS::Wire(aWire1));
994       THS.AddWire(TopoDS::Wire(aWire2));
995       THS.Build();
996       if (!THS.IsDone()) {
997         StdFail_NotDone::Raise("Side surface generation failed");
998       }
999       for (Standard_Integer i = 1; i <= 4; i++) {
1000         // fill face
1001         myFaces(i+2) = THS.GeneratedFace(myEdges(i));
1002
1003         // fill edge
1004         Standard_Integer ee = side_edge_id(i);
1005         TopTools_IndexedDataMapOfShapeListOfShape MVE;
1006         MapShapesAndAncestors(myFaces(i+2), TopAbs_VERTEX, TopAbs_EDGE, MVE);
1007         FindEdge(myEdges(ee),
1008                  myVertices(vertex_id_edge(ee, 1)),
1009                  myVertices(vertex_id_edge(ee, 2)),
1010                  MVE);
1011       }
1012     }
1013   }
1014 }
1015
1016 //=======================================================================
1017 //function : MapShapesAndAncestors
1018 //purpose  :
1019 //=======================================================================
1020 void GEOMImpl_Block6Explorer::MapShapesAndAncestors (const TopoDS_Shape& S,
1021                                                      const TopAbs_ShapeEnum TS,
1022                                                      const TopAbs_ShapeEnum TA,
1023                                                      TopTools_IndexedDataMapOfShapeListOfShape& M)
1024 {
1025   TopTools_ListOfShape empty;
1026   TopTools_MapOfShape mapA;
1027
1028   // visit ancestors
1029   TopExp_Explorer exa (S,TA);
1030   for (; exa.More(); exa.Next()) {
1031     // visit shapes
1032     const TopoDS_Shape& anc = exa.Current();
1033     if (mapA.Add(anc)) {
1034       TopExp_Explorer exs (anc,TS);
1035       TopTools_MapOfShape mapS;
1036       for (; exs.More(); exs.Next()) {
1037         if (mapS.Add(exs.Current())) {
1038           Standard_Integer index = M.FindIndex(exs.Current());
1039           if (index == 0) index = M.Add(exs.Current(),empty);
1040           M(index).Append(anc);
1041         }
1042       }
1043     }
1044   }
1045
1046   // visit shapes not under ancestors
1047   TopExp_Explorer ex (S,TS,TA);
1048   for (; ex.More(); ex.Next()) {
1049     Standard_Integer index = M.FindIndex(ex.Current());
1050     if (index == 0) index = M.Add(ex.Current(),empty);
1051   }
1052 }
1053
1054 //=======================================================================
1055 //function : IsSimilarEdges
1056 //purpose  :
1057 //=======================================================================
1058 Standard_Boolean GEOMImpl_Block6Explorer::IsSimilarEdges (const TopoDS_Shape& E1,
1059                                                           const TopoDS_Shape& E2)
1060 {
1061   TopoDS_Edge E1e = TopoDS::Edge(E1);
1062   TopoDS_Edge E2e = TopoDS::Edge(E2);
1063   TopoDS_Vertex V11, V12, V21, V22;
1064   TopExp::Vertices(E1e, V11, V12, Standard_True);
1065   TopExp::Vertices(E2e, V21, V22, Standard_True);
1066   if (BRepTools::Compare(V11, V21) && BRepTools::Compare(V12, V22))
1067     return Standard_True;
1068   if (BRepTools::Compare(V11, V22) && BRepTools::Compare(V12, V21))
1069     return Standard_True;
1070
1071   return Standard_False;
1072 }
1073
1074 //=======================================================================
1075 //function : FindEdge
1076 //purpose  :
1077 //=======================================================================
1078 Standard_Integer GEOMImpl_Block6Explorer::FindEdge
1079                    (TopoDS_Shape&       theResult,
1080                     const TopoDS_Shape& V1,
1081                     const TopoDS_Shape& V2,
1082                     const TopTools_IndexedDataMapOfShapeListOfShape& MVE,
1083                     const Standard_Boolean findAll)
1084 {
1085   Standard_Integer isFound = 0;
1086
1087   const TopTools_ListOfShape& anEdgesOfV1 = MVE.FindFromKey(V1);
1088   const TopTools_ListOfShape& anEdgesOfV2 = MVE.FindFromKey(V2);
1089
1090   TopTools_ListIteratorOfListOfShape it1 (anEdgesOfV1);
1091   for (; it1.More(); it1.Next()) {
1092     TopTools_ListIteratorOfListOfShape it2 (anEdgesOfV2);
1093     for (; it2.More(); it2.Next()) {
1094       if (it1.Value().IsSame(it2.Value())) {
1095         isFound++;
1096         theResult = it1.Value();
1097         if (!findAll) return isFound;
1098       }
1099     }
1100   }
1101
1102   return isFound;
1103 }
1104
1105 //=======================================================================
1106 //function : FindFace
1107 //purpose  :
1108 //=======================================================================
1109 Standard_Integer GEOMImpl_Block6Explorer::FindFace
1110                    (TopoDS_Shape&       theResult,
1111                     const TopoDS_Shape& V1,
1112                     const TopoDS_Shape& V2,
1113                     const TopoDS_Shape& V3,
1114                     const TopoDS_Shape& V4,
1115                     const TopTools_IndexedDataMapOfShapeListOfShape& MVF,
1116                     const Standard_Boolean findAll)
1117 {
1118   Standard_Integer isFound = Standard_False;
1119
1120   const TopTools_ListOfShape& aFacesOfV1 = MVF.FindFromKey(V1);
1121   const TopTools_ListOfShape& aFacesOfV2 = MVF.FindFromKey(V2);
1122   const TopTools_ListOfShape& aFacesOfV3 = MVF.FindFromKey(V3);
1123   const TopTools_ListOfShape& aFacesOfV4 = MVF.FindFromKey(V4);
1124
1125   TopTools_ListIteratorOfListOfShape it1 (aFacesOfV1);
1126   for (; it1.More(); it1.Next()) {
1127     TopTools_ListIteratorOfListOfShape it2 (aFacesOfV2);
1128     for (; it2.More(); it2.Next()) {
1129       if (it1.Value().IsSame(it2.Value())) {
1130         TopTools_ListIteratorOfListOfShape it3 (aFacesOfV3);
1131         for (; it3.More(); it3.Next()) {
1132           if (it1.Value().IsSame(it3.Value())) {
1133             TopTools_ListIteratorOfListOfShape it4 (aFacesOfV4);
1134             for (; it4.More(); it4.Next()) {
1135               if (it1.Value().IsSame(it4.Value())) {
1136                 isFound++;
1137                 theResult = it1.Value();
1138                 if (!findAll) return isFound;
1139               }
1140             }
1141           }
1142         }
1143       }
1144     }
1145   }
1146
1147   return isFound;
1148 }
1149
1150 //=======================================================================
1151 //function : MakeFace
1152 //purpose  :
1153 //=======================================================================
1154 void GEOMImpl_Block6Explorer::MakeFace (const TopoDS_Wire&     theWire,
1155                                         const Standard_Boolean isPlanarWanted,
1156                                         TopoDS_Shape&          theResult)
1157 {
1158   // try to build face on plane or on any surface under the edges of the wire
1159   BRepBuilderAPI_MakeFace MK (theWire, isPlanarWanted);
1160   if (MK.IsDone()) {
1161     theResult = MK.Shape();
1162     return;
1163   }
1164
1165   // try to construct filling surface
1166   if (!isPlanarWanted) {
1167     BRepOffsetAPI_MakeFilling MF;
1168
1169     Standard_Integer nbEdges = 0;
1170     BRepTools_WireExplorer aWE (theWire);
1171     for (; aWE.More(); aWE.Next(), nbEdges++) {
1172       MF.Add(TopoDS::Edge(aWE.Current()), GeomAbs_C0);
1173     }
1174
1175     MF.Build();
1176     if (MF.IsDone()) {
1177       // Result of filling
1178       TopoDS_Shape aFace = MF.Shape();
1179
1180       // Update tolerance
1181       Standard_Real aTol = MF.G0Error();
1182
1183       TColgp_Array1OfPnt aPnts (1,nbEdges); // points of the given wire
1184       aWE = BRepTools_WireExplorer(theWire);
1185       Standard_Integer vi = 1;
1186       for (; aWE.More() && vi <= nbEdges; aWE.Next(), vi++) {
1187         aPnts(vi) = BRep_Tool::Pnt(TopoDS::Vertex(aWE.CurrentVertex()));
1188       }
1189
1190       // Find maximum deviation in vertices
1191       TopExp_Explorer exp (aFace, TopAbs_VERTEX);
1192       TopTools_MapOfShape mapShape;
1193       for (; exp.More(); exp.Next()) {
1194         if (mapShape.Add(exp.Current())) {
1195           TopoDS_Vertex aV = TopoDS::Vertex(exp.Current());
1196           Standard_Real aTolV = BRep_Tool::Tolerance(aV);
1197           gp_Pnt aP = BRep_Tool::Pnt(aV);
1198           Standard_Real min_dist = aP.Distance(aPnts(1));
1199           for (vi = 2; vi <= nbEdges; vi++) {
1200             min_dist = Min(min_dist, aP.Distance(aPnts(vi)));
1201           }
1202           aTol = Max(aTol, aTolV);
1203           aTol = Max(aTol, min_dist);
1204         }
1205       }
1206
1207       if ((*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance() < aTol) {
1208         (*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance(aTol);
1209       }
1210       theResult = aFace;
1211     }
1212   }
1213 }