1 // Copyright (C) 2014-2019 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 <TopExp_Explorer.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <Precision.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <Bnd_Box.hxx>
32 #include <BRepBndLib.hxx>
33 #include <ShapeUpgrade_UnifySameDomain.hxx>
35 //==================================================================================================
36 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const ListOfShape& theShapes)
41 //==================================================================================================
42 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const GeomShapePtr& theShape)
44 build(theShape, false);
47 //==================================================================================================
48 void GeomAlgoAPI_UnifySameDomain::build(const ListOfShape& theShapes)
50 if(theShapes.empty()) {
55 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
57 GeomAlgoAPI_ShapeTools::combineShapes(
62 if(aResults.size() > 1 ||
63 (aResults.size() == 1 && aResults.front()->shapeType() > GeomAPI_Shape::SHELL)) {
67 const TopoDS_Shape& aShell = aResults.front()->impl<TopoDS_Shape>();
69 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
70 aShape->setImpl(new TopoDS_Shape(aShell));
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)
78 Standard_Real aTol = Precision::Confusion();
80 Standard_Real MinSize = RealLast();
81 TopExp_Explorer Explo(theShape, TopAbs_EDGE);
82 for (; Explo.More(); Explo.Next())
84 const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
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)
94 if (!Precision::IsInfinite(MinSize))
100 //==================================================================================================
101 void GeomAlgoAPI_UnifySameDomain::build(const GeomShapePtr& theShape,
102 const bool theIsToSimplifyShell)
104 ShapeUpgrade_UnifySameDomain* aUnifyAlgo = new ShapeUpgrade_UnifySameDomain();
105 this->setImpl(aUnifyAlgo);
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
113 TopoDS_Shape aResult = aUnifyAlgo->Shape();
114 if (aResult.IsNull()) {
117 // take off the compound if it consists of single sub-shape
118 if (aResult.ShapeType() == TopAbs_COMPOUND) {
119 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
122 if (theIsToSimplifyShell && aResult.ShapeType() == TopAbs_SHELL) {
124 TopoDS_Iterator anIt(aResult);
125 for (; anIt.More(); anIt.Next()) {
130 anIt.Initialize(aResult);
131 aResult = anIt.Value();
135 std::shared_ptr<GeomAPI_Shape> aResShape(new GeomAPI_Shape());
136 aResShape->setImpl(new TopoDS_Shape(aResult));
137 this->setShape(aResShape);
141 //==================================================================================================
142 void GeomAlgoAPI_UnifySameDomain::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
143 ListOfShape& theHistory)
145 if(!theShape.get()) {
149 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
150 const ShapeUpgrade_UnifySameDomain& aUnifyAlgo = this->impl<ShapeUpgrade_UnifySameDomain>();
152 for (int aIsModified = 0; aIsModified <= 1; aIsModified++) {
153 if (!aUnifyAlgo.History()->IsSupportedType(aShape)) // to avoid crash in BRepTools_History
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);