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