Salome HOME
0022746: [EDF] Improvement of Glue Faces and Glue Edges operations
[modules/geom.git] / src / GEOMImpl / GEOMImpl_GlueDriver.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_GlueDriver.hxx>
26 #include <GEOMImpl_IGlue.hxx>
27 #include <GEOMImpl_Types.hxx>
28
29 #include <GEOM_Object.hxx>
30 #include <GEOM_Function.hxx>
31
32 #include "GEOMAlgo_Gluer2.hxx"
33 #include "GEOMAlgo_ListIteratorOfListOfCoupleOfShapes.hxx"
34 #include "GEOMAlgo_CoupleOfShapes.hxx"
35 #include "GEOMAlgo_ListOfCoupleOfShapes.hxx"
36
37 #include <Basics_OCCTVersion.hxx>
38
39 #include "utilities.h"
40
41 #include <BRep_Builder.hxx>
42 #include <ShapeFix_Shape.hxx>
43 #include <TDataStd_IntegerArray.hxx>
44 #include <TopExp.hxx>
45 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <TopTools_ListOfShape.hxx>
49 #include <TopoDS_Shape.hxx>
50
51 #include <Standard_NullObject.hxx>
52 #include <Standard_Failure.hxx>
53
54 #define MSG_BAD_TOLERANCE "Tolerance is too big"
55 #define MSG_BAD_ARG_SHAPE "Argument shape is not a compound of hexahedral solids"
56
57 //=======================================================================
58 //function : GEOMImpl_GlueDriver
59 //purpose  :
60 //=======================================================================
61 GEOMImpl_GlueDriver::GEOMImpl_GlueDriver()
62 {
63 }
64
65 //=======================================================================
66 //function : GetID
67 //purpose  :
68 //=======================================================================
69 const Standard_GUID& GEOMImpl_GlueDriver::GetID()
70 {
71   static Standard_GUID aGlueDriver("FF1BBB63-5D14-4df2-980B-3A668264EA16");
72   return aGlueDriver;
73 }
74
75 //=======================================================================
76 //function : GlueFacesWithWarnings
77 //purpose  :
78 //=======================================================================
79 /*
80 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesWithWarnings (const TopoDS_Shape& theShape,
81                                                          const Standard_Real theTolerance,
82                                                          const Standard_Boolean doKeepNonSolids,
83                                                          TCollection_AsciiString& theWarning) const
84 {
85   Standard_Integer iErr, iWrn;
86   TopoDS_Shape aRes;
87   GEOMAlgo_Gluer aGluer;
88
89   aGluer.SetShape(theShape);
90   aGluer.SetTolerance(theTolerance);
91   aGluer.SetCheckGeometry(Standard_True);
92   aGluer.SetKeepNonSolids(doKeepNonSolids);
93
94   aGluer.Perform();
95
96   iErr = aGluer.ErrorStatus();
97   if (iErr) {
98     switch (iErr) {
99     case 2:
100       Standard_Failure::Raise("No vertices found in source shape");
101       break;
102     case 3:
103     case 4:
104       Standard_Failure::Raise(MSG_BAD_TOLERANCE " or " MSG_BAD_ARG_SHAPE);
105       break;
106     case 5:
107       Standard_Failure::Raise("Source shape is Null");
108       break;
109     case 6:
110       Standard_Failure::Raise("Result shape is Null");
111       break;
112     case 100:
113       Standard_Failure::Raise(MSG_BAD_TOLERANCE);
114       break;
115     case 101:
116     case 102:
117       Standard_Failure::Raise(MSG_BAD_ARG_SHAPE);
118       break;
119     case 200:
120       Standard_Failure::Raise("Error occured during check of geometric coincidence");
121       break;
122     default:
123       {
124         // description of all errors see in GEOMAlgo_Gluer.cxx
125         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
126         aMsg += TCollection_AsciiString(iErr);
127         Standard_Failure::Raise(aMsg.ToCString());
128         break;
129       }
130     }
131     return aRes;
132   }
133
134   iWrn = aGluer.WarningStatus();
135   if (iWrn) {
136     switch (iWrn) {
137     case 1:
138       {
139         Standard_Integer nbAlone = aGluer.AloneShapes();
140         theWarning = TCollection_AsciiString(nbAlone);
141         theWarning += " solid(s) can not be glued by faces";
142       }
143       break;
144     default:
145       // description of all warnings see in GEOMAlgo_Gluer.cxx
146       theWarning = "Warning in GEOMAlgo_Gluer with code ";
147       theWarning += TCollection_AsciiString(iWrn);
148       break;
149     }
150   }
151
152   aRes = aGluer.Result();
153
154   // SKL 18.01.2010 - patch for 20662
155   Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aRes);
156   aSfs->SetPrecision(Precision::Confusion());
157   aSfs->Perform();
158   aRes = aSfs->Shape();
159
160   // Fill history to be used by GetInPlace functionality
161   TopTools_IndexedMapOfShape aResIndices;
162   TopExp::MapShapes(aRes, aResIndices);
163
164   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
165
166   // history for all argument shapes
167   TDF_LabelSequence aLabelSeq;
168   aFunction->GetDependency(aLabelSeq);
169   Standard_Integer nbArg = aLabelSeq.Length();
170
171   for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
172
173     TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
174
175     Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
176     TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
177
178     TopTools_IndexedMapOfShape anArgumentIndices;
179     TopExp::MapShapes(anArgumentShape, anArgumentIndices);
180     Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
181
182     // Find corresponding label in history
183     TDF_Label anArgumentHistoryLabel =
184       aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
185
186     for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
187       TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
188       const TopTools_ListOfShape& aModified = aGluer.Modified(anEntity);
189       Standard_Integer nbModified = aModified.Extent();
190
191       if (nbModified > 0) {
192         TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
193         Handle(TDataStd_IntegerArray) anAttr =
194           TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
195
196         TopTools_ListIteratorOfListOfShape itM (aModified);
197         for (int im = 1; itM.More(); itM.Next(), ++im) {
198           int id = aResIndices.FindIndex(itM.Value());
199           anAttr->SetValue(im, id);
200         }
201       }
202     }
203   }
204
205   return aRes;
206 }
207
208 //=======================================================================
209 //function : GlueFaces
210 //purpose  :
211 //=======================================================================
212 TopoDS_Shape GEOMImpl_GlueDriver::GlueFaces (const TopoDS_Shape& theShape,
213                                              const Standard_Real theTolerance,
214                                              const Standard_Boolean doKeepNonSolids)
215 {
216   TopoDS_Shape aRes;
217   GEOMAlgo_Gluer aGluer;
218
219   aGluer.SetShape(theShape);
220   aGluer.SetTolerance(theTolerance);
221   aGluer.SetCheckGeometry(Standard_True);
222   aGluer.SetKeepNonSolids(doKeepNonSolids);
223
224   aGluer.Perform();
225
226   Standard_Integer iErr = aGluer.ErrorStatus();
227   if (iErr) {
228     switch (iErr) {
229     case 2:
230       Standard_Failure::Raise("No vertices found in source shape");
231       break;
232     case 5:
233       Standard_Failure::Raise("Source shape is Null");
234       break;
235     case 6:
236       Standard_Failure::Raise("Result shape is Null");
237       break;
238     case 200:
239       Standard_Failure::Raise("Error occured during check of geometric coincidence");
240       break;
241     default:
242       {
243         // description of all errors see in GEOMAlgo_Gluer.cxx
244         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
245         aMsg += TCollection_AsciiString(iErr);
246         Standard_Failure::Raise(aMsg.ToCString());
247         break;
248       }
249     }
250     return aRes;
251   }
252
253   Standard_Integer iWrn = aGluer.WarningStatus();
254   if (iWrn) {
255     switch (iWrn) {
256     case 1:
257       MESSAGE("Some shapes can not be glued by faces");
258       break;
259     default:
260       // description of all warnings see in GEOMAlgo_Gluer.cxx
261       MESSAGE("Warning in GEOMAlgo_Gluer with code " << iWrn);
262       break;
263     }
264   }
265
266   aRes = aGluer.Result();
267
268   return aRes;
269 }
270
271 //=======================================================================
272 //function : GlueFacesByList
273 //purpose  :
274 //=======================================================================
275 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesByList (const TopoDS_Shape& theShape,
276                                                    const Standard_Real theTolerance,
277                                                    const Standard_Boolean doKeepNonSolids,
278                                                    const TopTools_MapOfShape& aFaces)
279 {
280   TopoDS_Shape aRes;
281
282   GEOMAlgo_Gluer1 aGluer;
283   GEOMAlgo_ListIteratorOfListOfCoupleOfShapes aItCS;
284   GEOMAlgo_CoupleOfShapes aCS;
285   GEOMAlgo_ListOfCoupleOfShapes aLCS;
286
287   aGluer.SetShape(theShape);
288   aGluer.SetTolerance(theTolerance);
289   aGluer.SetKeepNonSolids(doKeepNonSolids);
290   aGluer.Perform();
291   Standard_Integer iErr = aGluer.ErrorStatus();
292   if (iErr) return aRes;
293
294   TopTools_ListOfShape listShape;
295   const GEOMAlgo_ListOfCoupleOfShapes& aLCSG = aGluer.GluedFaces();
296   // Access to faces
297   aItCS.Initialize(aLCSG);
298   for (; aItCS.More(); aItCS.Next()) {
299     const GEOMAlgo_CoupleOfShapes& aCSG = aItCS.Value();
300     const TopoDS_Shape& aF1 = aCSG.Shape1();
301     const TopoDS_Shape& aF2 = aCSG.Shape2();
302     if (aFaces.Contains(aF1) || aFaces.Contains(aF2))
303       continue;
304     aCS.SetShapes(aF1,aF2);
305     aLCS.Append(aCS);
306   }
307
308   //cout<<"aLCS.Extent() = "<<aLCS.Extent()<<endl;
309   if (aLCS.Extent()>0) {
310     aGluer.SetFacesToUnglue(aLCS);
311     aGluer.UnglueFaces();
312     iErr = aGluer.ErrorStatus();
313     if (iErr) return aRes;
314   }
315
316   aRes = aGluer.Result();
317
318   return aRes;
319 }
320 */
321
322 //=======================================================================
323 //function : GlueFaces
324 //purpose  :
325 //=======================================================================
326 TopoDS_Shape GEOMImpl_GlueDriver::GlueFaces (const TopoDS_Shape& theShape,
327                                              const Standard_Real theTolerance,
328                                              const Standard_Boolean doKeepNonSolids)
329 {
330   TopoDS_Shape aRes;
331
332   GEOMAlgo_Gluer2 aGA;
333
334   // 1. Initialization
335   aGA.SetArgument(theShape);
336   aGA.SetTolerance(theTolerance);
337   aGA.SetKeepNonSolids(doKeepNonSolids);
338
339   // 2. Detect interferred shapes
340   aGA.Detect();
341
342   //Standard_Integer iWrnDetect = aGA.WarningStatus();
343   //if (iWrnDetect == 2) {
344   //  Standard_Failure::Raise("GLUE_ERROR_STICKED_SHAPES");
345   //}
346
347   Standard_Integer iErr = aGA.ErrorStatus();
348   if (iErr) {
349     switch (iErr) {
350     case 11:
351       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
352       break;
353     case 13:
354     case 14:
355       Standard_Failure::Raise("PerformImagesToWork failed");
356       break;
357     default:
358       {
359         // description of all errors see in GEOMAlgo_Gluer2.cxx
360         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
361         aMsg += TCollection_AsciiString(iErr);
362         Standard_Failure::Raise(aMsg.ToCString());
363         break;
364       }
365     }
366     return aRes;
367   }
368
369   // 3. Set shapes to glue. If the operator is absent, the whole gluing will be done
370   //aGA.SetShapesToGlue(aMSG);
371
372   // 4. Gluing
373   aGA.Perform();
374   iErr = aGA.ErrorStatus();
375   if (iErr) {
376     switch (iErr) {
377     case 11:
378       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
379       break;
380     case 13:
381     case 14:
382       Standard_Failure::Raise("PerformImagesToWork failed");
383       break;
384     default:
385       {
386         // description of all errors see in GEOMAlgo_Gluer2.cxx
387         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
388         aMsg += TCollection_AsciiString(iErr);
389         Standard_Failure::Raise(aMsg.ToCString());
390         break;
391       }
392     }
393     return aRes;
394   }
395
396   Standard_Integer iWrn = aGA.WarningStatus();
397   if (iWrn) {
398     switch (iWrn) {
399     case 1:
400       MESSAGE("No shapes to glue");
401       break;
402     default:
403       // description of all warnings see in GEOMAlgo_Gluer2.cxx
404       MESSAGE("Warning in GEOMAlgo_Gluer2 with code " << iWrn);
405       break;
406     }
407   }
408
409   // 5. Result
410   aRes = aGA.Shape();
411
412   return aRes;
413 }
414
415 //=======================================================================
416 //function : GlueWithWarnings
417 //purpose  :
418 //=======================================================================
419 TopoDS_Shape GEOMImpl_GlueDriver::GlueWithWarnings (const TopoDS_Shape& theShape,
420                                                     const Standard_Real theTolerance,
421                                                     const TopAbs_ShapeEnum theShapeType,
422                                                     const Standard_Boolean doKeepNonSolids,
423                                                     TCollection_AsciiString& theWarning) const
424 {
425   TopoDS_Shape aRes;
426
427   GEOMAlgo_Gluer2 aGA;
428
429   // 1. Initialization
430   aGA.SetArgument(theShape);
431   aGA.SetTolerance(theTolerance);
432   aGA.SetKeepNonSolids(doKeepNonSolids);
433
434   // 2. Detect interferred shapes
435   aGA.Detect();
436
437   //modified by NIZNHY-PKV Tue Mar 13 14:07:12 2012f
438   Standard_Integer iWrnDetect = aGA.WarningStatus();
439   if (iWrnDetect == 2) {
440     Standard_Failure::Raise("GLUE_ERROR_STICKED_SHAPES");
441   }
442   //modified by NIZNHY-PKV Tue Mar 13 14:07:14 2012t
443
444   Standard_Integer iErr = aGA.ErrorStatus();
445   if (iErr) {
446     switch (iErr) {
447     case 11:
448       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
449       break;
450     case 13:
451     case 14:
452       Standard_Failure::Raise("PerformImagesToWork failed");
453       break;
454     default:
455       {
456         // description of all errors see in GEOMAlgo_Gluer2.cxx
457         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
458         aMsg += TCollection_AsciiString(iErr);
459         Standard_Failure::Raise(aMsg.ToCString());
460         break;
461       }
462     }
463     return aRes;
464   }
465
466   if (theShapeType != TopAbs_FACE) {
467     // 3. Fill shapes to glue aMSG
468     TopTools_DataMapOfShapeListOfShape aMSG;
469     const TopTools_DataMapOfShapeListOfShape& aMSD = aGA.ShapesDetected();
470     TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItMSD;
471     aItMSD.Initialize(aMSD);
472     for (; aItMSD.More(); aItMSD.Next()) {
473       const TopoDS_Shape& aSx = aItMSD.Key();
474       const TopTools_ListOfShape& aLSD = aItMSD.Value();
475       if (aSx.ShapeType() == theShapeType) {
476         aMSG.Bind(aSx, aLSD);
477       }
478     }
479
480     // 4. Set shapes to glue. If the operator is absent, the whole gluing will be done
481     aGA.SetShapesToGlue(aMSG);
482   }
483
484   // 5. Gluing
485   aGA.Perform();
486   iErr = aGA.ErrorStatus();
487   if (iErr) {
488     switch (iErr) {
489     case 11:
490       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
491       break;
492     case 13:
493     case 14:
494       Standard_Failure::Raise("PerformImagesToWork failed");
495       break;
496     default:
497       {
498         // description of all errors see in GEOMAlgo_Gluer2.cxx
499         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
500         aMsg += TCollection_AsciiString(iErr);
501         Standard_Failure::Raise(aMsg.ToCString());
502         break;
503       }
504     }
505     return aRes;
506   }
507
508   Standard_Integer iWrn = aGA.WarningStatus();
509   if (iWrn) {
510     switch (iWrn) {
511     case 1:
512       theWarning = "No shapes to glue";
513       break;
514     default:
515       // description of all warnings see in GEOMAlgo_Gluer2.cxx
516       theWarning = "Warning in GEOMAlgo_Gluer2 with code ";
517       theWarning += TCollection_AsciiString(iWrn);
518       break;
519     }
520   }
521
522   // 6. Result
523   aRes = aGA.Shape();
524
525   // 7. Fill history to be used by GetInPlace functionality
526   TopTools_IndexedMapOfShape aResIndices;
527   TopExp::MapShapes(aRes, aResIndices);
528
529   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
530
531   // history for all argument shapes
532   TDF_LabelSequence aLabelSeq;
533   aFunction->GetDependency(aLabelSeq);
534   Standard_Integer nbArg = aLabelSeq.Length();
535
536   for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
537
538     TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
539
540     Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
541     TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
542
543     TopTools_IndexedMapOfShape anArgumentIndices;
544     TopExp::MapShapes(anArgumentShape, anArgumentIndices);
545     Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
546
547     // Find corresponding label in history
548     TDF_Label anArgumentHistoryLabel =
549       aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
550
551     for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
552       TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
553       const TopTools_ListOfShape& aModified = aGA.Modified(anEntity);
554       Standard_Integer nbModified = aModified.Extent();
555
556       if (nbModified > 0) {
557         TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
558         Handle(TDataStd_IntegerArray) anAttr =
559           TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
560
561         TopTools_ListIteratorOfListOfShape itM (aModified);
562         for (int im = 1; itM.More(); itM.Next(), ++im) {
563           int id = aResIndices.FindIndex(itM.Value());
564           anAttr->SetValue(im, id);
565         }
566       }
567     }
568   }
569
570   return aRes;
571 }
572
573 //=======================================================================
574 //function : GlueByList
575 //purpose  :
576 //=======================================================================
577 TopoDS_Shape GEOMImpl_GlueDriver::GlueByList (const TopoDS_Shape& theShape,
578                                               const Standard_Real theTolerance,
579                                               const Standard_Boolean doKeepNonSolids,
580                                               const TopTools_MapOfShape& theShapesList,
581                                               const Standard_Boolean doGlueAllEdges)
582 {
583   TopoDS_Shape aRes;
584
585   GEOMAlgo_Gluer2 aGA;
586
587   // 1. Initialization
588   aGA.SetArgument(theShape);
589   aGA.SetTolerance(theTolerance);
590   aGA.SetKeepNonSolids(doKeepNonSolids);
591
592   // 2. Detect interferred shapes
593   aGA.Detect();
594
595   //modified by NIZNHY-PKV Tue Mar 13 14:07:12 2012f
596   Standard_Integer iWrnDetect = aGA.WarningStatus();
597   if (iWrnDetect == 2) {
598     /*
599     TopTools_ListIteratorOfListOfShape aItLS;
600
601     // Sticked shapes are detected
602     const TopTools_IndexedDataMapOfShapeListOfShape& aIDMSS = pGluer2->StickedShapes();
603
604     Standard_Integer i, aNb = aIDMSS.Extent();
605     for (i = 1; i <= aNb; ++i) {
606       // ancestor aSa (edge, wire face,..)  
607       const TopoDS_Shape& aSa = aIDMSS.FindKey(i); 
608
609       // successors aSs (vertex, edge, ...)
610       // of the ancestor that are sticked 
611       // for given value of the tolerance
612       const TopTools_ListOfShape& aLSS = aIDMSS.FindFromIndex(i);
613       aItLS.Initialize(aLSS);
614       for (; aItLS.More(); aItLS.Next()) {
615         const TopoDS_Shape& aSs = aItLS.Value();
616       }
617     }
618     */
619     Standard_Failure::Raise("GLUE_ERROR_STICKED_SHAPES");
620   }
621   //modified by NIZNHY-PKV Tue Mar 13 14:07:14 2012t
622
623   Standard_Integer iErr = aGA.ErrorStatus();
624   if (iErr) {
625     switch (iErr) {
626     case 11:
627       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
628       break;
629     case 13:
630     case 14:
631       Standard_Failure::Raise("PerformImagesToWork failed");
632       break;
633     default:
634       {
635         // description of all errors see in GEOMAlgo_Gluer2.cxx
636         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
637         aMsg += TCollection_AsciiString(iErr);
638         Standard_Failure::Raise(aMsg.ToCString());
639         break;
640       }
641     }
642     return aRes;
643   }
644
645   // 3. Fill shapes to glue aMSG
646   TopTools_DataMapOfShapeListOfShape aMSG;
647   const TopTools_DataMapOfShapeListOfShape& aMSD = aGA.ShapesDetected();
648   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItMSD;
649   aItMSD.Initialize(aMSD);
650   for (; aItMSD.More(); aItMSD.Next()) {
651     const TopoDS_Shape& aSx = aItMSD.Key();
652     const TopTools_ListOfShape& aLSD = aItMSD.Value();
653     TopTools_ListIteratorOfListOfShape anItLSD (aLSD);
654     bool isToGlue = false;
655     if (doGlueAllEdges && aSx.ShapeType() == TopAbs_EDGE) {
656       isToGlue = true;
657     }
658     else {
659       for (; anItLSD.More() && !isToGlue; anItLSD.Next()) {
660         if (theShapesList.Contains(anItLSD.Value())) {
661           isToGlue = true;
662         }
663       }
664     }
665     if (isToGlue) {
666       aMSG.Bind(aSx, aLSD);
667     }
668   }
669
670   // 4. Set shapes to glue. If the operator is absent, the whole gluing will be done
671   aGA.SetShapesToGlue(aMSG);
672
673   // 5. Gluing
674   aGA.Perform();
675   iErr = aGA.ErrorStatus();
676   if (iErr) {
677     switch (iErr) {
678     case 11:
679       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
680       break;
681     case 13:
682     case 14:
683       Standard_Failure::Raise("PerformImagesToWork failed");
684       break;
685     default:
686       {
687         // description of all errors see in GEOMAlgo_Gluer2.cxx
688         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
689         aMsg += TCollection_AsciiString(iErr);
690         Standard_Failure::Raise(aMsg.ToCString());
691         break;
692       }
693     }
694     return aRes;
695   }
696
697   Standard_Integer iWrn = aGA.WarningStatus();
698   if (iWrn) {
699     switch (iWrn) {
700     case 1:
701       MESSAGE("No shapes to glue");
702       break;
703     default:
704       // description of all warnings see in GEOMAlgo_Gluer2.cxx
705       MESSAGE("Warning in GEOMAlgo_Gluer2 with code " << iWrn);
706       break;
707     }
708   }
709
710   // 6. Result
711   aRes = aGA.Shape();
712
713   return aRes;
714 }
715
716 //=======================================================================
717 //function : Execute
718 //purpose  :
719 //=======================================================================
720 Standard_Integer GEOMImpl_GlueDriver::Execute(TFunction_Logbook& log) const
721 {
722   if (Label().IsNull()) return 0;
723   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
724
725   GEOMImpl_IGlue aCI (aFunction);
726   Standard_Integer aType = aFunction->GetType();
727
728   TopoDS_Shape aShape;
729   TCollection_AsciiString aWrn;
730
731   TopoDS_Shape aShapeBase;
732   Handle(TColStd_HSequenceOfTransient) aRefBases = aCI.GetBase();
733   if ( aRefBases && !aRefBases->IsEmpty() )
734   {
735     if ( aRefBases->Length() == 1 )
736     {
737       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRefBases->Value(1));
738       if ( !aRefShape.IsNull() )
739         aShapeBase = aRefShape->GetValue();
740     }
741     else
742     {
743       TopoDS_Compound compound;
744       BRep_Builder builder;
745       builder.MakeCompound( compound );
746       int ind = 1;
747       for ( ; ind <= aRefBases->Length(); ind++)
748       {
749         Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRefBases->Value(ind));
750         if ( aRefShape.IsNull() ) break;
751         TopoDS_Shape aShape = aRefShape->GetValue();
752         if (aShape.IsNull()) break;
753         builder.Add( compound, aShape );
754       }
755       if ( ind > aRefBases->Length() )
756         aShapeBase = compound;
757       else
758         aShapeBase.Nullify();
759     }
760   }
761   if (aShapeBase.IsNull()) {
762     Standard_NullObject::Raise("Shape for gluing is null");
763   }
764
765   Standard_Real tol3d = aCI.GetTolerance();
766
767   Standard_Boolean aKeepNonSolids = aCI.GetKeepNonSolids();
768
769   if (aType == GLUE_FACES) {
770     //aShape = GlueFacesWithWarnings(aShapeBase, tol3d, aKeepNonSolids, aWrn);
771     aShape = GlueWithWarnings(aShapeBase, tol3d, TopAbs_FACE, aKeepNonSolids, aWrn);
772   }
773   else if (aType == GLUE_EDGES) {
774     aShape = GlueWithWarnings(aShapeBase, tol3d, TopAbs_EDGE, aKeepNonSolids, aWrn);
775   }
776   else if (aType == GLUE_FACES_BY_LIST || aType == GLUE_EDGES_BY_LIST) {
777     Handle(TColStd_HSequenceOfTransient) SF = aCI.GetFaces();
778     TopTools_MapOfShape aFaces;
779     int i = 1;
780     for (; i <= SF->Length(); i++) {
781       Handle(Standard_Transient) anItem = SF->Value(i);
782       if (anItem.IsNull())
783         continue;
784       Handle(GEOM_Function) aRefSh = Handle(GEOM_Function)::DownCast(anItem);
785       if (aRefSh.IsNull())
786         continue;
787       TopoDS_Shape aFace = aRefSh->GetValue();
788       if (aFace.IsNull())
789         continue;
790       aFaces.Add(aFace);
791     }
792
793     Standard_Boolean aGlueAllEdges = Standard_False;
794     if (aType == GLUE_FACES_BY_LIST)
795       aGlueAllEdges = aCI.GetGlueAllEdges();
796
797     //aShape = GlueFacesByList(aShapeBase, tol3d, aKeepNonSolids, aFaces);
798     aShape = GlueByList(aShapeBase, tol3d, aKeepNonSolids, aFaces, aGlueAllEdges);
799   }
800
801   if (aShape.IsNull()) return 0;
802
803   aFunction->SetValue(aShape);
804
805   log.SetTouched(Label());
806
807   if (!aWrn.IsEmpty()) {
808     Standard_Failure::Raise(aWrn.ToCString());
809   }
810
811   return 1;
812 }
813
814 //================================================================================
815 /*!
816  * \brief Returns a name of creation operation and names and values of creation parameters
817  */
818 //================================================================================
819
820 bool GEOMImpl_GlueDriver::
821 GetCreationInformation(std::string&             theOperationName,
822                        std::vector<GEOM_Param>& theParams)
823 {
824   if (Label().IsNull()) return 0;
825   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
826
827   GEOMImpl_IGlue aCI( function );
828   Standard_Integer aType = function->GetType();
829
830   switch ( aType ) {
831   case GLUE_FACES:
832     theOperationName = "GLUE_FACES";
833     AddParam( theParams, "Selected shapes", aCI.GetBase() );
834     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
835     AddParam( theParams, "To keep non solids", aCI.GetKeepNonSolids() );
836     break;
837   case GLUE_EDGES:
838     theOperationName = "GLUE_EDGES";
839     AddParam( theParams, "Selected shapes", aCI.GetBase() );
840     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
841     break;
842   case GLUE_FACES_BY_LIST:
843     theOperationName = "GLUE_FACES";
844     AddParam( theParams, "Selected shapes", aCI.GetBase() );
845     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
846     AddParam( theParams, "Faces", aCI.GetFaces() );
847     AddParam( theParams, "To keep non solids", aCI.GetKeepNonSolids() );
848     AddParam( theParams, "To glue all edges", aCI.GetGlueAllEdges() );
849     break;
850   case GLUE_EDGES_BY_LIST:
851     theOperationName = "GLUE_EDGES";
852     AddParam( theParams, "Selected shapes", aCI.GetBase() );
853     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
854     AddParam( theParams, "Edges", aCI.GetFaces() );
855     break;
856   default:
857     return false;
858   }
859   
860   return true;
861 }
862
863 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_GlueDriver,GEOM_BaseDriver);
864 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_GlueDriver,GEOM_BaseDriver);