3 #include <BRepOffsetAPI_MakeFilling.hxx>
5 #include "GEOMImpl_Block6Explorer.hxx"
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>
21 #include <TopoDS_Shape.hxx>
22 #include <TopoDS_Edge.hxx>
23 #include <TopoDS_Wire.hxx>
24 #include <TopoDS_Solid.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>
33 #include <Geom_Curve.hxx>
34 #include <Geom_TrimmedCurve.hxx>
35 #include <GeomFill_Generator.hxx>
37 #include <Precision.hxx>
39 #include <TColgp_Array1OfPnt.hxx>
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>
51 static Standard_Integer mod4 (Standard_Integer nb)
53 if (nb <= 0) return nb + 4;
54 if (nb > 4) return nb - 4;
58 static Standard_Integer edge_id (const Standard_Integer theFaceID,
59 const Standard_Integer theEdgeNB)
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
69 return edge_ids[theFaceID - 1][theEdgeNB - 1];
72 static Standard_Integer side_edge_id (const Standard_Integer theEdgeNB)
74 static Standard_Integer side_edge_ids[4] = {9, 10, 11, 12};
76 return side_edge_ids[theEdgeNB - 1];
79 static Standard_Integer vertex_id (const Standard_Integer theFaceID,
80 const Standard_Integer theVertexNB)
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
90 return vertex_ids[theFaceID - 1][theVertexNB - 1];
93 static Standard_Integer vertex_id_edge (const Standard_Integer theEdgeID, // [1,12]
94 const Standard_Integer theVertexNB) // [1,2]
96 static Standard_Integer vertex_ids_edge[NBEDGES][2] = {
110 return vertex_ids_edge[theEdgeID - 1][theVertexNB - 1];
113 static Standard_Integer face_id_edges (const Standard_Integer theEdge1ID, // [1,12]
114 const Standard_Integer theEdge2ID) // [1,12]
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
131 return face_ids_edges[theEdge1ID - 1][theEdge2ID - 1];
134 static Standard_Integer edge_id_vertices (const Standard_Integer theVertex1ID, // [1,8]
135 const Standard_Integer theVertex2ID) // [1,8]
137 static Standard_Integer edge_ids_vertices[NBVERTS][NBVERTS] = {
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
148 return edge_ids_vertices[theVertex1ID - 1][theVertex2ID - 1];
151 static Standard_Integer edge_id_faces (const Standard_Integer theFace1ID, // [1,6]
152 const Standard_Integer theFace2ID) // [1,6]
154 static Standard_Integer edge_ids_faces[NBFACES][NBFACES] = {
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
163 return edge_ids_faces[theFace1ID - 1][theFace2ID - 1];
166 //=======================================================================
167 //function : GEOMImpl_Block6Explorer
168 //purpose : Constructor
169 //=======================================================================
170 GEOMImpl_Block6Explorer::GEOMImpl_Block6Explorer ()
171 : myFaces(1,NBFACES), myEdges(1,NBEDGES), myVertices(1,NBVERTS)
175 //=======================================================================
176 //function : GetVertex
178 //=======================================================================
179 TopoDS_Shape GEOMImpl_Block6Explorer::GetVertex (const Standard_Integer theVertexID)
181 TopoDS_Shape aNullShape;
182 if (theVertexID < 1 || theVertexID > NBVERTS) return aNullShape;
183 return myVertices(theVertexID);
186 //=======================================================================
187 //function : GetVertexID
189 //=======================================================================
190 Standard_Integer GEOMImpl_Block6Explorer::GetVertexID (const TopoDS_Shape& theVertex)
192 for (Standard_Integer id = 1; id <= NBVERTS; id++) {
193 if (theVertex.IsSame(myVertices(id))) return id;
195 Standard_NoSuchObject::Raise("The Vertex does not belong to the Block");
199 //=======================================================================
200 //function : GetVertexID
202 //=======================================================================
203 Standard_Integer GEOMImpl_Block6Explorer::GetVertexID (const Standard_Integer theFaceID,
204 const Standard_Integer theVertexNB)
206 return vertex_id(theFaceID, theVertexNB);
209 //=======================================================================
210 //function : GetVertexOnEdgeID
212 //=======================================================================
213 Standard_Integer GEOMImpl_Block6Explorer::GetVertexOnEdgeID
214 (const Standard_Integer theEdgeID,
215 const Standard_Integer theVertexNB)
217 return vertex_id_edge(theEdgeID, theVertexNB);
220 //=======================================================================
223 //=======================================================================
224 TopoDS_Shape GEOMImpl_Block6Explorer::GetEdge (const Standard_Integer theEdgeID,
225 const Standard_Boolean doMake)
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))));
235 Standard_ConstructionError::Raise("Edge construction failed");
237 myEdges(theEdgeID) = ME.Shape();
240 return myEdges(theEdgeID);
243 //=======================================================================
244 //function : GetEdgeID
246 //=======================================================================
247 Standard_Integer GEOMImpl_Block6Explorer::GetEdgeID (const TopoDS_Shape& theEdge)
249 for (Standard_Integer id = 1; id <= NBEDGES; id++) {
250 if (theEdge.IsSame(myEdges(id))) return id;
252 Standard_NoSuchObject::Raise("The Edge does not belong to the Block");
256 //=======================================================================
257 //function : GetEdgeID
259 //=======================================================================
260 Standard_Integer GEOMImpl_Block6Explorer::GetEdgeID (const Standard_Integer theFaceID,
261 const Standard_Integer theEdgeNB)
263 return edge_id(theFaceID, theEdgeNB);
266 //=======================================================================
267 //function : FindEdgeID
269 //=======================================================================
270 Standard_Integer GEOMImpl_Block6Explorer::FindEdgeID (const Standard_Integer theVertex1ID,
271 const Standard_Integer theVertex2ID)
273 return edge_id_vertices(theVertex1ID, theVertex2ID);
276 //=======================================================================
277 //function : FindCommonEdgeID
279 //=======================================================================
280 Standard_Integer GEOMImpl_Block6Explorer::FindCommonEdgeID
281 (const Standard_Integer theFace1ID,
282 const Standard_Integer theFace2ID)
284 return edge_id_faces(theFace1ID, theFace2ID);
287 //=======================================================================
290 //=======================================================================
291 TopoDS_Shape GEOMImpl_Block6Explorer::GetFace (const Standard_Integer theFaceID,
292 const Standard_Boolean doMake)
294 TopoDS_Shape aNullShape;
295 if (theFaceID < 1 || theFaceID > NBFACES) return aNullShape;
297 if (myFaces(theFaceID).IsNull() && doMake) {
299 // Create the required face between
300 // corresponding edges and put it in the Block's faces
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);
307 BRepBuilderAPI_MakeWire MW (TopoDS::Edge(E1),
312 Standard_ConstructionError::Raise("Wire construction failed");
315 MakeFace(MW, Standard_False, aFace);
316 if (aFace.IsNull()) {
317 Standard_ConstructionError::Raise("Face construction failed");
319 myFaces(theFaceID) = aFace;
322 return myFaces(theFaceID);
325 //=======================================================================
326 //function : GetFaceID
328 //=======================================================================
329 Standard_Integer GEOMImpl_Block6Explorer::GetFaceID (const TopoDS_Shape& theFace)
331 for (Standard_Integer id = 1; id <= NBFACES; id++) {
332 if (theFace.IsSame(myFaces(id))) return id;
334 Standard_NoSuchObject::Raise("The Face does not belong to the Block");
338 //=======================================================================
339 //function : FindFaceID
341 //=======================================================================
342 Standard_Integer GEOMImpl_Block6Explorer::FindFaceID (const Standard_Integer theEdge1ID,
343 const Standard_Integer theEdge2ID)
345 return face_id_edges(theEdge1ID, theEdge2ID);
348 //=======================================================================
349 //function : GetOppositeFaceID
351 //=======================================================================
352 Standard_Integer GEOMImpl_Block6Explorer::GetOppositeFaceID (const Standard_Integer theFaceID)
354 Standard_Integer opp_face_id[NBFACES + 1] = {
363 return opp_face_id[theFaceID];
366 //=======================================================================
367 //function : IsSimilarFaces
369 //=======================================================================
370 Standard_Boolean GEOMImpl_Block6Explorer::IsSimilarFaces (const Standard_Integer theFace1ID,
371 const Standard_Integer theFace2ID,
372 const gp_Trsf theTransformation)
374 Standard_Integer common_edge_id = FindCommonEdgeID(theFace1ID, theFace2ID);
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));
381 BRepBuilderAPI_Transform aTrsf (E1, theTransformation, Standard_False);
382 if (!IsSimilarEdges(aTrsf.Shape(), E2))
383 return Standard_False;
385 } else { // the faces have common edge
386 TopTools_Array1OfShape aVerts1 (1,4);
387 TopTools_Array1OfShape aVerts2 (1,4);
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);
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;
403 if (vid != common_vertex2 && FindEdgeID(vid, common_vertex2) == 0) {
404 not_common_v11 = vid;
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;
415 if (vid != common_vertex2 && FindEdgeID(vid, common_vertex2) == 0) {
416 not_common_v21 = vid;
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);
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;
435 return Standard_True;
438 //============ Initialization methods ===================================
440 //=======================================================================
441 //function : InitByBlock
443 //=======================================================================
444 void GEOMImpl_Block6Explorer::InitByBlock (const TopoDS_Shape& theBlock)
446 // 1. Find any one face of the block
447 TopExp_Explorer faces (theBlock, TopAbs_FACE);
449 Standard_ConstructionError::Raise("The block has no faces");
451 TopoDS_Shape aFirstFace = faces.Current();
453 // 2. Store all elements of the block relatively aFirstFace
454 InitByBlockAndFace(theBlock, aFirstFace);
457 //=======================================================================
458 //function : InitByBlockAndFace
460 //=======================================================================
461 void GEOMImpl_Block6Explorer::InitByBlockAndFace (const TopoDS_Shape& theBlock,
462 const TopoDS_Shape& theFace)
464 myFaces(1) = theFace;
466 // 2. Get wire of the first face
467 TopExp_Explorer wires (myFaces(1), TopAbs_WIRE);
469 Standard_ConstructionError::Raise("A face of the block has no wires");
471 TopoDS_Shape aWire = wires.Current();
474 Standard_ConstructionError::Raise("A face of the block has more than one wires");
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++) {
482 Standard_ConstructionError::Raise("A face of the block has more than four edges");
484 myEdges(edge_id(1, nb)) = aWE.Current();
485 myVertices(vertex_id(1, nb)) = aWE.CurrentVertex();
488 Standard_ConstructionError::Raise("A face of the block has less than four edges");
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)));
498 //=======================================================================
499 //function : InitByBlockAndEdges
501 //=======================================================================
502 void GEOMImpl_Block6Explorer::InitByBlockAndEdges (const TopoDS_Shape& theBlock,
503 const TopoDS_Shape& theEdge1,
504 const TopoDS_Shape& theEdge3)
506 // 1. Store vertices and edges of the first face
508 // 1.1. Store two given edges
509 myEdges(edge_id(1, 1)) = theEdge1;
510 myEdges(edge_id(1, 3)) = theEdge3;
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");
518 const TopTools_ListOfShape& aFacesOfE1 = MEF.FindFromKey(theEdge1);
519 const TopTools_ListOfShape& aFacesOfE3 = MEF.FindFromKey(theEdge3);
521 Standard_Boolean isFound = Standard_False;
522 TopTools_ListIteratorOfListOfShape anIterF1 (aFacesOfE1);
523 for (; anIterF1.More() && !isFound; anIterF1.Next()) {
525 TopTools_ListIteratorOfListOfShape anIterF3 (aFacesOfE3);
526 for (; anIterF3.More() && !isFound; anIterF3.Next()) {
528 if (anIterF1.Value().IsSame(anIterF3.Value())) {
529 isFound = Standard_True;
531 // Store the face, defined by two opposite edges
532 myFaces(1) = anIterF1.Value();
537 Standard_ConstructionError::Raise
538 ("Edges 1 and 2 do not belong to one face of the block");
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;
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");
556 // 1.4. Find and store others elements of the first face
558 // edges of the first vertex
559 TopoDS_Shape E1_f = M1.FindFromKey(V1).First();
560 TopoDS_Shape E1_l = M1.FindFromKey(V1).Last();
562 if (E1_f.IsSame(theEdge1)) {
563 myEdges(edge_id(1, 4)) = E1_l;
565 myEdges(edge_id(1, 4)) = E1_f;
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;
575 myVertices(vertex_id(1,4)) = V41;
578 // edges of the second vertex
579 TopoDS_Shape E2_f = M1.FindFromKey(V2).First();
580 TopoDS_Shape E2_l = M1.FindFromKey(V2).Last();
582 if (E2_f.IsSame(theEdge1)) {
583 myEdges(edge_id(1, 2)) = E2_l;
585 myEdges(edge_id(1, 2)) = E2_f;
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;
595 myVertices(vertex_id(1,3)) = V21;
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)));
605 //=======================================================================
606 //function : InitByBlockAndVertices
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)
614 // Here we suppose, that vertices are ordered, i.e. exists edge between
615 // theVertex1 and theVertex2 and edge between theVertex2 and theVertex3
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()) {
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;
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");
634 const TopTools_ListOfShape& aFacesOfV1 = MVF.FindFromKey(theVertex1);
635 const TopTools_ListOfShape& aFacesOfV3 = MVF.FindFromKey(theVertex3);
637 Standard_Boolean isFound = Standard_False;
638 TopTools_ListIteratorOfListOfShape anIterF1 (aFacesOfV1);
639 for (; anIterF1.More() && !isFound; anIterF1.Next()) {
641 TopTools_ListIteratorOfListOfShape anIterF3 (aFacesOfV3);
642 for (; anIterF3.More() && !isFound; anIterF3.Next()) {
644 if (anIterF1.Value().IsSame(anIterF3.Value())) {
645 isFound = Standard_True;
647 // Store the face, defined by two opposite vertices
648 myFaces(1) = anIterF1.Value();
653 Standard_ConstructionError::Raise
654 ("Vertices 1 and 3 do not belong to one face of the block");
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");
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);
669 TopTools_ListIteratorOfListOfShape anIterE2 (anEdgesOfV2);
670 for (; anIterE2.More(); anIterE2.Next()) {
672 TopTools_ListIteratorOfListOfShape anIterE1 (anEdgesOfV1);
673 for (; anIterE1.More(); anIterE1.Next()) {
675 if (anIterE1.Value().IsSame(anIterE2.Value())) {
676 // Store the first edge, defined by two vertices
677 myEdges(edge_id(1,1)) = anIterE1.Value();
680 // Store the last edge
681 myEdges(edge_id(1,4)) = anIterE1.Value();
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);
688 if (V1.IsSame(theVertex1)) {
689 myVertices(vertex_id(1,4)) = V2;
691 myVertices(vertex_id(1,4)) = V1;
696 TopTools_ListIteratorOfListOfShape anIterE3 (anEdgesOfV3);
697 for (; anIterE3.More(); anIterE3.Next()) {
699 if (anIterE3.Value().IsSame(anIterE2.Value())) {
700 // Store the second edge, defined by two vertices
701 myEdges(edge_id(1,2)) = anIterE3.Value();
704 // Store the fird edge
705 myEdges(edge_id(1,3)) = anIterE3.Value();
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");
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);
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");
730 // Get list of block's edges, sharing this Vertex
731 const TopTools_ListOfShape& anEdgesOfVi = MB.FindFromKey(Vi);
732 TopTools_ListIteratorOfListOfShape anEdgesIter (anEdgesOfVi);
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;
740 // Store the linking edge
741 TopoDS_Shape aLinkEdge = anEdgesIter.Value();
742 myEdges(side_edge_id(i)) = aLinkEdge;
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);
749 // Store the i-th vertex of the second (opposite to the first) face
751 myVertices(vertex_id(2, i)) = V2;
753 myVertices(vertex_id(2, i)) = V1;
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)));
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);
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()) {
775 TopTools_ListIteratorOfListOfShape anEdgesIterj (anEdgesOfVj);
776 for (; anEdgesIterj.More() && !isFound; anEdgesIterj.Next()) {
778 if (anEdgesIteri.Value().IsSame(anEdgesIterj.Value())) {
779 isFound = Standard_True;
781 // Store the linking edge
782 myEdges(edge_id(2, i)) = anEdgesIteri.Value();
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");
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);
801 Standard_Boolean isFound = Standard_False;
802 TopTools_ListIteratorOfListOfShape anIterEi1 (aFacesOfEi1);
803 for (; anIterEi1.More() && !isFound; anIterEi1.Next()) {
805 TopTools_ListIteratorOfListOfShape anIterEi2 (aFacesOfEi2);
806 for (; anIterEi2.More() && !isFound; anIterEi2.Next()) {
808 if (anIterEi1.Value().IsSame(anIterEi2.Value())) {
809 isFound = Standard_True;
811 // Store the face, defined by two edges
812 myFaces(i) = anIterEi1.Value();
819 //=======================================================================
820 //function : InitByTwoFaces
822 //=======================================================================
823 void GEOMImpl_Block6Explorer::InitByTwoFaces (const TopoDS_Shape& theFace1,
824 const TopoDS_Shape& theFace2)
826 if (theFace1.IsSame(theFace2)) {
827 Standard_ConstructionError::Raise("The faces must be different");
830 // Add two given faces in the structure
831 myFaces(1) = theFace1;
832 myFaces(2) = theFace2;
834 // Step 1. Order vertices (and edges)
836 // 1.1. Ordered vertices and edges of the first face we put in <myVertices>
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");
843 TopoDS_Shape aWire1 = wires1.Current();
846 Standard_ConstructionError::Raise("A face for the block has more than one wire");
849 BRepTools_WireExplorer aWE1 (TopoDS::Wire(aWire1), TopoDS::Face(myFaces(1)));
851 for (nb = 1; aWE1.More(); aWE1.Next(), nb++) {
853 Standard_ConstructionError::Raise("A face for the block has more than four edges");
855 myEdges(edge_id(1, nb)) = aWE1.Current();
856 myVertices(vertex_id(1, nb)) = aWE1.CurrentVertex();
859 Standard_ConstructionError::Raise("A face for the block has less than four edges");
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.
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)
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");
874 TopoDS_Shape aWire2 = wires2.Current();
877 Standard_ConstructionError::Raise("A face for the block has more than one wire");
880 BRepTools_WireExplorer aWE2 (TopoDS::Wire(aWire2), TopoDS::Face(myFaces(2)));
881 for (nb = 1; aWE2.More(); aWE2.Next(), nb++) {
883 Standard_ConstructionError::Raise("A face for the block has more than four edges");
885 anEdges2(nb) = aWE2.Current();
886 aVertis2(nb) = aWE2.CurrentVertex();
889 Standard_ConstructionError::Raise("A face for the block has less than four edges");
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;
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))));
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)));
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;
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;
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));
947 if (aPnts1(i).Distance(aPnts2(nb)) < Precision::Confusion()) {
948 Standard_ConstructionError::Raise("The faces are too close");
951 myVertices(vertex_id(2, i)) = aVertis2(nb);
953 if (s_min == -1) nb = mod4(nb - 1);
954 myEdges(edge_id(2, i)) = anEdges2(nb);
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);
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);
974 BRepBuilderAPI_MakeWire MW (TopoDS::Edge(E1),
979 Standard_ConstructionError::Raise("Wire construction failed");
982 BRepBuilderAPI_MakeFace MF (MW, Standard_False);
984 myFaces(i) = MF.Shape();
986 tryThru = Standard_True;
990 // Build side surface by ThruSections algorithm
992 BRepOffsetAPI_ThruSections THS;
993 THS.AddWire(TopoDS::Wire(aWire1));
994 THS.AddWire(TopoDS::Wire(aWire2));
997 StdFail_NotDone::Raise("Side surface generation failed");
999 for (Standard_Integer i = 1; i <= 4; i++) {
1001 myFaces(i+2) = THS.GeneratedFace(myEdges(i));
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)),
1016 //=======================================================================
1017 //function : MapShapesAndAncestors
1019 //=======================================================================
1020 void GEOMImpl_Block6Explorer::MapShapesAndAncestors (const TopoDS_Shape& S,
1021 const TopAbs_ShapeEnum TS,
1022 const TopAbs_ShapeEnum TA,
1023 TopTools_IndexedDataMapOfShapeListOfShape& M)
1025 TopTools_ListOfShape empty;
1026 TopTools_MapOfShape mapA;
1029 TopExp_Explorer exa (S,TA);
1030 for (; exa.More(); exa.Next()) {
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);
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);
1054 //=======================================================================
1055 //function : IsSimilarEdges
1057 //=======================================================================
1058 Standard_Boolean GEOMImpl_Block6Explorer::IsSimilarEdges (const TopoDS_Shape& E1,
1059 const TopoDS_Shape& E2)
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;
1071 return Standard_False;
1074 //=======================================================================
1075 //function : FindEdge
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)
1085 Standard_Integer isFound = 0;
1087 const TopTools_ListOfShape& anEdgesOfV1 = MVE.FindFromKey(V1);
1088 const TopTools_ListOfShape& anEdgesOfV2 = MVE.FindFromKey(V2);
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())) {
1096 theResult = it1.Value();
1097 if (!findAll) return isFound;
1105 //=======================================================================
1106 //function : FindFace
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)
1118 Standard_Integer isFound = Standard_False;
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);
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())) {
1137 theResult = it1.Value();
1138 if (!findAll) return isFound;
1150 //=======================================================================
1151 //function : MakeFace
1153 //=======================================================================
1154 void GEOMImpl_Block6Explorer::MakeFace (const TopoDS_Wire& theWire,
1155 const Standard_Boolean isPlanarWanted,
1156 TopoDS_Shape& theResult)
1158 // try to build face on plane or on any surface under the edges of the wire
1159 BRepBuilderAPI_MakeFace MK (theWire, isPlanarWanted);
1161 theResult = MK.Shape();
1165 // try to construct filling surface
1166 if (!isPlanarWanted) {
1167 BRepOffsetAPI_MakeFilling MF;
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);
1177 // Result of filling
1178 TopoDS_Shape aFace = MF.Shape();
1181 Standard_Real aTol = MF.G0Error();
1183 TColgp_Array1OfPnt aPnts (1,nbEdges); // points of the given wire
1184 BRepTools_WireExplorer aWE1 (theWire);
1185 Standard_Integer vi = 1;
1186 for (; aWE1.More() && vi <= nbEdges; aWE1.Next(), vi++) {
1187 aPnts(vi) = BRep_Tool::Pnt(TopoDS::Vertex(aWE1.CurrentVertex()));
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)));
1202 aTol = Max(aTol, aTolV);
1203 aTol = Max(aTol, min_dist);
1207 if ((*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance() < aTol) {
1208 (*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance(aTol);