1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_Block6Explorer.hxx>
27 #include <ShHealOper_ShapeProcess.hxx>
29 #include "utilities.h"
31 #include <BRep_Tool.hxx>
32 #include <BRep_TFace.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepLib.hxx>
35 #include <BRepLib_FindSurface.hxx>
36 #include <BRepTools.hxx>
37 #include <BRepTools_WireExplorer.hxx>
38 #include <BRepOffsetAPI_ThruSections.hxx>
39 #include <BRepOffsetAPI_MakeFilling.hxx>
40 #include <BRepCheck_Analyzer.hxx>
41 #include <BRepBuilderAPI_Copy.hxx>
42 #include <BRepBuilderAPI_MakeEdge.hxx>
43 #include <BRepBuilderAPI_MakeWire.hxx>
44 #include <BRepBuilderAPI_MakeFace.hxx>
45 #include <BRepBuilderAPI_Transform.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Wire.hxx>
52 #include <TopoDS_Solid.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <TopTools_MapOfShape.hxx>
56 #include <TopTools_ListOfShape.hxx>
57 #include <TopTools_ListIteratorOfListOfShape.hxx>
58 #include <TopTools_IndexedMapOfShape.hxx>
59 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
61 #include <Geom_Curve.hxx>
62 #include <Geom_TrimmedCurve.hxx>
63 #include <GeomFill_Generator.hxx>
65 #include <gce_MakePln.hxx>
67 #include <Precision.hxx>
70 #include <TColgp_Array1OfPnt.hxx>
72 #include <StdFail_NotDone.hxx>
73 #include <Standard_NullObject.hxx>
74 #include <Standard_TypeMismatch.hxx>
75 #include <Standard_ConstructionError.hxx>
76 #include <Standard_NoSuchObject.hxx>
82 #define PLANAR_FACE_MAX_TOLERANCE 1e-06
84 // The following macro, when enabled, causes pcurves upgrade after MakeFilling algorithm
85 // in MakeAnyFace function;
86 // WARNING: it may lead to extra vertices generation by partition algorithm
87 // in some cases, for example when fillet is made on a PipeTShape -
88 // see issues 0021568 and 0021550
89 // VSR (15/05/2012): macro commented out (disabled) to avoid extra vertices!
90 //#define MAKE_FACE_UPGRADE_PCURVES
92 // The following macro, when enabled, causes fixing tolerance for pcurves
93 // after BRepBuilderAPI_MakeFace + ShHealOper_ShapeProcess in MakeAnyFace function;
94 // This sometimes allows to fix problems of extra vertices generation
96 // VSR (17/11/2014): macro enabled
97 #define MAKE_FACE_PCURVES_FIX_TOLERANCE
99 #ifdef MAKE_FACE_PCURVES_FIX_TOLERANCE
100 #include <BOPTools_AlgoTools.hxx>
101 #include <NCollection_DataMap.hxx>
102 #include <ShapeFix_ShapeTolerance.hxx>
105 static Standard_Integer mod4 (Standard_Integer nb)
107 if (nb <= 0) return nb + 4;
108 if (nb > 4) return nb - 4;
112 static Standard_Integer edge_id (const Standard_Integer theFaceID,
113 const Standard_Integer theEdgeNB)
115 static Standard_Integer edge_ids[NBFACES][4] = {
116 { 1, 2, 3, 4 }, // face 1
117 { 5, 6, 7, 8 }, // face 2
118 { 9, 5, 10, 1 }, // face 3
119 { 12, 7, 11, 3 }, // face 4
120 { 4, 12, 8, 9 }, // face 5
121 { 2, 11, 6, 10 } }; // face 6
123 return edge_ids[theFaceID - 1][theEdgeNB - 1];
126 static Standard_Integer side_edge_id (const Standard_Integer theEdgeNB)
128 static Standard_Integer side_edge_ids[4] = {9, 10, 11, 12};
130 return side_edge_ids[theEdgeNB - 1];
133 static Standard_Integer vertex_id (const Standard_Integer theFaceID,
134 const Standard_Integer theVertexNB)
136 static Standard_Integer vertex_ids[NBFACES][4] = {
137 { 1, 2, 3, 4 }, // face 1
138 { 5, 6, 7, 8 }, // face 2
139 { 1, 5, 6, 2 }, // face 3
140 { 4, 8, 7, 3 }, // face 4
141 { 1, 4, 8, 5 }, // face 5
142 { 2, 3, 7, 6 } }; // face 6
144 return vertex_ids[theFaceID - 1][theVertexNB - 1];
147 static Standard_Integer vertex_id_edge (const Standard_Integer theEdgeID, // [1,12]
148 const Standard_Integer theVertexNB) // [1,2]
150 static Standard_Integer vertex_ids_edge[NBEDGES][2] = {
164 return vertex_ids_edge[theEdgeID - 1][theVertexNB - 1];
167 static Standard_Integer face_id_edges (const Standard_Integer theEdge1ID, // [1,12]
168 const Standard_Integer theEdge2ID) // [1,12]
170 static Standard_Integer face_ids_edges[NBEDGES][NBEDGES] = {
171 // 1 2 3 4 5 6 7 8 9 10 11 12
172 { 0, 1, 1, 1, 3, 0, 0, 0, 3, 3, 0, 0 }, // edge 1
173 { 1, 0, 1, 1, 0, 6, 0, 0, 0, 6, 6, 0 }, // edge 2
174 { 1, 1, 0, 1, 0, 0, 4, 0, 0, 0, 4, 4 }, // edge 3
175 { 1, 1, 1, 0, 0, 0, 0, 5, 5, 0, 0, 5 }, // edge 4
176 { 3, 0, 0, 0, 0, 2, 2, 2, 3, 3, 0, 0 }, // edge 5
177 { 0, 6, 0, 0, 2, 0, 2, 2, 0, 6, 6, 0 }, // edge 6
178 { 0, 0, 4, 0, 2, 2, 0, 2, 0, 0, 4, 4 }, // edge 7
179 { 0, 0, 0, 5, 2, 2, 2, 0, 5, 0, 0, 5 }, // edge 8
180 { 3, 0, 0, 5, 3, 0, 0, 5, 0, 3, 0, 5 }, // edge 9
181 { 3, 6, 0, 0, 3, 6, 0, 0, 3, 0, 6, 0 }, // edge 10
182 { 0, 6, 4, 0, 0, 6, 4, 0, 0, 6, 0, 4 }, // edge 11
183 { 0, 0, 4, 5, 0, 0, 4, 5, 5, 0, 4, 0 } }; // edge 12
185 return face_ids_edges[theEdge1ID - 1][theEdge2ID - 1];
188 static Standard_Integer edge_id_vertices (const Standard_Integer theVertex1ID, // [1,8]
189 const Standard_Integer theVertex2ID) // [1,8]
191 static Standard_Integer edge_ids_vertices[NBVERTS][NBVERTS] = {
193 { 0, 1, 0, 4, 9, 0, 0, 0}, // vertex 1
194 { 1, 0, 2, 0, 0, 10, 0, 0}, // vertex 2
195 { 0, 2, 0, 3, 0, 0, 11, 0}, // vertex 3
196 { 4, 0, 3, 0, 0, 0, 0, 12}, // vertex 4
197 { 9, 0, 0, 0, 0, 5, 0, 8}, // vertex 5
198 { 0, 10, 0, 0, 5, 0, 6, 0}, // vertex 6
199 { 0, 0, 11, 0, 0, 6, 0, 7}, // vertex 7
200 { 0, 0, 0, 12, 8, 0, 7, 0} }; // vertex 8
202 return edge_ids_vertices[theVertex1ID - 1][theVertex2ID - 1];
205 static Standard_Integer edge_id_faces (const Standard_Integer theFace1ID, // [1,6]
206 const Standard_Integer theFace2ID) // [1,6]
208 static Standard_Integer edge_ids_faces[NBFACES][NBFACES] = {
210 { 0, 0, 1, 3, 4, 2 }, // face 1
211 { 0, 0, 5, 7, 8, 6 }, // face 2
212 { 1, 5, 0, 0, 9, 10 }, // face 3
213 { 3, 7, 0, 0, 12, 11 }, // face 4
214 { 4, 8, 9, 12, 0, 0 }, // face 5
215 { 2, 6, 10, 11, 0, 0 } }; // face 6
217 return edge_ids_faces[theFace1ID - 1][theFace2ID - 1];
220 //=======================================================================
221 //function : GEOMImpl_Block6Explorer
222 //purpose : Constructor
223 //=======================================================================
224 GEOMImpl_Block6Explorer::GEOMImpl_Block6Explorer ()
225 : myFaces(1,NBFACES), myEdges(1,NBEDGES), myVertices(1,NBVERTS)
229 //=======================================================================
230 //function : GetVertex
232 //=======================================================================
233 TopoDS_Shape GEOMImpl_Block6Explorer::GetVertex (const Standard_Integer theVertexID)
235 TopoDS_Shape aNullShape;
236 if (theVertexID < 1 || theVertexID > NBVERTS) return aNullShape;
237 return myVertices(theVertexID);
240 //=======================================================================
241 //function : GetVertexID
243 //=======================================================================
244 Standard_Integer GEOMImpl_Block6Explorer::GetVertexID (const TopoDS_Shape& theVertex)
246 for (Standard_Integer id = 1; id <= NBVERTS; id++) {
247 if (theVertex.IsSame(myVertices(id))) return id;
249 Standard_NoSuchObject::Raise("The Vertex does not belong to the Block");
253 //=======================================================================
254 //function : GetVertexID
256 //=======================================================================
257 Standard_Integer GEOMImpl_Block6Explorer::GetVertexID (const Standard_Integer theFaceID,
258 const Standard_Integer theVertexNB)
260 return vertex_id(theFaceID, theVertexNB);
263 //=======================================================================
264 //function : GetVertexOnEdgeID
266 //=======================================================================
267 Standard_Integer GEOMImpl_Block6Explorer::GetVertexOnEdgeID
268 (const Standard_Integer theEdgeID,
269 const Standard_Integer theVertexNB)
271 return vertex_id_edge(theEdgeID, theVertexNB);
274 //=======================================================================
277 //=======================================================================
278 TopoDS_Shape GEOMImpl_Block6Explorer::GetEdge (const Standard_Integer theEdgeID,
279 const Standard_Boolean doMake)
281 TopoDS_Shape aNullShape;
282 if (theEdgeID < 1 || theEdgeID > NBEDGES) return aNullShape;
283 if (myEdges(theEdgeID).IsNull() && doMake) {
284 // Create the required edge as a linear segment between
285 // corresponding vertices and put it in the Block's edges
286 BRepBuilderAPI_MakeEdge ME (TopoDS::Vertex(myVertices(vertex_id_edge(theEdgeID, 1))),
287 TopoDS::Vertex(myVertices(vertex_id_edge(theEdgeID, 2))));
289 Standard_ConstructionError::Raise("Edge construction failed");
291 myEdges(theEdgeID) = ME.Shape();
294 return myEdges(theEdgeID);
297 //=======================================================================
298 //function : GetEdgeID
300 //=======================================================================
301 Standard_Integer GEOMImpl_Block6Explorer::GetEdgeID (const TopoDS_Shape& theEdge)
303 for (Standard_Integer id = 1; id <= NBEDGES; id++) {
304 if (theEdge.IsSame(myEdges(id))) return id;
306 Standard_NoSuchObject::Raise("The Edge does not belong to the Block");
310 //=======================================================================
311 //function : GetEdgeID
313 //=======================================================================
314 Standard_Integer GEOMImpl_Block6Explorer::GetEdgeID (const Standard_Integer theFaceID,
315 const Standard_Integer theEdgeNB)
317 return edge_id(theFaceID, theEdgeNB);
320 //=======================================================================
321 //function : FindEdgeID
323 //=======================================================================
324 Standard_Integer GEOMImpl_Block6Explorer::FindEdgeID (const Standard_Integer theVertex1ID,
325 const Standard_Integer theVertex2ID)
327 return edge_id_vertices(theVertex1ID, theVertex2ID);
330 //=======================================================================
331 //function : FindCommonEdgeID
333 //=======================================================================
334 Standard_Integer GEOMImpl_Block6Explorer::FindCommonEdgeID
335 (const Standard_Integer theFace1ID,
336 const Standard_Integer theFace2ID)
338 return edge_id_faces(theFace1ID, theFace2ID);
341 //=======================================================================
344 //=======================================================================
345 TopoDS_Shape GEOMImpl_Block6Explorer::GetFace (const Standard_Integer theFaceID,
346 const Standard_Boolean doMake)
348 TopoDS_Shape aNullShape;
349 if (theFaceID < 1 || theFaceID > NBFACES) return aNullShape;
351 if (myFaces(theFaceID).IsNull() && doMake) {
353 // Create the required face between
354 // corresponding edges and put it in the Block's faces
356 TopoDS_Shape E1 = GetEdge(edge_id(theFaceID, 1), doMake);
357 TopoDS_Shape E2 = GetEdge(edge_id(theFaceID, 2), doMake);
358 TopoDS_Shape E3 = GetEdge(edge_id(theFaceID, 3), doMake);
359 TopoDS_Shape E4 = GetEdge(edge_id(theFaceID, 4), doMake);
361 BRepBuilderAPI_MakeWire MW (TopoDS::Edge(E1),
366 Standard_ConstructionError::Raise("Wire construction failed");
369 MakeFace(MW, Standard_False, aFace);
370 if (aFace.IsNull()) {
371 Standard_ConstructionError::Raise("Face construction failed");
373 myFaces(theFaceID) = aFace;
376 return myFaces(theFaceID);
379 //=======================================================================
380 //function : GetFaceID
382 //=======================================================================
383 Standard_Integer GEOMImpl_Block6Explorer::GetFaceID (const TopoDS_Shape& theFace)
385 for (Standard_Integer id = 1; id <= NBFACES; id++) {
386 if (theFace.IsSame(myFaces(id))) return id;
388 Standard_NoSuchObject::Raise("The Face does not belong to the Block");
392 //=======================================================================
393 //function : FindFaceID
395 //=======================================================================
396 Standard_Integer GEOMImpl_Block6Explorer::FindFaceID (const Standard_Integer theEdge1ID,
397 const Standard_Integer theEdge2ID)
399 return face_id_edges(theEdge1ID, theEdge2ID);
402 //=======================================================================
403 //function : GetOppositeFaceID
405 //=======================================================================
406 Standard_Integer GEOMImpl_Block6Explorer::GetOppositeFaceID (const Standard_Integer theFaceID)
408 Standard_Integer opp_face_id[NBFACES + 1] = {
417 return opp_face_id[theFaceID];
420 //=======================================================================
421 //function : IsSimilarFaces
423 //=======================================================================
424 Standard_Boolean GEOMImpl_Block6Explorer::IsSimilarFaces (const Standard_Integer theFace1ID,
425 const Standard_Integer theFace2ID,
426 const gp_Trsf theTransformation)
428 Standard_Integer common_edge_id = FindCommonEdgeID(theFace1ID, theFace2ID);
430 if (common_edge_id == 0) { // opposite faces
431 for (Standard_Integer id = 1; id <= 4; id++) {
432 TopoDS_Shape E1 = GetEdge(edge_id(theFace1ID, id));
433 TopoDS_Shape E2 = GetEdge(edge_id(theFace2ID, id));
435 BRepBuilderAPI_Transform aTrsf (E1, theTransformation, Standard_False);
436 if (!IsSimilarEdges(aTrsf.Shape(), E2))
437 return Standard_False;
439 } else { // the faces have common edge
440 TopTools_Array1OfShape aVerts1 (1,4);
441 TopTools_Array1OfShape aVerts2 (1,4);
443 Standard_Integer common_vertex1 = GetVertexOnEdgeID(common_edge_id, 1);
444 Standard_Integer common_vertex2 = GetVertexOnEdgeID(common_edge_id, 2);
445 aVerts1(1) = myVertices(common_vertex1);
446 aVerts1(2) = myVertices(common_vertex2);
447 aVerts2(1) = myVertices(common_vertex1);
448 aVerts2(2) = myVertices(common_vertex2);
450 Standard_Integer not_common_v11 = 0, not_common_v12 = 0;
451 Standard_Integer vnb, vid;
452 for (vnb = 1; vnb <= 4; vnb++) {
453 vid = GetVertexID(theFace1ID, vnb);
454 if (vid != common_vertex1 && FindEdgeID(vid, common_vertex1) == 0) {
455 not_common_v12 = vid;
457 if (vid != common_vertex2 && FindEdgeID(vid, common_vertex2) == 0) {
458 not_common_v11 = vid;
463 Standard_Integer not_common_v21 = 0, not_common_v22 = 0;
464 for (vnb = 1; vnb <= 4; vnb++) {
465 vid = GetVertexID(theFace2ID, vnb);
466 if (vid != common_vertex1 && FindEdgeID(vid, common_vertex1) == 0) {
467 not_common_v22 = vid;
469 if (vid != common_vertex2 && FindEdgeID(vid, common_vertex2) == 0) {
470 not_common_v21 = vid;
474 aVerts1(3) = myVertices(not_common_v11);
475 aVerts1(4) = myVertices(not_common_v12);
476 aVerts2(3) = myVertices(not_common_v21);
477 aVerts2(4) = myVertices(not_common_v22);
479 for (Standard_Integer id = 1; id <= 4; id++) {
480 BRepBuilderAPI_Transform aTrsf (aVerts1(id), theTransformation, Standard_False);
481 TopoDS_Vertex V1 = TopoDS::Vertex(aTrsf.Shape());
482 TopoDS_Vertex V2 = TopoDS::Vertex(aVerts2(id));
483 if (!BRepTools::Compare(V1, V2)) {
484 return Standard_False;
489 return Standard_True;
492 //============ Initialization methods ===================================
494 //=======================================================================
495 //function : InitByBlock
497 //=======================================================================
498 void GEOMImpl_Block6Explorer::InitByBlock (const TopoDS_Shape& theBlock)
500 // 1. Find any one face of the block
501 TopExp_Explorer faces (theBlock, TopAbs_FACE);
503 Standard_ConstructionError::Raise("The block has no faces");
505 TopoDS_Shape aFirstFace = faces.Current();
507 // 2. Store all elements of the block relatively aFirstFace
508 InitByBlockAndFace(theBlock, aFirstFace);
511 //=======================================================================
512 //function : InitByBlockAndFace
514 //=======================================================================
515 void GEOMImpl_Block6Explorer::InitByBlockAndFace (const TopoDS_Shape& theBlock,
516 const TopoDS_Shape& theFace)
518 myFaces(1) = theFace;
520 // 2. Get wire of the first face
521 TopExp_Explorer wires (myFaces(1), TopAbs_WIRE);
523 Standard_ConstructionError::Raise("A face of the block has no wires");
525 TopoDS_Shape aWire = wires.Current();
528 Standard_ConstructionError::Raise("A face of the block has more than one wires");
531 // 3. Explore wire to init edges and vertices of the first face
532 BRepTools_WireExplorer aWE (TopoDS::Wire(aWire), TopoDS::Face(myFaces(1)));
533 Standard_Integer nb = 1;
534 for (; aWE.More(); aWE.Next(), nb++) {
536 Standard_ConstructionError::Raise("A face of the block has more than four edges");
538 myEdges(edge_id(1, nb)) = aWE.Current();
539 myVertices(vertex_id(1, nb)) = aWE.CurrentVertex();
542 Standard_ConstructionError::Raise("A face of the block has less than four edges");
545 // 2. Store all other elements of the block
546 InitByBlockAndVertices (theBlock,
547 myVertices(vertex_id(1,1)),
548 myVertices(vertex_id(1,2)),
549 myVertices(vertex_id(1,3)));
552 //=======================================================================
553 //function : InitByBlockAndEdges
555 //=======================================================================
556 void GEOMImpl_Block6Explorer::InitByBlockAndEdges (const TopoDS_Shape& theBlock,
557 const TopoDS_Shape& theEdge1,
558 const TopoDS_Shape& theEdge3)
560 // 1. Store vertices and edges of the first face
562 // 1.1. Store two given edges
563 myEdges(edge_id(1, 1)) = theEdge1;
564 myEdges(edge_id(1, 3)) = theEdge3;
566 // 1.2. Find and store the first face
567 TopTools_IndexedDataMapOfShapeListOfShape MEF;
568 MapShapesAndAncestors(theBlock, TopAbs_EDGE, TopAbs_FACE, MEF);
569 if (MEF.Extent() != NBEDGES) {
570 Standard_TypeMismatch::Raise("Block has wrong number of edges");
572 const TopTools_ListOfShape& aFacesOfE1 = MEF.FindFromKey(theEdge1);
573 const TopTools_ListOfShape& aFacesOfE3 = MEF.FindFromKey(theEdge3);
575 Standard_Boolean isFound = Standard_False;
576 TopTools_ListIteratorOfListOfShape anIterF1 (aFacesOfE1);
577 for (; anIterF1.More() && !isFound; anIterF1.Next()) {
579 TopTools_ListIteratorOfListOfShape anIterF3 (aFacesOfE3);
580 for (; anIterF3.More() && !isFound; anIterF3.Next()) {
582 if (anIterF1.Value().IsSame(anIterF3.Value())) {
583 isFound = Standard_True;
585 // Store the face, defined by two opposite edges
586 myFaces(1) = anIterF1.Value();
591 Standard_ConstructionError::Raise
592 ("Edges 1 and 2 do not belong to one face of the block");
595 // 1.3. Make vertices of the first edge the first and the
596 // second vertices of the first face. Order is free.
597 TopoDS_Edge E = TopoDS::Edge(theEdge1);
598 TopoDS_Vertex V1, V2;
599 TopExp::Vertices(E, V1, V2, Standard_True);
600 myVertices(vertex_id(1,1)) = V1;
601 myVertices(vertex_id(1,2)) = V2;
603 // Init maps vertex->list_of_edges for the face
604 TopTools_IndexedDataMapOfShapeListOfShape M1;
605 MapShapesAndAncestors(myFaces(1), TopAbs_VERTEX, TopAbs_EDGE, M1);
606 if (M1.Extent() != 4) {
607 Standard_TypeMismatch::Raise("The first face of block has wrong number of vertices");
610 // 1.4. Find and store others elements of the first face
612 // edges of the first vertex
613 TopoDS_Shape E1_f = M1.FindFromKey(V1).First();
614 TopoDS_Shape E1_l = M1.FindFromKey(V1).Last();
616 if (E1_f.IsSame(theEdge1)) {
617 myEdges(edge_id(1, 4)) = E1_l;
619 myEdges(edge_id(1, 4)) = E1_f;
623 TopoDS_Edge E4 = TopoDS::Edge(myEdges(edge_id(1, 4)));
624 TopoDS_Vertex V41, V42;
625 TopExp::Vertices(E4, V41, V42, Standard_True);
626 if (V41.IsSame(V1)) {
627 myVertices(vertex_id(1,4)) = V42;
629 myVertices(vertex_id(1,4)) = V41;
632 // edges of the second vertex
633 TopoDS_Shape E2_f = M1.FindFromKey(V2).First();
634 TopoDS_Shape E2_l = M1.FindFromKey(V2).Last();
636 if (E2_f.IsSame(theEdge1)) {
637 myEdges(edge_id(1, 2)) = E2_l;
639 myEdges(edge_id(1, 2)) = E2_f;
643 TopoDS_Edge E2 = TopoDS::Edge(myEdges(edge_id(1, 2)));
644 TopoDS_Vertex V21, V22;
645 TopExp::Vertices(E2, V21, V22, Standard_True);
646 if (V21.IsSame(V2)) {
647 myVertices(vertex_id(1,3)) = V22;
649 myVertices(vertex_id(1,3)) = V21;
652 // 2. Store all other elements of the block
653 InitByBlockAndVertices (theBlock,
654 myVertices(vertex_id(1,1)),
655 myVertices(vertex_id(1,2)),
656 myVertices(vertex_id(1,3)));
659 //=======================================================================
660 //function : InitByBlockAndVertices
662 //=======================================================================
663 void GEOMImpl_Block6Explorer::InitByBlockAndVertices (const TopoDS_Shape& theBlock,
664 const TopoDS_Shape& theVertex1,
665 const TopoDS_Shape& theVertex2,
666 const TopoDS_Shape& theVertex3)
668 // Here we suppose, that vertices are ordered, i.e. exists edge between
669 // theVertex1 and theVertex2 and edge between theVertex2 and theVertex3
671 // 1. Store vertices and edges of the first face.
672 // If the first face is initialized, it means, that this
673 // method is called from another initialization method, and all
674 // vertices and edges of the first face are also initialized
675 if (myFaces(1).IsNull()) {
677 // 1.1. Store first three vertices
678 myVertices(vertex_id(1, 1)) = theVertex1;
679 myVertices(vertex_id(1, 2)) = theVertex2;
680 myVertices(vertex_id(1, 3)) = theVertex3;
682 // 1.2. Find and store the first face
683 TopTools_IndexedDataMapOfShapeListOfShape MVF;
684 MapShapesAndAncestors(theBlock, TopAbs_VERTEX, TopAbs_FACE, MVF);
685 if (MVF.Extent() != NBVERTS) {
686 Standard_TypeMismatch::Raise("Block has wrong number of vertices");
688 const TopTools_ListOfShape& aFacesOfV1 = MVF.FindFromKey(theVertex1);
689 const TopTools_ListOfShape& aFacesOfV3 = MVF.FindFromKey(theVertex3);
691 Standard_Boolean isFound = Standard_False;
692 TopTools_ListIteratorOfListOfShape anIterF1 (aFacesOfV1);
693 for (; anIterF1.More() && !isFound; anIterF1.Next()) {
695 TopTools_ListIteratorOfListOfShape anIterF3 (aFacesOfV3);
696 for (; anIterF3.More() && !isFound; anIterF3.Next()) {
698 if (anIterF1.Value().IsSame(anIterF3.Value())) {
699 isFound = Standard_True;
701 // Store the face, defined by two opposite vertices
702 myFaces(1) = anIterF1.Value();
707 Standard_ConstructionError::Raise
708 ("Vertices 1 and 3 do not belong to one face of the block");
711 // Init maps vertex->list_of_edges for the face
712 TopTools_IndexedDataMapOfShapeListOfShape M1;
713 MapShapesAndAncestors(myFaces(1), TopAbs_VERTEX, TopAbs_EDGE, M1);
714 if (M1.Extent() != 4) {
715 Standard_TypeMismatch::Raise("The first face of block has wrong number of vertices");
718 // 1.3. Find and store edges and last vertex of the first face
719 const TopTools_ListOfShape& anEdgesOfV1 = M1.FindFromKey(theVertex1);
720 const TopTools_ListOfShape& anEdgesOfV2 = M1.FindFromKey(theVertex2);
721 const TopTools_ListOfShape& anEdgesOfV3 = M1.FindFromKey(theVertex3);
723 TopTools_ListIteratorOfListOfShape anIterE2 (anEdgesOfV2);
724 for (; anIterE2.More(); anIterE2.Next()) {
726 TopTools_ListIteratorOfListOfShape anIterE1 (anEdgesOfV1);
727 for (; anIterE1.More(); anIterE1.Next()) {
729 if (anIterE1.Value().IsSame(anIterE2.Value())) {
730 // Store the first edge, defined by two vertices
731 myEdges(edge_id(1,1)) = anIterE1.Value();
734 // Store the last edge
735 myEdges(edge_id(1,4)) = anIterE1.Value();
737 // Find and store the last vertex
738 TopoDS_Edge E = TopoDS::Edge(myEdges(4));
739 TopoDS_Vertex V1, V2;
740 TopExp::Vertices(E, V1, V2, Standard_True);
742 if (V1.IsSame(theVertex1)) {
743 myVertices(vertex_id(1,4)) = V2;
745 myVertices(vertex_id(1,4)) = V1;
750 TopTools_ListIteratorOfListOfShape anIterE3 (anEdgesOfV3);
751 for (; anIterE3.More(); anIterE3.Next()) {
753 if (anIterE3.Value().IsSame(anIterE2.Value())) {
754 // Store the second edge, defined by two vertices
755 myEdges(edge_id(1,2)) = anIterE3.Value();
758 // Store the third edge
759 myEdges(edge_id(1,3)) = anIterE3.Value();
765 // Init map vertex->list_of_edges for the block
766 TopTools_IndexedDataMapOfShapeListOfShape MB;
767 MapShapesAndAncestors(theBlock, TopAbs_VERTEX, TopAbs_EDGE, MB);
768 if (MB.Extent() != NBVERTS) {
769 Standard_TypeMismatch::Raise("Block has wrong number of vertices");
772 // 2. Store edges, linking the first face with the second one
773 // and vertices of the second face
774 TopTools_IndexedMapOfShape aFaceEdges;
775 TopExp::MapShapes(myFaces(1), TopAbs_EDGE, aFaceEdges);
777 Standard_Integer i = 1;
778 for (; i <= 4; i++) {
779 // Get i-th vertex of the face 1
780 TopoDS_Shape Vi = myVertices(vertex_id(1, i));
781 if (!MB.Contains(Vi)) {
782 Standard_ConstructionError::Raise("Face does not belong to the block");
785 // Get list of block's edges, sharing this Vertex
786 const TopTools_ListOfShape& anEdgesOfVi = MB.FindFromKey(Vi);
787 TopTools_ListIteratorOfListOfShape anEdgesIter (anEdgesOfVi);
789 // Get Edge (from the List), not belonging to the face 1
790 Standard_Boolean isFound = Standard_False;
791 for (; anEdgesIter.More() && !isFound; anEdgesIter.Next()) {
792 if (!aFaceEdges.Contains(anEdgesIter.Value())) {
793 isFound = Standard_True;
795 // Store the linking edge
796 TopoDS_Shape aLinkEdge = anEdgesIter.Value();
797 myEdges(side_edge_id(i)) = aLinkEdge;
799 // Get another vertex of the linking edge
800 TopoDS_Edge E = TopoDS::Edge(aLinkEdge);
801 TopoDS_Vertex V1, V2;
802 TopExp::Vertices(E, V1, V2, Standard_True);
804 // Store the i-th vertex of the second (opposite to the first) face
806 myVertices(vertex_id(2, i)) = V2;
808 myVertices(vertex_id(2, i)) = V1;
814 // 3. Store edges of the second (opposite to the first) face
815 for (i = 1; i <= 4; i++) {
816 // Get i-th and (i+1)-th vertices of the face 2
817 TopoDS_Shape Vi = myVertices(vertex_id(2, i));
818 TopoDS_Shape Vj = myVertices(vertex_id(2, mod4(i + 1)));
820 // Get list of block's edges, sharing Vi
821 const TopTools_ListOfShape& anEdgesOfVi = MB.FindFromKey(Vi);
822 // Get list of block's edges, sharing Vj
823 const TopTools_ListOfShape& anEdgesOfVj = MB.FindFromKey(Vj);
825 // Get Edge (from the List), linking this vertex with the next one
826 Standard_Boolean isFound = Standard_False;
827 TopTools_ListIteratorOfListOfShape anEdgesIteri (anEdgesOfVi);
828 for (; anEdgesIteri.More() && !isFound; anEdgesIteri.Next()) {
830 TopTools_ListIteratorOfListOfShape anEdgesIterj (anEdgesOfVj);
831 for (; anEdgesIterj.More() && !isFound; anEdgesIterj.Next()) {
833 if (anEdgesIteri.Value().IsSame(anEdgesIterj.Value())) {
834 isFound = Standard_True;
836 // Store the linking edge
837 myEdges(edge_id(2, i)) = anEdgesIteri.Value();
843 // 4. Store faces of the block
844 TopTools_IndexedDataMapOfShapeListOfShape MBE;
845 MapShapesAndAncestors(theBlock, TopAbs_EDGE, TopAbs_FACE, MBE);
846 if (MBE.Extent() != NBEDGES) {
847 Standard_TypeMismatch::Raise("Block has wrong number of edges");
850 for (i = 2; i <= NBFACES; i++) {
851 TopoDS_Shape Ei1 = myEdges(edge_id(i, 1));
852 TopoDS_Shape Ei2 = myEdges(edge_id(i, 2));
853 const TopTools_ListOfShape& aFacesOfEi1 = MBE.FindFromKey(Ei1);
854 const TopTools_ListOfShape& aFacesOfEi2 = MBE.FindFromKey(Ei2);
856 Standard_Boolean isFound = Standard_False;
857 TopTools_ListIteratorOfListOfShape anIterEi1 (aFacesOfEi1);
858 for (; anIterEi1.More() && !isFound; anIterEi1.Next()) {
860 TopTools_ListIteratorOfListOfShape anIterEi2 (aFacesOfEi2);
861 for (; anIterEi2.More() && !isFound; anIterEi2.Next()) {
863 if (anIterEi1.Value().IsSame(anIterEi2.Value())) {
864 isFound = Standard_True;
866 // Store the face, defined by two edges
867 myFaces(i) = anIterEi1.Value();
874 //=======================================================================
875 //function : InitByTwoFaces
877 //=======================================================================
878 void GEOMImpl_Block6Explorer::InitByTwoFaces (const TopoDS_Shape& theFace1,
879 const TopoDS_Shape& theFace2)
881 if (theFace1.IsSame(theFace2)) {
882 Standard_ConstructionError::Raise("The faces must be different");
885 // Add two given faces in the structure
886 myFaces(1) = theFace1;
887 myFaces(2) = theFace2;
889 // Step 1. Order vertices (and edges)
891 // 1.1. Ordered vertices and edges of the first face we put in <myVertices>
893 // Get wire of the first face
894 TopExp_Explorer wires1 (myFaces(1), TopAbs_WIRE);
895 if (!wires1.More()) {
896 Standard_ConstructionError::Raise("A face for the block has no wires");
898 TopoDS_Shape aWire1 = wires1.Current();
901 Standard_ConstructionError::Raise("A face for the block has more than one wire");
904 BRepTools_WireExplorer aWE1 (TopoDS::Wire(aWire1), TopoDS::Face(myFaces(1)));
906 for (nb = 1; aWE1.More(); aWE1.Next(), nb++) {
908 Standard_ConstructionError::Raise("A face for the block has more than four edges");
910 myEdges(edge_id(1, nb)) = aWE1.Current();
911 myVertices(vertex_id(1, nb)) = aWE1.CurrentVertex();
914 Standard_ConstructionError::Raise("A face for the block has less than four edges");
917 // 1.2. Ordered vertices and edges of the second face we temporarily store
918 // in arrays, to find for them the right location in <myVertices> in Step 2.
921 TopTools_Array1OfShape aVertis2(1,4); // ordered vertices of the second face
922 TopTools_Array1OfShape anEdges2(1,4); // anEdges2(i) links aVertis2(i) and aVertis2(i+1)
924 // Get wire of the second face
925 TopExp_Explorer wires2 (myFaces(2), TopAbs_WIRE);
926 if (!wires2.More()) {
927 Standard_ConstructionError::Raise("A face for the block has no wires");
929 TopoDS_Shape aWire2 = wires2.Current();
932 Standard_ConstructionError::Raise("A face for the block has more than one wire");
935 BRepTools_WireExplorer aWE2 (TopoDS::Wire(aWire2), TopoDS::Face(myFaces(2)));
936 for (nb = 1; aWE2.More(); aWE2.Next(), nb++) {
938 Standard_ConstructionError::Raise("A face for the block has more than four edges");
940 anEdges2(nb) = aWE2.Current();
941 aVertis2(nb) = aWE2.CurrentVertex();
944 Standard_ConstructionError::Raise("A face for the block has less than four edges");
947 // Step 2. Find right place in <myVertices> for the <aVertis2>,
948 // so as to minimize common length of linking edges
949 // between face 1 and face 2.
950 // Each linking edge (of four) will link vertices of the
951 // faces 1 and 2 with equal local numbers.
952 // The right place is defined by:
953 // - vertex <aVertis2(i_min)>, which will become the first vertex
954 // of the second face <myVertices(vertex_id(2,1))>
955 // - orientation of <aVertis2> relatively their future location
956 // in <myVertices> (s_min = 1 if direct, s_min = -1 if reversed)
957 Standard_Integer i_min = 0, s_min = 0;
959 TColgp_Array1OfPnt aPnts1 (1,4); // points of the first face
960 aPnts1(1) = BRep_Tool::Pnt(TopoDS::Vertex(myVertices(vertex_id(1, 1))));
961 aPnts1(2) = BRep_Tool::Pnt(TopoDS::Vertex(myVertices(vertex_id(1, 2))));
962 aPnts1(3) = BRep_Tool::Pnt(TopoDS::Vertex(myVertices(vertex_id(1, 3))));
963 aPnts1(4) = BRep_Tool::Pnt(TopoDS::Vertex(myVertices(vertex_id(1, 4))));
965 TColgp_Array1OfPnt aPnts2 (1,4); // points of the second face
966 aPnts2(1) = BRep_Tool::Pnt(TopoDS::Vertex(aVertis2(1)));
967 aPnts2(2) = BRep_Tool::Pnt(TopoDS::Vertex(aVertis2(2)));
968 aPnts2(3) = BRep_Tool::Pnt(TopoDS::Vertex(aVertis2(3)));
969 aPnts2(4) = BRep_Tool::Pnt(TopoDS::Vertex(aVertis2(4)));
971 Standard_Real Dist_min = RealLast();
972 // try all possible locations to find the best (with minimum sum distance)
973 Standard_Integer i = 1;
974 for (; i <= 4; i++) {
975 // try direct orientation
976 Standard_Real Dist_plus = aPnts1(1).Distance(aPnts2(i)) +
977 aPnts1(2).Distance(aPnts2(mod4(i + 1))) +
978 aPnts1(3).Distance(aPnts2(mod4(i + 2))) +
979 aPnts1(4).Distance(aPnts2(mod4(i + 3)));
980 if (Dist_plus < Dist_min) {
981 Dist_min = Dist_plus;
986 // try reversed orientation
987 Standard_Real Dist_minus = aPnts1(1).Distance(aPnts2(i)) +
988 aPnts1(2).Distance(aPnts2(mod4(i - 1))) +
989 aPnts1(3).Distance(aPnts2(mod4(i - 2))) +
990 aPnts1(4).Distance(aPnts2(mod4(i - 3)));
991 if (Dist_minus < Dist_min) {
992 Dist_min = Dist_minus;
998 // 3. Put vertices and edges of the second face to they
999 // permanent location in <myVertices> and <myEdges>
1000 for (i = 1; i <= 4; i++) {
1001 Standard_Integer nb = mod4(i_min + s_min*(i - 1));
1003 if (aPnts1(i).Distance(aPnts2(nb)) < Precision::Confusion()) {
1004 Standard_ConstructionError::Raise("The faces are too close");
1007 myVertices(vertex_id(2, i)) = aVertis2(nb);
1009 if (s_min == -1) nb = mod4(nb - 1);
1010 myEdges(edge_id(2, i)) = anEdges2(nb);
1013 // check the wires closure
1014 TopoDS_Wire wire1 = TopoDS::Wire(aWire1);
1015 TopoDS_Wire wire2 = TopoDS::Wire(aWire2);
1016 TopoDS_Vertex aV1, aV2;
1018 TopExp::Vertices(wire1, aV1, aV2);
1019 if (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2))
1020 aWire1.Closed(true);
1022 TopExp::Vertices(wire2, aV1, aV2);
1023 if (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2))
1024 aWire2.Closed(true);
1026 // 4. Generate side surface
1027 if (!aWire1.Closed() || !aWire2.Closed()) {
1028 // BRepOffsetAPI_ThruSections is not applicable on not closed wires
1029 GetFace(3, Standard_True);
1030 GetFace(4, Standard_True);
1031 GetFace(5, Standard_True);
1032 GetFace(6, Standard_True);
1034 // try to build faces on native surfaces of edges or planar
1035 Standard_Boolean tryThru = Standard_False;
1036 for (Standard_Integer i = 3; i <= 6 && !tryThru; i++) {
1037 Standard_Boolean doMake = Standard_True;
1038 TopoDS_Shape E1 = GetEdge(edge_id(i, 1), doMake);
1039 TopoDS_Shape E2 = GetEdge(edge_id(i, 2), doMake);
1040 TopoDS_Shape E3 = GetEdge(edge_id(i, 3), doMake);
1041 TopoDS_Shape E4 = GetEdge(edge_id(i, 4), doMake);
1043 BRepBuilderAPI_MakeWire MW (TopoDS::Edge(E1),
1048 Standard_ConstructionError::Raise("Wire construction failed");
1051 BRepBuilderAPI_MakeFace MF (MW, Standard_False);
1053 myFaces(i) = MF.Shape();
1055 tryThru = Standard_True;
1059 // Build side surface by ThruSections algorithm
1061 BRepOffsetAPI_ThruSections THS;
1062 THS.AddWire(TopoDS::Wire(aWire1));
1063 THS.AddWire(TopoDS::Wire(aWire2));
1065 if (!THS.IsDone()) {
1066 StdFail_NotDone::Raise("Side surface generation failed");
1068 for (Standard_Integer i = 1; i <= 4; i++) {
1070 myFaces(i+2) = THS.GeneratedFace(myEdges(i));
1073 Standard_Integer ee = side_edge_id(i);
1074 TopTools_IndexedDataMapOfShapeListOfShape MVE;
1075 MapShapesAndAncestors(myFaces(i+2), TopAbs_VERTEX, TopAbs_EDGE, MVE);
1076 FindEdge(myEdges(ee),
1077 myVertices(vertex_id_edge(ee, 1)),
1078 myVertices(vertex_id_edge(ee, 2)),
1085 //=======================================================================
1086 //function : MapShapesAndAncestors
1088 //=======================================================================
1089 void GEOMImpl_Block6Explorer::MapShapesAndAncestors (const TopoDS_Shape& S,
1090 const TopAbs_ShapeEnum TS,
1091 const TopAbs_ShapeEnum TA,
1092 TopTools_IndexedDataMapOfShapeListOfShape& M)
1094 TopTools_ListOfShape empty;
1095 TopTools_MapOfShape mapA;
1098 TopExp_Explorer exa (S,TA);
1099 for (; exa.More(); exa.Next()) {
1101 const TopoDS_Shape& anc = exa.Current();
1102 if (mapA.Add(anc)) {
1103 TopExp_Explorer exs (anc,TS);
1104 TopTools_MapOfShape mapS;
1105 for (; exs.More(); exs.Next()) {
1106 if (mapS.Add(exs.Current())) {
1107 Standard_Integer index = M.FindIndex(exs.Current());
1108 if (index == 0) index = M.Add(exs.Current(),empty);
1109 M(index).Append(anc);
1115 // visit shapes not under ancestors
1116 TopExp_Explorer ex (S,TS,TA);
1117 for (; ex.More(); ex.Next()) {
1118 Standard_Integer index = M.FindIndex(ex.Current());
1119 if (index == 0) index = M.Add(ex.Current(),empty);
1123 //=======================================================================
1124 //function : IsSimilarEdges
1126 //=======================================================================
1127 Standard_Boolean GEOMImpl_Block6Explorer::IsSimilarEdges (const TopoDS_Shape& E1,
1128 const TopoDS_Shape& E2)
1130 TopoDS_Edge E1e = TopoDS::Edge(E1);
1131 TopoDS_Edge E2e = TopoDS::Edge(E2);
1132 TopoDS_Vertex V11, V12, V21, V22;
1133 TopExp::Vertices(E1e, V11, V12, Standard_True);
1134 TopExp::Vertices(E2e, V21, V22, Standard_True);
1135 if (BRepTools::Compare(V11, V21) && BRepTools::Compare(V12, V22))
1136 return Standard_True;
1137 if (BRepTools::Compare(V11, V22) && BRepTools::Compare(V12, V21))
1138 return Standard_True;
1140 return Standard_False;
1143 //=======================================================================
1144 //function : FindEdge
1146 //=======================================================================
1147 Standard_Integer GEOMImpl_Block6Explorer::FindEdge
1148 (TopoDS_Shape& theResult,
1149 const TopoDS_Shape& V1,
1150 const TopoDS_Shape& V2,
1151 const TopTools_IndexedDataMapOfShapeListOfShape& MVE,
1152 const Standard_Boolean findAll)
1154 Standard_Integer isFound = 0;
1156 const TopTools_ListOfShape& anEdgesOfV1 = MVE.FindFromKey(V1);
1157 const TopTools_ListOfShape& anEdgesOfV2 = MVE.FindFromKey(V2);
1159 TopTools_ListIteratorOfListOfShape it1 (anEdgesOfV1);
1160 for (; it1.More(); it1.Next()) {
1161 TopTools_ListIteratorOfListOfShape it2 (anEdgesOfV2);
1162 for (; it2.More(); it2.Next()) {
1163 if (it1.Value().IsSame(it2.Value())) {
1165 theResult = it1.Value();
1166 if (!findAll) return isFound;
1174 //=======================================================================
1175 //function : FindFace
1177 //=======================================================================
1178 Standard_Integer GEOMImpl_Block6Explorer::FindFace
1179 (TopoDS_Shape& theResult,
1180 const TopoDS_Shape& V1,
1181 const TopoDS_Shape& V2,
1182 const TopoDS_Shape& V3,
1183 const TopoDS_Shape& V4,
1184 const TopTools_IndexedDataMapOfShapeListOfShape& MVF,
1185 const Standard_Boolean findAll)
1187 Standard_Integer isFound = Standard_False;
1189 const TopTools_ListOfShape& aFacesOfV1 = MVF.FindFromKey(V1);
1190 const TopTools_ListOfShape& aFacesOfV2 = MVF.FindFromKey(V2);
1191 const TopTools_ListOfShape& aFacesOfV3 = MVF.FindFromKey(V3);
1192 const TopTools_ListOfShape& aFacesOfV4 = MVF.FindFromKey(V4);
1194 TopTools_ListIteratorOfListOfShape it1 (aFacesOfV1);
1195 for (; it1.More(); it1.Next()) {
1196 TopTools_ListIteratorOfListOfShape it2 (aFacesOfV2);
1197 for (; it2.More(); it2.Next()) {
1198 if (it1.Value().IsSame(it2.Value())) {
1199 TopTools_ListIteratorOfListOfShape it3 (aFacesOfV3);
1200 for (; it3.More(); it3.Next()) {
1201 if (it1.Value().IsSame(it3.Value())) {
1202 TopTools_ListIteratorOfListOfShape it4 (aFacesOfV4);
1203 for (; it4.More(); it4.Next()) {
1204 if (it1.Value().IsSame(it4.Value())) {
1206 theResult = it1.Value();
1207 if (!findAll) return isFound;
1219 //=======================================================================
1220 //function : MakeFace
1222 //=======================================================================
1223 TCollection_AsciiString GEOMImpl_Block6Explorer::MakeFace (const TopoDS_Wire& theWire,
1224 const Standard_Boolean isPlanarWanted,
1225 TopoDS_Shape& theResult)
1227 if (!isPlanarWanted)
1228 return MakeAnyFace(theWire, theResult);
1230 // Try to build a planar face.
1232 // If required tolerance increase will be
1233 // higher than PLANAR_FACE_MAX_TOLERANCE,
1234 // we will try to build a non-planar face.
1236 TCollection_AsciiString aWarning;
1237 BRepBuilderAPI_MakeFace MK (theWire, isPlanarWanted);
1239 theResult = MK.Shape();
1243 // try to update wire tolerances to build a planar face
1246 Standard_Real aToleranceReached, aTol;
1247 BRepLib_FindSurface aFS;
1248 aFS.Init(theWire, -1., isPlanarWanted);
1249 aToleranceReached = aFS.ToleranceReached();
1250 aTol = aFS.Tolerance();
1253 aFS.Init(theWire, aToleranceReached, isPlanarWanted);
1254 if (!aFS.Found()) return aWarning;
1255 aToleranceReached = aFS.ToleranceReached();
1256 aTol = aFS.Tolerance();
1258 aTol = Max(1.2 * aToleranceReached, aTol);
1260 // Mantis issue 0021432: EDF GEOM: Faces with huge tolerance can be built in GEOM
1261 if (aTol > PLANAR_FACE_MAX_TOLERANCE) {
1262 aWarning = MakeAnyFace(theWire, theResult);
1263 if (aWarning.IsEmpty() && !theResult.IsNull())
1264 aWarning = "MAKE_FACE_TOLERANCE_TOO_BIG";
1268 // Copy the wire, because it can be updated with very-very big tolerance here
1269 BRepBuilderAPI_Copy aMC (theWire);
1270 if (!aMC.IsDone()) return aWarning;
1271 TopoDS_Wire aWire = TopoDS::Wire(aMC.Shape());
1272 // Update tolerances to <aTol>
1274 for (TopExp_Explorer expE (aWire, TopAbs_EDGE); expE.More(); expE.Next()) {
1275 TopoDS_Edge anE = TopoDS::Edge(expE.Current());
1276 B.UpdateEdge(anE, aTol);
1278 for (TopExp_Explorer expV (aWire, TopAbs_VERTEX); expV.More(); expV.Next()) {
1279 TopoDS_Vertex aV = TopoDS::Vertex(expV.Current());
1280 B.UpdateVertex(aV, aTol);
1282 //BRepLib::UpdateTolerances(aWire);
1284 BRepBuilderAPI_MakeFace MK1 (aWire, isPlanarWanted);
1286 theResult = MK1.Shape();
1287 // Mantis issue 0021432: EDF GEOM: Faces with huge tolerance can be built in GEOM
1288 //if (aTol > PLANAR_FACE_MAX_TOLERANCE)
1289 // aWarning = "MAKE_FACE_TOLERANCE_TOO_BIG";
1295 //=======================================================================
1296 //function : MakeAnyFace
1298 //=======================================================================
1299 TCollection_AsciiString GEOMImpl_Block6Explorer::MakeAnyFace (const TopoDS_Wire& theWire,
1300 TopoDS_Shape& theResult)
1302 TCollection_AsciiString aWarning;
1304 // try to build a face on any surface under the edges of the wire
1305 BRepBuilderAPI_MakeFace MK (theWire, Standard_False);
1307 theResult = MK.Shape();
1311 // try to construct filling surface
1312 BRepOffsetAPI_MakeFilling MF;
1314 Standard_Integer nbEdges = 0;
1315 BRepTools_WireExplorer aWE (theWire);
1316 for (; aWE.More(); aWE.Next(), nbEdges++) {
1317 MF.Add(TopoDS::Edge(aWE.Current()), GeomAbs_C0);
1322 aWarning = "BRepOffsetAPI_MakeFilling failed";
1326 // Result of filling
1327 TopoDS_Shape aFace = MF.Shape();
1329 // 12.04.2006 for PAL12149 begin
1330 Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aFace));
1332 #ifdef MAKE_FACE_UPGRADE_PCURVES
1334 TopoDS_Iterator itw(theWire);
1335 for (; itw.More(); itw.Next())
1337 const TopoDS_Edge& anEdge = TopoDS::Edge(itw.Value());
1338 TopoDS_Edge NewEdge = TopoDS::Edge(MF.Generated(anEdge).First());
1339 Standard_Real fpar, lpar;
1340 Handle(Geom2d_Curve) NewPCurve = BRep_Tool::CurveOnSurface(NewEdge, TopoDS::Face(aFace), fpar, lpar);
1341 TopLoc_Location aLoc;
1342 Standard_Real NewTol = BRep_Tool::Tolerance(NewEdge);
1343 BB.UpdateEdge(anEdge, NewPCurve, aGS, aLoc, NewTol);
1347 BRepBuilderAPI_MakeFace MK1 (aGS, theWire);
1349 TopoDS_Shape aFace1 = MK1.Shape();
1351 BRepCheck_Analyzer ana (aFace1, false);
1352 if (!ana.IsValid()) {
1353 TopoDS_Shape aFace2;
1354 ShHealOper_ShapeProcess aHealer;
1355 aHealer.Perform(aFace1, aFace2);
1356 if (aHealer.isDone())
1360 // 12.04.2006 for PAL12149 end
1362 if (!theResult.IsNull()) {
1363 // try to deal with result of BRepBuilderAPI_MakeFace + ShHealOper_ShapeProcess
1364 #ifdef MAKE_FACE_PCURVES_FIX_TOLERANCE
1365 // check and fix pcurves, if necessary
1366 Standard_Real aT, aTolE, aD, aDMax;
1367 TopExp_Explorer aExpF, aExpE;
1368 NCollection_DataMap<TopoDS_Shape, Standard_Real, TopTools_ShapeMapHasher> aDMETol;
1369 aExpF.Init(theResult, TopAbs_FACE);
1370 for (; aExpF.More(); aExpF.Next()) {
1371 const TopoDS_Face& aF = *(TopoDS_Face*)&aExpF.Current();
1372 aExpE.Init(aF, TopAbs_EDGE);
1373 for (; aExpE.More(); aExpE.Next()) {
1374 const TopoDS_Edge& aE = *(TopoDS_Edge*)&aExpE.Current();
1375 if (!BOPTools_AlgoTools::ComputeTolerance(aF, aE, aDMax, aT)) continue;
1376 aTolE = BRep_Tool::Tolerance(aE);
1377 if (aDMax < aTolE) continue;
1378 if (aDMETol.IsBound(aE)) {
1379 aD = aDMETol.Find(aE);
1382 aDMETol.Bind(aE, aDMax);
1386 aDMETol.Bind(aE, aDMax);
1390 NCollection_DataMap<TopoDS_Shape, Standard_Real, TopTools_ShapeMapHasher>::Iterator aDMETolIt(aDMETol);
1391 ShapeFix_ShapeTolerance sat;
1392 for (; aDMETolIt.More(); aDMETolIt.Next()) {
1393 sat.LimitTolerance(aDMETolIt.Key(), aDMETolIt.Value());
1398 // try to deal with pure result of BRepOffsetAPI_MakeFilling
1401 Standard_Real aTol = MF.G0Error();
1403 TColgp_Array1OfPnt aPnts (1,nbEdges); // points of the given wire
1404 BRepTools_WireExplorer aWE1 (theWire);
1405 Standard_Integer vi = 1;
1406 for (; aWE1.More() && vi <= nbEdges; aWE1.Next(), vi++) {
1407 aPnts(vi) = BRep_Tool::Pnt(TopoDS::Vertex(aWE1.CurrentVertex()));
1410 // Find maximum deviation in vertices
1411 TopExp_Explorer exp (aFace, TopAbs_VERTEX);
1412 TopTools_MapOfShape mapShape;
1413 for (; exp.More(); exp.Next()) {
1414 if (mapShape.Add(exp.Current())) {
1415 TopoDS_Vertex aV = TopoDS::Vertex(exp.Current());
1416 Standard_Real aTolV = BRep_Tool::Tolerance(aV);
1417 gp_Pnt aP = BRep_Tool::Pnt(aV);
1418 Standard_Real min_dist = aP.Distance(aPnts(1));
1419 for (vi = 2; vi <= nbEdges; vi++) {
1420 min_dist = Min(min_dist, aP.Distance(aPnts(vi)));
1422 aTol = Max(aTol, aTolV);
1423 aTol = Max(aTol, min_dist);
1427 if ((*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance() < aTol) {
1428 (*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance(aTol);