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_ShapeTools.h>
25 #include <ShapeUpgrade_UnifySameDomain.hxx>
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>
34 //==================================================================================================
35 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const ListOfShape& theShapes)
40 //==================================================================================================
41 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const GeomShapePtr& theShape)
43 build(theShape, false);
46 //==================================================================================================
47 void GeomAlgoAPI_UnifySameDomain::build(const ListOfShape& theShapes)
49 if(theShapes.empty()) {
54 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
56 GeomAlgoAPI_ShapeTools::combineShapes(
61 if(aResults.size() > 1 ||
62 (aResults.size() == 1 && aResults.front()->shapeType() > GeomAPI_Shape::SHELL)) {
66 const TopoDS_Shape& aShell = aResults.front()->impl<TopoDS_Shape>();
68 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
69 aShape->setImpl(new TopoDS_Shape(aShell));
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)
77 Standard_Real aTol = Precision::Confusion();
79 Standard_Real MinSize = RealLast();
80 TopExp_Explorer Explo(theShape, TopAbs_EDGE);
81 for (; Explo.More(); Explo.Next())
83 const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
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)
93 if (!Precision::IsInfinite(MinSize))
99 //==================================================================================================
100 void GeomAlgoAPI_UnifySameDomain::build(const GeomShapePtr& theShape,
101 const bool theIsToSimplifyShell)
103 ShapeUpgrade_UnifySameDomain* aUnifyAlgo = new ShapeUpgrade_UnifySameDomain();
104 this->setImpl(aUnifyAlgo);
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
112 TopoDS_Shape aResult = aUnifyAlgo->Shape();
113 if (aResult.IsNull()) {
117 if (theIsToSimplifyShell && aResult.ShapeType() == TopAbs_SHELL) {
119 TopoDS_Iterator anIt(aResult);
120 for (; anIt.More(); anIt.Next()) {
125 anIt.Initialize(aResult);
126 aResult = anIt.Value();
130 std::shared_ptr<GeomAPI_Shape> aResShape(new GeomAPI_Shape());
131 aResShape->setImpl(new TopoDS_Shape(aResult));
132 this->setShape(aResShape);
136 //==================================================================================================
137 void GeomAlgoAPI_UnifySameDomain::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
138 ListOfShape& theHistory)
140 if(!theShape.get()) {
144 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
145 const ShapeUpgrade_UnifySameDomain& aUnifyAlgo = this->impl<ShapeUpgrade_UnifySameDomain>();
147 for (int aIsModified = 0; aIsModified <= 1; aIsModified++) {
148 if (!aUnifyAlgo.History()->IsSupportedType(aShape)) // to avoid crash in BRepTools_History
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);