Salome HOME
Update copyrights
[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_ShapeTools.h>
24
25 #include <ShapeUpgrade_UnifySameDomain.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS_Shape.hxx>
28
29 //==================================================================================================
30 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const ListOfShape& theShapes)
31 {
32   build(theShapes);
33 }
34
35 //==================================================================================================
36 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const GeomShapePtr& theShape)
37 {
38   build(theShape, false);
39 }
40
41 //==================================================================================================
42 void GeomAlgoAPI_UnifySameDomain::build(const ListOfShape& theShapes)
43 {
44   if(theShapes.empty()) {
45     return;
46   }
47
48   // Make compound.
49   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
50   ListOfShape aResults;
51   GeomAlgoAPI_ShapeTools::combineShapes(
52     aCompound,
53     GeomAPI_Shape::SHELL,
54     aResults);
55
56   if(aResults.size() > 1 ||
57      (aResults.size() == 1 && aResults.front()->shapeType() > GeomAPI_Shape::SHELL)) {
58     return;
59   }
60
61   const TopoDS_Shape& aShell = aResults.front()->impl<TopoDS_Shape>();
62
63   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
64   aShape->setImpl(new TopoDS_Shape(aShell));
65   build(aShape, true);
66 }
67
68 //==================================================================================================
69 void GeomAlgoAPI_UnifySameDomain::build(const GeomShapePtr& theShape,
70                                         const bool theIsToSimplifyShell)
71 {
72   ShapeUpgrade_UnifySameDomain* aUnifyAlgo = new ShapeUpgrade_UnifySameDomain();
73   this->setImpl(aUnifyAlgo);
74
75   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
76   aUnifyAlgo->Initialize(aShape);
77   aUnifyAlgo->Build();
78
79   TopoDS_Shape aResult = aUnifyAlgo->Shape();
80   if (aResult.IsNull()) {
81     return;
82   }
83
84   if (theIsToSimplifyShell && aResult.ShapeType() == TopAbs_SHELL) {
85     int aNb = 0;
86     TopoDS_Iterator anIt(aResult);
87     for (; anIt.More(); anIt.Next()) {
88       ++aNb;
89     }
90
91     if (aNb == 1) {
92       anIt.Initialize(aResult);
93       aResult = anIt.Value();
94     }
95   }
96
97   std::shared_ptr<GeomAPI_Shape> aResShape(new GeomAPI_Shape());
98   aResShape->setImpl(new TopoDS_Shape(aResult));
99   this->setShape(aResShape);
100   this->setDone(true);
101 }
102
103 //==================================================================================================
104 void GeomAlgoAPI_UnifySameDomain::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
105                                   ListOfShape& theHistory)
106 {
107   if(!theShape.get()) {
108     return;
109   }
110
111   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
112   const ShapeUpgrade_UnifySameDomain& aUnifyAlgo = this->impl<ShapeUpgrade_UnifySameDomain>();
113
114   for (int aIsModified = 0; aIsModified <= 1; aIsModified++) {
115     const TopTools_ListOfShape& aMList = aIsModified ?
116       aUnifyAlgo.History()->Modified(aShape) : aUnifyAlgo.History()->Generated(aShape);
117     for (TopTools_ListIteratorOfListOfShape aModified(aMList); aModified.More(); aModified.Next()) {
118       for (TopExp_Explorer anExp(aModified.Value(), aShape.ShapeType());
119            anExp.More(); anExp.Next()) {
120         GeomShapePtr aGeomShape(new GeomAPI_Shape());
121         aGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
122         theHistory.push_back(aGeomShape);
123       }
124     }
125   }
126 }