Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IBlocksOperations.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #ifdef WNT
21 #pragma warning( disable:4786 )
22 #endif
23
24 using namespace std;
25
26 #include <Standard_Stream.hxx>
27
28 #include <GEOMImpl_IBlocksOperations.hxx>
29
30 #include <GEOMImpl_Types.hxx>
31
32 #include <GEOMImpl_BlockDriver.hxx>
33 #include <GEOMImpl_IBlocks.hxx>
34 #include <GEOMImpl_IBlockTrsf.hxx>
35 #include <GEOMImpl_CopyDriver.hxx>
36 #include <GEOMImpl_Block6Explorer.hxx>
37
38 #include <GEOM_Function.hxx>
39 #include <GEOM_PythonDump.hxx>
40
41 #include <GEOMAlgo_GlueAnalyser.hxx>
42 #include <GEOMAlgo_CoupleOfShapes.hxx>
43 #include <GEOMAlgo_ListOfCoupleOfShapes.hxx>
44 #include <GEOMAlgo_ListIteratorOfListOfCoupleOfShapes.hxx>
45 #include <BlockFix_CheckTool.hxx>
46
47 #include "utilities.h"
48 #include <OpUtil.hxx>
49 #include <Utils_ExceptHandlers.hxx>
50
51 #include <TFunction_DriverTable.hxx>
52 #include <TFunction_Driver.hxx>
53 #include <TFunction_Logbook.hxx>
54 #include <TDataStd_Integer.hxx>
55 #include <TDF_Tool.hxx>
56
57 #include <BRep_Tool.hxx>
58 #include <BRep_Builder.hxx>
59 #include <BRepTools.hxx>
60 #include <BRepTools_WireExplorer.hxx>
61 #include <BRepGProp.hxx>
62 #include <BRepBndLib.hxx>
63 #include <BRepAdaptor_Surface.hxx>
64 #include <BRepClass_FaceClassifier.hxx>
65 #include <BRepClass3d_SolidClassifier.hxx>
66 #include <BRepExtrema_DistShapeShape.hxx>
67
68 #include <TopAbs.hxx>
69 #include <TopoDS.hxx>
70 #include <TopoDS_Edge.hxx>
71 #include <TopoDS_Vertex.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_Array1OfShape.hxx>
78 #include <TopTools_IndexedMapOfShape.hxx>
79 #include <TopTools_DataMapOfShapeInteger.hxx>
80 #include <TopTools_ListIteratorOfListOfShape.hxx>
81 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
82 #include <TopTools_DataMapIteratorOfDataMapOfShapeInteger.hxx>
83
84 #include <Bnd_Box.hxx>
85 #include <GProp_GProps.hxx>
86
87 #include <Geom_Surface.hxx>
88 #include <ShapeAnalysis_Surface.hxx>
89
90 #include <TColStd_MapOfInteger.hxx>
91 #include <TColStd_Array1OfReal.hxx>
92 #include <TColStd_Array1OfInteger.hxx>
93 #include <TColStd_Array2OfInteger.hxx>
94
95 //#include <OSD_Timer.hxx>
96
97 #include <Precision.hxx>
98
99 #include <Standard_Failure.hxx>
100 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
101
102 //=============================================================================
103 /*!
104  *   constructor:
105  */
106 //=============================================================================
107 GEOMImpl_IBlocksOperations::GEOMImpl_IBlocksOperations (GEOM_Engine* theEngine, int theDocID)
108 : GEOM_IOperations(theEngine, theDocID)
109 {
110   MESSAGE("GEOMImpl_IBlocksOperations::GEOMImpl_IBlocksOperations");
111 }
112
113 //=============================================================================
114 /*!
115  *  destructor
116  */
117 //=============================================================================
118 GEOMImpl_IBlocksOperations::~GEOMImpl_IBlocksOperations()
119 {
120   MESSAGE("GEOMImpl_IBlocksOperations::~GEOMImpl_IBlocksOperations");
121 }
122
123
124 //=============================================================================
125 /*!
126  *  MakeQuad
127  */
128 //=============================================================================
129 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::MakeQuad
130                      (Handle(GEOM_Object) theEdge1, Handle(GEOM_Object) theEdge2,
131                       Handle(GEOM_Object) theEdge3, Handle(GEOM_Object) theEdge4)
132 {
133   SetErrorCode(KO);
134
135   if (theEdge1.IsNull() || theEdge2.IsNull() ||
136       theEdge3.IsNull() || theEdge4.IsNull()) return NULL;
137
138   //Add a new Face object
139   Handle(GEOM_Object) aFace = GetEngine()->AddObject(GetDocID(), GEOM_FACE);
140
141   //Add a new Face function
142   Handle(GEOM_Function) aFunction =
143     aFace->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_FACE_FOUR_EDGES);
144
145   //Check if the function is set correctly
146   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
147
148   GEOMImpl_IBlocks aPI (aFunction);
149
150   Handle(GEOM_Function) aRef1 = theEdge1->GetLastFunction();
151   Handle(GEOM_Function) aRef2 = theEdge2->GetLastFunction();
152   Handle(GEOM_Function) aRef3 = theEdge3->GetLastFunction();
153   Handle(GEOM_Function) aRef4 = theEdge4->GetLastFunction();
154   if (aRef1.IsNull() || aRef2.IsNull() ||
155       aRef3.IsNull() || aRef4.IsNull()) return NULL;
156
157   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
158   aShapesSeq->Append(aRef1);
159   aShapesSeq->Append(aRef2);
160   aShapesSeq->Append(aRef3);
161   aShapesSeq->Append(aRef4);
162
163   aPI.SetShapes(aShapesSeq);
164
165   //Compute the Face value
166   try {
167 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
168     OCC_CATCH_SIGNALS;
169 #endif
170     if (!GetSolver()->ComputeFunction(aFunction)) {
171       SetErrorCode("Block driver failed to compute a face");
172       return NULL;
173     }
174   }
175   catch (Standard_Failure) {
176     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
177     SetErrorCode(aFail->GetMessageString());
178     return NULL;
179   }
180
181   //Make a Python command
182   GEOM::TPythonDump(aFunction) << aFace << " = geompy.MakeQuad("
183     << theEdge1 << ", " << theEdge2 << ", " << theEdge3 << ", " << theEdge4 << ")";
184
185   SetErrorCode(OK);
186   return aFace;
187 }
188
189 //=============================================================================
190 /*!
191  *  MakeQuad2Edges
192  */
193 //=============================================================================
194 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::MakeQuad2Edges
195                      (Handle(GEOM_Object) theEdge1, Handle(GEOM_Object) theEdge2)
196 {
197   SetErrorCode(KO);
198
199   if (theEdge1.IsNull() || theEdge2.IsNull()) return NULL;
200
201   //Add a new Face object
202   Handle(GEOM_Object) aFace = GetEngine()->AddObject(GetDocID(), GEOM_FACE);
203
204   //Add a new Face function
205   Handle(GEOM_Function) aFunction =
206     aFace->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_FACE_TWO_EDGES);
207
208   //Check if the function is set correctly
209   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
210
211   GEOMImpl_IBlocks aPI (aFunction);
212
213   Handle(GEOM_Function) aRef1 = theEdge1->GetLastFunction();
214   Handle(GEOM_Function) aRef2 = theEdge2->GetLastFunction();
215   if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
216
217   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
218   aShapesSeq->Append(aRef1);
219   aShapesSeq->Append(aRef2);
220
221   aPI.SetShapes(aShapesSeq);
222
223   //Compute the Face value
224   try {
225 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
226     OCC_CATCH_SIGNALS;
227 #endif
228     if (!GetSolver()->ComputeFunction(aFunction)) {
229       SetErrorCode("Block driver failed to compute a face");
230       return NULL;
231     }
232   }
233   catch (Standard_Failure) {
234     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
235     SetErrorCode(aFail->GetMessageString());
236     return NULL;
237   }
238
239   //Make a Python command
240   GEOM::TPythonDump(aFunction) << aFace << " = geompy.MakeQuad2Edges("
241                                << theEdge1 << ", " << theEdge2 << ")";
242
243   SetErrorCode(OK);
244   return aFace;
245 }
246
247 //=============================================================================
248 /*!
249  *  MakeQuad4Vertices
250  */
251 //=============================================================================
252 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::MakeQuad4Vertices
253                      (Handle(GEOM_Object) thePnt1, Handle(GEOM_Object) thePnt2,
254                       Handle(GEOM_Object) thePnt3, Handle(GEOM_Object) thePnt4)
255 {
256   SetErrorCode(KO);
257
258   if (thePnt1.IsNull() || thePnt2.IsNull() ||
259       thePnt3.IsNull() || thePnt4.IsNull()) return NULL;
260
261   //Add a new Face object
262   Handle(GEOM_Object) aFace = GetEngine()->AddObject(GetDocID(), GEOM_FACE);
263
264   //Add a new Face function
265   Handle(GEOM_Function) aFunction =
266     aFace->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_FACE_FOUR_PNT);
267
268   //Check if the function is set correctly
269   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
270
271   GEOMImpl_IBlocks aPI (aFunction);
272
273   Handle(GEOM_Function) aRef1 = thePnt1->GetLastFunction();
274   Handle(GEOM_Function) aRef2 = thePnt2->GetLastFunction();
275   Handle(GEOM_Function) aRef3 = thePnt3->GetLastFunction();
276   Handle(GEOM_Function) aRef4 = thePnt4->GetLastFunction();
277   if (aRef1.IsNull() || aRef2.IsNull() ||
278       aRef3.IsNull() || aRef4.IsNull()) return NULL;
279
280   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
281   aShapesSeq->Append(aRef1);
282   aShapesSeq->Append(aRef2);
283   aShapesSeq->Append(aRef3);
284   aShapesSeq->Append(aRef4);
285
286   aPI.SetShapes(aShapesSeq);
287
288   //Compute the Face value
289   try {
290 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
291     OCC_CATCH_SIGNALS;
292 #endif
293     if (!GetSolver()->ComputeFunction(aFunction)) {
294       SetErrorCode("Block driver failed to compute a face");
295       return NULL;
296     }
297   }
298   catch (Standard_Failure) {
299     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
300     SetErrorCode(aFail->GetMessageString());
301     return NULL;
302   }
303
304   //Make a Python command
305   GEOM::TPythonDump(aFunction) << aFace << " = geompy.MakeQuad4Vertices("
306     << thePnt1 << ", " << thePnt2 << ", " << thePnt3 << ", " << thePnt4 << ")";
307
308   SetErrorCode(OK);
309   return aFace;
310 }
311
312 //=============================================================================
313 /*!
314  *  MakeHexa
315  */
316 //=============================================================================
317 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::MakeHexa
318                      (Handle(GEOM_Object) theFace1, Handle(GEOM_Object) theFace2,
319                       Handle(GEOM_Object) theFace3, Handle(GEOM_Object) theFace4,
320                       Handle(GEOM_Object) theFace5, Handle(GEOM_Object) theFace6)
321 {
322   SetErrorCode(KO);
323
324   if (theFace1.IsNull() || theFace2.IsNull() ||
325       theFace3.IsNull() || theFace4.IsNull() ||
326       theFace5.IsNull() || theFace6.IsNull()) return NULL;
327
328   //Add a new Solid object
329   Handle(GEOM_Object) aBlock = GetEngine()->AddObject(GetDocID(), GEOM_BLOCK);
330
331   //Add a new Block function
332   Handle(GEOM_Function) aFunction =
333     aBlock->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_SIX_FACES);
334
335   //Check if the function is set correctly
336   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
337
338   GEOMImpl_IBlocks aPI (aFunction);
339
340   Handle(GEOM_Function) aRef1 = theFace1->GetLastFunction();
341   Handle(GEOM_Function) aRef2 = theFace2->GetLastFunction();
342   Handle(GEOM_Function) aRef3 = theFace3->GetLastFunction();
343   Handle(GEOM_Function) aRef4 = theFace4->GetLastFunction();
344   Handle(GEOM_Function) aRef5 = theFace5->GetLastFunction();
345   Handle(GEOM_Function) aRef6 = theFace6->GetLastFunction();
346   if (aRef1.IsNull() || aRef2.IsNull() ||
347       aRef3.IsNull() || aRef4.IsNull() ||
348       aRef5.IsNull() || aRef6.IsNull()) return NULL;
349
350   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
351   aShapesSeq->Append(aRef1);
352   aShapesSeq->Append(aRef2);
353   aShapesSeq->Append(aRef3);
354   aShapesSeq->Append(aRef4);
355   aShapesSeq->Append(aRef5);
356   aShapesSeq->Append(aRef6);
357
358   aPI.SetShapes(aShapesSeq);
359
360   //Compute the Block value
361   try {
362 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
363     OCC_CATCH_SIGNALS;
364 #endif
365     if (!GetSolver()->ComputeFunction(aFunction)) {
366       SetErrorCode("Block driver failed to compute a block");
367       return NULL;
368     }
369   }
370   catch (Standard_Failure) {
371     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
372     SetErrorCode(aFail->GetMessageString());
373     return NULL;
374   }
375
376   //Make a Python command
377   GEOM::TPythonDump(aFunction) << aBlock << " = geompy.MakeHexa("
378     << theFace1 << ", " << theFace2 << ", " << theFace3 << ", "
379       << theFace4 << ", " << theFace5 << ", " << theFace6 << ")";
380
381   SetErrorCode(OK);
382   return aBlock;
383 }
384
385 //=============================================================================
386 /*!
387  *  MakeHexa2Faces
388  */
389 //=============================================================================
390 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::MakeHexa2Faces
391                    (Handle(GEOM_Object) theFace1, Handle(GEOM_Object) theFace2)
392 {
393   SetErrorCode(KO);
394
395   if (theFace1.IsNull() || theFace2.IsNull()) return NULL;
396
397   //Add a new Solid object
398   Handle(GEOM_Object) aBlock = GetEngine()->AddObject(GetDocID(), GEOM_BLOCK);
399
400   //Add a new Block function
401   Handle(GEOM_Function) aFunction =
402     aBlock->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_TWO_FACES);
403
404   //Check if the function is set correctly
405   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
406
407   GEOMImpl_IBlocks aPI (aFunction);
408
409   Handle(GEOM_Function) aRef1 = theFace1->GetLastFunction();
410   Handle(GEOM_Function) aRef2 = theFace2->GetLastFunction();
411   if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
412
413   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
414   aShapesSeq->Append(aRef1);
415   aShapesSeq->Append(aRef2);
416
417   aPI.SetShapes(aShapesSeq);
418
419   //Compute the Block value
420   try {
421 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
422     OCC_CATCH_SIGNALS;
423 #endif
424     if (!GetSolver()->ComputeFunction(aFunction)) {
425       SetErrorCode("Block driver failed to compute a block");
426       return NULL;
427     }
428   }
429   catch (Standard_Failure) {
430     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
431     SetErrorCode(aFail->GetMessageString());
432     return NULL;
433   }
434
435   //Make a Python command
436   GEOM::TPythonDump(aFunction) << aBlock << " = geompy.MakeHexa2Faces("
437                                << theFace1 << ", " << theFace2 << ")";
438
439   SetErrorCode(OK);
440   return aBlock;
441 }
442
443 //=============================================================================
444 /*!
445  *  MakeBlockCompound
446  */
447 //=============================================================================
448 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::MakeBlockCompound
449                                               (Handle(GEOM_Object) theCompound)
450 {
451   SetErrorCode(KO);
452
453   if (theCompound.IsNull()) return NULL;
454
455   //Add a new object
456   Handle(GEOM_Object) aBlockComp = GetEngine()->AddObject(GetDocID(), GEOM_COMPOUND);
457
458   //Add a new BlocksComp function
459   Handle(GEOM_Function) aFunction =
460     aBlockComp->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_COMPOUND_GLUE);
461
462   //Check if the function is set correctly
463   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
464
465   GEOMImpl_IBlocks aPI (aFunction);
466
467   Handle(GEOM_Function) aRef1 = theCompound->GetLastFunction();
468   if (aRef1.IsNull()) return NULL;
469
470   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
471   aShapesSeq->Append(aRef1);
472
473   aPI.SetShapes(aShapesSeq);
474
475   //Compute the Blocks Compound value
476   try {
477 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
478     OCC_CATCH_SIGNALS;
479 #endif
480     if (!GetSolver()->ComputeFunction(aFunction)) {
481       SetErrorCode("Block driver failed to compute a blocks compound");
482       return NULL;
483     }
484   }
485   catch (Standard_Failure) {
486     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
487     SetErrorCode(aFail->GetMessageString());
488     return NULL;
489   }
490
491   //Make a Python command
492   GEOM::TPythonDump(aFunction) << aBlockComp
493     << " = geompy.MakeBlockCompound(" << theCompound << ")";
494
495   SetErrorCode(OK);
496   return aBlockComp;
497 }
498
499 //=============================================================================
500 /*!
501  *  GetPoint
502  */
503 //=============================================================================
504 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetPoint
505                                                (Handle(GEOM_Object) theShape,
506                                                 const Standard_Real theX,
507                                                 const Standard_Real theY,
508                                                 const Standard_Real theZ,
509                                                 const Standard_Real theEpsilon)
510 {
511   SetErrorCode(KO);
512
513   //New Point object
514   Handle(GEOM_Object) aResult;
515
516   // Arguments
517   if (theShape.IsNull()) return NULL;
518
519   TopoDS_Shape aBlockOrComp = theShape->GetValue();
520   if (aBlockOrComp.IsNull()) {
521     SetErrorCode("Given shape is null");
522     return NULL;
523   }
524
525   //Compute the Vertex value
526   gp_Pnt P (theX, theY, theZ);
527   Standard_Real eps = Max(theEpsilon, Precision::Confusion());
528
529   TopoDS_Shape V;
530   Standard_Integer isFound = 0;
531   TopTools_MapOfShape mapShape;
532   TopExp_Explorer exp (aBlockOrComp, TopAbs_VERTEX);
533
534   for (; exp.More(); exp.Next()) {
535     if (mapShape.Add(exp.Current())) {
536       TopoDS_Vertex aVi = TopoDS::Vertex(exp.Current());
537       gp_Pnt aPi = BRep_Tool::Pnt(aVi);
538       if (aPi.Distance(P) < eps) {
539         V = aVi;
540         isFound++;
541       }
542     }
543   }
544
545   if (isFound == 0) {
546     SetErrorCode("Vertex has not been found");
547     return NULL;
548   } else if (isFound > 1) {
549     SetErrorCode("Multiple vertices found by the given coordinates and epsilon");
550     return NULL;
551   } else {
552     TopTools_IndexedMapOfShape anIndices;
553     TopExp::MapShapes(aBlockOrComp, anIndices);
554     Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
555     anArray->SetValue(1, anIndices.FindIndex(V));
556     aResult = GetEngine()->AddSubShape(theShape, anArray);
557   }
558
559   //The GetPoint() doesn't change object so no new function is required.
560   Handle(GEOM_Function) aFunction = theShape->GetLastFunction();
561
562   //Make a Python command
563   GEOM::TPythonDump(aFunction, /*append=*/true)
564     << aResult << " = geompy.GetPoint(" << theShape << ", "
565     << theX << ", " << theY << ", " << theZ << ", " << theEpsilon << ")";
566
567   SetErrorCode(OK);
568   return aResult;
569 }
570
571 //=============================================================================
572 /*!
573  *  GetEdge
574  */
575 //=============================================================================
576 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetEdge
577                                                 (Handle(GEOM_Object) theShape,
578                                                  Handle(GEOM_Object) thePoint1,
579                                                  Handle(GEOM_Object) thePoint2)
580 {
581   SetErrorCode(KO);
582
583   //New Edge object
584   Handle(GEOM_Object) aResult;
585
586   // Arguments
587   if (theShape.IsNull() || thePoint1.IsNull() || thePoint2.IsNull()) return NULL;
588
589   TopoDS_Shape aBlockOrComp = theShape->GetValue();
590   if (aBlockOrComp.IsNull()) {
591     SetErrorCode("Given shape is null");
592     return NULL;
593   }
594
595   TopoDS_Shape anArg1 = thePoint1->GetValue();
596   TopoDS_Shape anArg2 = thePoint2->GetValue();
597   if (anArg1.IsNull() || anArg2.IsNull()) {
598     SetErrorCode("Null shape is given as argument");
599     return NULL;
600   }
601   if (anArg1.ShapeType() != TopAbs_VERTEX ||
602       anArg2.ShapeType() != TopAbs_VERTEX) {
603     SetErrorCode("Element for edge identification is not a vertex");
604     return NULL;
605   }
606
607   //Compute the Edge value
608   try {
609 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
610     OCC_CATCH_SIGNALS;
611 #endif
612     TopTools_IndexedDataMapOfShapeListOfShape MVE;
613     GEOMImpl_Block6Explorer::MapShapesAndAncestors
614       (aBlockOrComp, TopAbs_VERTEX, TopAbs_EDGE, MVE);
615
616     TopoDS_Shape V1,V2;
617     Standard_Integer ish, ext = MVE.Extent();
618
619     if (MVE.Contains(anArg1)) {
620       V1 = anArg1;
621     } else {
622       for (ish = 1; ish <= ext; ish++) {
623         TopoDS_Shape aShi = MVE.FindKey(ish);
624         if (BRepTools::Compare(TopoDS::Vertex(anArg1), TopoDS::Vertex(aShi))) {
625           V1 = aShi;
626           break;
627         }
628       }
629     }
630
631     if (MVE.Contains(anArg2)) {
632       V2 = anArg2;
633     } else {
634       for (ish = 1; ish <= ext; ish++) {
635         TopoDS_Shape aShi = MVE.FindKey(ish);
636         if (BRepTools::Compare(TopoDS::Vertex(anArg2), TopoDS::Vertex(aShi))) {
637           V2 = aShi;
638           break;
639         }
640       }
641     }
642
643     if (V1.IsNull() || V2.IsNull()) {
644       SetErrorCode("The given vertex does not belong to the shape");
645       return NULL;
646     }
647
648     TopoDS_Shape anEdge;
649     Standard_Integer isFound =
650       GEOMImpl_Block6Explorer::FindEdge(anEdge, V1, V2, MVE, Standard_True);
651     if (isFound == 0) {
652       SetErrorCode("The given vertices do not belong to one edge of the given shape");
653       return NULL;
654     } else if (isFound > 1) {
655       SetErrorCode("Multiple edges found by the given vertices of the shape");
656       return NULL;
657     } else {
658       TopTools_IndexedMapOfShape anIndices;
659       TopExp::MapShapes(aBlockOrComp, anIndices);
660       Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
661       anArray->SetValue(1, anIndices.FindIndex(anEdge));
662       aResult = GetEngine()->AddSubShape(theShape, anArray);
663     }
664   } catch (Standard_Failure) {
665     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
666     SetErrorCode(aFail->GetMessageString());
667     return NULL;
668   }
669
670   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
671
672   //Make a Python command
673   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetEdge("
674     << theShape << ", " << thePoint1 << ", " << thePoint2 << ")";
675
676   SetErrorCode(OK);
677   return aResult;
678 }
679
680 //=============================================================================
681 /*!
682  *  GetEdgeNearPoint
683  */
684 //=============================================================================
685 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetEdgeNearPoint
686                                                 (Handle(GEOM_Object) theShape,
687                                                  Handle(GEOM_Object) thePoint)
688 {
689   SetErrorCode(KO);
690
691   //New object
692   Handle(GEOM_Object) aResult;
693
694   // Arguments
695   if (theShape.IsNull() || thePoint.IsNull()) return NULL;
696
697   TopoDS_Shape aBlockOrComp = theShape->GetValue();
698   if (aBlockOrComp.IsNull()) {
699     SetErrorCode("Given shape is null");
700     return NULL;
701   }
702
703   TopoDS_Shape anArg = thePoint->GetValue();
704   if (anArg.IsNull()) {
705     SetErrorCode("Null shape is given as argument");
706     return NULL;
707   }
708   if (anArg.ShapeType() != TopAbs_VERTEX) {
709     SetErrorCode("Element for edge identification is not a vertex");
710     return NULL;
711   }
712
713   //Compute the Edge value
714   try {
715 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
716     OCC_CATCH_SIGNALS;
717 #endif
718     TopoDS_Shape aShape;
719
720     TopoDS_Vertex aVert = TopoDS::Vertex(anArg);
721
722     // 1. Explode blocks on edges
723     TopTools_MapOfShape mapShape;
724     Standard_Integer nbEdges = 0;
725     TopExp_Explorer exp (aBlockOrComp, TopAbs_EDGE);
726     for (; exp.More(); exp.Next()) {
727       if (mapShape.Add(exp.Current())) {
728         nbEdges++;
729       }
730     }
731
732     if (nbEdges == 0) {
733       SetErrorCode("Given shape contains no edges");
734       return NULL;
735     }
736
737     mapShape.Clear();
738     Standard_Integer ind = 1;
739     TopTools_Array1OfShape anEdges (1, nbEdges);
740     TColStd_Array1OfReal aDistances (1, nbEdges);
741     for (exp.Init(aBlockOrComp, TopAbs_EDGE); exp.More(); exp.Next()) {
742       if (mapShape.Add(exp.Current())) {
743         TopoDS_Shape anEdge = exp.Current();
744         anEdges(ind) = anEdge;
745
746         // 2. Classify the point relatively each edge
747         BRepExtrema_DistShapeShape aDistTool (aVert, anEdges(ind));
748         if (!aDistTool.IsDone()) {
749           SetErrorCode("Can not find a distance from the given point to one of edges");
750           return NULL;
751         }
752         aDistances(ind) = aDistTool.Value();
753         ind++;
754       }
755     }
756
757     // 3. Define edge, having minimum distance to the point
758     Standard_Real nearest = RealLast(), nbFound = 0;
759     Standard_Real prec = Precision::Confusion();
760     for (ind = 1; ind <= nbEdges; ind++) {
761       if (Abs(aDistances(ind) - nearest) < prec) {
762         nbFound++;
763       } else if (aDistances(ind) < nearest) {
764         nearest = aDistances(ind);
765         aShape = anEdges(ind);
766         nbFound = 1;
767       } else {
768       }
769     }
770     if (nbFound > 1) {
771       SetErrorCode("Multiple edges near the given point are found");
772       return NULL;
773     } else if (nbFound == 0) {
774       SetErrorCode("There are no edges near the given point");
775       return NULL;
776     } else {
777       TopTools_IndexedMapOfShape anIndices;
778       TopExp::MapShapes(aBlockOrComp, anIndices);
779       Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
780       anArray->SetValue(1, anIndices.FindIndex(aShape));
781       aResult = GetEngine()->AddSubShape(theShape, anArray);
782     }
783   }
784   catch (Standard_Failure) {
785     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
786     SetErrorCode(aFail->GetMessageString());
787     return NULL;
788   }
789
790   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
791
792   //Make a Python command
793   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetEdgeNearPoint("
794                                << theShape << ", " << thePoint << ")";
795
796   SetErrorCode(OK);
797   return aResult;
798 }
799
800 //=============================================================================
801 /*!
802  *  GetFaceByPoints
803  */
804 //=============================================================================
805 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetFaceByPoints
806                                                 (Handle(GEOM_Object) theShape,
807                                                  Handle(GEOM_Object) thePoint1,
808                                                  Handle(GEOM_Object) thePoint2,
809                                                  Handle(GEOM_Object) thePoint3,
810                                                  Handle(GEOM_Object) thePoint4)
811 {
812   SetErrorCode(KO);
813
814   //New object
815   Handle(GEOM_Object) aResult;
816
817   // Arguments
818   if (theShape.IsNull() ||
819       thePoint1.IsNull() || thePoint2.IsNull() ||
820       thePoint3.IsNull() || thePoint4.IsNull()) return NULL;
821
822   TopoDS_Shape aBlockOrComp = theShape->GetValue();
823   if (aBlockOrComp.IsNull()) {
824     SetErrorCode("Block or compound is null");
825     return NULL;
826   }
827
828   TopoDS_Shape anArg1 = thePoint1->GetValue();
829   TopoDS_Shape anArg2 = thePoint2->GetValue();
830   TopoDS_Shape anArg3 = thePoint3->GetValue();
831   TopoDS_Shape anArg4 = thePoint4->GetValue();
832   if (anArg1.IsNull() || anArg2.IsNull() ||
833       anArg3.IsNull() || anArg4.IsNull()) {
834     SetErrorCode("Null shape is given as argument");
835     return NULL;
836   }
837   if (anArg1.ShapeType() != TopAbs_VERTEX ||
838       anArg2.ShapeType() != TopAbs_VERTEX ||
839       anArg3.ShapeType() != TopAbs_VERTEX ||
840       anArg4.ShapeType() != TopAbs_VERTEX) {
841     SetErrorCode("Element for face identification is not a vertex");
842     return NULL;
843   }
844
845   //Compute the Face value
846   try {
847 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
848     OCC_CATCH_SIGNALS;
849 #endif
850     TopoDS_Shape aShape;
851
852     TopTools_IndexedDataMapOfShapeListOfShape MVF;
853     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aBlockOrComp, TopAbs_VERTEX, TopAbs_FACE, MVF);
854
855     TopoDS_Shape V1,V2,V3,V4;
856     Standard_Integer ish, ext = MVF.Extent();
857
858     if (MVF.Contains(anArg1)) {
859       V1 = anArg1;
860     } else {
861       for (ish = 1; ish <= ext; ish++) {
862         TopoDS_Shape aShi = MVF.FindKey(ish);
863         if (BRepTools::Compare(TopoDS::Vertex(anArg1), TopoDS::Vertex(aShi))) {
864           V1 = aShi;
865           break;
866         }
867       }
868     }
869
870     if (MVF.Contains(anArg2)) {
871       V2 = anArg2;
872     } else {
873       for (ish = 1; ish <= ext; ish++) {
874         TopoDS_Shape aShi = MVF.FindKey(ish);
875         if (BRepTools::Compare(TopoDS::Vertex(anArg2), TopoDS::Vertex(aShi))) {
876           V2 = aShi;
877           break;
878         }
879       }
880     }
881
882     if (MVF.Contains(anArg3)) {
883       V3 = anArg3;
884     } else {
885       for (ish = 1; ish <= ext; ish++) {
886         TopoDS_Shape aShi = MVF.FindKey(ish);
887         if (BRepTools::Compare(TopoDS::Vertex(anArg3), TopoDS::Vertex(aShi))) {
888           V3 = aShi;
889           break;
890         }
891       }
892     }
893
894     if (MVF.Contains(anArg4)) {
895       V4 = anArg4;
896     } else {
897       for (ish = 1; ish <= ext; ish++) {
898         TopoDS_Shape aShi = MVF.FindKey(ish);
899         if (BRepTools::Compare(TopoDS::Vertex(anArg4), TopoDS::Vertex(aShi))) {
900           V4 = aShi;
901           break;
902         }
903       }
904     }
905
906     if (V1.IsNull() || V2.IsNull() || V3.IsNull() || V4.IsNull()) {
907       SetErrorCode("The given vertex does not belong to the shape");
908       return NULL;
909     }
910
911     Standard_Integer isFound =
912       GEOMImpl_Block6Explorer::FindFace(aShape, V1, V2, V3, V4, MVF, Standard_True);
913     if (isFound == 0) {
914       SetErrorCode("The given vertices do not belong to one face of the given shape");
915       return NULL;
916     } else if (isFound > 1) {
917       SetErrorCode("The given vertices belong to several faces of the given shape");
918       return NULL;
919     } else {
920       TopTools_IndexedMapOfShape anIndices;
921       TopExp::MapShapes(aBlockOrComp, anIndices);
922       Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
923       anArray->SetValue(1, anIndices.FindIndex(aShape));
924       aResult = GetEngine()->AddSubShape(theShape, anArray);
925     }
926   }
927   catch (Standard_Failure) {
928     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
929     SetErrorCode(aFail->GetMessageString());
930     return NULL;
931   }
932
933   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
934
935   //Make a Python command
936   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetFaceByPoints("
937     << theShape << ", " << thePoint1 << ", " << thePoint2
938       << ", " << thePoint3 << ", " << thePoint4 << ")";
939
940   SetErrorCode(OK);
941   return aResult;
942 }
943
944 //=============================================================================
945 /*!
946  *  GetFaceByEdges
947  */
948 //=============================================================================
949 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetFaceByEdges
950                                                 (Handle(GEOM_Object) theShape,
951                                                  Handle(GEOM_Object) theEdge1,
952                                                  Handle(GEOM_Object) theEdge2)
953 {
954   SetErrorCode(KO);
955
956   //New object
957   Handle(GEOM_Object) aResult;
958
959   // Arguments
960   if (theShape.IsNull() || theEdge1.IsNull() || theEdge2.IsNull()) return NULL;
961
962   TopoDS_Shape aBlockOrComp = theShape->GetValue();
963   if (aBlockOrComp.IsNull()) {
964     SetErrorCode("Block or compound is null");
965     return NULL;
966   }
967
968   TopoDS_Shape anArg1 = theEdge1->GetValue();
969   TopoDS_Shape anArg2 = theEdge2->GetValue();
970   if (anArg1.IsNull() || anArg2.IsNull()) {
971     SetErrorCode("Null shape is given as argument");
972     return NULL;
973   }
974   if (anArg1.ShapeType() != TopAbs_EDGE ||
975       anArg2.ShapeType() != TopAbs_EDGE) {
976     SetErrorCode("Element for face identification is not an edge");
977     return NULL;
978   }
979
980   //Compute the Face value
981   try {
982 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
983     OCC_CATCH_SIGNALS;
984 #endif
985     TopoDS_Shape aShape;
986
987     TopTools_IndexedDataMapOfShapeListOfShape MEF;
988     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aBlockOrComp, TopAbs_EDGE, TopAbs_FACE, MEF);
989
990     TopoDS_Shape E1,E2;
991     Standard_Integer ish, ext = MEF.Extent();
992
993     if (MEF.Contains(anArg1)) {
994       E1 = anArg1;
995     } else {
996       for (ish = 1; ish <= ext; ish++) {
997         TopoDS_Shape aShi = MEF.FindKey(ish);
998         if (GEOMImpl_Block6Explorer::IsSimilarEdges(anArg1, aShi)) {
999           E1 = aShi;
1000         }
1001       }
1002     }
1003
1004     if (MEF.Contains(anArg2)) {
1005       E2 = anArg2;
1006     } else {
1007       for (ish = 1; ish <= ext; ish++) {
1008         TopoDS_Shape aShi = MEF.FindKey(ish);
1009         if (GEOMImpl_Block6Explorer::IsSimilarEdges(anArg2, aShi)) {
1010           E2 = aShi;
1011         }
1012       }
1013     }
1014
1015     if (E1.IsNull() || E2.IsNull()) {
1016       SetErrorCode("The given edge does not belong to the shape");
1017       return NULL;
1018     }
1019
1020     const TopTools_ListOfShape& aFacesOfE1 = MEF.FindFromKey(E1);
1021     const TopTools_ListOfShape& aFacesOfE2 = MEF.FindFromKey(E2);
1022
1023     Standard_Integer isFound = 0;
1024     TopTools_ListIteratorOfListOfShape anIterF1 (aFacesOfE1);
1025     for (; anIterF1.More(); anIterF1.Next()) {
1026
1027       TopTools_ListIteratorOfListOfShape anIterF2 (aFacesOfE2);
1028       for (; anIterF2.More(); anIterF2.Next()) {
1029
1030         if (anIterF1.Value().IsSame(anIterF2.Value())) {
1031           isFound++;
1032
1033           // Store the face, defined by two edges
1034           aShape = anIterF1.Value();
1035         }
1036       }
1037     }
1038     if (isFound == 0) {
1039       SetErrorCode("The given edges do not belong to one face of the given shape");
1040       return NULL;
1041     } else if (isFound > 1) {
1042       SetErrorCode("The given edges belong to several faces of the given shape");
1043       return NULL;
1044     } else {
1045       TopTools_IndexedMapOfShape anIndices;
1046       TopExp::MapShapes(aBlockOrComp, anIndices);
1047       Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
1048       anArray->SetValue(1, anIndices.FindIndex(aShape));
1049       aResult = GetEngine()->AddSubShape(theShape, anArray);
1050     }
1051   }
1052   catch (Standard_Failure) {
1053     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1054     SetErrorCode(aFail->GetMessageString());
1055     return NULL;
1056   }
1057
1058   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
1059
1060   //Make a Python command
1061   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetFaceByEdges("
1062     << theShape << ", " << theEdge1 << ", " << theEdge2 << ")";
1063
1064   SetErrorCode(OK);
1065   return aResult;
1066 }
1067
1068 //=============================================================================
1069 /*!
1070  *  GetOppositeFace
1071  */
1072 //=============================================================================
1073 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetOppositeFace
1074                                                 (Handle(GEOM_Object) theShape,
1075                                                  Handle(GEOM_Object) theFace)
1076 {
1077   SetErrorCode(KO);
1078
1079   //New object
1080   Handle(GEOM_Object) aResult;
1081
1082   // Arguments
1083   if (theShape.IsNull() || theFace.IsNull()) return NULL;
1084
1085   TopoDS_Shape aBlockOrComp = theShape->GetValue();
1086   if (aBlockOrComp.IsNull()) {
1087     SetErrorCode("Block is null");
1088     return NULL;
1089   }
1090   if (aBlockOrComp.ShapeType() != TopAbs_SOLID) {
1091     SetErrorCode("Shape is not a block");
1092     return NULL;
1093   }
1094
1095   TopoDS_Shape anArg = theFace->GetValue();
1096   if (anArg.IsNull()) {
1097     SetErrorCode("Null shape is given as argument");
1098     return NULL;
1099   }
1100   if (anArg.ShapeType() != TopAbs_FACE) {
1101     SetErrorCode("Element for face identification is not a face");
1102     return NULL;
1103   }
1104
1105   //Compute the Face value
1106   try {
1107 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1108     OCC_CATCH_SIGNALS;
1109 #endif
1110     TopoDS_Shape aShape;
1111
1112     GEOMImpl_Block6Explorer aBlockTool;
1113     aBlockTool.InitByBlockAndFace(aBlockOrComp, anArg);
1114     aShape = aBlockTool.GetFace(2);
1115
1116     TopTools_IndexedMapOfShape anIndices;
1117     TopExp::MapShapes(aBlockOrComp, anIndices);
1118     Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
1119     anArray->SetValue(1, anIndices.FindIndex(aShape));
1120     aResult = GetEngine()->AddSubShape(theShape, anArray);
1121   }
1122   catch (Standard_Failure) {
1123     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1124     SetErrorCode(aFail->GetMessageString());
1125     return NULL;
1126   }
1127
1128   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
1129
1130   //Make a Python command
1131   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetOppositeFace("
1132                                << theShape << ", " << theFace << ")";
1133
1134   SetErrorCode(OK);
1135   return aResult;
1136 }
1137
1138 //=============================================================================
1139 /*!
1140  *  GetFaceNearPoint
1141  */
1142 //=============================================================================
1143 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetFaceNearPoint
1144                                                 (Handle(GEOM_Object) theShape,
1145                                                  Handle(GEOM_Object) thePoint)
1146 {
1147   SetErrorCode(KO);
1148
1149   //New object
1150   Handle(GEOM_Object) aResult;
1151
1152   // Arguments
1153   if (theShape.IsNull() || thePoint.IsNull()) return NULL;
1154
1155   TopoDS_Shape aBlockOrComp = theShape->GetValue();
1156   if (aBlockOrComp.IsNull()) {
1157     SetErrorCode("Block or compound is null");
1158     return NULL;
1159   }
1160
1161   TopoDS_Shape anArg = thePoint->GetValue();
1162   if (anArg.IsNull()) {
1163     SetErrorCode("Null shape is given as argument");
1164     return NULL;
1165   }
1166   if (anArg.ShapeType() != TopAbs_VERTEX) {
1167     SetErrorCode("Element for face identification is not a vertex");
1168     return NULL;
1169   }
1170
1171   //Compute the Face value
1172   try {
1173 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1174     OCC_CATCH_SIGNALS;
1175 #endif
1176     TopoDS_Shape aShape;
1177
1178     TopoDS_Vertex aVert = TopoDS::Vertex(anArg);
1179     gp_Pnt aPnt = BRep_Tool::Pnt(aVert);
1180     Standard_Real PX, PY, PZ;
1181     aPnt.Coord(PX, PY, PZ);
1182
1183     // 1. Classify the point relatively each face
1184     Standard_Integer nearest = 2, nbFound = 0;
1185     TopTools_DataMapOfShapeInteger mapShapeDist;
1186     TopExp_Explorer exp (aBlockOrComp, TopAbs_FACE);
1187     for (; exp.More(); exp.Next()) {
1188       TopoDS_Shape aFace = exp.Current();
1189
1190       if (!mapShapeDist.IsBound(aFace)) {
1191         Standard_Integer aDistance = 2;
1192
1193         // 1.a. Classify relatively Surface
1194         Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aFace));
1195         Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
1196         gp_Pnt2d p2dOnSurf = aSurfAna->ValueOfUV(aPnt, Precision::Confusion());
1197         gp_Pnt p3dOnSurf = aSurfAna->Value(p2dOnSurf);
1198         Standard_Real aDist = p3dOnSurf.Distance(aPnt);
1199         if (aDist > Precision::Confusion()) {
1200           // OUT of Surface
1201           aDistance = 1;
1202         } else {
1203           // 1.b. Classify relatively the face itself
1204           BRepClass_FaceClassifier FC (TopoDS::Face(aFace), p2dOnSurf, Precision::Confusion());
1205           if (FC.State() == TopAbs_IN) {
1206             aDistance = -1;
1207           } else if (FC.State() == TopAbs_ON) {
1208             aDistance = 0;
1209           } else { // OUT
1210             aDistance = 1;
1211           }
1212         }
1213
1214         if (aDistance < nearest) {
1215           nearest = aDistance;
1216           aShape = aFace;
1217           nbFound = 1;
1218
1219           // A first found face, containing the point inside, will be returned.
1220           // It is the solution, if there are no
1221           // coincident or intersecting faces in the compound.
1222           if (nearest == -1) break;
1223
1224         } else if (aDistance == nearest) {
1225           nbFound++;
1226         } else {
1227         }
1228
1229         mapShapeDist.Bind(aFace, aDistance);
1230       } // if (!mapShapeDist.IsBound(aFace))
1231     }
1232
1233     // 2. Define face, containing the point or having minimum distance to it
1234     if (nbFound > 1) {
1235       if (nearest == 0) {
1236         // The point is on boundary of some faces and there are
1237         // no faces, having the point inside
1238         SetErrorCode("Multiple faces near the given point are found");
1239         return NULL;
1240
1241       } else if (nearest == 1) {
1242         // The point is outside some faces and there are
1243         // no faces, having the point inside or on boundary.
1244         // We will get a nearest face
1245         Standard_Real bigReal = RealLast();
1246         Standard_Real minDist = bigReal;
1247         TopTools_DataMapIteratorOfDataMapOfShapeInteger mapShapeDistIter (mapShapeDist);
1248         for (; mapShapeDistIter.More(); mapShapeDistIter.Next()) {
1249           if (mapShapeDistIter.Value() == 1) {
1250             TopoDS_Shape aFace = mapShapeDistIter.Key();
1251             Standard_Real aDist = bigReal;
1252
1253             // 2.a. Fast check of distance - if point projection on surface is on face
1254             Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aFace));
1255             Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
1256             gp_Pnt2d p2dOnSurf = aSurfAna->ValueOfUV(aPnt, Precision::Confusion());
1257             gp_Pnt p3dOnSurf = aSurfAna->Value(p2dOnSurf);
1258             aDist = p3dOnSurf.Distance(aPnt);
1259
1260             BRepClass_FaceClassifier FC (TopoDS::Face(aFace), p2dOnSurf, Precision::Confusion());
1261             if (FC.State() == TopAbs_OUT) {
1262               if (aDist < minDist) {
1263                 // 2.b. Slow check - if point projection on surface is outside of face
1264                 BRepExtrema_DistShapeShape aDistTool (aVert, aFace);
1265                 if (!aDistTool.IsDone()) {
1266                   SetErrorCode("Can not find a distance from the given point to one of faces");
1267                   return NULL;
1268                 }
1269                 aDist = aDistTool.Value();
1270               } else {
1271                 aDist = bigReal;
1272               }
1273             }
1274
1275             if (aDist < minDist) {
1276               minDist = aDist;
1277               aShape = aFace;
1278             }
1279           }
1280         }
1281       } else { // nearest == -1
1282 //        // The point is inside some faces.
1283 //        // We will get a face with nearest center
1284 //        Standard_Real minDist = RealLast();
1285 //        TopTools_DataMapIteratorOfDataMapOfShapeInteger mapShapeDistIter (mapShapeDist);
1286 //        for (; mapShapeDistIter.More(); mapShapeDistIter.Next()) {
1287 //          if (mapShapeDistIter.Value() == -1) {
1288 //            TopoDS_Shape aFace = mapShapeDistIter.Key();
1289 //            GProp_GProps aSystem;
1290 //            BRepGProp::SurfaceProperties(aFace, aSystem);
1291 //            gp_Pnt aCenterMass = aSystem.CentreOfMass();
1292 //
1293 //            Standard_Real aDist = aCenterMass.Distance(aPnt);
1294 //            if (aDist < minDist) {
1295 //              minDist = aDist;
1296 //              aShape = aFace;
1297 //            }
1298 //          }
1299 //        }
1300       }
1301     } // if (nbFound > 1)
1302
1303     if (nbFound == 0) {
1304       SetErrorCode("There are no faces near the given point");
1305       return NULL;
1306     } else {
1307       TopTools_IndexedMapOfShape anIndices;
1308       TopExp::MapShapes(aBlockOrComp, anIndices);
1309       Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
1310       anArray->SetValue(1, anIndices.FindIndex(aShape));
1311       aResult = GetEngine()->AddSubShape(theShape, anArray);
1312     }
1313   }
1314   catch (Standard_Failure) {
1315     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1316     SetErrorCode(aFail->GetMessageString());
1317     return NULL;
1318   }
1319
1320   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
1321
1322   //Make a Python command
1323   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetFaceNearPoint("
1324                                << theShape << ", " << thePoint << ")";
1325
1326   SetErrorCode(OK);
1327   return aResult;
1328 }
1329
1330 //=============================================================================
1331 /*!
1332  *  GetFaceByNormale
1333  */
1334 //=============================================================================
1335 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetFaceByNormale
1336                                                 (Handle(GEOM_Object) theShape,
1337                                                  Handle(GEOM_Object) theVector)
1338 {
1339   SetErrorCode(KO);
1340
1341   //New object
1342   Handle(GEOM_Object) aResult;
1343
1344   // Arguments
1345   if (theShape.IsNull() || theVector.IsNull()) return NULL;
1346
1347   TopoDS_Shape aBlockOrComp = theShape->GetValue();
1348   if (aBlockOrComp.IsNull()) {
1349     SetErrorCode("Block or compound is null");
1350     return NULL;
1351   }
1352
1353   TopoDS_Shape anArg = theVector->GetValue();
1354   if (anArg.IsNull()) {
1355     SetErrorCode("Null shape is given as argument");
1356     return NULL;
1357   }
1358   if (anArg.ShapeType() != TopAbs_EDGE) {
1359     SetErrorCode("Element for normale identification is not an edge");
1360     return NULL;
1361   }
1362
1363   //Compute the Face value
1364   try {
1365 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1366     OCC_CATCH_SIGNALS;
1367 #endif
1368     TopoDS_Shape aShape;
1369
1370     TopoDS_Edge anEdge = TopoDS::Edge(anArg);
1371     TopoDS_Vertex V1, V2;
1372     TopExp::Vertices(anEdge, V1, V2, Standard_True);
1373     gp_Pnt P1 = BRep_Tool::Pnt(V1);
1374     gp_Pnt P2 = BRep_Tool::Pnt(V2);
1375     gp_Vec aVec (P1, P2);
1376     if (aVec.Magnitude() < Precision::Confusion()) {
1377       SetErrorCode("Vector with null magnitude is given");
1378       return NULL;
1379     }
1380
1381     Standard_Real minAngle = RealLast();
1382     TopTools_MapOfShape mapShape;
1383     TopExp_Explorer exp (aBlockOrComp, TopAbs_FACE);
1384     for (; exp.More(); exp.Next()) {
1385       if (mapShape.Add(exp.Current())) {
1386         TopoDS_Face aFace = TopoDS::Face(exp.Current());
1387         BRepAdaptor_Surface SF (aFace);
1388
1389         Standard_Real u, v, x;
1390
1391         // find a point on the surface to get normal direction in
1392         u = SF.FirstUParameter();
1393         x = SF.LastUParameter();
1394         if (Precision::IsInfinite(u)) {
1395           u =  (Precision::IsInfinite(x)) ? 0. : x;
1396         } else if (!Precision::IsInfinite(x)) {
1397           u = (u+x) / 2.;
1398         }
1399
1400         v = SF.FirstVParameter();
1401         x = SF.LastVParameter();
1402         if (Precision::IsInfinite(v)) {
1403           v =  (Precision::IsInfinite(x)) ? 0. : x;
1404         } else if (!Precision::IsInfinite(x)) {
1405           v = (v+x) / 2.;
1406         }
1407
1408         // compute the normal direction
1409         gp_Vec Vec1,Vec2;
1410         SF.D1(u,v,P1,Vec1,Vec2);
1411         gp_Vec V = Vec1.Crossed(Vec2);
1412         x = V.Magnitude();
1413         if (V.Magnitude() < Precision::Confusion()) {
1414           SetErrorCode("Normal vector of a face has null magnitude");
1415           return NULL;
1416         }
1417
1418         // consider the face orientation
1419         if (aFace.Orientation() == TopAbs_REVERSED ||
1420             aFace.Orientation() == TopAbs_INTERNAL) {
1421           V = - V;
1422         }
1423
1424         // compute the angle and compare with the minimal one
1425         Standard_Real anAngle = aVec.Angle(V);
1426         if (anAngle < minAngle) {
1427           minAngle = anAngle;
1428           aShape = aFace;
1429         }
1430       }
1431     }
1432
1433     if (aShape.IsNull()) {
1434       SetErrorCode("Failed to find a face by the given normale");
1435       return NULL;
1436     } else {
1437       TopTools_IndexedMapOfShape anIndices;
1438       TopExp::MapShapes(aBlockOrComp, anIndices);
1439       Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
1440       anArray->SetValue(1, anIndices.FindIndex(aShape));
1441       aResult = GetEngine()->AddSubShape(theShape, anArray);
1442     }
1443   }
1444   catch (Standard_Failure) {
1445     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1446     SetErrorCode(aFail->GetMessageString());
1447     return NULL;
1448   }
1449
1450   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
1451
1452   //Make a Python command
1453   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetFaceByNormale("
1454     << theShape << ", " << theVector << ")";
1455
1456   SetErrorCode(OK);
1457   return aResult;
1458 }
1459
1460 //=============================================================================
1461 /*!
1462  *  IsCompoundOfBlocks
1463  */
1464 //=============================================================================
1465 Standard_Boolean GEOMImpl_IBlocksOperations::IsCompoundOfBlocks
1466                                                 (Handle(GEOM_Object)    theCompound,
1467                                                  const Standard_Integer theMinNbFaces,
1468                                                  const Standard_Integer theMaxNbFaces,
1469                                                  Standard_Integer&      theNbBlocks)
1470 {
1471   SetErrorCode(KO);
1472   Standard_Boolean isCompOfBlocks = Standard_False;
1473   theNbBlocks = 0;
1474
1475   if (theCompound.IsNull()) return isCompOfBlocks;
1476   TopoDS_Shape aBlockOrComp = theCompound->GetValue();
1477
1478   //Check
1479   isCompOfBlocks = Standard_True;
1480   try {
1481 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1482     OCC_CATCH_SIGNALS;
1483 #endif
1484     TopTools_MapOfShape mapShape;
1485     TopExp_Explorer exp (aBlockOrComp, TopAbs_SOLID);
1486     for (; exp.More(); exp.Next()) {
1487       if (mapShape.Add(exp.Current())) {
1488         TopoDS_Shape aSolid = exp.Current();
1489
1490         TopTools_MapOfShape mapFaces;
1491         TopExp_Explorer expF (aSolid, TopAbs_FACE);
1492         Standard_Integer nbFaces = 0;
1493         for (; expF.More(); expF.Next()) {
1494           if (mapFaces.Add(expF.Current())) {
1495             nbFaces++;
1496             if (nbFaces > theMaxNbFaces) {
1497               isCompOfBlocks = Standard_False;
1498               break;
1499             }
1500           }
1501         }
1502         if (nbFaces < theMinNbFaces || theMaxNbFaces < nbFaces) {
1503           isCompOfBlocks = Standard_False;
1504         } else {
1505           theNbBlocks++;
1506         }
1507       }
1508     }
1509   }
1510   catch (Standard_Failure) {
1511     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1512     SetErrorCode(aFail->GetMessageString());
1513     return isCompOfBlocks;
1514   }
1515
1516   SetErrorCode(OK);
1517   return isCompOfBlocks;
1518 }
1519
1520 //=============================================================================
1521 /*!
1522  *  Set of functions, used by CheckCompoundOfBlocks() method
1523  */
1524 //=============================================================================
1525 void GEOMImpl_IBlocksOperations::AddBlocksFrom (const TopoDS_Shape&   theShape,
1526                                                 TopTools_ListOfShape& BLO,
1527                                                 TopTools_ListOfShape& NOT,
1528                                                 TopTools_ListOfShape& EXT)
1529 {
1530   TopAbs_ShapeEnum aType = theShape.ShapeType();
1531   switch (aType) {
1532   case TopAbs_COMPOUND:
1533   case TopAbs_COMPSOLID:
1534     {
1535       TopoDS_Iterator It (theShape);
1536       for (; It.More(); It.Next()) {
1537         AddBlocksFrom(It.Value(), BLO, NOT, EXT);
1538       }
1539     }
1540     break;
1541   case TopAbs_SOLID:
1542     {
1543       // Check, if there are seam or degenerated edges
1544       BlockFix_CheckTool aTool;
1545       aTool.SetShape(theShape);
1546       aTool.Perform();
1547       if (aTool.NbPossibleBlocks() > 0) {
1548         EXT.Append(theShape);
1549       } else {
1550         // Count faces and edges in each face to recognize blocks
1551         TopTools_MapOfShape mapFaces;
1552         Standard_Integer nbFaces = 0;
1553         Standard_Boolean hasNonQuadr = Standard_False;
1554         TopExp_Explorer expF (theShape, TopAbs_FACE);
1555
1556         for (; expF.More(); expF.Next()) {
1557           if (mapFaces.Add(expF.Current())) {
1558             nbFaces++;
1559             if (nbFaces > 6) break;
1560
1561             // get wire
1562             TopoDS_Shape aF = expF.Current();
1563             TopExp_Explorer wires (aF, TopAbs_WIRE);
1564             if (!wires.More()) {
1565               // no wire in the face
1566               hasNonQuadr = Standard_True;
1567               break;
1568             }
1569             TopoDS_Shape aWire = wires.Current();
1570             wires.Next();
1571             if (wires.More()) {
1572               // multiple wires in the face
1573               hasNonQuadr = Standard_True;
1574               break;
1575             }
1576
1577             // Check number of edges in the face
1578             Standard_Integer nbEdges = 0;
1579             TopTools_MapOfShape mapEdges;
1580             TopExp_Explorer expW (aWire, TopAbs_EDGE);
1581             for (; expW.More(); expW.Next()) {
1582               if (mapEdges.Add(expW.Current())) {
1583                 nbEdges++;
1584                 if (nbEdges > 4) break;
1585               }
1586             }
1587             if (nbEdges != 4) {
1588               hasNonQuadr = Standard_True;
1589             }
1590           }
1591         }
1592
1593         if (nbFaces == 6 && !hasNonQuadr) {
1594           BLO.Append(theShape);
1595         } else {
1596           NOT.Append(theShape);
1597         }
1598       }
1599     }
1600     break;
1601   default:
1602     NOT.Append(theShape);
1603   }
1604 }
1605
1606 void AddBlocksFromOld (const TopoDS_Shape&   theShape,
1607                        TopTools_ListOfShape& BLO,
1608                        TopTools_ListOfShape& NOT,
1609                        TopTools_ListOfShape& DEG,
1610                        TopTools_ListOfShape& SEA)
1611 {
1612   TopAbs_ShapeEnum aType = theShape.ShapeType();
1613   switch (aType) {
1614   case TopAbs_COMPOUND:
1615   case TopAbs_COMPSOLID:
1616     {
1617       TopoDS_Iterator It (theShape);
1618       for (; It.More(); It.Next()) {
1619         AddBlocksFromOld(It.Value(), BLO, NOT, DEG, SEA);
1620       }
1621     }
1622     break;
1623   case TopAbs_SOLID:
1624     {
1625       TopTools_MapOfShape mapFaces;
1626       TopExp_Explorer expF (theShape, TopAbs_FACE);
1627       Standard_Integer nbFaces = 0;
1628       Standard_Boolean hasNonQuadr = Standard_False;
1629       Standard_Boolean hasDegenerated = Standard_False;
1630       Standard_Boolean hasSeam = Standard_False;
1631       for (; expF.More(); expF.Next()) {
1632         if (mapFaces.Add(expF.Current())) {
1633           nbFaces++;
1634           if (nbFaces > 6) break;
1635
1636           // Check number of edges in the face
1637           Standard_Integer nbEdges = 0;
1638           TopTools_MapOfShape mapEdges;
1639
1640           // get wire
1641           TopoDS_Shape aF = expF.Current();
1642           TopExp_Explorer wires (aF, TopAbs_WIRE);
1643           if (!wires.More()) {
1644             // no wire in the face
1645             hasNonQuadr = Standard_True;
1646             break;
1647           }
1648           TopoDS_Shape aWire = wires.Current();
1649           wires.Next();
1650           if (wires.More()) {
1651             // multiple wires in the face
1652             hasNonQuadr = Standard_True;
1653             break;
1654           }
1655
1656           // iterate on wire
1657           BRepTools_WireExplorer aWE (TopoDS::Wire(aWire), TopoDS::Face(aF));
1658           for (; aWE.More(); aWE.Next(), nbEdges++) {
1659             if (BRep_Tool::Degenerated(aWE.Current())) {
1660               // degenerated edge found
1661               hasDegenerated = Standard_True;
1662 //              break;
1663             }
1664             if (mapEdges.Contains(aWE.Current())) {
1665               // seam edge found
1666               hasSeam = Standard_True;
1667 //              break;
1668             }
1669             mapEdges.Add(aWE.Current());
1670           }
1671           if (nbEdges != 4) {
1672             hasNonQuadr = Standard_True;
1673           }
1674         }
1675       }
1676       if (nbFaces == 6) {
1677         if (hasDegenerated || hasSeam) {
1678           if (hasDegenerated) {
1679             DEG.Append(theShape);
1680           }
1681           if (hasSeam) {
1682             SEA.Append(theShape);
1683           }
1684         } else if (hasNonQuadr) {
1685           NOT.Append(theShape);
1686         } else {
1687           BLO.Append(theShape);
1688         }
1689       } else {
1690         NOT.Append(theShape);
1691       }
1692     }
1693     break;
1694   default:
1695     NOT.Append(theShape);
1696   }
1697 }
1698
1699 #define REL_NOT_CONNECTED 0
1700 #define REL_OK            1
1701 #define REL_NOT_GLUED     2
1702 #define REL_COLLISION_VV  3
1703 #define REL_COLLISION_FF  4
1704 #define REL_COLLISION_EE  5
1705 #define REL_UNKNOWN       6
1706
1707 Standard_Integer BlocksRelation (const TopoDS_Shape& theBlock1,
1708                                  const TopoDS_Shape& theBlock2)
1709 {
1710   // Compare bounding boxes before calling BRepExtrema_DistShapeShape
1711   Standard_Real Xmin1, Ymin1, Zmin1, Xmax1, Ymax1, Zmax1;
1712   Standard_Real Xmin2, Ymin2, Zmin2, Xmax2, Ymax2, Zmax2;
1713   Bnd_Box B1, B2;
1714   BRepBndLib::Add(theBlock1, B1);
1715   BRepBndLib::Add(theBlock2, B2);
1716   B1.Get(Xmin1, Ymin1, Zmin1, Xmax1, Ymax1, Zmax1);
1717   B2.Get(Xmin2, Ymin2, Zmin2, Xmax2, Ymax2, Zmax2);
1718   if (Xmax2 < Xmin1 || Xmax1 < Xmin2 ||
1719       Ymax2 < Ymin1 || Ymax1 < Ymin2 ||
1720       Zmax2 < Zmin1 || Zmax1 < Zmin2) {
1721     return REL_NOT_CONNECTED;
1722   }
1723
1724   BRepExtrema_DistShapeShape dst (theBlock1, theBlock2);
1725   if (!dst.IsDone()) {
1726     return REL_UNKNOWN;
1727   }
1728
1729   if (dst.Value() > Precision::Confusion()) {
1730     return REL_NOT_CONNECTED;
1731   }
1732
1733   if (dst.InnerSolution()) {
1734     return REL_COLLISION_VV;
1735   }
1736
1737   Standard_Integer nbSol = dst.NbSolution();
1738   Standard_Integer relation = REL_OK;
1739   Standard_Integer nbVerts = 0;
1740   Standard_Integer nbEdges = 0;
1741   Standard_Integer sol = 1;
1742   for (; sol <= nbSol; sol++) {
1743     BRepExtrema_SupportType supp1 = dst.SupportTypeShape1(sol);
1744     BRepExtrema_SupportType supp2 = dst.SupportTypeShape2(sol);
1745     if (supp1 == BRepExtrema_IsVertex && supp2 == BRepExtrema_IsVertex) {
1746       nbVerts++;
1747     } else if (supp1 == BRepExtrema_IsInFace || supp2 == BRepExtrema_IsInFace) {
1748       return REL_COLLISION_FF;
1749     } else if (supp1 == BRepExtrema_IsOnEdge && supp2 == BRepExtrema_IsOnEdge) {
1750       nbEdges++;
1751     } else if ((supp1 == BRepExtrema_IsOnEdge && supp2 == BRepExtrema_IsVertex) ||
1752                (supp2 == BRepExtrema_IsOnEdge && supp1 == BRepExtrema_IsVertex)) {
1753       relation = REL_COLLISION_EE;
1754     } else {
1755     }
1756   }
1757
1758   if (relation != REL_OK) {
1759     return relation;
1760   }
1761
1762   TColStd_Array1OfInteger vertSol (1, nbVerts);
1763   TopTools_Array1OfShape V1 (1, nbVerts);
1764   TopTools_Array1OfShape V2 (1, nbVerts);
1765   Standard_Integer ivs = 0;
1766   for (sol = 1; sol <= nbSol; sol++) {
1767     if (dst.SupportTypeShape1(sol) == BRepExtrema_IsVertex &&
1768         dst.SupportTypeShape2(sol) == BRepExtrema_IsVertex) {
1769       TopoDS_Vertex Vcur = TopoDS::Vertex(dst.SupportOnShape1(sol));
1770       // Check, that this vertex is far enough from other solution vertices.
1771       Standard_Integer ii = 1;
1772       for (; ii <= ivs; ii++) {
1773         if (BRepTools::Compare(TopoDS::Vertex(V1(ii)), Vcur)) {
1774           continue;
1775         }
1776       }
1777       ivs++;
1778       vertSol(ivs) = sol;
1779       V1(ivs) = Vcur;
1780       V2(ivs) = dst.SupportOnShape2(sol);
1781     }
1782   }
1783
1784   // As we deal only with quadrangles,
1785   // 2, 3 or 4 vertex solutions can be found.
1786   if (ivs <= 1) {
1787     if (nbEdges > 0) {
1788       return REL_COLLISION_FF;
1789     }
1790     return REL_NOT_CONNECTED;
1791   }
1792   if (ivs > 4) {
1793     return REL_UNKNOWN;
1794   }
1795
1796   // Check sharing of coincident entities.
1797   if (ivs == 2 || ivs == 3) {
1798     // Map vertices and edges of the blocks
1799     TopTools_IndexedDataMapOfShapeListOfShape MVE1, MVE2;
1800     GEOMImpl_Block6Explorer::MapShapesAndAncestors
1801       (theBlock1, TopAbs_VERTEX, TopAbs_EDGE, MVE1);
1802     GEOMImpl_Block6Explorer::MapShapesAndAncestors
1803       (theBlock2, TopAbs_VERTEX, TopAbs_EDGE, MVE2);
1804
1805     if (ivs == 2) {
1806       // Find common edge
1807       TopoDS_Shape anEdge1, anEdge2;
1808       GEOMImpl_Block6Explorer::FindEdge(anEdge1, V1(1), V1(2), MVE1);
1809       if (anEdge1.IsNull()) return REL_UNKNOWN;
1810
1811       GEOMImpl_Block6Explorer::FindEdge(anEdge2, V2(1), V2(2), MVE2);
1812       if (anEdge2.IsNull()) return REL_UNKNOWN;
1813
1814       if (!anEdge1.IsSame(anEdge2)) return REL_NOT_GLUED;
1815
1816     } else { // ivs == 3
1817       // Find common edges
1818       Standard_Integer e1_v1 = 1;
1819       Standard_Integer e1_v2 = 2;
1820       Standard_Integer e2_v1 = 3;
1821       Standard_Integer e2_v2 = 1;
1822
1823       TopoDS_Shape anEdge11, anEdge12;
1824       GEOMImpl_Block6Explorer::FindEdge(anEdge11, V1(e1_v1), V1(e1_v2), MVE1);
1825       if (anEdge11.IsNull()) {
1826         e1_v2 = 3;
1827         e2_v1 = 2;
1828         GEOMImpl_Block6Explorer::FindEdge(anEdge11, V1(e1_v1), V1(e1_v2), MVE1);
1829         if (anEdge11.IsNull()) return REL_UNKNOWN;
1830       }
1831       GEOMImpl_Block6Explorer::FindEdge(anEdge12, V1(e2_v1), V1(e2_v2), MVE1);
1832       if (anEdge12.IsNull()) {
1833         e2_v2 = 5 - e2_v1;
1834         GEOMImpl_Block6Explorer::FindEdge(anEdge12, V1(e2_v1), V1(e2_v2), MVE1);
1835         if (anEdge12.IsNull()) return REL_UNKNOWN;
1836       }
1837
1838       TopoDS_Shape anEdge21, anEdge22;
1839       GEOMImpl_Block6Explorer::FindEdge(anEdge21, V2(e1_v1), V2(e1_v2), MVE2);
1840       if (anEdge21.IsNull()) return REL_UNKNOWN;
1841       GEOMImpl_Block6Explorer::FindEdge(anEdge22, V2(e2_v1), V2(e2_v2), MVE2);
1842       if (anEdge22.IsNull()) return REL_UNKNOWN;
1843
1844       // Check of edges coincidence (with some precision) have to be done here
1845       // if (!anEdge11.IsEqual(anEdge21)) return REL_UNKNOWN;
1846       // if (!anEdge12.IsEqual(anEdge22)) return REL_UNKNOWN;
1847
1848       // Check of edges sharing
1849       if (!anEdge11.IsSame(anEdge21)) return REL_NOT_GLUED;
1850       if (!anEdge12.IsSame(anEdge22)) return REL_NOT_GLUED;
1851     }
1852   }
1853
1854   if (ivs == 4) {
1855     // Map vertices and faces of the blocks
1856     TopTools_IndexedDataMapOfShapeListOfShape MVF1, MVF2;
1857     GEOMImpl_Block6Explorer::MapShapesAndAncestors
1858       (theBlock1, TopAbs_VERTEX, TopAbs_FACE, MVF1);
1859     GEOMImpl_Block6Explorer::MapShapesAndAncestors
1860       (theBlock2, TopAbs_VERTEX, TopAbs_FACE, MVF2);
1861
1862     TopoDS_Shape aFace1, aFace2;
1863     GEOMImpl_Block6Explorer::FindFace(aFace1, V1(1), V1(2), V1(3), V1(4), MVF1);
1864     if (aFace1.IsNull()) return REL_UNKNOWN;
1865     GEOMImpl_Block6Explorer::FindFace(aFace2, V2(1), V2(2), V2(3), V2(4), MVF2);
1866     if (aFace2.IsNull()) return REL_UNKNOWN;
1867
1868     // Check of faces coincidence (with some precision) have to be done here
1869     // if (!aFace1.IsEqual(aFace2)) return REL_UNKNOWN;
1870
1871     // Check of faces sharing
1872     if (!aFace1.IsSame(aFace2)) return REL_NOT_GLUED;
1873   }
1874
1875   return REL_OK;
1876 }
1877
1878 void FindConnected (const Standard_Integer         theBlockIndex,
1879                     const TColStd_Array2OfInteger& theRelations,
1880                     TColStd_MapOfInteger&          theProcessedMap,
1881                     TColStd_MapOfInteger&          theConnectedMap)
1882 {
1883   theConnectedMap.Add(theBlockIndex);
1884   theProcessedMap.Add(theBlockIndex);
1885
1886   Standard_Integer nbBlocks = theRelations.ColLength();
1887   Standard_Integer col = 1;
1888   for (; col <= nbBlocks; col++) {
1889     if (theRelations(theBlockIndex, col) == REL_OK ||
1890         theRelations(theBlockIndex, col) == REL_NOT_GLUED) {
1891       if (!theProcessedMap.Contains(col)) {
1892         FindConnected(col, theRelations, theProcessedMap, theConnectedMap);
1893       }
1894     }
1895   }
1896 }
1897
1898 Standard_Boolean HasAnyConnection (const Standard_Integer         theBlockIndex,
1899                                    const TColStd_MapOfInteger&    theWith,
1900                                    const TColStd_Array2OfInteger& theRelations,
1901                                    TColStd_MapOfInteger&          theProcessedMap)
1902 {
1903   theProcessedMap.Add(theBlockIndex);
1904
1905   Standard_Integer nbBlocks = theRelations.ColLength();
1906   Standard_Integer col = 1;
1907   for (; col <= nbBlocks; col++) {
1908     if (theRelations(theBlockIndex, col) != REL_NOT_CONNECTED) {
1909       if (!theProcessedMap.Contains(col)) {
1910         if (theWith.Contains(col))
1911           return Standard_True;
1912         if (HasAnyConnection(col, theWith, theRelations, theProcessedMap))
1913           return Standard_True;
1914       }
1915     }
1916   }
1917
1918   return Standard_False;
1919 }
1920
1921 //=============================================================================
1922 /*!
1923  *  CheckCompoundOfBlocksOld
1924  */
1925 //=============================================================================
1926 Standard_Boolean GEOMImpl_IBlocksOperations::CheckCompoundOfBlocksOld
1927                                                 (Handle(GEOM_Object) theCompound,
1928                                                  list<BCError>&      theErrors)
1929 {
1930   SetErrorCode(KO);
1931
1932   if (theCompound.IsNull()) return Standard_False;
1933   TopoDS_Shape aBlockOrComp = theCompound->GetValue();
1934
1935   Standard_Boolean isCompOfBlocks = Standard_True;
1936
1937   // Map sub-shapes and their indices
1938   TopTools_IndexedMapOfShape anIndices;
1939   TopExp::MapShapes(aBlockOrComp, anIndices);
1940
1941   // 1. Report non-blocks
1942   TopTools_ListOfShape NOT; // Not blocks
1943   TopTools_ListOfShape DEG; // Hexahedral solids, having degenerated edges
1944   TopTools_ListOfShape SEA; // Hexahedral solids, having seam edges
1945   TopTools_ListOfShape BLO; // All blocks from the given compound
1946   AddBlocksFromOld(aBlockOrComp, BLO, NOT, DEG, SEA);
1947
1948   if (NOT.Extent() > 0) {
1949     isCompOfBlocks = Standard_False;
1950     BCError anErr;
1951     anErr.error = NOT_BLOCK;
1952     TopTools_ListIteratorOfListOfShape it (NOT);
1953     for (; it.More(); it.Next()) {
1954       anErr.incriminated.push_back(anIndices.FindIndex(it.Value()));
1955     }
1956     theErrors.push_back(anErr);
1957   }
1958
1959   if (DEG.Extent() > 0 || SEA.Extent() > 0) {
1960     isCompOfBlocks = Standard_False;
1961     BCError anErr;
1962     anErr.error = EXTRA_EDGE;
1963
1964     TopTools_ListIteratorOfListOfShape itDEG (DEG);
1965     for (; itDEG.More(); itDEG.Next()) {
1966       anErr.incriminated.push_back(anIndices.FindIndex(itDEG.Value()));
1967     }
1968
1969     TopTools_ListIteratorOfListOfShape itSEA (SEA);
1970     for (; itSEA.More(); itSEA.Next()) {
1971       anErr.incriminated.push_back(anIndices.FindIndex(itSEA.Value()));
1972     }
1973
1974     theErrors.push_back(anErr);
1975   }
1976
1977   Standard_Integer nbBlocks = BLO.Extent();
1978   if (nbBlocks == 0) {
1979     isCompOfBlocks = Standard_False;
1980     SetErrorCode(OK);
1981     return isCompOfBlocks;
1982   }
1983   if (nbBlocks == 1) {
1984     SetErrorCode(OK);
1985     return isCompOfBlocks;
1986   }
1987
1988   // Convert list of blocks into array for easy and fast access
1989   Standard_Integer ibl = 1;
1990   TopTools_Array1OfShape aBlocks (1, nbBlocks);
1991   TopTools_ListIteratorOfListOfShape BLOit (BLO);
1992   for (; BLOit.More(); BLOit.Next(), ibl++) {
1993     aBlocks.SetValue(ibl, BLOit.Value());
1994   }
1995
1996   // 2. Find relations between all blocks,
1997   //    report connection errors (NOT_GLUED and INVALID_CONNECTION)
1998   TColStd_Array2OfInteger aRelations (1, nbBlocks, 1, nbBlocks);
1999   aRelations.Init(REL_NOT_CONNECTED);
2000
2001   Standard_Integer row = 1;
2002   for (row = 1; row <= nbBlocks; row++) {
2003     TopoDS_Shape aBlock = aBlocks.Value(row);
2004
2005     Standard_Integer col = row + 1;
2006     for (; col <= nbBlocks; col++) {
2007       Standard_Integer aRel = BlocksRelation(aBlock, aBlocks.Value(col));
2008       if (aRel != REL_NOT_CONNECTED) {
2009         aRelations.SetValue(row, col, aRel);
2010         aRelations.SetValue(col, row, aRel);
2011         if (aRel == REL_NOT_GLUED) {
2012           // report connection error
2013           isCompOfBlocks = Standard_False;
2014           BCError anErr;
2015           anErr.error = NOT_GLUED;
2016           anErr.incriminated.push_back(anIndices.FindIndex(aBlocks.Value(row)));
2017           anErr.incriminated.push_back(anIndices.FindIndex(aBlocks.Value(col)));
2018           theErrors.push_back(anErr);
2019         } else if (aRel == REL_COLLISION_VV ||
2020                    aRel == REL_COLLISION_FF ||
2021                    aRel == REL_COLLISION_EE ||
2022                    aRel == REL_UNKNOWN) {
2023           // report connection error
2024           isCompOfBlocks = Standard_False;
2025           BCError anErr;
2026           anErr.error = INVALID_CONNECTION;
2027           anErr.incriminated.push_back(anIndices.FindIndex(aBlocks.Value(row)));
2028           anErr.incriminated.push_back(anIndices.FindIndex(aBlocks.Value(col)));
2029           theErrors.push_back(anErr);
2030         } else {
2031         }
2032       }
2033     }
2034   }
2035
2036   // 3. Find largest set of connected (good connection or not glued) blocks
2037   TColStd_MapOfInteger aProcessedMap;
2038   TColStd_MapOfInteger aLargestSet;
2039   TColStd_MapOfInteger aCurrentSet;
2040   for (ibl = 1; ibl <= nbBlocks; ibl++) {
2041     if (!aProcessedMap.Contains(ibl)) {
2042       aCurrentSet.Clear();
2043       FindConnected(ibl, aRelations, aProcessedMap, aCurrentSet);
2044       if (aCurrentSet.Extent() > aLargestSet.Extent()) {
2045         aLargestSet = aCurrentSet;
2046       }
2047     }
2048   }
2049
2050   // 4. Report all blocks, isolated from <aLargestSet>
2051   BCError anErr;
2052   anErr.error = NOT_CONNECTED;
2053   Standard_Boolean hasIsolated = Standard_False;
2054   for (ibl = 1; ibl <= nbBlocks; ibl++) {
2055     if (!aLargestSet.Contains(ibl)) {
2056       aProcessedMap.Clear();
2057       if (!HasAnyConnection(ibl, aLargestSet, aRelations, aProcessedMap)) {
2058         // report connection absence
2059         hasIsolated = Standard_True;
2060         anErr.incriminated.push_back(anIndices.FindIndex(aBlocks.Value(ibl)));
2061       }
2062     }
2063   }
2064   if (hasIsolated) {
2065     isCompOfBlocks = Standard_False;
2066     theErrors.push_back(anErr);
2067   }
2068
2069   SetErrorCode(OK);
2070   return isCompOfBlocks;
2071 }
2072
2073 //=============================================================================
2074 /*!
2075  *  PrintBCErrors
2076  */
2077 //=============================================================================
2078 TCollection_AsciiString GEOMImpl_IBlocksOperations::PrintBCErrors
2079                                                 (Handle(GEOM_Object)  theCompound,
2080                                                  const list<BCError>& theErrors)
2081 {
2082   TCollection_AsciiString aDescr;
2083
2084   list<BCError>::const_iterator errIt = theErrors.begin();
2085   int i = 0;
2086   for (; errIt != theErrors.end(); i++, errIt++) {
2087     BCError errStruct = *errIt;
2088
2089     switch (errStruct.error) {
2090     case NOT_BLOCK:
2091       aDescr += "\n\tNot a Blocks: ";
2092       break;
2093     case EXTRA_EDGE:
2094       aDescr += "\n\tHexahedral solids with degenerated and/or seam edges: ";
2095       break;
2096     case INVALID_CONNECTION:
2097       aDescr += "\n\tInvalid connection between two blocks: ";
2098       break;
2099     case NOT_CONNECTED:
2100       aDescr += "\n\tBlocks, not connected with main body: ";
2101       break;
2102     case NOT_GLUED:
2103       aDescr += "\n\tNot glued blocks: ";
2104       break;
2105     default:
2106       break;
2107     }
2108
2109     list<int> sshList = errStruct.incriminated;
2110     list<int>::iterator sshIt = sshList.begin();
2111     int jj = 0;
2112     for (; sshIt != sshList.end(); jj++, sshIt++) {
2113       if (jj > 0)
2114         aDescr += ", ";
2115       aDescr += TCollection_AsciiString(*sshIt);
2116     }
2117   }
2118
2119   return aDescr;
2120 }
2121
2122 //=============================================================================
2123 /*!
2124  *  CheckCompoundOfBlocks
2125  */
2126 //=============================================================================
2127 Standard_Boolean GEOMImpl_IBlocksOperations::CheckCompoundOfBlocks
2128                                               (Handle(GEOM_Object) theCompound,
2129                                                list<BCError>&      theErrors)
2130 {
2131   SetErrorCode(KO);
2132
2133   if (theCompound.IsNull()) return Standard_False;
2134   TopoDS_Shape aBlockOrComp = theCompound->GetValue();
2135
2136   Standard_Boolean isCompOfBlocks = Standard_True;
2137
2138   // Map sub-shapes and their indices
2139   TopTools_IndexedMapOfShape anIndices;
2140   TopExp::MapShapes(aBlockOrComp, anIndices);
2141
2142   // 1. Separate blocks from non-blocks
2143   TopTools_ListOfShape NOT; // Not blocks
2144   TopTools_ListOfShape EXT; // Hexahedral solids, having degenerated and/or seam edges
2145   TopTools_ListOfShape BLO; // All blocks from the given compound
2146   AddBlocksFrom(aBlockOrComp, BLO, NOT, EXT);
2147
2148   // Report non-blocks
2149   if (NOT.Extent() > 0) {
2150     isCompOfBlocks = Standard_False;
2151     BCError anErr;
2152     anErr.error = NOT_BLOCK;
2153     TopTools_ListIteratorOfListOfShape it (NOT);
2154     for (; it.More(); it.Next()) {
2155       anErr.incriminated.push_back(anIndices.FindIndex(it.Value()));
2156     }
2157     theErrors.push_back(anErr);
2158   }
2159
2160   // Report solids, having degenerated and/or seam edges
2161   if (EXT.Extent() > 0) {
2162     isCompOfBlocks = Standard_False;
2163     BCError anErr;
2164     anErr.error = EXTRA_EDGE;
2165     TopTools_ListIteratorOfListOfShape it (EXT);
2166     for (; it.More(); it.Next()) {
2167       anErr.incriminated.push_back(anIndices.FindIndex(it.Value()));
2168     }
2169     theErrors.push_back(anErr);
2170   }
2171
2172   Standard_Integer nbBlocks = BLO.Extent();
2173   if (nbBlocks == 0) {
2174     isCompOfBlocks = Standard_False;
2175     SetErrorCode(OK);
2176     return isCompOfBlocks;
2177   }
2178   if (nbBlocks == 1) {
2179     SetErrorCode(OK);
2180     return isCompOfBlocks;
2181   }
2182
2183   // Prepare data for 2. and 3.
2184   TColStd_Array2OfInteger aRelations (1, nbBlocks, 1, nbBlocks);
2185   aRelations.Init(REL_NOT_CONNECTED);
2186
2187   TopTools_IndexedMapOfShape mapBlocks;
2188
2189   BRep_Builder BB;
2190   TopoDS_Compound aComp;
2191   BB.MakeCompound(aComp);
2192
2193   TopTools_ListIteratorOfListOfShape BLOit (BLO);
2194   for (; BLOit.More(); BLOit.Next()) {
2195     mapBlocks.Add(BLOit.Value());
2196     BB.Add(aComp, BLOit.Value());
2197   }
2198
2199   // 2. Find glued blocks (having shared faces)
2200   TopTools_IndexedDataMapOfShapeListOfShape mapFaceBlocks;
2201   GEOMImpl_Block6Explorer::MapShapesAndAncestors
2202     (aComp, TopAbs_FACE, TopAbs_SOLID, mapFaceBlocks);
2203
2204   Standard_Integer prevInd = 0, curInd = 0;
2205   Standard_Integer ind = 1, nbFaces = mapFaceBlocks.Extent();
2206   for (; ind <= nbFaces; ind++) {
2207     const TopTools_ListOfShape& aGluedBlocks = mapFaceBlocks.FindFromIndex(ind);
2208     if (aGluedBlocks.Extent() > 1) { // Shared face found
2209       TopTools_ListIteratorOfListOfShape aGluedBlocksIt (aGluedBlocks);
2210       TopoDS_Shape prevBlock, curBlock;
2211       for (; aGluedBlocksIt.More(); aGluedBlocksIt.Next()) {
2212         curBlock = aGluedBlocksIt.Value();
2213         if (!prevBlock.IsNull()) {
2214           prevInd = mapBlocks.FindIndex(prevBlock);
2215           curInd  = mapBlocks.FindIndex(curBlock);
2216           aRelations.SetValue(prevInd, curInd, REL_OK);
2217           aRelations.SetValue(curInd, prevInd, REL_OK);
2218         }
2219         prevBlock = curBlock;
2220       }
2221     }
2222   }
2223
2224   // 3. Find not glued blocks
2225   GEOMAlgo_GlueAnalyser aGD; 
2226
2227   aGD.SetShape(aComp);
2228   aGD.SetTolerance(Precision::Confusion());
2229   aGD.SetCheckGeometry(Standard_True);
2230   aGD.Perform();
2231
2232   Standard_Integer iErr, iWrn;
2233   iErr = aGD.ErrorStatus();
2234   if (iErr) {
2235     SetErrorCode("Error in GEOMAlgo_GlueAnalyser");
2236     return isCompOfBlocks;
2237   }
2238   iWrn = aGD.WarningStatus();
2239   if (iWrn) {
2240     MESSAGE("Warning in GEOMAlgo_GlueAnalyser");
2241   }
2242
2243   // Report not glued blocks
2244   if (aGD.HasSolidsToGlue()) {
2245     isCompOfBlocks = Standard_False;
2246     Standard_Integer aSx1Ind, aSx2Ind;
2247
2248     const GEOMAlgo_ListOfCoupleOfShapes& aLCS = aGD.SolidsToGlue();
2249     GEOMAlgo_ListIteratorOfListOfCoupleOfShapes aItCS (aLCS);
2250     for (; aItCS.More(); aItCS.Next()) {
2251       const GEOMAlgo_CoupleOfShapes& aCS = aItCS.Value();
2252       const TopoDS_Shape& aSx1 = aCS.Shape1();
2253       const TopoDS_Shape& aSx2 = aCS.Shape2();
2254
2255       aSx1Ind = mapBlocks.FindIndex(aSx1);
2256       aSx2Ind = mapBlocks.FindIndex(aSx2);
2257       aRelations.SetValue(aSx1Ind, aSx2Ind, NOT_GLUED);
2258       aRelations.SetValue(aSx2Ind, aSx1Ind, NOT_GLUED);
2259
2260       BCError anErr;
2261       anErr.error = NOT_GLUED;
2262       anErr.incriminated.push_back(anIndices.FindIndex(aSx1));
2263       anErr.incriminated.push_back(anIndices.FindIndex(aSx2));
2264       theErrors.push_back(anErr);
2265     }
2266   }
2267
2268   // 4. Find largest set of connected (good connection or not glued) blocks
2269   Standard_Integer ibl = 1;
2270   TColStd_MapOfInteger aProcessedMap;
2271   TColStd_MapOfInteger aLargestSet;
2272   TColStd_MapOfInteger aCurrentSet;
2273   for (ibl = 1; ibl <= nbBlocks; ibl++) {
2274     if (!aProcessedMap.Contains(ibl)) {
2275       aCurrentSet.Clear();
2276       FindConnected(ibl, aRelations, aProcessedMap, aCurrentSet);
2277       if (aCurrentSet.Extent() > aLargestSet.Extent()) {
2278         aLargestSet = aCurrentSet;
2279       }
2280     }
2281   }
2282
2283   // 5. Report all blocks, isolated from <aLargestSet>
2284   BCError anErr;
2285   anErr.error = NOT_CONNECTED;
2286   Standard_Boolean hasIsolated = Standard_False;
2287   for (ibl = 1; ibl <= nbBlocks; ibl++) {
2288     if (!aLargestSet.Contains(ibl)) {
2289       aProcessedMap.Clear();
2290       if (!HasAnyConnection(ibl, aLargestSet, aRelations, aProcessedMap)) {
2291         // report connection absence
2292         hasIsolated = Standard_True;
2293         anErr.incriminated.push_back(anIndices.FindIndex(mapBlocks.FindKey(ibl)));
2294       }
2295     }
2296   }
2297   if (hasIsolated) {
2298     isCompOfBlocks = Standard_False;
2299     theErrors.push_back(anErr);
2300   }
2301
2302   SetErrorCode(OK);
2303   return isCompOfBlocks;
2304 }
2305
2306 //=============================================================================
2307 /*!
2308  *  RemoveExtraEdges
2309  */
2310 //=============================================================================
2311 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::RemoveExtraEdges
2312                                              (Handle(GEOM_Object) theObject)
2313 {
2314   SetErrorCode(KO);
2315
2316   if (theObject.IsNull()) return NULL;
2317
2318   Handle(GEOM_Function) aLastFunction = theObject->GetLastFunction();
2319   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be fixed
2320
2321   //Add a new Copy object
2322   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
2323
2324   //Add a function
2325   Handle(GEOM_Function) aFunction =
2326     aCopy->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_REMOVE_EXTRA);
2327
2328   //Check if the function is set correctly
2329   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
2330
2331   GEOMImpl_IBlockTrsf aTI (aFunction);
2332   aTI.SetOriginal(aLastFunction);
2333
2334   //Compute the fixed shape
2335   try {
2336 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2337     OCC_CATCH_SIGNALS;
2338 #endif
2339     if (!GetSolver()->ComputeFunction(aFunction)) {
2340       SetErrorCode("Block driver failed to remove extra edges of the given shape");
2341       return NULL;
2342     }
2343   }
2344   catch (Standard_Failure) {
2345     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2346     SetErrorCode(aFail->GetMessageString());
2347     return NULL;
2348   }
2349
2350   //Make a Python command
2351   GEOM::TPythonDump(aFunction) << aCopy
2352     << " = geompy.RemoveExtraEdges(" << theObject << ")";
2353
2354   SetErrorCode(OK);
2355   return aCopy;
2356 }
2357
2358 //=============================================================================
2359 /*!
2360  *  CheckAndImprove
2361  */
2362 //=============================================================================
2363 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::CheckAndImprove
2364                                              (Handle(GEOM_Object) theObject)
2365 {
2366   SetErrorCode(KO);
2367
2368   if (theObject.IsNull()) return NULL;
2369
2370   Handle(GEOM_Function) aLastFunction = theObject->GetLastFunction();
2371   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be fixed
2372
2373   //Add a new Copy object
2374   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
2375
2376   //Add a function
2377   Handle(GEOM_Function) aFunction =
2378     aCopy->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_COMPOUND_IMPROVE);
2379
2380   //Check if the function is set correctly
2381   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
2382
2383   GEOMImpl_IBlockTrsf aTI (aFunction);
2384   aTI.SetOriginal(aLastFunction);
2385
2386   //Compute the fixed shape
2387   try {
2388 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2389     OCC_CATCH_SIGNALS;
2390 #endif
2391     if (!GetSolver()->ComputeFunction(aFunction)) {
2392       SetErrorCode("Block driver failed to improve the given blocks compound");
2393       return NULL;
2394     }
2395   }
2396   catch (Standard_Failure) {
2397     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2398     SetErrorCode(aFail->GetMessageString());
2399     return NULL;
2400   }
2401
2402   //Make a Python command
2403   GEOM::TPythonDump(aFunction) << aCopy
2404     << " = geompy.CheckAndImprove(" << theObject << ")";
2405
2406   SetErrorCode(OK);
2407   return aCopy;
2408 }
2409
2410 //=============================================================================
2411 /*!
2412  *  ExplodeCompoundOfBlocks
2413  */
2414 //=============================================================================
2415 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IBlocksOperations::ExplodeCompoundOfBlocks
2416                                                 (Handle(GEOM_Object)    theCompound,
2417                                                  const Standard_Integer theMinNbFaces,
2418                                                  const Standard_Integer theMaxNbFaces)
2419 {
2420   SetErrorCode(KO);
2421
2422   if (theCompound.IsNull()) return NULL;
2423   TopoDS_Shape aBlockOrComp = theCompound->GetValue();
2424   if (aBlockOrComp.IsNull()) return NULL;
2425
2426   Handle(TColStd_HSequenceOfTransient) aBlocks = new TColStd_HSequenceOfTransient;
2427   Handle(GEOM_Object) anObj;
2428   Handle(GEOM_Function) aFunction;
2429
2430   TopTools_MapOfShape mapShape;
2431   TCollection_AsciiString anAsciiList, anEntry;
2432
2433   // Map shapes
2434   TopTools_IndexedMapOfShape anIndices;
2435   TopExp::MapShapes(aBlockOrComp, anIndices);
2436   Handle(TColStd_HArray1OfInteger) anArray;
2437
2438   // Explode
2439   try {
2440 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2441     OCC_CATCH_SIGNALS;
2442 #endif
2443     TopExp_Explorer exp (aBlockOrComp, TopAbs_SOLID);
2444     for (; exp.More(); exp.Next()) {
2445       if (mapShape.Add(exp.Current())) {
2446         TopoDS_Shape aSolid = exp.Current();
2447
2448         TopTools_MapOfShape mapFaces;
2449         TopExp_Explorer expF (aSolid, TopAbs_FACE);
2450         Standard_Integer nbFaces = 0;
2451         for (; expF.More(); expF.Next()) {
2452           if (mapFaces.Add(expF.Current())) {
2453             nbFaces++;
2454           }
2455         }
2456
2457         if (theMinNbFaces <= nbFaces && nbFaces <= theMaxNbFaces) {
2458           anArray = new TColStd_HArray1OfInteger(1,1);
2459           anArray->SetValue(1, anIndices.FindIndex(aSolid));
2460           anObj = GetEngine()->AddSubShape(theCompound, anArray);
2461           aBlocks->Append(anObj);
2462
2463           //Make a Python command
2464           TDF_Tool::Entry(anObj->GetEntry(), anEntry);
2465           anAsciiList += anEntry + ", ";
2466         }
2467       }
2468     }
2469   }
2470   catch (Standard_Failure) {
2471     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2472     SetErrorCode(aFail->GetMessageString());
2473     return aBlocks;
2474   }
2475
2476   if (aBlocks->IsEmpty()) {
2477     SetErrorCode("There are no specified blocks in the given shape");
2478     return aBlocks;
2479   }
2480
2481   anAsciiList.Trunc(anAsciiList.Length() - 2);
2482
2483   //The explode doesn't change object so no new function is required.
2484   aFunction = theCompound->GetLastFunction();
2485
2486   //Make a Python command
2487   GEOM::TPythonDump(aFunction, /*append=*/true)
2488     << "[" << anAsciiList.ToCString() << "] = geompy.MakeBlockExplode("
2489     << theCompound << ", " << theMinNbFaces << ", " << theMaxNbFaces << ")";
2490
2491   SetErrorCode(OK);
2492   return aBlocks;
2493 }
2494
2495 //=============================================================================
2496 /*!
2497  *  GetBlockNearPoint
2498  */
2499 //=============================================================================
2500 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetBlockNearPoint
2501                                                 (Handle(GEOM_Object) theCompound,
2502                                                  Handle(GEOM_Object) thePoint)
2503 {
2504   SetErrorCode(KO);
2505
2506   //New object
2507   Handle(GEOM_Object) aResult;
2508
2509   // Arguments
2510   if (theCompound.IsNull() || thePoint.IsNull()) return NULL;
2511
2512   TopoDS_Shape aBlockOrComp = theCompound->GetValue();
2513   if (aBlockOrComp.IsNull()) {
2514     SetErrorCode("Compound is null");
2515     return NULL;
2516   }
2517   if (aBlockOrComp.ShapeType() != TopAbs_COMPOUND &&
2518       aBlockOrComp.ShapeType() != TopAbs_COMPSOLID) {
2519     SetErrorCode("Shape to find block in is not a compound");
2520     return NULL;
2521   }
2522
2523   TopoDS_Shape anArg = thePoint->GetValue();
2524   if (anArg.IsNull()) {
2525     SetErrorCode("Point is null");
2526     return NULL;
2527   }
2528   if (anArg.ShapeType() != TopAbs_VERTEX) {
2529     SetErrorCode("Shape for block identification is not a vertex");
2530     return NULL;
2531   }
2532
2533   //Compute the Block value
2534   try {
2535 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2536     OCC_CATCH_SIGNALS;
2537 #endif
2538     TopoDS_Shape aShape;
2539
2540     TopoDS_Vertex aVert = TopoDS::Vertex(anArg);
2541     gp_Pnt aPnt = BRep_Tool::Pnt(aVert);
2542     Standard_Real PX, PY, PZ;
2543     aPnt.Coord(PX, PY, PZ);
2544
2545     // 1. Classify the point relatively each block
2546     Standard_Integer nearest = 2, nbFound = 0;
2547     TopTools_DataMapOfShapeInteger mapShapeDist;
2548     TopExp_Explorer exp (aBlockOrComp, TopAbs_SOLID);
2549     for (; exp.More(); exp.Next()) {
2550       TopoDS_Shape aSolid = exp.Current();
2551
2552       if (!mapShapeDist.IsBound(aSolid)) {
2553         Standard_Integer aDistance = 2;
2554
2555         // 1.a. Classify relatively Bounding box
2556         Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
2557         Bnd_Box BB;
2558         BRepBndLib::Add(aSolid, BB);
2559         BB.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
2560         if (PX < Xmin || Xmax < PX ||
2561             PY < Ymin || Ymax < PY ||
2562             PZ < Zmin || Zmax < PZ) {
2563           // OUT of bounding box
2564           aDistance = 1;
2565         } else {
2566           // 1.b. Classify relatively the solid itself
2567           BRepClass3d_SolidClassifier SC (aSolid, aPnt, Precision::Confusion());
2568           if (SC.State() == TopAbs_IN) {
2569             aDistance = -1;
2570           } else if (SC.State() == TopAbs_ON) {
2571             aDistance = 0;
2572           } else { // OUT
2573             aDistance = 1;
2574           }
2575         }
2576
2577         if (aDistance < nearest) {
2578           nearest = aDistance;
2579           aShape = aSolid;
2580           nbFound = 1;
2581
2582           // A first found block, containing the point inside, will be returned.
2583           // It is the solution, if there are no intersecting blocks in the compound.
2584           if (nearest == -1) break;
2585
2586         } else if (aDistance == nearest) {
2587           nbFound++;
2588         } else {
2589         }
2590
2591         mapShapeDist.Bind(aSolid, aDistance);
2592       } // if (!mapShapeDist.IsBound(aSolid))
2593     }
2594
2595     // 2. Define block, containing the point or having minimum distance to it
2596     if (nbFound > 1) {
2597       if (nearest == 0) {
2598         // The point is on boundary of some blocks and there are
2599         // no blocks, having the point inside their volume
2600         SetErrorCode("Multiple blocks near the given point are found");
2601         return NULL;
2602
2603       } else if (nearest == 1) {
2604         // The point is outside some blocks and there are
2605         // no blocks, having the point inside or on boundary.
2606         // We will get a nearest block
2607         Standard_Real minDist = RealLast();
2608         TopTools_DataMapIteratorOfDataMapOfShapeInteger mapShapeDistIter (mapShapeDist);
2609         for (; mapShapeDistIter.More(); mapShapeDistIter.Next()) {
2610           if (mapShapeDistIter.Value() == 1) {
2611             TopoDS_Shape aSolid = mapShapeDistIter.Key();
2612             BRepExtrema_DistShapeShape aDistTool (aVert, aSolid);
2613             if (!aDistTool.IsDone()) {
2614               SetErrorCode("Can not find a distance from the given point to one of blocks");
2615               return NULL;
2616             }
2617             Standard_Real aDist = aDistTool.Value();
2618             if (aDist < minDist) {
2619               minDist = aDist;
2620               aShape = aSolid;
2621             }
2622           }
2623         }
2624       } else { // nearest == -1
2625 //        // The point is inside some blocks.
2626 //        // We will get a block with nearest center
2627 //        Standard_Real minDist = RealLast();
2628 //        TopTools_DataMapIteratorOfDataMapOfShapeInteger mapShapeDistIter (mapShapeDist);
2629 //        for (; mapShapeDistIter.More(); mapShapeDistIter.Next()) {
2630 //          if (mapShapeDistIter.Value() == -1) {
2631 //            TopoDS_Shape aSolid = mapShapeDistIter.Key();
2632 //            GProp_GProps aSystem;
2633 //            BRepGProp::VolumeProperties(aSolid, aSystem);
2634 //            gp_Pnt aCenterMass = aSystem.CentreOfMass();
2635 //
2636 //            Standard_Real aDist = aCenterMass.Distance(aPnt);
2637 //            if (aDist < minDist) {
2638 //              minDist = aDist;
2639 //              aShape = aSolid;
2640 //            }
2641 //          }
2642 //        }
2643       }
2644     } // if (nbFound > 1)
2645
2646     if (nbFound == 0) {
2647       SetErrorCode("There are no blocks near the given point");
2648       return NULL;
2649     } else {
2650       TopTools_IndexedMapOfShape anIndices;
2651       TopExp::MapShapes(aBlockOrComp, anIndices);
2652       Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
2653       anArray->SetValue(1, anIndices.FindIndex(aShape));
2654       aResult = GetEngine()->AddSubShape(theCompound, anArray);
2655     }
2656   }
2657   catch (Standard_Failure) {
2658     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2659     SetErrorCode(aFail->GetMessageString());
2660     return NULL;
2661   }
2662
2663   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
2664
2665   //Make a Python command
2666   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetBlockNearPoint("
2667                                << theCompound << ", " << thePoint << ")";
2668
2669   SetErrorCode(OK);
2670   return aResult;
2671 }
2672
2673 //=============================================================================
2674 /*!
2675  *  GetBlockByParts
2676  */
2677 //=============================================================================
2678 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::GetBlockByParts
2679                       (Handle(GEOM_Object)                         theCompound,
2680                        const Handle(TColStd_HSequenceOfTransient)& theParts)
2681 {
2682   SetErrorCode(KO);
2683
2684   Handle(GEOM_Object) aResult;
2685
2686   if (theCompound.IsNull() || theParts.IsNull()) return NULL;
2687   TopoDS_Shape aBlockOrComp = theCompound->GetValue();
2688   if (aBlockOrComp.IsNull()) return NULL;
2689
2690   //Get the parts
2691   Standard_Integer argi, aLen = theParts->Length();
2692   TopTools_Array1OfShape anArgs (1, aLen);
2693   TCollection_AsciiString anEntry, aPartsDescr;
2694   for (argi = 1; argi <= aLen; argi++) {
2695     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theParts->Value(argi));
2696     Handle(GEOM_Function) aRef = anObj->GetLastFunction();
2697     if (aRef.IsNull()) return NULL;
2698
2699     TopoDS_Shape anArg = aRef->GetValue();
2700     if (anArg.IsNull()) {
2701       SetErrorCode("Null shape is given as argument");
2702       return NULL;
2703     }
2704     anArgs(argi) = anArg;
2705
2706     // For Python command
2707     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
2708     if (argi > 1) aPartsDescr += ", ";
2709     aPartsDescr += anEntry;
2710   }
2711
2712   //Compute the Block value
2713   try {
2714 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2715     OCC_CATCH_SIGNALS;
2716 #endif
2717     // 1. Explode compound on solids
2718     TopTools_MapOfShape mapShape;
2719     Standard_Integer nbSolids = 0;
2720     TopExp_Explorer exp (aBlockOrComp, TopAbs_SOLID);
2721     for (; exp.More(); exp.Next()) {
2722       if (mapShape.Add(exp.Current())) {
2723         nbSolids++;
2724       }
2725     }
2726
2727     mapShape.Clear();
2728     Standard_Integer ind = 1;
2729     TopTools_Array1OfShape aSolids (1, nbSolids);
2730     TColStd_Array1OfInteger aNbParts (1, nbSolids);
2731     for (exp.Init(aBlockOrComp, TopAbs_SOLID); exp.More(); exp.Next(), ind++) {
2732       if (mapShape.Add(exp.Current())) {
2733         TopoDS_Shape aSolid = exp.Current();
2734         aSolids(ind) = aSolid;
2735         aNbParts(ind) = 0;
2736
2737         // 2. Define quantity of parts, contained in each solid
2738         TopTools_IndexedMapOfShape aSubShapes;
2739         TopExp::MapShapes(aSolid, aSubShapes);
2740         for (argi = 1; argi <= aLen; argi++) {
2741           if (aSubShapes.Contains(anArgs(argi))) {
2742             aNbParts(ind)++;
2743           }
2744         }
2745       }
2746     }
2747
2748     // 3. Define solid, containing maximum quantity of parts
2749     Standard_Integer maxNb = 0, nbFound = 0;
2750     TopoDS_Shape aShape;
2751     for (ind = 1; ind <= nbSolids; ind++) {
2752       if (aNbParts(ind) > maxNb) {
2753         maxNb = aNbParts(ind);
2754         aShape = aSolids(ind);
2755         nbFound = 1;
2756       } else if (aNbParts(ind) == maxNb) {
2757         nbFound++;
2758       } else {
2759       }
2760     }
2761     if (nbFound > 1) {
2762       SetErrorCode("Multiple blocks, containing maximum quantity of the given parts, are found");
2763       return NULL;
2764     } else if (nbFound == 0) {
2765       SetErrorCode("There are no blocks, containing the given parts");
2766       return NULL;
2767     } else {
2768       TopTools_IndexedMapOfShape anIndices;
2769       TopExp::MapShapes(aBlockOrComp, anIndices);
2770       Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
2771       anArray->SetValue(1, anIndices.FindIndex(aShape));
2772       aResult = GetEngine()->AddSubShape(theCompound, anArray);
2773     }
2774   } catch (Standard_Failure) {
2775     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2776     SetErrorCode(aFail->GetMessageString());
2777     return NULL;
2778   }
2779
2780   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
2781
2782   //Make a Python command
2783   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetBlockByParts("
2784     << theCompound << ", [" << aPartsDescr.ToCString() << "])";
2785
2786   SetErrorCode(OK);
2787   return aResult;
2788 }
2789
2790 //=============================================================================
2791 /*!
2792  *  GetBlocksByParts
2793  */
2794 //=============================================================================
2795 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IBlocksOperations::GetBlocksByParts
2796                       (Handle(GEOM_Object)                         theCompound,
2797                        const Handle(TColStd_HSequenceOfTransient)& theParts)
2798 {
2799   SetErrorCode(KO);
2800
2801   if (theCompound.IsNull() || theParts.IsNull()) return NULL;
2802   TopoDS_Shape aBlockOrComp = theCompound->GetValue();
2803   if (aBlockOrComp.IsNull()) return NULL;
2804
2805   Handle(TColStd_HSequenceOfTransient) aBlocks = new TColStd_HSequenceOfTransient;
2806   Handle(GEOM_Object) anObj;
2807   Handle(GEOM_Function) aFunction;
2808
2809   //Get the parts
2810   Standard_Integer argi, aLen = theParts->Length();
2811   TopTools_Array1OfShape anArgs (1, aLen);
2812   TCollection_AsciiString anEntry, aPartsDescr, anAsciiList;
2813
2814   for (argi = 1; argi <= aLen; argi++) {
2815     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theParts->Value(argi));
2816     Handle(GEOM_Function) aRef = anObj->GetLastFunction();
2817     if (aRef.IsNull()) return NULL;
2818
2819     TopoDS_Shape anArg = aRef->GetValue();
2820     if (anArg.IsNull()) {
2821       SetErrorCode("Null shape is given as argument");
2822       return NULL;
2823     }
2824     anArgs(argi) = anArg;
2825
2826     // For Python command
2827     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
2828     aPartsDescr += anEntry + ", ";
2829   }
2830
2831   //Get the Blocks
2832   try {
2833 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2834     OCC_CATCH_SIGNALS;
2835 #endif
2836     TopTools_MapOfShape mapShape;
2837     Standard_Integer nbSolids = 0;
2838     TopExp_Explorer exp (aBlockOrComp, TopAbs_SOLID);
2839     for (; exp.More(); exp.Next()) {
2840       if (mapShape.Add(exp.Current())) {
2841         nbSolids++;
2842       }
2843     }
2844
2845     mapShape.Clear();
2846     Standard_Integer ind = 1;
2847     TopTools_Array1OfShape aSolids (1, nbSolids);
2848     TColStd_Array1OfInteger aNbParts (1, nbSolids);
2849     for (exp.Init(aBlockOrComp, TopAbs_SOLID); exp.More(); exp.Next(), ind++) {
2850       if (mapShape.Add(exp.Current())) {
2851         TopoDS_Shape aSolid = exp.Current();
2852         aSolids(ind) = aSolid;
2853         aNbParts(ind) = 0;
2854
2855         // 2. Define quantity of parts, contained in each solid
2856         TopTools_IndexedMapOfShape aSubShapes;
2857         TopExp::MapShapes(aSolid, aSubShapes);
2858         for (argi = 1; argi <= aLen; argi++) {
2859           if (aSubShapes.Contains(anArgs(argi))) {
2860             aNbParts(ind)++;
2861           }
2862         }
2863       }
2864     }
2865
2866     // 3. Define solid, containing maximum quantity of parts
2867     Standard_Integer maxNb = 0, nbFound = 0;
2868     for (ind = 1; ind <= nbSolids; ind++) {
2869       if (aNbParts(ind) > maxNb) {
2870         maxNb = aNbParts(ind);
2871         nbFound = 1;
2872       } else if (aNbParts(ind) == maxNb) {
2873         nbFound++;
2874       } else {
2875       }
2876     }
2877     if (nbFound == 0) {
2878       SetErrorCode("There are no blocks, containing the given parts");
2879       return NULL;
2880     }
2881
2882     // Map shapes
2883     TopTools_IndexedMapOfShape anIndices;
2884     TopExp::MapShapes(aBlockOrComp, anIndices);
2885     Handle(TColStd_HArray1OfInteger) anArray;
2886
2887     for (ind = 1; ind <= nbSolids; ind++) {
2888       if (aNbParts(ind) == maxNb) {
2889         anArray = new TColStd_HArray1OfInteger(1,1);
2890         anArray->SetValue(1, anIndices.FindIndex(aSolids(ind)));
2891         anObj = GetEngine()->AddSubShape(theCompound, anArray);
2892         aBlocks->Append(anObj);
2893
2894         // For Python command
2895         TDF_Tool::Entry(anObj->GetEntry(), anEntry);
2896         anAsciiList += anEntry + ", ";
2897         if (aFunction.IsNull())
2898           aFunction = anObj->GetLastFunction();
2899       }
2900     }
2901   }
2902   catch (Standard_Failure) {
2903     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2904     SetErrorCode(aFail->GetMessageString());
2905     return NULL;
2906   }
2907
2908   //Make a Python command
2909   aPartsDescr.Trunc(aPartsDescr.Length() - 2);
2910   anAsciiList.Trunc(anAsciiList.Length() - 2);
2911
2912   GEOM::TPythonDump(aFunction) << "[" << anAsciiList.ToCString()
2913     << "] = geompy.GetBlocksByParts(" << theCompound
2914       << ", [" << aPartsDescr.ToCString() << "])";
2915
2916   SetErrorCode(OK);
2917   return aBlocks;
2918 }
2919
2920 //=============================================================================
2921 /*!
2922  *  MakeMultiTransformation1D
2923  */
2924 //=============================================================================
2925 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::MakeMultiTransformation1D
2926                                              (Handle(GEOM_Object)    theObject,
2927                                               const Standard_Integer theDirFace1,
2928                                               const Standard_Integer theDirFace2,
2929                                               const Standard_Integer theNbTimes)
2930 {
2931   SetErrorCode(KO);
2932
2933   if (theObject.IsNull()) return NULL;
2934
2935   Handle(GEOM_Function) aLastFunction = theObject->GetLastFunction();
2936   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be moved
2937
2938   //Add a new Copy object
2939   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
2940
2941   //Add a translate function
2942   Handle(GEOM_Function) aFunction =
2943     aCopy->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_MULTI_TRANSFORM_1D);
2944
2945   //Check if the function is set correctly
2946   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
2947
2948   GEOMImpl_IBlockTrsf aTI (aFunction);
2949   aTI.SetOriginal(aLastFunction);
2950   aTI.SetFace1U(theDirFace1);
2951   aTI.SetFace2U(theDirFace2);
2952   aTI.SetNbIterU(theNbTimes);
2953
2954   //Compute the transformation
2955   try {
2956 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
2957     OCC_CATCH_SIGNALS;
2958 #endif
2959     if (!GetSolver()->ComputeFunction(aFunction)) {
2960       SetErrorCode("Block driver failed to make multi-transformation");
2961       return NULL;
2962     }
2963   }
2964   catch (Standard_Failure) {
2965     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2966     SetErrorCode(aFail->GetMessageString());
2967     return NULL;
2968   }
2969
2970   //Make a Python command
2971   GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeMultiTransformation1D("
2972     << theObject << ", " << theDirFace1 << ", " << theDirFace2 << ", " << theNbTimes << ")";
2973
2974   SetErrorCode(OK);
2975   return aCopy;
2976 }
2977
2978 //=============================================================================
2979 /*!
2980  *  MakeMultiTransformation2D
2981  */
2982 //=============================================================================
2983 Handle(GEOM_Object) GEOMImpl_IBlocksOperations::MakeMultiTransformation2D
2984                                              (Handle(GEOM_Object)    theObject,
2985                                               const Standard_Integer theDirFace1U,
2986                                               const Standard_Integer theDirFace2U,
2987                                               const Standard_Integer theNbTimesU,
2988                                               const Standard_Integer theDirFace1V,
2989                                               const Standard_Integer theDirFace2V,
2990                                               const Standard_Integer theNbTimesV)
2991 {
2992   SetErrorCode(KO);
2993
2994   if (theObject.IsNull()) return NULL;
2995
2996   Handle(GEOM_Function) aLastFunction = theObject->GetLastFunction();
2997   if (aLastFunction.IsNull()) return NULL; //There is no function which creates an object to be moved
2998
2999   //Add a new Copy object
3000   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
3001
3002   //Add a translate function
3003   Handle(GEOM_Function) aFunction =
3004     aCopy->AddFunction(GEOMImpl_BlockDriver::GetID(), BLOCK_MULTI_TRANSFORM_2D);
3005
3006   //Check if the function is set correctly
3007   if (aFunction->GetDriverGUID() != GEOMImpl_BlockDriver::GetID()) return NULL;
3008
3009   GEOMImpl_IBlockTrsf aTI (aFunction);
3010   aTI.SetOriginal(aLastFunction);
3011   aTI.SetFace1U(theDirFace1U);
3012   aTI.SetFace2U(theDirFace2U);
3013   aTI.SetNbIterU(theNbTimesU);
3014   aTI.SetFace1V(theDirFace1V);
3015   aTI.SetFace2V(theDirFace2V);
3016   aTI.SetNbIterV(theNbTimesV);
3017
3018   //Compute the transformation
3019   try {
3020 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
3021     OCC_CATCH_SIGNALS;
3022 #endif
3023     if (!GetSolver()->ComputeFunction(aFunction)) {
3024       SetErrorCode("Block driver failed to make multi-transformation");
3025       return NULL;
3026     }
3027   }
3028   catch (Standard_Failure) {
3029     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
3030     SetErrorCode(aFail->GetMessageString());
3031     return NULL;
3032   }
3033
3034   //Make a Python command
3035   GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeMultiTransformation2D("
3036     << theObject << ", " << theDirFace1U << ", " << theDirFace2U << ", " << theNbTimesU
3037       << ", " << theDirFace1V << ", " << theDirFace2V << ", " << theNbTimesV << ")";
3038
3039   SetErrorCode(OK);
3040   return aCopy;
3041 }
3042
3043 //=============================================================================
3044 /*!
3045  *  Propagate
3046  */
3047 //=============================================================================
3048 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IBlocksOperations::Propagate
3049                                                  (Handle(GEOM_Object) theShape)
3050 {
3051   SetErrorCode(KO);
3052
3053   if (theShape.IsNull()) return NULL;
3054
3055   TopoDS_Shape aShape = theShape->GetValue();
3056   if (aShape.IsNull()) return NULL;
3057
3058   TopTools_IndexedMapOfShape anIndices;
3059   TopExp::MapShapes(aShape, anIndices);
3060
3061   TopTools_IndexedDataMapOfShapeListOfShape MEW;
3062   GEOMImpl_Block6Explorer::MapShapesAndAncestors
3063     (aShape, TopAbs_EDGE, TopAbs_WIRE, MEW);
3064   Standard_Integer ie, nbEdges = MEW.Extent();
3065
3066   // Result
3067   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
3068
3069   TopTools_MapOfShape mapAcceptedEdges;
3070   TCollection_AsciiString aListRes, anEntry;
3071
3072   for (ie = 1; ie <= nbEdges; ie++) {
3073     TopoDS_Shape curE = MEW.FindKey(ie);
3074
3075     if (mapAcceptedEdges.Contains(curE)) continue;
3076
3077     // Build the chain
3078     TopTools_ListOfShape currentChain;
3079     TopTools_ListOfShape listPrevEdges;
3080
3081     currentChain.Append(curE);
3082     listPrevEdges.Append(curE);
3083     mapAcceptedEdges.Add(curE);
3084
3085     // Collect all edges pass by pass
3086     while (listPrevEdges.Extent() > 0) {
3087       // List of edges, added to chain on this cycle pass
3088       TopTools_ListOfShape listCurEdges;
3089
3090       // Find the next portion of edges
3091       TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
3092       for (; itE.More(); itE.Next()) {
3093         TopoDS_Shape anE = itE.Value();
3094
3095         // Iterate on faces, having edge <anE>
3096         TopTools_ListIteratorOfListOfShape itW (MEW.FindFromKey(anE));
3097         for (; itW.More(); itW.Next()) {
3098           TopoDS_Shape aW = itW.Value();
3099           TopoDS_Shape anOppE;
3100
3101           BRepTools_WireExplorer aWE (TopoDS::Wire(aW));
3102           Standard_Integer nb = 1, found = 0;
3103           TopTools_Array1OfShape anEdges (1,4);
3104           for (; aWE.More(); aWE.Next(), nb++) {
3105             if (nb > 4) {
3106               found = 0;
3107               break;
3108             }
3109             anEdges(nb) = aWE.Current();
3110             if (anEdges(nb).IsSame(anE)) found = nb;
3111           }
3112
3113           if (nb == 5 && found > 0) {
3114             // Quadrangle face found, get an opposite edge
3115             Standard_Integer opp = found + 2;
3116             if (opp > 4) opp -= 4;
3117             anOppE = anEdges(opp);
3118
3119             if (!mapAcceptedEdges.Contains(anOppE)) {
3120               // Add found edge to the chain
3121               currentChain.Append(anOppE);
3122               listCurEdges.Append(anOppE);
3123               mapAcceptedEdges.Add(anOppE);
3124             }
3125           } // if (nb == 5 && found > 0)
3126         } // for (; itF.More(); itF.Next())
3127       } // for (; itE.More(); itE.Next())
3128
3129       listPrevEdges = listCurEdges;
3130     } // while (listPrevEdges.Extent() > 0)
3131
3132     // Store the chain in the document
3133     Handle(TColStd_HArray1OfInteger) anArray =
3134       new TColStd_HArray1OfInteger (1, currentChain.Extent());
3135
3136     // Fill array of sub-shape indices
3137     TopTools_ListIteratorOfListOfShape itSub (currentChain);
3138     for (int index = 1; itSub.More(); itSub.Next(), ++index) {
3139       int id = anIndices.FindIndex(itSub.Value());
3140       anArray->SetValue(index, id);
3141     }
3142
3143     // Add a new group object
3144     Handle(GEOM_Object) aChain = GetEngine()->AddSubShape(theShape, anArray);
3145
3146     // Set a GROUP type
3147     aChain->SetType(GEOM_GROUP);
3148
3149     // Set a sub shape type
3150     TDF_Label aFreeLabel = aChain->GetFreeLabel();
3151     TDataStd_Integer::Set(aFreeLabel, (Standard_Integer)TopAbs_EDGE);
3152
3153     // Add the chain to the result
3154     aSeq->Append(aChain);
3155
3156     //Make a Python command
3157     TDF_Tool::Entry(aChain->GetEntry(), anEntry);
3158     aListRes += anEntry + ", ";
3159   }
3160
3161   if (aSeq->IsEmpty()) {
3162     SetErrorCode("There are no quadrangle faces in the shape");
3163     return aSeq;
3164   }
3165
3166   aListRes.Trunc(aListRes.Length() - 2);
3167
3168   // The Propagation doesn't change object so no new function is required.
3169   Handle(GEOM_Function) aFunction = theShape->GetLastFunction();
3170
3171   // Make a Python command
3172   GEOM::TPythonDump(aFunction, /*append=*/true)
3173     << "[" << aListRes.ToCString() << "] = geompy.Propagate(" << theShape << ")";
3174
3175   SetErrorCode(OK);
3176   return aSeq;
3177 }