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