Salome HOME
Update copyrights
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Gluer.cxx
1 // Copyright (C) 2007-2019  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 // File: GEOMAlgo_Gluer.cxx
24 // Created: Sat Dec 04 12:45:53 2004
25 // Author: Peter KURNEV
26 //  <peter@PREFEX>
27 //
28 #include <GEOMAlgo_Gluer.hxx>
29
30 #include <Basics_OCCTVersion.hxx>
31
32 #include <NCollection_UBTreeFiller.hxx>
33
34 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
35 #include <TColStd_MapOfInteger.hxx>
36 #include <TColStd_ListOfInteger.hxx>
37 #include <TColStd_ListIteratorOfListOfInteger.hxx>
38
39 #include <gp_Pnt.hxx>
40 #include <gp_Dir.hxx>
41 #include <gp_XYZ.hxx>
42
43 #include <Geom_Curve.hxx>
44 #include <Geom_Surface.hxx>
45
46 #include <Bnd_Box.hxx>
47 #include <Bnd_HArray1OfBox.hxx>
48 #include <Bnd_BoundSortBox.hxx>
49
50 #include <TopLoc_Location.hxx>
51 #include <TopAbs_ShapeEnum.hxx>
52 #include <TopAbs_Orientation.hxx>
53
54 #include <TopoDS.hxx>
55 #include <TopoDS_Edge.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <TopoDS_Shape.hxx>
58 #include <TopoDS_Compound.hxx>
59 #include <TopoDS_Wire.hxx>
60 #include <TopoDS_Shell.hxx>
61 #include <TopoDS_Solid.hxx>
62 #include <TopoDS_Iterator.hxx>
63
64 #include <TopTools_IndexedMapOfShape.hxx>
65 #include <TopTools_ListOfShape.hxx>
66 #include <TopTools_ListIteratorOfListOfShape.hxx>
67 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
68 #include <TopTools_MapOfShape.hxx>
69 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
70
71 #include <TopExp.hxx>
72 #include <TopExp_Explorer.hxx>
73
74 #include <BRep_Tool.hxx>
75 #include <BRep_Builder.hxx>
76 #include <BRepLib.hxx>
77 #include <BRepTools.hxx>
78 #include <BRepBndLib.hxx>
79 //
80 #include <IntTools_Tools.hxx>
81 #include <IntTools_Context.hxx>
82 #include <BOPTools_AlgoTools.hxx>
83 #include <BOPTools_AlgoTools3D.hxx>
84 #include <BOPTools_AlgoTools2D.hxx>
85
86 #include <GEOMAlgo_BoxBndTree.hxx>
87 #include <GEOMAlgo_AlgoTools.hxx>
88 #include <GEOMAlgo_IndexedDataMapOfIntegerShape.hxx>
89 #include <GEOMAlgo_IndexedDataMapOfShapeBox.hxx>
90 #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
91 #include <GEOMAlgo_PassKeyShape.hxx>
92 #include <GEOMAlgo_BndSphereTree.hxx>
93 #include <GEOMAlgo_BndSphere.hxx>
94 #include <GEOMAlgo_IndexedDataMapOfShapeBndSphere.hxx>
95
96 //
97 static
98   void GetSubShapes(const TopoDS_Shape& aS,
99       TopTools_IndexedMapOfShape& aMSS);
100
101 //=======================================================================
102 //function : GEOMAlgo_Gluer
103 //purpose  :
104 //=======================================================================
105 GEOMAlgo_Gluer::GEOMAlgo_Gluer()
106 :
107   GEOMAlgo_ShapeAlgo()
108 {
109   myTolerance=0.0001;
110   myTol=myTolerance;
111   myCheckGeometry=Standard_True;
112   myKeepNonSolids=Standard_False;
113   myNbAlone=0;
114 }
115 //=======================================================================
116 //function : ~GEOMAlgo_Gluer
117 //purpose  :
118 //=======================================================================
119 GEOMAlgo_Gluer::~GEOMAlgo_Gluer()
120 {
121 }
122 //=======================================================================
123 //function : SetCheckGeometry
124 //purpose  :
125 //=======================================================================
126 void GEOMAlgo_Gluer::SetCheckGeometry(const Standard_Boolean aFlag)
127 {
128   myCheckGeometry=aFlag;
129 }
130 //=======================================================================
131 //function : CheckGeometry
132 //purpose  :
133 //=======================================================================
134 Standard_Boolean GEOMAlgo_Gluer::CheckGeometry() const
135 {
136   return myCheckGeometry;
137 }
138 //=======================================================================
139 //function : SetKeepNonSolids
140 //purpose  :
141 //=======================================================================
142 void GEOMAlgo_Gluer::SetKeepNonSolids(const Standard_Boolean aFlag)
143 {
144   myKeepNonSolids=aFlag;
145 }
146 //=======================================================================
147 //function : KeepNonSolids
148 //purpose  :
149 //=======================================================================
150 Standard_Boolean GEOMAlgo_Gluer::KeepNonSolids()const
151 {
152   return myKeepNonSolids;
153 }
154 //=======================================================================
155 //function : AloneShapes
156 //purpose  :
157 //=======================================================================
158 Standard_Integer GEOMAlgo_Gluer::AloneShapes()const
159 {
160   return myNbAlone;
161 }
162 //=======================================================================
163 //function : Images
164 //purpose  :
165 //=======================================================================
166 const TopTools_DataMapOfShapeListOfShape& GEOMAlgo_Gluer::Images()const
167 {
168   return myImages;
169 }
170 //=======================================================================
171 //function : Origins
172 //purpose  :
173 //=======================================================================
174 const TopTools_DataMapOfShapeShape& GEOMAlgo_Gluer::Origins()const
175 {
176   return myOrigins;
177 }
178 //=======================================================================
179 //function : Perform
180 //purpose  :
181 //=======================================================================
182 void GEOMAlgo_Gluer::Perform()
183 {
184   const Standard_Integer aNb=8;
185   Standard_Integer i;
186   //
187   myErrorStatus=0;
188   myWarningStatus=0;
189   //
190   // Initialize the context
191   GEOMAlgo_ShapeAlgo::Perform();
192   //
193   void (GEOMAlgo_Gluer::* pF[aNb])()={
194     &GEOMAlgo_Gluer::CheckData,       &GEOMAlgo_Gluer::InnerTolerance,
195     &GEOMAlgo_Gluer::MakeVertices,    &GEOMAlgo_Gluer::MakeEdges,
196     &GEOMAlgo_Gluer::MakeFaces,       &GEOMAlgo_Gluer::MakeShells,
197     &GEOMAlgo_Gluer::MakeSolids,      &GEOMAlgo_Gluer::CheckResult
198   };
199   //
200   for (i=0; i<aNb; ++i) {
201     (this->*pF[i])();
202     if (myErrorStatus) {
203       return;
204     }
205   }
206 }
207
208 //=======================================================================
209 //function : MakeVertices
210 //purpose  :
211 //=======================================================================
212 void GEOMAlgo_Gluer::MakeVertices()
213 {
214   myErrorStatus=0;
215   //
216   Standard_Integer j, i, aNbV, aNbVSD;
217   Standard_Real aTolV;
218   gp_Pnt aPV;
219   TColStd_ListIteratorOfListOfInteger aIt;
220   TopoDS_Shape aVF;
221   TopoDS_Vertex aVnew;
222   TopTools_IndexedMapOfShape aMV, aMVProcessed;
223   TopTools_ListIteratorOfListOfShape aItS;
224   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
225   TopTools_DataMapOfShapeListOfShape aMVV;
226   GEOMAlgo_IndexedDataMapOfIntegerShape aMIS;
227   GEOMAlgo_IndexedDataMapOfShapeBndSphere aMSB;
228   //
229   GEOMAlgo_BndSphereTreeSelector aSelector;
230   GEOMAlgo_BndSphereTree aBBTree;
231   NCollection_UBTreeFiller <Standard_Integer, GEOMAlgo_BndSphere> aTreeFiller(aBBTree);
232   //
233   TopExp::MapShapes(myShape, TopAbs_VERTEX, aMV);
234   aNbV=aMV.Extent();
235   if (!aNbV) {
236     myErrorStatus=2; // no vertices in source shape
237     return;
238   }
239   //
240   for (i=1; i<=aNbV; ++i) {
241     GEOMAlgo_BndSphere aBox;
242     //
243     const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aMV(i));
244     aPV=BRep_Tool::Pnt(aV);
245     aTolV=BRep_Tool::Tolerance(aV);
246     //
247     aBox.SetGap(myTol);
248     aBox.SetCenter(aPV);
249     aBox.SetRadius(aTolV);
250     //
251     aTreeFiller.Add(i, aBox);
252     //
253     aMIS.Add(i, aV);
254     aMSB.Add(aV, aBox);
255   }
256   //
257   aTreeFiller.Fill();
258   //
259   //------------------------------
260   // Chains
261   for (i=1; i<=aNbV; ++i) {
262     const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aMV(i));
263     //
264     if (aMVProcessed.Contains(aV)) {
265       continue;
266     }
267     //
268     Standard_Integer aNbIP, aIP, aNbIP1, aIP1;
269     TopTools_ListOfShape aLVSD;
270     TColStd_MapOfInteger aMIP, aMIP1, aMIPC;
271     TColStd_MapIteratorOfMapOfInteger aIt1;
272     //
273     aMIP.Add(i);
274     for(;;) {
275       aNbIP=aMIP.Extent();
276       aIt1.Initialize(aMIP);
277       for(; aIt1.More(); aIt1.Next()) {
278         aIP=aIt1.Key();
279         if (aMIPC.Contains(aIP)) {
280           continue;
281         }
282         //
283         const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
284         const GEOMAlgo_BndSphere& aBoxVP=aMSB.FindFromKey(aVP);
285         //
286         aSelector.Clear();
287         aSelector.SetBox(aBoxVP);
288         //
289         aNbVSD=aBBTree.Select(aSelector);
290         if (!aNbVSD) {
291           continue;  // it must not be
292  }
293         //
294         const TColStd_ListOfInteger& aLI=aSelector.Indices();
295         //
296         aIt.Initialize(aLI);
297         for (; aIt.More(); aIt.Next()) {
298           aIP1=aIt.Value();
299           if (aMIP.Contains(aIP1)) {
300             continue;
301           }
302           aMIP1.Add(aIP1);
303         } //for (; aIt.More(); aIt.Next()) {
304       }//for(; aIt1.More(); aIt1.Next()) {
305       //
306       aNbIP1=aMIP1.Extent();
307       if (!aNbIP1) {
308         break;
309       }
310       //
311       aIt1.Initialize(aMIP);
312       for(; aIt1.More(); aIt1.Next()) {
313         aIP=aIt1.Key();
314         aMIPC.Add(aIP);
315       }
316       //
317       aMIP.Clear();
318       aIt1.Initialize(aMIP1);
319       for(; aIt1.More(); aIt1.Next()) {
320         aIP=aIt1.Key();
321         aMIP.Add(aIP);
322       }
323       aMIP1.Clear();
324     }// while(1)
325     //
326     // Fill myImages
327     aNbIP=aMIPC.Extent();
328     //
329     if (!aNbIP) {// no SD vertices founded
330       aVF=aV;
331       aLVSD.Append(aV);
332       aMVProcessed.Add(aV);
333     }
334     else { // SD vertices founded [ aMIPC ]
335       aIt1.Initialize(aMIPC);
336       for(j=0; aIt1.More(); aIt1.Next(), ++j) {
337         aIP=aIt1.Key();
338         const TopoDS_Shape& aVP=aMIS.FindFromKey(aIP);
339         if (!j) {
340           aVF=aVP;
341         }
342         aLVSD.Append(aVP);
343         aMVProcessed.Add(aVP);
344       }
345     }
346     myImages.Bind(aVF, aLVSD);
347   }// for (i=1; i<=aNbV; ++i) {
348   //------------------------------
349   //
350   // Make new vertices
351   aMV.Clear();
352   aItIm.Initialize(myImages);
353   for (; aItIm.More(); aItIm.Next()) {
354     const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aItIm.Key());
355     const TopTools_ListOfShape& aLVSD=aItIm.Value();
356     aNbVSD=aLVSD.Extent();
357     if (aNbVSD>1) {
358       aMV.Add(aV);
359       MakeVertex(aLVSD, aVnew);
360       aMVV.Bind(aVnew, aLVSD);
361     }
362   }
363   //
364   // UnBind old vertices
365   aNbV=aMV.Extent();
366   for (i=1; i<=aNbV; ++i) {
367     const TopoDS_Shape& aV=aMV(i);
368     myImages.UnBind(aV);
369   }
370   //
371   // Bind new vertices
372   aItIm.Initialize(aMVV);
373   for (; aItIm.More(); aItIm.Next()) {
374     const TopoDS_Shape& aV=aItIm.Key();
375     const TopTools_ListOfShape& aLVSD=aItIm.Value();
376     myImages.Bind(aV, aLVSD);
377   }
378   //
379   // Origins
380   aItIm.Initialize(myImages);
381   for (; aItIm.More(); aItIm.Next()) {
382     const TopoDS_Shape& aV=aItIm.Key();
383     const TopTools_ListOfShape& aLVSD=aItIm.Value();
384     aItS.Initialize(aLVSD);
385     for (; aItS.More(); aItS.Next()) {
386       const TopoDS_Shape& aVSD=aItS.Value();
387       if (!myOrigins.IsBound(aVSD)) {
388         myOrigins.Bind(aVSD, aV);
389       }
390     }
391   }
392 }
393 //=======================================================================
394 //function : MakeSubShapes
395 //purpose  :
396 //=======================================================================
397 void GEOMAlgo_Gluer::MakeSubShapes (const TopoDS_Shape&  theShape,
398                                     TopTools_MapOfShape& theMS,
399                                     TopoDS_Compound&     theResult)
400 {
401   if (theMS.Contains(theShape))
402     return;
403   //
404   BRep_Builder aBB;
405   //
406   theMS.Add(theShape);
407   //
408   if (theShape.ShapeType() == TopAbs_COMPOUND ||
409       theShape.ShapeType() == TopAbs_COMPSOLID)  {
410     TopoDS_Iterator It (theShape, Standard_True, Standard_True);
411     for (; It.More(); It.Next())    {
412       MakeSubShapes(It.Value(), theMS, theResult);
413     }
414   }
415   else if (theShape.ShapeType() == TopAbs_SOLID)  {
416     // build a solid
417     TopoDS_Solid aNewSolid;
418     TopExp_Explorer aExpS, aExp;
419     //
420     const TopoDS_Solid& aSolid = TopoDS::Solid(theShape);
421     //
422     TopAbs_Orientation anOr = aSolid.Orientation();
423     //
424     aBB.MakeSolid(aNewSolid);
425     aNewSolid.Orientation(anOr);
426     //
427     aExp.Init(aSolid, TopAbs_SHELL);
428     for (; aExp.More(); aExp.Next())
429     {
430       const TopoDS_Shape& aShell=aExp.Current();
431       const TopoDS_Shape& aShellR=myOrigins.Find(aShell);
432       aBB.Add(aNewSolid, aShellR);
433     }
434     //
435     TopTools_ListOfShape aLS;
436     //
437     aLS.Append(aSolid);
438     myImages.Bind(aNewSolid, aLS);
439     myOrigins.Bind(aSolid, aNewSolid);
440     //
441     aBB.Add(theResult, aNewSolid);
442   }
443   else if (theShape.ShapeType() == TopAbs_WIRE)  {
444     if (myKeepNonSolids)    {
445       // just add image
446       if (!myOrigins.IsBound(theShape))   {
447         // build wire
448         const TopoDS_Wire& aW=TopoDS::Wire(theShape);
449         //
450         TopoDS_Wire newWire;
451         aBB.MakeWire(newWire);
452         //
453         TopExp_Explorer aExpE (aW, TopAbs_EDGE);
454         for (; aExpE.More(); aExpE.Next()) {
455           const TopoDS_Edge& aE=TopoDS::Edge(aExpE.Current());
456           TopoDS_Edge aER=TopoDS::Edge(myOrigins.Find(aE));
457           //
458           aER.Orientation(TopAbs_FORWARD);
459           if (!BRep_Tool::Degenerated(aER)) {
460             // orient image
461             Standard_Boolean bIsToReverse=
462               GEOMAlgo_AlgoTools::IsSplitToReverse(aER, aE, myContext);
463             if (bIsToReverse) {
464               aER.Reverse();
465             }
466           }
467           else {
468             aER.Orientation(aE.Orientation());
469           }
470           //
471           aBB.Add(newWire, aER);
472         }
473         // xf
474         TopTools_ListOfShape aLW;
475         //
476         aLW.Append(aW);
477         myImages.Bind(newWire, aLW);
478         myOrigins.Bind(aW, newWire);
479       }
480       const TopoDS_Shape& aShapeR = myOrigins.Find(theShape);
481       aBB.Add(theResult, aShapeR);
482     }
483   }
484   else  {
485     if (myKeepNonSolids)    {
486       // just add image
487       const TopoDS_Shape& aShapeR = myOrigins.Find(theShape);
488       aBB.Add(theResult, aShapeR);
489     }
490   }
491 }
492 //=======================================================================
493 //function : MakeSolids
494 //purpose  :
495 //=======================================================================
496 void GEOMAlgo_Gluer::MakeSolids()
497 {
498   myErrorStatus=0;
499   //
500   BRep_Builder aBB;
501   TopoDS_Compound aCmp;
502   TopTools_MapOfShape aMS;
503   //
504   aBB.MakeCompound(aCmp);
505   //
506   // Add images of all initial sub-shapes in the result.
507   // If myKeepNonSolids==false, add only solids images.
508   MakeSubShapes(myShape, aMS, aCmp);
509   //
510   myResult=aCmp;
511   //
512   if (aMS.Extent()) {
513 #if OCC_VERSION_LARGE > 0x07020001
514     TopTools_IndexedMapOfShape aMapToAvoid;
515     BOPTools_AlgoTools::CorrectCurveOnSurface(myResult, aMapToAvoid, 0.0001);
516 #else
517     BOPTools_AlgoTools::CorrectCurveOnSurface(myResult, 0.0001);
518 #endif
519   }
520 }
521 //=======================================================================
522 //function : MakeShells
523 //purpose  :
524 //=======================================================================
525 void GEOMAlgo_Gluer::MakeShells()
526 {
527   myErrorStatus=0;
528   //
529   Standard_Boolean bIsToReverse;
530   Standard_Integer i, aNbS;
531   TopAbs_Orientation anOr;
532   TopoDS_Shell aNewShell;
533   TopoDS_Face aFR;
534   TopTools_IndexedMapOfShape aMS;
535   TopExp_Explorer aExp;
536   BRep_Builder aBB;
537   //
538   TopExp::MapShapes(myShape, TopAbs_SHELL, aMS);
539   //
540   aNbS=aMS.Extent();
541   for (i=1; i<=aNbS; ++i) {
542     const TopoDS_Shell& aShell=TopoDS::Shell(aMS(i));
543     anOr=aShell.Orientation();
544     //
545     aBB.MakeShell(aNewShell);
546     aNewShell.Orientation(anOr);
547     aExp.Init(aShell, TopAbs_FACE);
548     for (; aExp.More(); aExp.Next()) {
549       const TopoDS_Face& aF=TopoDS::Face(aExp.Current());
550       aFR=TopoDS::Face(myOrigins.Find(aF));
551       if (aFR.IsSame(aF)) {
552         aBB.Add(aNewShell, aF);
553         continue;
554       }
555       bIsToReverse=IsToReverse(aFR, aF);
556       if (bIsToReverse) {
557         aFR.Reverse();
558       }
559       aBB.Add(aNewShell, aFR);
560     }
561     //
562     TopTools_ListOfShape aLS;
563     //
564     aLS.Append(aShell);
565     myImages.Bind(aNewShell, aLS);
566     myOrigins.Bind(aShell, aNewShell);
567   }
568 }
569 //=======================================================================
570 //function : MakeFaces
571 //purpose  :
572 //=======================================================================
573 void GEOMAlgo_Gluer::MakeFaces()
574 {
575   MakeShapes(TopAbs_FACE);
576 }
577 //=======================================================================
578 //function : MakeEdges
579 //purpose  :
580 //=======================================================================
581 void GEOMAlgo_Gluer::MakeEdges()
582 {
583   MakeShapes(TopAbs_EDGE);
584 }
585 //=======================================================================
586 //function : MakeShapes
587 //purpose  :
588 //=======================================================================
589 void GEOMAlgo_Gluer::MakeShapes(const TopAbs_ShapeEnum aType)
590 {
591   myErrorStatus=0;
592   //
593   Standard_Boolean bHasNewSubShape;
594   Standard_Integer i, aNbF, aNbSDF, iErr;
595   TopoDS_Shape aNewShape;
596   TopTools_IndexedMapOfShape aMF;
597   TopTools_ListIteratorOfListOfShape aItS;
598   GEOMAlgo_PassKeyShape aPKF;
599   GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLF;
600   //
601   TopExp::MapShapes(myShape, aType, aMF);
602   //
603   aNbF=aMF.Extent();
604   for (i=1; i<=aNbF; ++i) {
605     const TopoDS_Shape& aS=aMF(i);
606     //
607     if (aType==TopAbs_FACE) {
608       const TopoDS_Face& aF=TopoDS::Face(aS);
609       FacePassKey(aF, aPKF);
610     }
611     else if (aType==TopAbs_EDGE) {
612       const TopoDS_Edge& aE=TopoDS::Edge(aS);
613       EdgePassKey(aE, aPKF);
614     }
615     //
616     if (myErrorStatus) {
617       return;
618     }
619     //
620     if (aMPKLF.Contains(aPKF)) {
621       TopTools_ListOfShape& aLSDF=aMPKLF.ChangeFromKey(aPKF);
622       aLSDF.Append(aS);
623     }
624     else {
625       TopTools_ListOfShape aLSDF;
626       //
627       aLSDF.Append(aS);
628       aMPKLF.Add(aPKF, aLSDF);
629     }
630   }
631   // check geometric coincidence
632   if (myCheckGeometry) {
633     iErr=GEOMAlgo_AlgoTools::RefineSDShapes(aMPKLF, myTol, myContext);
634     if (iErr) {
635       myErrorStatus=200;
636       return;
637     }
638   }
639   //
640   // Images/Origins
641   //
642   aNbF=aMPKLF.Extent();
643   for (i=1; i<=aNbF; ++i) {
644     const TopTools_ListOfShape& aLSDF=aMPKLF(i);
645     aNbSDF=aLSDF.Extent();
646     if (!aNbSDF) {
647       myErrorStatus=4; // it must not be
648     }
649     //
650     const TopoDS_Shape& aS1=aLSDF.First();
651     //
652     bHasNewSubShape=Standard_True;
653     // prevent creation of a new shape if there are not
654     // new sub-shapes of aSS among the originals
655     if (aNbSDF==1) {
656       bHasNewSubShape=HasNewSubShape(aS1);
657       if (!bHasNewSubShape) {
658         aNewShape=aS1;
659         aNewShape.Orientation(TopAbs_FORWARD);
660       }
661     }
662     //
663     if (bHasNewSubShape) {
664       if (aType==TopAbs_FACE) {
665         TopoDS_Face aNewFace;
666         //
667         const TopoDS_Face& aF1=TopoDS::Face(aS1);
668         MakeFace(aF1, aNewFace);
669         aNewShape=aNewFace;
670       }
671       else if (aType==TopAbs_EDGE) {
672         TopoDS_Edge aNewEdge;
673         //
674         const TopoDS_Edge& aE1=TopoDS::Edge(aS1);
675         MakeEdge(aE1, aNewEdge);
676         aNewShape=aNewEdge;
677       }
678     }
679     //
680     myImages.Bind(aNewShape, aLSDF);
681     // origins
682     aItS.Initialize(aLSDF);
683     for (; aItS.More(); aItS.Next()) {
684       const TopoDS_Shape& aFSD=aItS.Value();
685       if (!myOrigins.IsBound(aFSD)) {
686         myOrigins.Bind(aFSD, aNewShape);
687       }
688     }
689   }
690 }
691 //=======================================================================
692 //function : CheckResult
693 //purpose  :
694 //=======================================================================
695 void GEOMAlgo_Gluer::CheckResult()
696 {
697   myErrorStatus=0;
698   //
699   if (myResult.IsNull()) {
700     myErrorStatus=6;
701     return;
702   }
703   //
704   Standard_Boolean bFound;
705   Standard_Integer i, j, aNbS, aNbFS, aNbSx;
706   TopTools_IndexedMapOfShape aMS, aMFS;
707   TopTools_IndexedDataMapOfShapeListOfShape aMFR;
708   //
709   TopExp::MapShapesAndAncestors(myResult, TopAbs_FACE, TopAbs_SOLID, aMFR);
710   TopExp::MapShapes(myResult, TopAbs_SOLID, aMS);
711   //
712
713   myNbAlone=0;
714   aNbS=aMS.Extent();
715   for (i=1; i<=aNbS; ++i) {
716     const TopoDS_Shape& aSolid=aMS(i);
717     //
718     aMFS.Clear();
719     TopExp::MapShapes(aSolid, TopAbs_FACE, aMFS);
720     //
721     bFound=Standard_False;
722     aNbFS=aMFS.Extent();
723     for (j=1; j<=aNbFS; ++j) {
724       const TopoDS_Shape& aFS=aMFS(j);
725       if (aMFR.Contains(aFS)) {
726         const TopTools_ListOfShape& aLSx=aMFR.FindFromKey(aFS);
727         aNbSx=aLSx.Extent();
728         if (aNbSx==2) {
729           bFound=!bFound;
730           break;
731         }
732       }
733     }
734     //
735     if (!bFound) {
736       myWarningStatus=1;
737       ++myNbAlone;
738       //break;
739     }
740   }
741 }
742 //=======================================================================
743 //function : CheckData
744 //purpose  :
745 //=======================================================================
746 void GEOMAlgo_Gluer::CheckData()
747 {
748   myErrorStatus=0;
749   //
750   if (myShape.IsNull()) {
751     myErrorStatus=5;
752     return;
753   }
754 }
755 //=======================================================================
756 //function : InnerTolerance
757 //purpose  :
758 //=======================================================================
759 void GEOMAlgo_Gluer::InnerTolerance()
760 {
761   myErrorStatus=0;
762   //
763   /*
764   Standard_Integer i;
765   Standard_Real aX[3][2], dH, dHmin, aCoef, aTolTresh;
766   Bnd_Box aBox;
767   //
768   BRepBndLib::Add(myShape, aBox);
769   aBox.Get(aX[0][0], aX[1][0], aX[2][0], aX[0][1], aX[1][1], aX[2][1]);
770   //
771   dHmin=aX[0][1]-aX[0][0];
772   for (i=1; i<3; ++i) {
773     dH=aX[i][1]-aX[i][0];
774     if (dH<dHmin) {
775       dHmin=dH;
776     }
777   }
778   //
779   myTol=myTolerance;
780   aCoef=0.01;
781   aTolTresh=aCoef*dHmin;
782   if (myTol>aTolTresh) {
783     myTol=aTolTresh;
784   }
785   */
786   myTol=myTolerance;
787 }
788 //=======================================================================
789 //function : FacePassKey
790 //purpose  :
791 //=======================================================================
792 void GEOMAlgo_Gluer::FacePassKey(const TopoDS_Face& aF,
793      GEOMAlgo_PassKeyShape& aPK)
794 {
795   Standard_Integer i, aNbE;
796   TopTools_ListOfShape aLE;
797   TopTools_IndexedMapOfShape aME;
798   //
799   TopExp::MapShapes(aF, TopAbs_EDGE, aME);
800   aNbE=aME.Extent();
801   //
802   for (i=1; i<=aNbE; ++i) {
803     const TopoDS_Shape& aE=aME(i);
804     if (!myOrigins.IsBound(aE)) {
805       myErrorStatus=102;
806       return;
807     }
808     const TopoDS_Shape& aER=myOrigins.Find(aE);
809     aLE.Append(aER);
810   }
811   aPK.SetShapes(aLE);
812 }
813 //=======================================================================
814 //function : EdgePassKey
815 //purpose  :
816 //=======================================================================
817 void GEOMAlgo_Gluer::EdgePassKey(const TopoDS_Edge& aE,
818      GEOMAlgo_PassKeyShape& aPK)
819 {
820   TopoDS_Vertex aV1, aV2;
821   //
822   TopExp::Vertices(aE, aV1, aV2);
823   //
824   if (!myOrigins.IsBound(aV1) || !myOrigins.IsBound(aV2) ) {
825      myErrorStatus=100;
826      return;
827   }
828   const TopoDS_Shape& aVR1=myOrigins.Find(aV1);
829   const TopoDS_Shape& aVR2=myOrigins.Find(aV2);
830   aPK.SetShapes(aVR1, aVR2);
831 }
832 //=======================================================================
833 //function : MakeVertex
834 //purpose  :
835 //=======================================================================
836 void GEOMAlgo_Gluer::MakeVertex(const TopTools_ListOfShape& aLV,
837     TopoDS_Vertex& aNewVertex)
838 {
839   Standard_Integer aNbV;
840   Standard_Real aTolV, aD, aDmax;
841   gp_XYZ aGC;
842   gp_Pnt aP3D, aPGC;
843   TopoDS_Vertex aVx;
844   BRep_Builder aBB;
845   TopTools_ListIteratorOfListOfShape aIt;
846   //
847   aNbV=aLV.Extent();
848   if (!aNbV) {
849     return;
850   }
851   //
852   // center of gravity
853   aGC.SetCoord(0.,0.,0.);
854   aIt.Initialize(aLV);
855   for (; aIt.More(); aIt.Next()) {
856     aVx=TopoDS::Vertex(aIt.Value());
857     aP3D=BRep_Tool::Pnt(aVx);
858     aGC+=aP3D.XYZ();
859   }
860   aGC/=(Standard_Real)aNbV;
861   aPGC.SetXYZ(aGC);
862   //
863   // tolerance value
864   aDmax=-1.;
865   aIt.Initialize(aLV);
866   for (; aIt.More(); aIt.Next()) {
867     aVx=TopoDS::Vertex(aIt.Value());
868     aP3D=BRep_Tool::Pnt(aVx);
869     aTolV=BRep_Tool::Tolerance(aVx);
870     aD=aPGC.Distance(aP3D)+aTolV;
871     if (aD>aDmax) {
872       aDmax=aD;
873     }
874   }
875   //
876   aBB.MakeVertex (aNewVertex, aPGC, aDmax);
877 }
878 //=======================================================================
879 //function : MakeEdge
880 //purpose  :
881 //=======================================================================
882 void GEOMAlgo_Gluer::MakeEdge(const TopoDS_Edge& aE,
883          TopoDS_Edge& aNewEdge)
884 {
885   myErrorStatus=0;
886   //
887   Standard_Boolean bIsDE;
888   Standard_Real aT1, aT2;
889   TopoDS_Vertex aV1, aV2, aVR1, aVR2;
890   TopoDS_Edge aEx;
891   //
892   bIsDE=BRep_Tool::Degenerated(aE);
893   //
894   aEx=aE;
895   aEx.Orientation(TopAbs_FORWARD);
896   //
897   TopExp::Vertices(aEx, aV1, aV2);
898   //
899   aT1=BRep_Tool::Parameter(aV1, aEx);
900   aT2=BRep_Tool::Parameter(aV2, aEx);
901   //
902   aVR1=TopoDS::Vertex(myOrigins.Find(aV1));
903   aVR1.Orientation(TopAbs_FORWARD);
904   aVR2=TopoDS::Vertex(myOrigins.Find(aV2));
905   aVR2.Orientation(TopAbs_REVERSED);
906   //
907   if (bIsDE) {
908     Standard_Real aTol;
909     BRep_Builder aBB;
910     TopoDS_Edge E;
911     //TopAbs_Orientation anOrE;
912     //
913     //anOrE=aE.Orientation();
914     aTol=BRep_Tool::Tolerance(aE);
915     //
916     E=aEx;
917     E.EmptyCopy();
918     //
919     aBB.Add  (E, aVR1);
920     aBB.Add  (E, aVR2);
921     aBB.Range(E, aT1, aT2);
922     aBB.Degenerated(E, Standard_True);
923     aBB.UpdateEdge(E, aTol);
924     //
925     aNewEdge=E;
926   }
927   //
928   else {
929     BOPTools_AlgoTools::MakeSplitEdge(aEx, aVR1, aT1, aVR2, aT2, aNewEdge);
930   }
931 }
932 //=======================================================================
933 //function : MakeFace
934 //purpose  :
935 //=======================================================================
936 void GEOMAlgo_Gluer::MakeFace(const TopoDS_Face& aF,
937          TopoDS_Face& aNewFace)
938 {
939   myErrorStatus=0;
940   //
941   Standard_Boolean bIsToReverse, bIsUPeriodic;
942   Standard_Real aTol, aUMin, aUMax, aVMin, aVMax;
943   TopoDS_Edge aER;
944   TopoDS_Wire newWire;
945   TopoDS_Face aFFWD, newFace;
946   TopLoc_Location aLoc;
947   Handle(Geom_Surface) aS;
948   Handle(Geom2d_Curve) aC2D;
949   TopExp_Explorer aExpW, aExpE;
950   BRep_Builder aBB;
951   //
952   aFFWD=aF;
953   aFFWD.Orientation(TopAbs_FORWARD);
954   //
955   aS=BRep_Tool::Surface(aFFWD, aLoc);
956   bIsUPeriodic=GEOMAlgo_AlgoTools::IsUPeriodic(aS);
957   aTol=BRep_Tool::Tolerance(aFFWD);
958   BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
959   //
960   aBB.MakeFace (newFace, aS, aLoc, aTol);
961   //
962   aExpW.Init(aFFWD, TopAbs_WIRE);
963   for (; aExpW.More(); aExpW.Next()) {
964     aBB.MakeWire(newWire);
965     const TopoDS_Wire& aW=TopoDS::Wire(aExpW.Current());
966     aExpE.Init(aW, TopAbs_EDGE);
967     for (; aExpE.More(); aExpE.Next()) {
968       const TopoDS_Edge& aE=TopoDS::Edge(aExpE.Current());
969       aER=TopoDS::Edge(myOrigins.Find(aE));
970       //
971       aER.Orientation(TopAbs_FORWARD);
972       if (!BRep_Tool::Degenerated(aER)) {
973         // build p-curve
974         if (bIsUPeriodic) {
975           GEOMAlgo_AlgoTools::RefinePCurveForEdgeOnFace(aER, aFFWD, aUMin, aUMax);
976           }
977         BOPTools_AlgoTools2D::BuildPCurveForEdgeOnFace(aER, aFFWD);
978         
979         // orient image
980         bIsToReverse=GEOMAlgo_AlgoTools::IsSplitToReverse(aER, aE, myContext);
981         if (bIsToReverse) {
982           aER.Reverse();
983         }
984       }
985       else {
986         aER.Orientation(aE.Orientation());
987       }
988       //
989       aBB.Add(newWire, aER);
990     }
991     // xf
992     TopTools_ListOfShape aLW;
993     //
994     aLW.Append(aW);
995     myImages.Bind(newWire, aLW);
996     myOrigins.Bind(aW, newWire);
997     // xt
998     aBB.Add(newFace, newWire);
999   }
1000   aNewFace=newFace;
1001 }
1002 //=======================================================================
1003 //function : IsToReverse
1004 //purpose  :
1005 //=======================================================================
1006 Standard_Boolean GEOMAlgo_Gluer::IsToReverse(const TopoDS_Face& aFR,
1007                                              const TopoDS_Face& aF)
1008 {
1009   Standard_Boolean bRet;
1010   Standard_Real aT, aT1, aT2, aTR, aScPr;
1011   TopExp_Explorer aExp;
1012   Handle(Geom_Curve)aC3D;
1013   gp_Pnt aP;
1014   gp_Dir aDNF, aDNFR;
1015   //
1016   bRet=Standard_False;
1017   //
1018   aExp.Init(aF, TopAbs_EDGE);
1019   for (; aExp.More(); aExp.Next()) {
1020     const TopoDS_Edge& aE=TopoDS::Edge(aExp.Current());
1021     //
1022     if (BRep_Tool::Degenerated(aE)) {
1023       continue;
1024     }
1025     //
1026     const TopoDS_Edge& aER=TopoDS::Edge(myOrigins.Find(aE));
1027     //
1028     aC3D=BRep_Tool::Curve(aE, aT1, aT2);
1029     aT=IntTools_Tools::IntermediatePoint(aT1, aT2);
1030     aC3D->D0(aT, aP);
1031     myContext->ProjectPointOnEdge(aP, aER, aTR);
1032     //
1033     BOPTools_AlgoTools3D::GetNormalToFaceOnEdge (aE, aF, aT, aDNF);
1034     if (aF.Orientation()==TopAbs_REVERSED) {
1035       aDNF.Reverse();
1036     }
1037     //
1038     BOPTools_AlgoTools3D::GetNormalToFaceOnEdge (aER, aFR, aTR, aDNFR);
1039     if (aFR.Orientation()==TopAbs_REVERSED) {
1040       aDNFR.Reverse();
1041     }
1042     //
1043     aScPr=aDNF*aDNFR;
1044     return (aScPr<0.);
1045   }
1046   return bRet;
1047 }
1048 //=======================================================================
1049 //function : HasNewSubShape
1050 //purpose  :
1051 //=======================================================================
1052 Standard_Boolean GEOMAlgo_Gluer::HasNewSubShape(const TopoDS_Shape& aS)const
1053 {
1054   Standard_Boolean bRet;
1055   Standard_Integer i, aNbSS;
1056   TopTools_IndexedMapOfShape aMSS;
1057   //
1058   GetSubShapes(aS, aMSS);
1059   //
1060   bRet=Standard_False;
1061   aNbSS=aMSS.Extent();
1062   for (i=1; i<=aNbSS; ++i) {
1063     const TopoDS_Shape& aSS=aMSS(i);
1064     if (aSS.ShapeType()==TopAbs_WIRE) {
1065       continue;
1066     }
1067     //
1068     bRet=!myOrigins.IsBound(aSS);
1069     if (bRet) {
1070       return bRet;
1071     }
1072     //
1073     const TopoDS_Shape& aSSIm=myOrigins.Find(aSS);
1074     bRet=!aSSIm.IsSame(aSS);
1075     if (bRet) {
1076       return bRet;
1077     }
1078   }
1079   return bRet;
1080 }
1081 //=======================================================================
1082 //function : GetSubShapes
1083 //purpose  :
1084 //=======================================================================
1085 void GetSubShapes(const TopoDS_Shape& aS,
1086     TopTools_IndexedMapOfShape& aMSS)
1087 {
1088   Standard_Integer aR;
1089   TopAbs_ShapeEnum aType;
1090   TopoDS_Iterator aIt;
1091   //
1092   aType=aS.ShapeType();
1093   aR=(Standard_Integer)aType+1;
1094   if (aR>TopAbs_VERTEX) {
1095     return;
1096   }
1097   //
1098   aIt.Initialize(aS);
1099   for (; aIt.More(); aIt.Next()) {
1100     const TopoDS_Shape& aSS=aIt.Value();
1101     aMSS.Add(aSS);
1102     GetSubShapes(aSS, aMSS);
1103   }
1104 }
1105 //=======================================================================
1106 //function : Modified
1107 //purpose  :
1108 //=======================================================================
1109 const TopTools_ListOfShape& GEOMAlgo_Gluer::Modified
1110   (const TopoDS_Shape& aS)
1111 {
1112   TopAbs_ShapeEnum aType;
1113   //
1114   myGenerated.Clear();
1115   //
1116   aType=aS.ShapeType();
1117   if (aType==TopAbs_VERTEX ||
1118       aType==TopAbs_EDGE   ||
1119       aType==TopAbs_WIRE   ||
1120       aType==TopAbs_FACE   ||
1121       aType==TopAbs_SHELL  ||
1122       aType==TopAbs_SOLID) {
1123     if(myOrigins.IsBound(aS)) {
1124       const TopoDS_Shape& aSnew=myOrigins.Find(aS);
1125       if (!aSnew.IsSame(aS)) {
1126         myGenerated.Append(aSnew);
1127       }
1128     }
1129   }
1130   //
1131   return myGenerated;
1132 }
1133 //=======================================================================
1134 //function : Generated
1135 //purpose  :
1136 //=======================================================================
1137 const TopTools_ListOfShape& GEOMAlgo_Gluer::Generated(const TopoDS_Shape& )
1138 {
1139   myGenerated.Clear();
1140   return myGenerated;
1141 }
1142 //=======================================================================
1143 //function : IsDeleted
1144 //purpose  :
1145 //=======================================================================
1146 Standard_Boolean GEOMAlgo_Gluer::IsDeleted (const TopoDS_Shape& aS)
1147 {
1148   Standard_Boolean bRet=Standard_False;
1149   //
1150   const TopTools_ListOfShape& aL=Modified(aS);
1151   bRet=!aL.IsEmpty();
1152   //
1153   return bRet;
1154 }
1155
1156 //
1157 // ErrorStatus
1158 //
1159 // 1   - the object is just initialized
1160 // 2   - no vertices found in source shape
1161 // 3   - nb same domain vertices for the vertex Vi =0
1162 // 4   - nb same domain edges(faces) for the edge Ei(face Fi)  =0
1163 // 5   - source shape is Null
1164 // 6   - result shape is Null
1165 // 101 - nb edges > PassKey.NbMax() in FacesPassKey()
1166 // 102 - the edge Ei can not be found in myOrigins Map
1167 // 100 - the vertex Vi can not be found in myOrigins Map
1168 //
1169 // WarningStatus
1170 //
1171 // 1   - some shapes can not be glued by faces
1172 //