1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_UnifySameDomain.h"
22 #include <GeomAlgoAPI_CompoundBuilder.h>
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
26 #include <BRep_Tool.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <Precision.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <Bnd_Box.hxx>
33 #include <BRepBndLib.hxx>
34 #include <ShapeUpgrade_UnifySameDomain.hxx>
36 //==================================================================================================
37 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const ListOfShape& theShapes)
42 //==================================================================================================
43 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const GeomShapePtr& theShape)
45 build(theShape, false);
48 //==================================================================================================
49 void GeomAlgoAPI_UnifySameDomain::build(const ListOfShape& theShapes)
51 if(theShapes.empty()) {
56 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
58 GeomAlgoAPI_ShapeTools::combineShapes(
63 if(aResults.size() > 1 ||
64 (aResults.size() == 1 && aResults.front()->shapeType() > GeomAPI_Shape::SHELL)) {
68 const TopoDS_Shape& aShell = aResults.front()->impl<TopoDS_Shape>();
70 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
71 aShape->setImpl(new TopoDS_Shape(aShell));
75 // calculates maximum possible tolerance on edges of shape
76 static Standard_Real defineLinearTolerance(const TopoDS_Shape& theShape)
78 Standard_Real aMaxTol = Precision::Confusion();
80 TopExp_Explorer Explo(theShape, TopAbs_EDGE);
81 for (; Explo.More(); Explo.Next())
83 const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
84 aMaxTol = Max(aMaxTol, BRep_Tool::Tolerance(anEdge));
90 //==================================================================================================
91 void GeomAlgoAPI_UnifySameDomain::build(const GeomShapePtr& theShape,
92 const bool theIsToSimplifyShell)
94 ShapeUpgrade_UnifySameDomain* aUnifyAlgo = new ShapeUpgrade_UnifySameDomain();
95 this->setImpl(aUnifyAlgo);
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
103 TopoDS_Shape aResult = aUnifyAlgo->Shape();
104 if (aResult.IsNull()) {
107 // take off the compound if it consists of single sub-shape
108 if (aResult.ShapeType() == TopAbs_COMPOUND) {
109 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
112 if (theIsToSimplifyShell && aResult.ShapeType() == TopAbs_SHELL) {
114 TopoDS_Iterator anIt(aResult);
115 for (; anIt.More(); anIt.Next()) {
120 anIt.Initialize(aResult);
121 aResult = anIt.Value();
125 std::shared_ptr<GeomAPI_Shape> aResShape(new GeomAPI_Shape());
126 aResShape->setImpl(new TopoDS_Shape(aResult));
127 this->setShape(aResShape);
131 //==================================================================================================
132 void GeomAlgoAPI_UnifySameDomain::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
133 ListOfShape& theHistory)
135 if(!theShape.get()) {
139 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
140 const ShapeUpgrade_UnifySameDomain& aUnifyAlgo = this->impl<ShapeUpgrade_UnifySameDomain>();
142 for (int aIsModified = 0; aIsModified <= 1; aIsModified++) {
143 if (!aUnifyAlgo.History()->IsSupportedType(aShape)) // to avoid crash in BRepTools_History
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);