Salome HOME
Mantis issue 0021465: EDF 2067 GEOM: Extrusion along a path leads to a self-intersect...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BlockDriver.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <BRepOffsetAPI_MakeFilling.hxx>
26
27 #include <GEOMImpl_BlockDriver.hxx>
28 #include <GEOMImpl_IBlocks.hxx>
29 #include <GEOMImpl_IBlockTrsf.hxx>
30 #include <GEOMImpl_GlueDriver.hxx>
31 #include <GEOMImpl_Types.hxx>
32 #include <GEOMImpl_ILocalOperations.hxx>
33 #include <GEOMImpl_Block6Explorer.hxx>
34 #include <GEOMImpl_IBlocksOperations.hxx>
35
36 #include <GEOM_Function.hxx>
37
38 #include <ShHealOper_Sewing.hxx>
39 #include <ShHealOper_ShapeProcess.hxx>
40 //#include <GEOMAlgo_Gluer.hxx>
41 #include <BlockFix_BlockFixAPI.hxx>
42
43 #include "utilities.h"
44
45 #include <TNaming_CopyShape.hxx>
46
47 #include <BRepAdaptor_Curve.hxx>
48 #include <BRepLib.hxx>
49 #include <BRep_Tool.hxx>
50 #include <BRepTools.hxx>
51 #include <BRepGProp.hxx>
52 #include <BRep_Builder.hxx>
53 #include <BRepTools_Quilt.hxx>
54 #include <BRepTools_WireExplorer.hxx>
55 #include <BRepBuilderAPI_MakeEdge.hxx>
56 #include <BRepBuilderAPI_MakeWire.hxx>
57 #include <BRepBuilderAPI_MakePolygon.hxx>
58 #include <BRepBuilderAPI_Transform.hxx>
59 #include <BRepCheck_Analyzer.hxx>
60 #include <BRepClass_FaceClassifier.hxx>
61 #include <BRepClass3d_SolidClassifier.hxx>
62 #include <BRepExtrema_ExtPF.hxx>
63 #include <BRepExtrema_DistShapeShape.hxx>
64
65 #include <TopAbs.hxx>
66 #include <TopoDS.hxx>
67 #include <TopoDS_Shape.hxx>
68 #include <TopoDS_Edge.hxx>
69 #include <TopoDS_Wire.hxx>
70 #include <TopoDS_Shell.hxx>
71 #include <TopoDS_Solid.hxx>
72 #include <TopoDS_Compound.hxx>
73 #include <TopoDS_Iterator.hxx>
74 #include <TopExp.hxx>
75 #include <TopExp_Explorer.hxx>
76 #include <TopTools_MapOfShape.hxx>
77 #include <TopTools_MapIteratorOfMapOfShape.hxx>
78 #include <TopTools_Array1OfShape.hxx>
79 #include <TopTools_SequenceOfShape.hxx>
80 #include <TopTools_ListOfShape.hxx>
81 #include <TopTools_ListIteratorOfListOfShape.hxx>
82 #include <TopTools_IndexedMapOfShape.hxx>
83 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
84
85 #include <GProp_GProps.hxx>
86
87 #include <Geom_Line.hxx>
88 #include <GC_MakeLine.hxx>
89
90 #include <gp.hxx>
91 #include <gp_Pnt.hxx>
92 #include <gp_Ax3.hxx>
93 #include <Precision.hxx>
94 #include <TColgp_Array1OfPnt.hxx>
95 #include <TColStd_Array1OfInteger.hxx>
96 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
97 #include <StdFail_NotDone.hxx>
98 #include <Standard_NullObject.hxx>
99 #include <Standard_TypeMismatch.hxx>
100 #include <Standard_ConstructionError.hxx>
101
102 //=======================================================================
103 //function : GetID
104 //purpose  :
105 //=======================================================================
106 const Standard_GUID& GEOMImpl_BlockDriver::GetID()
107 {
108   static Standard_GUID aBlockDriver("FF1BBB67-5D14-4df2-980B-3A668264EA16");
109   return aBlockDriver;
110 }
111
112
113 //=======================================================================
114 //function : GEOMImpl_BlockDriver
115 //purpose  :
116 //=======================================================================
117 GEOMImpl_BlockDriver::GEOMImpl_BlockDriver()
118 {
119 }
120
121 //=======================================================================
122 //function : Execute
123 //purpose  :
124 //=======================================================================
125 Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
126 {
127   if (Label().IsNull()) return 0;
128   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
129
130   Standard_Integer aType = aFunction->GetType();
131
132   TopoDS_Shape aShape;
133   BRep_Builder B;
134
135   Standard_Real prec = Precision::Confusion();
136
137   Standard_Integer aNbSub = 0;
138   if (aType == BLOCK_COMPOUND_GLUE) {
139
140     aNbSub = 1;
141
142   } else if (aType == BLOCK_FACE_TWO_EDGES ||
143              aType == BLOCK_TWO_FACES) {
144
145     aNbSub = 2;
146
147   } else if (aType == BLOCK_FACE_FOUR_PNT ||
148              aType == BLOCK_FACE_FOUR_EDGES) {
149
150     aNbSub = 4;
151
152   } else if (aType == BLOCK_SIX_FACES) {
153
154     aNbSub = 6;
155
156   } else {
157   }
158
159   if (aNbSub > 0) {
160     GEOMImpl_IBlocks aCI (aFunction);
161     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
162     Standard_Integer nbshapes = aShapes->Length();
163
164     if (nbshapes != aNbSub) {
165       Standard_TypeMismatch::Raise
166         ("Number of elements for object construction does not correspond to the used constructor");
167     }
168
169     TopTools_Array1OfShape anArgs (1, aNbSub);
170     Standard_Integer argi;
171     for (argi = 1; argi <= aNbSub; argi++) {
172       Handle(GEOM_Function) aRef = Handle(GEOM_Function)::DownCast(aShapes->Value(argi));
173       TopoDS_Shape anArg = aRef->GetValue();
174       if (anArg.IsNull()) {
175         Standard_NullObject::Raise("Null shape is given as argument");
176       }
177       anArgs(argi) = anArg;
178     }
179
180     if (aType == BLOCK_FACE_FOUR_EDGES) {
181
182       // Make face from four edges
183       if (anArgs(1).ShapeType() != TopAbs_EDGE || anArgs(2).ShapeType() != TopAbs_EDGE ||
184           anArgs(3).ShapeType() != TopAbs_EDGE || anArgs(4).ShapeType() != TopAbs_EDGE) {
185         Standard_TypeMismatch::Raise("Shape for face construction is not an edge");
186       }
187
188       // count corner vertices
189       TopTools_MapOfShape aVertMap;
190       for (Standard_Integer ii = 1; ii <= 4; ii++) {
191         TopoDS_Edge anEdge = TopoDS::Edge(anArgs(ii));
192         TopoDS_Vertex V1, V2;
193         TopExp::Vertices(anEdge, V1, V2, Standard_True);
194         if (V1.IsNull() || V2.IsNull()) {
195           Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
196         }
197         if (BRepTools::Compare(V1,V2)) {
198           Standard_ConstructionError::Raise("Edge ends are too close");
199         }
200         Standard_Boolean isCoin1 = Standard_False, isCoin2 = Standard_False;
201         TopTools_MapIteratorOfMapOfShape anIter (aVertMap);
202         for (; anIter.More(); anIter.Next()) {
203           TopoDS_Vertex V = TopoDS::Vertex(anIter.Key());
204           if (BRepTools::Compare(V,V1)) isCoin1 = Standard_True;
205           if (BRepTools::Compare(V,V2)) isCoin2 = Standard_True;
206         }
207         if (!isCoin1) aVertMap.Add(V1);
208         if (!isCoin2) aVertMap.Add(V2);
209       }
210       if (aVertMap.Extent() != 4) {
211         Standard_ConstructionError::Raise("The edges must form a closed wire");
212       }
213
214       TopoDS_Edge anEdge1 = TopoDS::Edge(anArgs(1));
215       TopoDS_Edge anEdge2 = TopoDS::Edge(anArgs(2));
216       TopoDS_Edge anEdge3 = TopoDS::Edge(anArgs(3));
217       TopoDS_Edge anEdge4 = TopoDS::Edge(anArgs(4));
218
219       // check, if anEdge1 has common/coincident vertex with anEdge2,
220       Standard_Boolean isConnected12 = Standard_False;
221       TopoDS_Vertex V11, V12, V21, V22;
222       TopExp::Vertices(anEdge1, V11, V12, Standard_True);
223       TopExp::Vertices(anEdge2, V21, V22, Standard_True);
224       if (BRepTools::Compare(V11,V21) || BRepTools::Compare(V11,V22) ||
225           BRepTools::Compare(V12,V21) || BRepTools::Compare(V12,V22)) {
226         // the edges have common vertex
227         isConnected12 = Standard_True;
228       }
229
230       // build wire in right order, corresponding to edges connexity
231       BRepBuilderAPI_MakeWire* MW;
232       if (isConnected12)
233         MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge2, anEdge3, anEdge4);
234       else
235         MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
236
237       if (!MW->IsDone()) {
238         Standard_ConstructionError::Raise
239           ("Impossible to build a connected wire from the given edges");
240       }
241       TopoDS_Wire aWire = *MW;
242       delete MW;
243
244       // check the wire closure
245       TopoDS_Vertex aV1, aV2;
246       TopExp::Vertices(aWire, aV1, aV2);
247       if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
248         aWire.Closed( true );
249
250       if (!aWire.Closed()) {
251         Standard_ConstructionError::Raise
252           ("Impossible to build a closed wire from the given edges");
253       }
254
255       // try to build face on the wire
256       GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
257       if (aShape.IsNull()) {
258         Standard_ConstructionError::Raise("Face construction failed");
259       }
260
261     } else if (aType == BLOCK_FACE_TWO_EDGES) {
262
263       // Make face from two opposite edges
264       if (anArgs(1).ShapeType() != TopAbs_EDGE ||
265           anArgs(2).ShapeType() != TopAbs_EDGE) {
266         Standard_TypeMismatch::Raise("Shape for face construction is not an edge");
267       }
268
269       TopoDS_Edge anEdge1 = TopoDS::Edge(anArgs(1));
270       TopoDS_Edge anEdge2 = TopoDS::Edge(anArgs(2));
271
272       if (anEdge1.IsSame(anEdge2)) {
273         Standard_ConstructionError::Raise("The edges must be different");
274       }
275
276       // create two edges, linking ends of the given edges
277       TopoDS_Vertex V11, V12, V21, V22;
278       TopExp::Vertices(anEdge1, V11, V12, Standard_False);
279       TopExp::Vertices(anEdge2, V21, V22, Standard_False);
280       if (V11.IsNull() || V12.IsNull() ||
281           V21.IsNull() || V22.IsNull()) {
282         Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
283       }
284
285       BRepAdaptor_Curve C1 (anEdge1);
286       BRepAdaptor_Curve C2 (anEdge2);
287       gp_Pnt P11, P12, P21, P22;
288
289       // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
290       P11 = C1.Value(C1.FirstParameter());
291       P12 = C1.Value(C1.LastParameter());
292       P21 = C2.Value(C2.FirstParameter());
293       P22 = C2.Value(C2.LastParameter());
294       //gp_Pnt P11 = BRep_Tool::Pnt(V11);
295       //gp_Pnt P12 = BRep_Tool::Pnt(V12);
296       //gp_Pnt P21 = BRep_Tool::Pnt(V21);
297       //gp_Pnt P22 = BRep_Tool::Pnt(V22);
298
299       if (P11.Distance(P21) < prec || P12.Distance(P22) < prec ||
300           P11.Distance(P22) < prec || P12.Distance(P21) < prec) {
301         Standard_ConstructionError::Raise("Given edges have too close ends");
302       }
303
304       Standard_Real per11 = P11.Distance(P21) + P12.Distance(P22);
305       Standard_Real per12 = P11.Distance(P22) + P12.Distance(P21);
306
307       BRep_Builder BB;
308
309       TopoDS_Edge anEdge3;
310       TopoDS_Edge anEdge4;
311
312       // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
313       if (per11 < per12) {
314         Handle(Geom_Line) Line1 = GC_MakeLine(P11, P21).Value();
315         Handle(Geom_Line) Line2 = GC_MakeLine(P12, P22).Value();
316
317         BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
318         BB.Range(anEdge3, 0., P11.Distance(P21));
319         BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
320         BB.Add(anEdge3, V21.Oriented(TopAbs_REVERSED));
321
322         BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
323         BB.Range(anEdge4, 0., P12.Distance(P22));
324         BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
325         BB.Add(anEdge4, V22.Oriented(TopAbs_REVERSED));
326       }
327       else {
328         Handle(Geom_Line) Line1 = GC_MakeLine(P11, P22).Value();
329         Handle(Geom_Line) Line2 = GC_MakeLine(P12, P21).Value();
330
331         BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
332         BB.Range(anEdge3, 0., P11.Distance(P22));
333         BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
334         BB.Add(anEdge3, V22.Oriented(TopAbs_REVERSED));
335
336         BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
337         BB.Range(anEdge4, 0., P12.Distance(P21));
338         BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
339         BB.Add(anEdge4, V21.Oriented(TopAbs_REVERSED));
340       }
341       //if (per11 < per12) {
342       //  anEdge3 = BRepBuilderAPI_MakeEdge(V11, V21);
343       //  anEdge4 = BRepBuilderAPI_MakeEdge(V12, V22);
344       //} else {
345       //  anEdge3 = BRepBuilderAPI_MakeEdge(V11, V22);
346       //  anEdge4 = BRepBuilderAPI_MakeEdge(V12, V21);
347       //}
348
349       // build a wire
350       BRepBuilderAPI_MakeWire* MW;
351       MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
352       if (!MW->IsDone()) {
353         Standard_ConstructionError::Raise("Wire construction failed");
354       }
355
356       TopoDS_Wire aWire = *MW;
357       delete MW;
358
359       TopoDS_Vertex aV1, aV2;
360       TopExp::Vertices(aWire, aV1, aV2);
361       if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
362         aWire.Closed( true );
363
364       if (!aWire.Closed()) {
365         Standard_ConstructionError::Raise
366           ("Impossible to build a closed wire from the given edges");
367       }
368
369       // try to build face on the wire
370       GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
371       if (aShape.IsNull()) {
372         Standard_ConstructionError::Raise("Face construction failed");
373       }
374
375     } else if (aType == BLOCK_FACE_FOUR_PNT) {
376
377       // Make face from four corner vertices
378       if (anArgs(1).ShapeType() != TopAbs_VERTEX ||
379           anArgs(2).ShapeType() != TopAbs_VERTEX ||
380           anArgs(3).ShapeType() != TopAbs_VERTEX ||
381           anArgs(4).ShapeType() != TopAbs_VERTEX) {
382         Standard_TypeMismatch::Raise("Shape for face construction is not a vertex");
383       }
384
385       TopoDS_Vertex V1 = TopoDS::Vertex(anArgs(1));
386       TopoDS_Vertex V2 = TopoDS::Vertex(anArgs(2));
387       TopoDS_Vertex V3 = TopoDS::Vertex(anArgs(3));
388       TopoDS_Vertex V4 = TopoDS::Vertex(anArgs(4));
389
390       gp_Pnt P1 = BRep_Tool::Pnt(V1);
391       gp_Pnt P2 = BRep_Tool::Pnt(V2);
392       gp_Pnt P3 = BRep_Tool::Pnt(V3);
393       gp_Pnt P4 = BRep_Tool::Pnt(V4);
394
395       if (P1.Distance(P2) < prec || P1.Distance(P3) < prec ||
396           P1.Distance(P4) < prec || P2.Distance(P3) < prec ||
397           P2.Distance(P4) < prec || P3.Distance(P4) < prec) {
398         Standard_ConstructionError::Raise("Four not coincident points must be given");
399       }
400
401       // calculate perimeters
402       Standard_Real per1234 = P1.Distance(P2) + P2.Distance(P3) +
403                               P3.Distance(P4) + P4.Distance(P1);
404       Standard_Real per1243 = P1.Distance(P2) + P2.Distance(P4) +
405                               P4.Distance(P3) + P3.Distance(P1);
406       Standard_Real per1324 = P1.Distance(P3) + P3.Distance(P2) +
407                               P2.Distance(P4) + P4.Distance(P1);
408
409       // order vertices
410       if (per1243 < per1234 && per1243 < per1324) {
411         TopoDS_Vertex Vtmp = V3;
412         V3 = V4;
413         V4 = Vtmp;
414       } else if (per1324 < per1234 && per1324 < per1243) {
415         TopoDS_Vertex Vtmp = V3;
416         V3 = V2;
417         V2 = Vtmp;
418       } else {
419       }
420
421       // build wire
422       BRepBuilderAPI_MakePolygon aMkPoly (V1, V2, V3, V4, Standard_True);
423       if (!aMkPoly.IsDone()) {
424         Standard_ConstructionError::Raise("Polygon construction failed");
425       }
426
427       // try to build face on the wire
428       aMkPoly.Close();
429       GEOMImpl_Block6Explorer::MakeFace(aMkPoly, Standard_False, aShape);
430       if (aShape.IsNull()) {
431         Standard_ConstructionError::Raise("Face construction failed");
432       }
433
434     } else if (aType == BLOCK_SIX_FACES || aType == BLOCK_TWO_FACES) {
435
436       BRepTools_Quilt Glue;
437
438       if (aType == BLOCK_SIX_FACES) {
439         // Make block (hexahedral solid) from six faces
440         for (Standard_Integer ind = 1; ind <= nbshapes; ind++) {
441           if (anArgs(ind).ShapeType() != TopAbs_FACE) {
442             Standard_TypeMismatch::Raise("Shape for block construction is not a face");
443           }
444           Glue.Add(anArgs(ind));
445         }
446
447       }
448       else {
449         // Make block (hexahedral solid) from two opposite faces
450         if (anArgs(1).ShapeType() != TopAbs_FACE ||
451             anArgs(2).ShapeType() != TopAbs_FACE) {
452           Standard_TypeMismatch::Raise("Shape for block construction is not a face");
453         }
454
455         // Get wires of the given faces
456         TopExp_Explorer wires1 (anArgs(1), TopAbs_WIRE);
457         TopExp_Explorer wires2 (anArgs(2), TopAbs_WIRE);
458         if (!wires1.More() || !wires2.More()) {
459           Standard_ConstructionError::Raise("A face for the block has no wires");
460         }
461         TopoDS_Shape aWire1 = wires1.Current();
462         TopoDS_Shape aWire2 = wires2.Current();
463         wires1.Next();
464         wires2.Next();
465         if (wires1.More() || wires2.More()) {
466           Standard_ConstructionError::Raise("A face for the block has more than one wire");
467         }
468
469         GEOMImpl_Block6Explorer aBlockTool;
470         aBlockTool.InitByTwoFaces(anArgs(1), anArgs(2));
471
472         // Construct the linking faces and add them in the gluing tool
473         Glue.Add(anArgs(1));
474         Glue.Add(aBlockTool.GetFace(3, Standard_True));
475         Glue.Add(aBlockTool.GetFace(4, Standard_True));
476         Glue.Add(aBlockTool.GetFace(5, Standard_True));
477         Glue.Add(aBlockTool.GetFace(6, Standard_True));
478         Glue.Add(anArgs(2));
479       }
480
481       TopExp_Explorer exp (Glue.Shells(), TopAbs_SHELL);
482       Standard_Integer ish = 0;
483       TopTools_MapOfShape mapShape;
484       for (; exp.More(); exp.Next()) {
485         if (mapShape.Add(exp.Current())) {
486           aShape = exp.Current();
487           ish++;
488         }
489       }
490
491       if (ish > 1) {
492         aShape = Glue.Shells();
493         Standard_Real aTol = prec; // Precision::Confusion()
494         TopExp_Explorer expF (aShape, TopAbs_FACE);
495         TopTools_MapOfShape mapF;
496         TopoDS_Shell Shell;
497         B.MakeShell(Shell);
498         for (; expF.More(); expF.Next()) {
499           if (mapF.Add(expF.Current())) {
500             B.Add(Shell, expF.Current());
501             Standard_Real aToler = BRep_Tool::Tolerance(TopoDS::Face(expF.Current()));
502             if (aToler > aTol)
503               aTol = aToler;
504           }
505         }
506         ShHealOper_Sewing aHealer (Shell, aTol);
507         if (aHealer.Perform())
508           aShape = aHealer.GetResultShape();
509         else
510           Standard_ConstructionError::Raise
511             ("Impossible to build a connected shell on the given faces");
512       }
513
514       if (aType == BLOCK_SIX_FACES) {
515         if (!aShape.Closed()) {
516           Standard_ConstructionError::Raise
517             ("Impossible to build a closed shell on the given faces");
518         }
519       }
520
521       TopoDS_Solid Sol;
522       B.MakeSolid(Sol);
523       B.Add(Sol, aShape);
524       BRepClass3d_SolidClassifier SC (Sol);
525       SC.PerformInfinitePoint(prec);
526       if (SC.State() == TopAbs_IN) {
527         B.MakeSolid(Sol);
528         B.Add(Sol, aShape.Reversed());
529       }
530       aShape = Sol;
531       BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
532     }
533     else if (aType == BLOCK_COMPOUND_GLUE) {
534       // Make blocks compound from a compound
535       if (anArgs(1).ShapeType() != TopAbs_COMPOUND &&
536           anArgs(2).ShapeType() != TopAbs_COMPSOLID) {
537         Standard_TypeMismatch::Raise("Not a compound given");
538       }
539
540       TopoDS_Shape aCompound = anArgs(1);
541
542       // Glue coincident faces and edges
543       aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
544       //GEOMAlgo_Gluer aGluer;
545       //aGluer.SetShape(aCompound);
546       //aGluer.SetCheckGeometry(Standard_True);
547       //aGluer.Perform();
548       //aShape = aGluer.Result();
549     }
550     else {
551     }
552
553   } else { // Multi-transformations and compound improving
554
555     if (aType == BLOCK_REMOVE_EXTRA ||
556         aType == BLOCK_COMPOUND_IMPROVE) {
557
558       GEOMImpl_IBlockTrsf aCI (aFunction);
559       Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
560       TopoDS_Shape aBlockOrComp = aRefShape->GetValue();
561       if (aBlockOrComp.IsNull()) {
562         Standard_NullObject::Raise("Null Shape given");
563       }
564
565       // 1. Improve solids with seam and/or degenerated edges
566       BlockFix_BlockFixAPI aTool;
567       //aTool.Tolerance() = toler;
568       aTool.OptimumNbFaces() = aCI.GetOptimumNbFaces();
569       aTool.SetShape(aBlockOrComp);
570       aTool.Perform();
571
572       TopoDS_Shape aFixedExtra = aTool.Shape();
573
574       // Repair result
575       BRepCheck_Analyzer ana (aFixedExtra, false);
576       if (!ana.IsValid()) {
577         TopoDS_Shape aFixed;
578         ShHealOper_ShapeProcess aHealer;
579         aHealer.Perform(aFixedExtra, aFixed);
580         if (aHealer.isDone())
581           aFixedExtra = aFixed;
582       }
583
584       if (aType == BLOCK_REMOVE_EXTRA)
585       {
586         aShape = aFixedExtra;
587
588         if (aShape == aBlockOrComp) {
589           MESSAGE("No modifications have been done");
590         }
591       }
592       else // aType == BLOCK_COMPOUND_IMPROVE
593       {
594         // 2. Separate non-blocks
595         TopTools_ListOfShape BLO; // All blocks from the given compound
596         TopTools_ListOfShape NOT; // Not blocks
597         TopTools_ListOfShape EXT; // Hexahedral solids, having degenerated and/or seam edges
598         GEOMImpl_IBlocksOperations::AddBlocksFrom(aFixedExtra, BLO, NOT, EXT);
599
600         if (NOT.Extent() > 0) {
601           MESSAGE("Some non-blocks have been removed");
602         }
603
604         // 3. Warn about staying extra-edges
605         if (EXT.Extent() > 0) {
606           MESSAGE("Warning: Not all seam or degenerated edges was removed");
607         }
608
609         // ??? Throw away standalone blocks ???
610
611         // 4. Create compound of all blocks
612         TopoDS_Compound aComp;
613         BRep_Builder BB;
614         BB.MakeCompound(aComp);
615         TopTools_ListIteratorOfListOfShape BLOit (BLO);
616         for (; BLOit.More(); BLOit.Next()) {
617           BB.Add(aComp, BLOit.Value());
618         }
619
620         // 5. Glue Faces
621         aShape = GEOMImpl_GlueDriver::GlueFaces(aComp, Precision::Confusion(), Standard_False);
622       }
623
624     } else if (aType == BLOCK_MULTI_TRANSFORM_1D ||
625                aType == BLOCK_MULTI_TRANSFORM_2D) {
626
627       TopoDS_Shape aMulti;
628       GEOMImpl_IBlockTrsf aCI (aFunction);
629       Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
630       TopoDS_Shape aBlockIni = aRefShape->GetValue();
631       if (aBlockIni.IsNull()) {
632         Standard_NullObject::Raise("Null Block");
633       }
634
635       // Copy block to avoid problems (PAL6706)
636       TColStd_IndexedDataMapOfTransientTransient aMap;
637       TopoDS_Shape aBlock;
638       TNaming_CopyShape::CopyTool(aBlockIni, aMap, aBlock);
639
640       // Block tolerance in vertices
641       Standard_Real aTol = prec;
642       TopExp_Explorer expV (aBlock, TopAbs_VERTEX);
643       TopTools_MapOfShape mapShape;
644       for (; expV.More(); expV.Next()) {
645         if (mapShape.Add(expV.Current())) {
646           TopoDS_Vertex aV = TopoDS::Vertex(expV.Current());
647           aTol = Max(BRep_Tool::Tolerance(aV), aTol);
648         }
649       }
650
651       if (aType == BLOCK_MULTI_TRANSFORM_1D) {
652         // Retrieve a faces by Ids
653         Standard_Integer aFace1Id = aCI.GetFace1U();
654         Standard_Integer aFace2Id = aCI.GetFace2U();
655         TopoDS_Shape aFace1, aFace2;
656         if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1Id, aFace1)) {
657           Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
658         }
659         if (aFace1.ShapeType() != TopAbs_FACE) {
660           Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
661         }
662
663         if (aFace2Id > 0) {
664           if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2Id, aFace2)) {
665             Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
666           }
667           if (aFace2.ShapeType() != TopAbs_FACE) {
668             Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
669           }
670         }
671
672         Standard_Integer aNbIter = aCI.GetNbIterU();
673
674         MultiTransformate1D(aBlock, aFace1, aFace2, aNbIter, aMulti);
675
676       } else { // aType == BLOCK_MULTI_TRANSFORM_2D
677         // Retrieve a faces by Ids
678         Standard_Integer aFace1UId = aCI.GetFace1U();
679         Standard_Integer aFace2UId = aCI.GetFace2U();
680         Standard_Integer aFace1VId = aCI.GetFace1V();
681         Standard_Integer aFace2VId = aCI.GetFace2V();
682
683         TopoDS_Shape aFace1U, aFace2U, aFace1V, aFace2V;
684         if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1UId, aFace1U) ||
685             !GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1VId, aFace1V)) {
686           Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
687         }
688
689         if (aFace1U.ShapeType() != TopAbs_FACE ||
690             aFace1V.ShapeType() != TopAbs_FACE) {
691           Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
692         }
693
694         if (aFace2UId > 0) {
695           if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2UId, aFace2U)) {
696             Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
697           }
698
699           if (aFace2U.ShapeType() != TopAbs_FACE) {
700             Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
701           }
702         }
703
704         if (aFace2VId > 0) {
705           if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2VId, aFace2V)) {
706             Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
707           }
708
709           if (aFace2V.ShapeType() != TopAbs_FACE) {
710             Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
711           }
712         }
713
714         Standard_Integer aNbIterU = aCI.GetNbIterU();
715         Standard_Integer aNbIterV = aCI.GetNbIterV();
716
717         MultiTransformate2D(aBlock,
718                             aFace1U, aFace2U, aNbIterU,
719                             aFace1V, aFace2V, aNbIterV, aMulti);
720       }
721
722       if (aMulti.IsNull()) {
723         StdFail_NotDone::Raise("Multi-transformation failed");
724       }
725
726       // Glue faces of the multi-block
727       aShape = GEOMImpl_GlueDriver::GlueFaces(aMulti, aTol, Standard_False);
728
729     } else { // unknown function type
730       return 0;
731     }
732   }
733
734   if (aShape.IsNull()) return 0;
735
736   aFunction->SetValue(aShape);
737
738   log.SetTouched(Label());
739
740   return 1;
741 }
742
743 //=======================================================================
744 //function :  MultiTransformate1D
745 //purpose  :
746 //=======================================================================
747 void GEOMImpl_BlockDriver::MultiTransformate1D (const TopoDS_Shape&    theBlock,
748                                                 const TopoDS_Shape&    theFace1,
749                                                 const TopoDS_Shape&    theFace2,
750                                                 const Standard_Integer theNbIter,
751                                                 TopoDS_Shape&          theResult) const
752 {
753   // Construct Tool, where <theFace1> will be the first face,
754   // and a face, opposite to <theFace1>, will be the second face
755   GEOMImpl_Block6Explorer aBlockTool;
756   aBlockTool.InitByBlockAndFace(theBlock, theFace1);
757
758   // Find IDs of the faces
759   Standard_Integer dir_face1 = 1, dir_face2 = 2;
760   if (!theFace2.IsNull()) {
761     dir_face2 = aBlockTool.GetFaceID(theFace2);
762   }
763
764   // Find three pairs of points
765   Standard_Integer v11_id = 0, v12_id = 0, v13_id = 0; // vertices of the first face
766   Standard_Integer v21_id = 0, v22_id = 0, v23_id = 0; // vertices of the second face
767
768   if (dir_face2 == 2) { // <theFace2> is opposite to <theFace1>
769
770     // We will take vertices with equal local numbers on the faces,
771     // as the Block6Explorer gives equal local numbers
772     // to the linked vertices on the opposite faces,
773     // i.e. v1* is linked with the v2* by an edge:
774
775     //          _________
776     //        /|v23     /|
777     //       / |       / |    dir_face1 - bottom
778     //      /  |      /  |
779     //     /________ /   |    dir_face2 - top
780     //    |v21 |    |v22 |
781     //    |    |____|____|
782     //    |   / v13 |   /
783     //    |  /      |  /
784     //    | /       | /
785     //    |/________|/
786     //     v11       v12
787
788     v11_id = aBlockTool.GetVertexID(dir_face1, 1);
789     v12_id = aBlockTool.GetVertexID(dir_face1, 2);
790     v13_id = aBlockTool.GetVertexID(dir_face1, 4);
791
792     v21_id = aBlockTool.GetVertexID(dir_face2, 1);
793     v22_id = aBlockTool.GetVertexID(dir_face2, 2);
794     v23_id = aBlockTool.GetVertexID(dir_face2, 4);
795
796   } else {
797
798     //          _________
799     //        /|        /|
800     //       / |       / |    dir_face1 - bottom
801     //      /  |      /  |
802     //     /________ /   |    dir_face2 - right (for example)
803     //    |    |    |v23 |
804     //    |    |____|____|
805     //    |   /     |   /v12 = v22 (common_vertex2)
806     //    |  /      |  /
807     //    | /       | /
808     //    |/________|/
809     //     v13       v11 = v21 (common_vertex1)
810
811     Standard_Integer common_edge_id = aBlockTool.FindCommonEdgeID(dir_face1, dir_face2);
812     Standard_Integer common_vertex1 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 1);
813     Standard_Integer common_vertex2 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 2);
814
815     Standard_Integer not_common_v1 = 0;
816     Standard_Integer vid = 1;
817     Standard_Boolean isFound = Standard_False;
818     while (!isFound && vid <= 4) {
819       not_common_v1 = aBlockTool.GetVertexID(dir_face1, vid);
820       isFound = (not_common_v1 != common_vertex2 &&
821                  aBlockTool.FindEdgeID(not_common_v1, common_vertex1) != 0);
822       vid++;
823     }
824
825     Standard_Integer not_common_v2 = 0;
826     vid = 1;
827     isFound = Standard_False;
828     while (!isFound && vid <= 4) {
829       not_common_v2 = aBlockTool.GetVertexID(dir_face2, vid);
830       isFound = (not_common_v2 != common_vertex2 &&
831                  aBlockTool.FindEdgeID(not_common_v2, common_vertex1) != 0);
832       vid++;
833     }
834
835     v11_id = common_vertex1;
836     v12_id = common_vertex2;
837     v13_id = not_common_v1;
838
839     v21_id = common_vertex1;
840     v22_id = common_vertex2;
841     v23_id = not_common_v2;
842   }
843
844   // Construct a transformation operator
845   TopoDS_Vertex V11 = TopoDS::Vertex(aBlockTool.GetVertex(v11_id));
846   TopoDS_Vertex V12 = TopoDS::Vertex(aBlockTool.GetVertex(v12_id));
847   TopoDS_Vertex V13 = TopoDS::Vertex(aBlockTool.GetVertex(v13_id));
848
849   TopoDS_Vertex V21 = TopoDS::Vertex(aBlockTool.GetVertex(v21_id));
850   TopoDS_Vertex V22 = TopoDS::Vertex(aBlockTool.GetVertex(v22_id));
851   TopoDS_Vertex V23 = TopoDS::Vertex(aBlockTool.GetVertex(v23_id));
852
853   // Axes of the first direction face
854   gp_Pnt P1 = BRep_Tool::Pnt(V11);
855   gp_Vec VecN1 (P1, BRep_Tool::Pnt(V12));
856   gp_Vec VecX1 (P1, BRep_Tool::Pnt(V13));
857   gp_Ax3 Ax1 (P1, VecN1, VecX1);
858
859   // Axes of the second direction face
860   gp_Pnt P2 = BRep_Tool::Pnt(V21);
861   gp_Vec VecN2 (P2, BRep_Tool::Pnt(V22));
862   gp_Vec VecX2 (P2, BRep_Tool::Pnt(V23));
863   gp_Ax3 Ax2 (P2, VecN2, VecX2);
864
865   gp_Trsf aTrsf;
866   aTrsf.SetDisplacement(Ax1, Ax2);
867
868   // Check, that <theFace2> is similar to <theFace1>.
869   // Actually, we need only to check right position of one
870   // vertex, not involved into the transformation construction.
871   if (!aBlockTool.IsSimilarFaces(dir_face1, dir_face2, aTrsf)) {
872     Standard_ConstructionError::Raise("The direction faces are not similar");
873   }
874
875   // Perform multi-transformation
876   TopoDS_Compound aCompound;
877   BRep_Builder B;
878   B.MakeCompound(aCompound);
879
880   TopoDS_Shape aPrevShape = theBlock;
881   for (Standard_Integer i = 0; i < theNbIter; i++) {
882     B.Add(aCompound, aPrevShape);
883     BRepBuilderAPI_Transform aTransformation (aPrevShape, aTrsf, Standard_False);
884     aPrevShape = aTransformation.Shape();
885   }
886   theResult = aCompound;
887 }
888
889 //=======================================================================
890 //function :  MultiTransformate2D
891 //purpose  :
892 //=======================================================================
893 void GEOMImpl_BlockDriver::MultiTransformate2D (const TopoDS_Shape&    theBlock,
894                                                 const TopoDS_Shape&    theFace1U,
895                                                 const TopoDS_Shape&    theFace2U,
896                                                 const Standard_Integer theNbIterU,
897                                                 const TopoDS_Shape&    theFace1V,
898                                                 const TopoDS_Shape&    theFace2V,
899                                                 const Standard_Integer theNbIterV,
900                                                 TopoDS_Shape&          theResult) const
901 {
902   // Construct Tool, where <theFace1U> will be the first face,
903   // and a face, opposite to <theFace1U>, will be the second face
904   GEOMImpl_Block6Explorer aBlockTool;
905   aBlockTool.InitByBlockAndFace(theBlock, theFace1U);
906
907   gp_Trsf aTrsfU, aTrsfV;
908   gp_Ax3 Ax1V, Ax2V;
909   for (Standard_Integer uv = 1; uv <= 2; uv++) {
910     // U transformation
911     TopoDS_Shape theFace1 = theFace1U;
912     TopoDS_Shape theFace2 = theFace2U;
913     if (uv == 2) {
914       // V transformation
915       theFace1 = theFace1V;
916       theFace2 = theFace2V;
917     }
918
919     // Find IDs of the faces
920     Standard_Integer dir_face1 = aBlockTool.GetFaceID(theFace1);
921     Standard_Integer dir_face2 = 0;
922     Standard_Integer opp_face1 = aBlockTool.GetOppositeFaceID(dir_face1);
923     if (theFace2.IsNull()) {
924       dir_face2 = opp_face1;
925     } else {
926       dir_face2 = aBlockTool.GetFaceID(theFace2);
927     }
928
929     // Find three pairs of points
930     Standard_Integer v11_id = 0, v12_id = 0, v13_id = 0; // vertices of the first face
931     Standard_Integer v21_id = 0, v22_id = 0, v23_id = 0; // vertices of the second face
932
933     if (dir_face2 == opp_face1) { // <theFace2> is opposite to <theFace1>
934
935       // We will take vertices with equal local numbers on the faces,
936       // as the Block6Explorer gives equal local numbers
937       // to the linked vertices on the opposite faces,
938       // i.e. v1* is linked with the v2* by an edge:
939
940       v11_id = aBlockTool.GetVertexID(dir_face1, 1);
941       v12_id = aBlockTool.GetVertexID(dir_face1, 2);
942       v13_id = aBlockTool.GetVertexID(dir_face1, 4);
943
944       v21_id = aBlockTool.GetVertexID(dir_face2, 1);
945       v22_id = aBlockTool.GetVertexID(dir_face2, 2);
946       v23_id = aBlockTool.GetVertexID(dir_face2, 4);
947
948     } else {
949
950       Standard_Integer common_edge_id = aBlockTool.FindCommonEdgeID(dir_face1, dir_face2);
951       Standard_Integer common_vertex1 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 1);
952       Standard_Integer common_vertex2 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 2);
953
954       Standard_Integer not_common_v1 = 0;
955       Standard_Integer vid = 1;
956       Standard_Boolean isFound = Standard_False;
957       while (!isFound && vid <= 4) {
958         not_common_v1 = aBlockTool.GetVertexID(dir_face1, vid);
959         isFound = (not_common_v1 != common_vertex2 &&
960                    aBlockTool.FindEdgeID(not_common_v1, common_vertex1) != 0);
961         vid++;
962       }
963
964       Standard_Integer not_common_v2 = 0;
965       vid = 1;
966       isFound = Standard_False;
967       while (!isFound && vid <= 4) {
968         not_common_v2 = aBlockTool.GetVertexID(dir_face2, vid);
969         isFound = (not_common_v2 != common_vertex2 &&
970                    aBlockTool.FindEdgeID(not_common_v2, common_vertex1) != 0);
971         vid++;
972       }
973
974       v11_id = common_vertex1;
975       v12_id = common_vertex2;
976       v13_id = not_common_v1;
977
978       v21_id = common_vertex1;
979       v22_id = common_vertex2;
980       v23_id = not_common_v2;
981     }
982
983     // Construct a transformation operator
984     TopoDS_Vertex V11 = TopoDS::Vertex(aBlockTool.GetVertex(v11_id));
985     TopoDS_Vertex V12 = TopoDS::Vertex(aBlockTool.GetVertex(v12_id));
986     TopoDS_Vertex V13 = TopoDS::Vertex(aBlockTool.GetVertex(v13_id));
987
988     TopoDS_Vertex V21 = TopoDS::Vertex(aBlockTool.GetVertex(v21_id));
989     TopoDS_Vertex V22 = TopoDS::Vertex(aBlockTool.GetVertex(v22_id));
990     TopoDS_Vertex V23 = TopoDS::Vertex(aBlockTool.GetVertex(v23_id));
991
992     // Axes of the first direction face
993     gp_Pnt P1 = BRep_Tool::Pnt(V11);
994     gp_Vec VecN1 (P1, BRep_Tool::Pnt(V12));
995     gp_Vec VecX1 (P1, BRep_Tool::Pnt(V13));
996     gp_Ax3 Ax1 (P1, VecN1, VecX1);
997
998     // Axes of the second direction face
999     gp_Pnt P2 = BRep_Tool::Pnt(V21);
1000     gp_Vec VecN2 (P2, BRep_Tool::Pnt(V22));
1001     gp_Vec VecX2 (P2, BRep_Tool::Pnt(V23));
1002     gp_Ax3 Ax2 (P2, VecN2, VecX2);
1003
1004     gp_Trsf aTrsf;
1005     aTrsf.SetDisplacement(Ax1, Ax2);
1006     if (uv == 1) {
1007       aTrsfU = aTrsf;
1008     } else {
1009       aTrsfV = aTrsf;
1010       Ax1V = Ax1;
1011       Ax2V = Ax2;
1012     }
1013
1014     // Check, that <theFace2> is similar to <theFace1>.
1015     // Actually, we need only to check right position of one
1016     // vertex, not involved into the transformation construction.
1017     if (!aBlockTool.IsSimilarFaces(dir_face1, dir_face2, aTrsf)) {
1018       Standard_ConstructionError::Raise("The direction faces are not similar");
1019     }
1020   }
1021
1022   // Perform multi-transformation
1023   TopoDS_Compound aCompound;
1024   BRep_Builder B;
1025   B.MakeCompound(aCompound);
1026
1027   TopoDS_Shape aPrevShapeU = theBlock;
1028   for (int i = 0; i < theNbIterU; i++) {
1029     TopoDS_Shape aPrevShapeV = aPrevShapeU;
1030     for (int j = 0; j < theNbIterV; j++) {
1031       B.Add(aCompound, aPrevShapeV);
1032       BRepBuilderAPI_Transform aTransformationV (aPrevShapeV, aTrsfV, Standard_False);
1033       aPrevShapeV = aTransformationV.Shape();
1034     }
1035     BRepBuilderAPI_Transform aTransformationU (aPrevShapeU, aTrsfU, Standard_False);
1036     aPrevShapeU = aTransformationU.Shape();
1037     // Correction of the second transformation according to the first transformation
1038     Ax1V.Transform(aTrsfU);
1039     Ax2V.Transform(aTrsfU);
1040     aTrsfV.SetDisplacement(Ax1V, Ax2V);
1041     // Correction done
1042   }
1043   theResult = aCompound;
1044 }
1045
1046 //=======================================================================
1047 //function :  GEOMImpl_BlockDriver_Type_
1048 //purpose  :
1049 //=======================================================================
1050 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BlockDriver_Type_()
1051 {
1052
1053   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
1054   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
1055   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
1056   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
1057   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
1058   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
1059
1060
1061   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
1062   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BlockDriver",
1063                                                          sizeof(GEOMImpl_BlockDriver),
1064                                                          1,
1065                                                          (Standard_Address)_Ancestors,
1066                                                          (Standard_Address)NULL);
1067
1068   return _aType;
1069 }
1070
1071 //=======================================================================
1072 //function : DownCast
1073 //purpose  :
1074 //=======================================================================
1075 const Handle(GEOMImpl_BlockDriver) Handle(GEOMImpl_BlockDriver)::DownCast
1076   (const Handle(Standard_Transient)& AnObject)
1077 {
1078   Handle(GEOMImpl_BlockDriver) _anOtherObject;
1079
1080   if (!AnObject.IsNull()) {
1081      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BlockDriver))) {
1082        _anOtherObject = Handle(GEOMImpl_BlockDriver)((Handle(GEOMImpl_BlockDriver)&)AnObject);
1083      }
1084   }
1085
1086   return _anOtherObject;
1087 }