Salome HOME
Issue #2873: Freeze when inserting a new folder
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_UnifySameDomain.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_UnifySameDomain.h"
21
22 #include <GeomAlgoAPI_CompoundBuilder.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
24
25 #include <ShapeUpgrade_UnifySameDomain.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <Precision.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <Bnd_Box.hxx>
32 #include <BRepBndLib.hxx>
33
34 //==================================================================================================
35 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const ListOfShape& theShapes)
36 {
37   build(theShapes);
38 }
39
40 //==================================================================================================
41 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const GeomShapePtr& theShape)
42 {
43   build(theShape, false);
44 }
45
46 //==================================================================================================
47 void GeomAlgoAPI_UnifySameDomain::build(const ListOfShape& theShapes)
48 {
49   if(theShapes.empty()) {
50     return;
51   }
52
53   // Make compound.
54   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
55   ListOfShape aResults;
56   GeomAlgoAPI_ShapeTools::combineShapes(
57     aCompound,
58     GeomAPI_Shape::SHELL,
59     aResults);
60
61   if(aResults.size() > 1 ||
62      (aResults.size() == 1 && aResults.front()->shapeType() > GeomAPI_Shape::SHELL)) {
63     return;
64   }
65
66   const TopoDS_Shape& aShell = aResults.front()->impl<TopoDS_Shape>();
67
68   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
69   aShape->setImpl(new TopoDS_Shape(aShell));
70   build(aShape, true);
71 }
72
73 // calculates maximum possible tolerance on edges of shape
74 // (method from GEOM module BlockFix_UnionFaces.cxx)
75 static Standard_Real defineLinearTolerance(const TopoDS_Shape& theShape)
76 {
77   Standard_Real aTol = Precision::Confusion();
78
79   Standard_Real MinSize = RealLast();
80   TopExp_Explorer Explo(theShape, TopAbs_EDGE);
81   for (; Explo.More(); Explo.Next())
82   {
83     const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
84     Bnd_Box aBox;
85     BRepBndLib::Add(anEdge, aBox);
86     Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
87     aBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
88     Standard_Real MaxSize = Max(Xmax - Xmin, Max(Ymax - Ymin, Zmax - Zmin));
89     if (MaxSize < MinSize)
90       MinSize = MaxSize;
91   }
92
93   if (!Precision::IsInfinite(MinSize))
94     aTol = 0.1 * MinSize;
95
96   return aTol;
97 }
98
99 //==================================================================================================
100 void GeomAlgoAPI_UnifySameDomain::build(const GeomShapePtr& theShape,
101                                         const bool theIsToSimplifyShell)
102 {
103   ShapeUpgrade_UnifySameDomain* aUnifyAlgo = new ShapeUpgrade_UnifySameDomain();
104   this->setImpl(aUnifyAlgo);
105
106   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
107   aUnifyAlgo->Initialize(aShape);
108   aUnifyAlgo->SetLinearTolerance(defineLinearTolerance(aShape));
109   aUnifyAlgo->SetAngularTolerance(1.e-6); // for #2697
110   aUnifyAlgo->Build();
111
112   TopoDS_Shape aResult = aUnifyAlgo->Shape();
113   if (aResult.IsNull()) {
114     return;
115   }
116
117   if (theIsToSimplifyShell && aResult.ShapeType() == TopAbs_SHELL) {
118     int aNb = 0;
119     TopoDS_Iterator anIt(aResult);
120     for (; anIt.More(); anIt.Next()) {
121       ++aNb;
122     }
123
124     if (aNb == 1) {
125       anIt.Initialize(aResult);
126       aResult = anIt.Value();
127     }
128   }
129
130   std::shared_ptr<GeomAPI_Shape> aResShape(new GeomAPI_Shape());
131   aResShape->setImpl(new TopoDS_Shape(aResult));
132   this->setShape(aResShape);
133   this->setDone(true);
134 }
135
136 //==================================================================================================
137 void GeomAlgoAPI_UnifySameDomain::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
138                                   ListOfShape& theHistory)
139 {
140   if(!theShape.get()) {
141     return;
142   }
143
144   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
145   const ShapeUpgrade_UnifySameDomain& aUnifyAlgo = this->impl<ShapeUpgrade_UnifySameDomain>();
146
147   for (int aIsModified = 0; aIsModified <= 1; aIsModified++) {
148     if (!aUnifyAlgo.History()->IsSupportedType(aShape)) // to avoid crash in BRepTools_History
149       continue;
150     const TopTools_ListOfShape& aMList = aIsModified ?
151       aUnifyAlgo.History()->Modified(aShape) : aUnifyAlgo.History()->Generated(aShape);
152     for (TopTools_ListIteratorOfListOfShape aModified(aMList); aModified.More(); aModified.Next()) {
153       for (TopExp_Explorer anExp(aModified.Value(), aShape.ShapeType());
154            anExp.More(); anExp.Next()) {
155         GeomShapePtr aGeomShape(new GeomAPI_Shape());
156         aGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
157         theHistory.push_back(aGeomShape);
158       }
159     }
160   }
161 }