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