Salome HOME
Merge remote-tracking branch 'origin/cbr/export_to_geom_via_xao'
[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 void GeomAlgoAPI_UnifySameDomain::build(const ListOfShape& theShapes)
37 {
38   if(theShapes.empty()) {
39     return;
40   }
41
42   // Make compound.
43   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
44   ListOfShape aCombined, aFree;
45   GeomAlgoAPI_ShapeTools::combineShapes(
46     aCompound,
47     GeomAPI_Shape::SHELL,
48     aCombined,
49     aFree);
50
51   if(aFree.size() > 0 || aCombined.size() > 1) {
52     return;
53   }
54
55   const TopoDS_Shape& aShell = aCombined.front()->impl<TopoDS_Shape>();
56
57   ShapeUpgrade_UnifySameDomain* aUnifyAlgo = new ShapeUpgrade_UnifySameDomain();
58   this->setImpl(aUnifyAlgo);
59
60   aUnifyAlgo->Initialize(aShell);
61   aUnifyAlgo->UnifyFacesAndEdges();
62   aUnifyAlgo->Build();
63
64   TopoDS_Shape aResult = aUnifyAlgo->Shape();
65   if (aResult.IsNull()) {
66     return;
67   }
68
69   if (aResult.ShapeType() == TopAbs_SHELL) {
70     int aNb = 0;
71     TopoDS_Iterator anIt(aResult);
72     for (; anIt.More(); anIt.Next()) {
73       ++aNb;
74     }
75
76     if (aNb == 1) {
77       anIt.Initialize(aResult);
78       aResult = anIt.Value();
79     }
80   }
81
82   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
83   aShape->setImpl(new TopoDS_Shape(aResult));
84   this->setShape(aShape);
85   this->setDone(true);
86 }
87
88 //==================================================================================================
89 void GeomAlgoAPI_UnifySameDomain::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
90                                   ListOfShape& theHistory)
91 {
92   if(!theShape.get()) {
93     return;
94   }
95
96   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
97   const ShapeUpgrade_UnifySameDomain& aUnifyAlgo = this->impl<ShapeUpgrade_UnifySameDomain>();
98
99   TopoDS_Shape aModifiedShape = aUnifyAlgo.Generated(aShape);
100
101   for(TopExp_Explorer anExp(aModifiedShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
102     GeomShapePtr aGeomShape(new GeomAPI_Shape());
103     aGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
104     theHistory.push_back(aGeomShape);
105   }
106 }