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