]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_GlueDriver.cxx
Salome HOME
Mantis issue 0021532: EDF 2202 GEOM: Problem when restoring groups containing a singl...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_GlueDriver.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_GlueDriver.hxx>
25 #include <GEOMImpl_IGlue.hxx>
26 #include <GEOMImpl_Types.hxx>
27
28 #include <GEOM_Object.hxx>
29 #include <GEOM_Function.hxx>
30
31 #include "GEOMAlgo_Gluer2.hxx"
32 #include "GEOMAlgo_ListIteratorOfListOfCoupleOfShapes.hxx"
33 #include "GEOMAlgo_CoupleOfShapes.hxx"
34 #include "GEOMAlgo_ListOfCoupleOfShapes.hxx"
35
36 #include <Basics_OCCTVersion.hxx>
37
38 #include "utilities.h"
39
40 #include <TDataStd_IntegerArray.hxx>
41
42 #include <TopExp.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46 #include <TopTools_ListOfShape.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
48
49 #include <ShapeFix_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   Standard_Integer iErr = aGA.ErrorStatus();
342   if (iErr) {
343     switch (iErr) {
344     case 11:
345       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
346       break;
347     case 13:
348     case 14:
349       Standard_Failure::Raise("PerformImagesToWork failed");
350       break;
351     default:
352       {
353         // description of all errors see in GEOMAlgo_Gluer2.cxx
354         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
355         aMsg += TCollection_AsciiString(iErr);
356         Standard_Failure::Raise(aMsg.ToCString());
357         break;
358       }
359     }
360     return aRes;
361   }
362
363   // 3. Set shapes to glue. If the operator is absent, the whole gluing will be done
364   //aGA.SetShapesToGlue(aMSG);
365
366   // 4. Gluing
367   aGA.Perform();
368   iErr = aGA.ErrorStatus();
369   if (iErr) {
370     switch (iErr) {
371     case 11:
372       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
373       break;
374     case 13:
375     case 14:
376       Standard_Failure::Raise("PerformImagesToWork failed");
377       break;
378     default:
379       {
380         // description of all errors see in GEOMAlgo_Gluer2.cxx
381         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
382         aMsg += TCollection_AsciiString(iErr);
383         Standard_Failure::Raise(aMsg.ToCString());
384         break;
385       }
386     }
387     return aRes;
388   }
389
390   Standard_Integer iWrn = aGA.WarningStatus();
391   if (iWrn) {
392     switch (iWrn) {
393     case 1:
394       MESSAGE("No shapes to glue");
395       break;
396     default:
397       // description of all warnings see in GEOMAlgo_Gluer2.cxx
398       MESSAGE("Warning in GEOMAlgo_Gluer2 with code " << iWrn);
399       break;
400     }
401   }
402
403   // 5. Result
404   aRes = aGA.Shape();
405
406   return aRes;
407 }
408
409 //=======================================================================
410 //function : GlueWithWarnings
411 //purpose  :
412 //=======================================================================
413 TopoDS_Shape GEOMImpl_GlueDriver::GlueWithWarnings (const TopoDS_Shape& theShape,
414                                                     const Standard_Real theTolerance,
415                                                     const TopAbs_ShapeEnum theShapeType,
416                                                     const Standard_Boolean doKeepNonSolids,
417                                                     TCollection_AsciiString& theWarning) const
418 {
419   TopoDS_Shape aRes;
420
421   GEOMAlgo_Gluer2 aGA;
422
423   // 1. Initialization
424   aGA.SetArgument(theShape);
425   aGA.SetTolerance(theTolerance);
426   aGA.SetKeepNonSolids(doKeepNonSolids);
427
428   // 2. Detect interferred shapes
429   aGA.Detect();
430
431   //modified by NIZNHY-PKV Tue Mar 13 14:07:12 2012f
432 #if OCC_VERSION_LARGE > 0x06050200
433   Standard_Integer iWrnDetect = aGA.WarningStatus();
434   if (iWrnDetect == 2) {
435     /*
436     TopTools_ListIteratorOfListOfShape aItLS;
437
438     // Sticked shapes are detected
439     const TopTools_IndexedDataMapOfShapeListOfShape& aIDMSS = pGluer2->StickedShapes();
440
441     Standard_Integer i, aNb = aIDMSS.Extent();
442     for (i = 1; i <= aNb; ++i) {
443       // ancestor aSa (edge, wire face,..)  
444       const TopoDS_Shape& aSa = aIDMSS.FindKey(i); 
445
446       // successors aSs (vertex, edge, ...)
447       // of the ancestor that are sticked 
448       // for given value of the tolerance
449       const TopTools_ListOfShape& aLSS = aIDMSS.FindFromIndex(i);
450       aItLS.Initialize(aLSS);
451       for (; aItLS.More(); aItLS.Next()) {
452         const TopoDS_Shape& aSs = aItLS.Value();
453       }
454     }
455     */
456     Standard_Failure::Raise("GLUE_ERROR_STICKED_SHAPES");
457     //Standard_Failure::Raise("Sticked shapes are detected. The tolerance value is too big.");
458     //theWarning = "Sticked shapes are detected. The tolerance value is too big.";
459   }
460 #endif
461   //modified by NIZNHY-PKV Tue Mar 13 14:07:14 2012t
462
463   Standard_Integer iErr = aGA.ErrorStatus();
464   if (iErr) {
465     switch (iErr) {
466     case 11:
467       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
468       break;
469     case 13:
470     case 14:
471       Standard_Failure::Raise("PerformImagesToWork failed");
472       break;
473     default:
474       {
475         // description of all errors see in GEOMAlgo_Gluer2.cxx
476         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
477         aMsg += TCollection_AsciiString(iErr);
478         Standard_Failure::Raise(aMsg.ToCString());
479         break;
480       }
481     }
482     return aRes;
483   }
484
485   if (theShapeType != TopAbs_FACE) {
486     // 3. Fill shapes to glue aMSG
487     TopTools_DataMapOfShapeListOfShape aMSG;
488     const TopTools_DataMapOfShapeListOfShape& aMSD = aGA.ShapesDetected();
489     TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItMSD;
490     aItMSD.Initialize(aMSD);
491     for (; aItMSD.More(); aItMSD.Next()) {
492       const TopoDS_Shape& aSx = aItMSD.Key();
493       const TopTools_ListOfShape& aLSD = aItMSD.Value();
494       if (aSx.ShapeType() == theShapeType) {
495         aMSG.Bind(aSx, aLSD);
496       }
497     }
498
499     // 4. Set shapes to glue. If the operator is absent, the whole gluing will be done
500     aGA.SetShapesToGlue(aMSG);
501   }
502
503   // 5. Gluing
504   aGA.Perform();
505   iErr = aGA.ErrorStatus();
506   if (iErr) {
507     switch (iErr) {
508     case 11:
509       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
510       break;
511     case 13:
512     case 14:
513       Standard_Failure::Raise("PerformImagesToWork failed");
514       break;
515     default:
516       {
517         // description of all errors see in GEOMAlgo_Gluer2.cxx
518         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
519         aMsg += TCollection_AsciiString(iErr);
520         Standard_Failure::Raise(aMsg.ToCString());
521         break;
522       }
523     }
524     return aRes;
525   }
526
527   Standard_Integer iWrn = aGA.WarningStatus();
528   if (iWrn) {
529     switch (iWrn) {
530     case 1:
531       theWarning = "No shapes to glue";
532       break;
533     default:
534       // description of all warnings see in GEOMAlgo_Gluer2.cxx
535       theWarning = "Warning in GEOMAlgo_Gluer2 with code ";
536       theWarning += TCollection_AsciiString(iWrn);
537       break;
538     }
539   }
540
541   // 6. Result
542   aRes = aGA.Shape();
543
544   // 7. Fill history to be used by GetInPlace functionality
545   TopTools_IndexedMapOfShape aResIndices;
546   TopExp::MapShapes(aRes, aResIndices);
547
548   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
549
550   // history for all argument shapes
551   TDF_LabelSequence aLabelSeq;
552   aFunction->GetDependency(aLabelSeq);
553   Standard_Integer nbArg = aLabelSeq.Length();
554
555   for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
556
557     TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
558
559     Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
560     TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
561
562     TopTools_IndexedMapOfShape anArgumentIndices;
563     TopExp::MapShapes(anArgumentShape, anArgumentIndices);
564     Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
565
566     // Find corresponding label in history
567     TDF_Label anArgumentHistoryLabel =
568       aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
569
570     for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
571       TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
572       const TopTools_ListOfShape& aModified = aGA.Modified(anEntity);
573       Standard_Integer nbModified = aModified.Extent();
574
575       if (nbModified > 0) {
576         TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
577         Handle(TDataStd_IntegerArray) anAttr =
578           TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
579
580         TopTools_ListIteratorOfListOfShape itM (aModified);
581         for (int im = 1; itM.More(); itM.Next(), ++im) {
582           int id = aResIndices.FindIndex(itM.Value());
583           anAttr->SetValue(im, id);
584         }
585       }
586     }
587   }
588
589   return aRes;
590 }
591
592 //=======================================================================
593 //function : GlueByList
594 //purpose  :
595 //=======================================================================
596 TopoDS_Shape GEOMImpl_GlueDriver::GlueByList (const TopoDS_Shape& theShape,
597                                               const Standard_Real theTolerance,
598                                               const Standard_Boolean doKeepNonSolids,
599                                               const TopTools_MapOfShape& theShapesList,
600                                               const Standard_Boolean doGlueAllEdges)
601 {
602   TopoDS_Shape aRes;
603
604   GEOMAlgo_Gluer2 aGA;
605
606   // 1. Initialization
607   aGA.SetArgument(theShape);
608   aGA.SetTolerance(theTolerance);
609   aGA.SetKeepNonSolids(doKeepNonSolids);
610
611   // 2. Detect interferred shapes
612   aGA.Detect();
613   Standard_Integer iErr = aGA.ErrorStatus();
614   if (iErr) {
615     switch (iErr) {
616     case 11:
617       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
618       break;
619     case 13:
620     case 14:
621       Standard_Failure::Raise("PerformImagesToWork failed");
622       break;
623     default:
624       {
625         // description of all errors see in GEOMAlgo_Gluer2.cxx
626         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
627         aMsg += TCollection_AsciiString(iErr);
628         Standard_Failure::Raise(aMsg.ToCString());
629         break;
630       }
631     }
632     return aRes;
633   }
634
635   // 3. Fill shapes to glue aMSG
636   TopTools_DataMapOfShapeListOfShape aMSG;
637   const TopTools_DataMapOfShapeListOfShape& aMSD = aGA.ShapesDetected();
638   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItMSD;
639   aItMSD.Initialize(aMSD);
640   for (; aItMSD.More(); aItMSD.Next()) {
641     const TopoDS_Shape& aSx = aItMSD.Key();
642     const TopTools_ListOfShape& aLSD = aItMSD.Value();
643     TopTools_ListIteratorOfListOfShape anItLSD (aLSD);
644     bool isToGlue = false;
645     if (doGlueAllEdges && aSx.ShapeType() == TopAbs_EDGE) {
646       isToGlue = true;
647     }
648     else {
649       for (; anItLSD.More() && !isToGlue; anItLSD.Next()) {
650         if (theShapesList.Contains(anItLSD.Value())) {
651           isToGlue = true;
652         }
653       }
654     }
655     if (isToGlue) {
656       aMSG.Bind(aSx, aLSD);
657     }
658   }
659
660   // 4. Set shapes to glue. If the operator is absent, the whole gluing will be done
661   aGA.SetShapesToGlue(aMSG);
662
663   // 5. Gluing
664   aGA.Perform();
665   iErr = aGA.ErrorStatus();
666   if (iErr) {
667     switch (iErr) {
668     case 11:
669       Standard_Failure::Raise("GEOMAlgo_GlueDetector failed");
670       break;
671     case 13:
672     case 14:
673       Standard_Failure::Raise("PerformImagesToWork failed");
674       break;
675     default:
676       {
677         // description of all errors see in GEOMAlgo_Gluer2.cxx
678         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer2 with code ");
679         aMsg += TCollection_AsciiString(iErr);
680         Standard_Failure::Raise(aMsg.ToCString());
681         break;
682       }
683     }
684     return aRes;
685   }
686
687   Standard_Integer iWrn = aGA.WarningStatus();
688   if (iWrn) {
689     switch (iWrn) {
690     case 1:
691       MESSAGE("No shapes to glue");
692       break;
693     default:
694       // description of all warnings see in GEOMAlgo_Gluer2.cxx
695       MESSAGE("Warning in GEOMAlgo_Gluer2 with code " << iWrn);
696       break;
697     }
698   }
699
700   // 6. Result
701   aRes = aGA.Shape();
702
703   return aRes;
704 }
705
706 //=======================================================================
707 //function : Execute
708 //purpose  :
709 //=======================================================================
710 Standard_Integer GEOMImpl_GlueDriver::Execute(TFunction_Logbook& log) const
711 {
712   if (Label().IsNull()) return 0;
713   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
714
715   GEOMImpl_IGlue aCI (aFunction);
716   Standard_Integer aType = aFunction->GetType();
717
718   TopoDS_Shape aShape;
719   TCollection_AsciiString aWrn;
720
721   Handle(GEOM_Function) aRefBase = aCI.GetBase();
722   TopoDS_Shape aShapeBase = aRefBase->GetValue();
723   if (aShapeBase.IsNull()) {
724     Standard_NullObject::Raise("Shape for gluing is null");
725   }
726
727   Standard_Real tol3d = aCI.GetTolerance();
728
729   Standard_Boolean aKeepNonSolids = aCI.GetKeepNonSolids();
730
731   if (aType == GLUE_FACES) {
732     //aShape = GlueFacesWithWarnings(aShapeBase, tol3d, aKeepNonSolids, aWrn);
733     aShape = GlueWithWarnings(aShapeBase, tol3d, TopAbs_FACE, aKeepNonSolids, aWrn);
734   }
735   else if (aType == GLUE_EDGES) {
736     aShape = GlueWithWarnings(aShapeBase, tol3d, TopAbs_EDGE, aKeepNonSolids, aWrn);
737   }
738   else if (aType == GLUE_FACES_BY_LIST || aType == GLUE_EDGES_BY_LIST) {
739     Handle(TColStd_HSequenceOfTransient) SF = aCI.GetFaces();
740     TopTools_MapOfShape aFaces;
741     int i = 1;
742     for (; i <= SF->Length(); i++) {
743       Handle(Standard_Transient) anItem = SF->Value(i);
744       if (anItem.IsNull())
745         continue;
746       Handle(GEOM_Function) aRefSh = Handle(GEOM_Function)::DownCast(anItem);
747       if (aRefSh.IsNull())
748         continue;
749       TopoDS_Shape aFace = aRefSh->GetValue();
750       if (aFace.IsNull())
751         continue;
752       if (!aFaces.Contains(aFace))
753         aFaces.Add(aFace);
754     }
755
756     Standard_Boolean aGlueAllEdges = Standard_False;
757     if (aType == GLUE_FACES_BY_LIST)
758       aGlueAllEdges = aCI.GetGlueAllEdges();
759
760     //aShape = GlueFacesByList(aShapeBase, tol3d, aKeepNonSolids, aFaces);
761     aShape = GlueByList(aShapeBase, tol3d, aKeepNonSolids, aFaces, aGlueAllEdges);
762   }
763
764   if (aShape.IsNull()) return 0;
765
766   aFunction->SetValue(aShape);
767
768   log.SetTouched(Label());
769
770   if (!aWrn.IsEmpty()) {
771     Standard_Failure::Raise(aWrn.ToCString());
772   }
773
774   return 1;
775 }
776
777 //=======================================================================
778 //function :  GEOMImpl_GlueDriver_Type_
779 //purpose  :
780 //=======================================================================
781 Standard_EXPORT Handle_Standard_Type& GEOMImpl_GlueDriver_Type_()
782 {
783
784   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
785   if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
786   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
787   if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
788   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
789   if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
790
791
792   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
793   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_GlueDriver",
794                                                          sizeof(GEOMImpl_GlueDriver),
795                                                          1,
796                                                          (Standard_Address)_Ancestors,
797                                                          (Standard_Address)NULL);
798
799   return _aType;
800 }
801
802 //=======================================================================
803 //function : DownCast
804 //purpose  :
805 //=======================================================================
806 const Handle(GEOMImpl_GlueDriver) Handle(GEOMImpl_GlueDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
807 {
808   Handle(GEOMImpl_GlueDriver) _anOtherObject;
809
810   if (!AnObject.IsNull()) {
811      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_GlueDriver))) {
812        _anOtherObject = Handle(GEOMImpl_GlueDriver)((Handle(GEOMImpl_GlueDriver)&)AnObject);
813      }
814   }
815
816   return _anOtherObject;
817 }