Salome HOME
Merge branch 'V9_4_BR'
[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_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25
26 #include <ShapeUpgrade_UnifySameDomain.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <Precision.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <Bnd_Box.hxx>
33 #include <BRepBndLib.hxx>
34
35 //==================================================================================================
36 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const ListOfShape& theShapes)
37 {
38   build(theShapes);
39 }
40
41 //==================================================================================================
42 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const GeomShapePtr& theShape)
43 {
44   build(theShape, false);
45 }
46
47 //==================================================================================================
48 void GeomAlgoAPI_UnifySameDomain::build(const ListOfShape& theShapes)
49 {
50   if(theShapes.empty()) {
51     return;
52   }
53
54   // Make compound.
55   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
56   ListOfShape aResults;
57   GeomAlgoAPI_ShapeTools::combineShapes(
58     aCompound,
59     GeomAPI_Shape::SHELL,
60     aResults);
61
62   if(aResults.size() > 1 ||
63      (aResults.size() == 1 && aResults.front()->shapeType() > GeomAPI_Shape::SHELL)) {
64     return;
65   }
66
67   const TopoDS_Shape& aShell = aResults.front()->impl<TopoDS_Shape>();
68
69   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
70   aShape->setImpl(new TopoDS_Shape(aShell));
71   build(aShape, true);
72 }
73
74 // calculates maximum possible tolerance on edges of shape
75 // (method from GEOM module BlockFix_UnionFaces.cxx)
76 static Standard_Real defineLinearTolerance(const TopoDS_Shape& theShape)
77 {
78   Standard_Real aTol = Precision::Confusion();
79
80   Standard_Real MinSize = RealLast();
81   TopExp_Explorer Explo(theShape, TopAbs_EDGE);
82   for (; Explo.More(); Explo.Next())
83   {
84     const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
85     Bnd_Box aBox;
86     BRepBndLib::Add(anEdge, aBox);
87     Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
88     aBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
89     Standard_Real MaxSize = Max(Xmax - Xmin, Max(Ymax - Ymin, Zmax - Zmin));
90     if (MaxSize < MinSize)
91       MinSize = MaxSize;
92   }
93
94   if (!Precision::IsInfinite(MinSize))
95     aTol = 0.1 * MinSize;
96
97   return aTol;
98 }
99
100 //==================================================================================================
101 void GeomAlgoAPI_UnifySameDomain::build(const GeomShapePtr& theShape,
102                                         const bool theIsToSimplifyShell)
103 {
104   ShapeUpgrade_UnifySameDomain* aUnifyAlgo = new ShapeUpgrade_UnifySameDomain();
105   this->setImpl(aUnifyAlgo);
106
107   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
108   aUnifyAlgo->Initialize(aShape);
109   aUnifyAlgo->SetLinearTolerance(defineLinearTolerance(aShape));
110   aUnifyAlgo->SetAngularTolerance(1.e-6); // for #2697
111   aUnifyAlgo->Build();
112
113   TopoDS_Shape aResult = aUnifyAlgo->Shape();
114   if (aResult.IsNull()) {
115     return;
116   }
117   // take off the compound if it consists of single sub-shape
118   if (aResult.ShapeType() == TopAbs_COMPOUND) {
119     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
120   }
121
122   if (theIsToSimplifyShell && aResult.ShapeType() == TopAbs_SHELL) {
123     int aNb = 0;
124     TopoDS_Iterator anIt(aResult);
125     for (; anIt.More(); anIt.Next()) {
126       ++aNb;
127     }
128
129     if (aNb == 1) {
130       anIt.Initialize(aResult);
131       aResult = anIt.Value();
132     }
133   }
134
135   std::shared_ptr<GeomAPI_Shape> aResShape(new GeomAPI_Shape());
136   aResShape->setImpl(new TopoDS_Shape(aResult));
137   this->setShape(aResShape);
138   this->setDone(true);
139 }
140
141 //==================================================================================================
142 void GeomAlgoAPI_UnifySameDomain::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
143                                   ListOfShape& theHistory)
144 {
145   if(!theShape.get()) {
146     return;
147   }
148
149   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
150   const ShapeUpgrade_UnifySameDomain& aUnifyAlgo = this->impl<ShapeUpgrade_UnifySameDomain>();
151
152   for (int aIsModified = 0; aIsModified <= 1; aIsModified++) {
153     if (!aUnifyAlgo.History()->IsSupportedType(aShape)) // to avoid crash in BRepTools_History
154       continue;
155     const TopTools_ListOfShape& aMList = aIsModified ?
156       aUnifyAlgo.History()->Modified(aShape) : aUnifyAlgo.History()->Generated(aShape);
157     for (TopTools_ListIteratorOfListOfShape aModified(aMList); aModified.More(); aModified.Next()) {
158       for (TopExp_Explorer anExp(aModified.Value(), aShape.ShapeType());
159            anExp.More(); anExp.Next()) {
160         GeomShapePtr aGeomShape(new GeomAPI_Shape());
161         aGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
162         theHistory.push_back(aGeomShape);
163       }
164     }
165   }
166 }