Salome HOME
Merge branch 'V9_2_2_BR'
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Sewing.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_Sewing.h"
21
22 #include <BRep_Builder.hxx>
23 #include <BRepBuilderAPI_MakeShell.hxx>
24 #include <BRepBuilderAPI_Sewing.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <TopoDS_Iterator.hxx>
27 #include <TopoDS_Shape.hxx>
28
29 //==================================================================================================
30 GeomAlgoAPI_Sewing::GeomAlgoAPI_Sewing(const ListOfShape& theShapes)
31 {
32   build(theShapes);
33 }
34
35 void GeomAlgoAPI_Sewing::build(const ListOfShape& theShapes)
36 {
37   if(theShapes.empty()) {
38     return;
39   }
40
41   BRepBuilderAPI_Sewing* aSewingBuilder = new BRepBuilderAPI_Sewing();
42   this->setImpl(aSewingBuilder);
43
44   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
45     const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
46     aSewingBuilder->Add(aShape);
47   }
48
49   aSewingBuilder->Perform();
50
51   TopoDS_Shape aResult = aSewingBuilder->SewedShape();
52   BRep_Builder aBuilder;
53   if(aResult.ShapeType() == TopAbs_COMPOUND) {
54     TopoDS_Compound aResultCompound;
55     aBuilder.MakeCompound(aResultCompound);
56     for(TopoDS_Iterator anIt(aResult); anIt.More(); anIt.Next()) {
57       const TopoDS_Shape aSubShape = anIt.Value();
58       if(aSubShape.ShapeType() == TopAbs_SHELL) {
59         aBuilder.Add(aResultCompound, aSubShape);
60       } else if (aSubShape.ShapeType() == TopAbs_FACE) {
61         TopoDS_Shell aShell;
62         aBuilder.MakeShell(aShell);
63         aBuilder.Add(aShell, aSubShape);
64         aBuilder.Add(aResultCompound, aShell);
65       }
66     }
67     aResult = aResultCompound;
68   } else if(aResult.ShapeType() == TopAbs_FACE) {
69     TopoDS_Shell aShell;
70     aBuilder.MakeShell(aShell);
71     aBuilder.Add(aShell, aResult);
72     aResult = aShell;
73   }
74
75   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
76   aShape->setImpl(new TopoDS_Shape(aResult));
77   this->setShape(aShape);
78   this->setDone(true);
79 }
80
81 //==================================================================================================
82 #include <GeomAPI_ShapeExplorer.h>
83 #include <GeomAPI_ShapeIterator.h>
84
85 typedef std::map<GeomShapePtr, ListOfShape, GeomAPI_Shape::Comparator> MapFaceSolid;
86 static void facesBelongingToSolids(const GeomShapePtr& theShape,
87                                    MapFaceSolid& theShapeRelations)
88 {
89   for (GeomAPI_ShapeExplorer aSolidExp(theShape, GeomAPI_Shape::SHELL);
90        aSolidExp.more(); aSolidExp.next()) {
91     GeomShapePtr aSolid = aSolidExp.current();
92     for (GeomAPI_ShapeExplorer aFaceExp(aSolid, GeomAPI_Shape::FACE);
93          aFaceExp.more(); aFaceExp.next())
94       theShapeRelations[aFaceExp.current()].push_back(aSolid);
95   }
96 }
97
98 static bool isShapeInList(const GeomShapePtr& theShape, const ListOfShape& theList)
99 {
100   for (ListOfShape::const_iterator anIt = theList.begin(); anIt != theList.end(); ++anIt)
101     if (theShape->isEqual(*anIt))
102       return true;
103   return false;
104 }
105
106 void GeomAlgoAPI_Sewing::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
107                                   ListOfShape& theHistory)
108 {
109   static int anIndex = 0;
110   if(!theShape.get()) {
111     return;
112   }
113
114   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
115   const BRepBuilderAPI_Sewing& aSewingBuilder = this->impl<BRepBuilderAPI_Sewing>();
116
117   TopoDS_Shape aModifiedShape = aSewingBuilder.Modified(aShape);
118   if(aModifiedShape.IsEqual(aShape)) {
119     aModifiedShape = aSewingBuilder.ModifiedSubShape(aShape);
120   }
121
122   for(TopExp_Explorer anExp(aModifiedShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
123     GeomShapePtr aGeomShape(new GeomAPI_Shape());
124     aGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
125     theHistory.push_back(aGeomShape);
126   }
127
128   if (theShape->shapeType() < GeomAPI_Shape::FACE) {
129     ListOfShape aNewShapes;
130     // collect faces and parent shapes, if it is not done yet
131     if (!isNewShapesCollected(theShape, GeomAPI_Shape::FACE))
132       collectNewShapes(theShape, GeomAPI_Shape::FACE);
133
134     for (GeomAPI_ShapeIterator anIt(shape()); anIt.more(); anIt.next()) {
135       GeomShapePtr anOldShapesCompound =
136           oldShapesForNew(theShape, anIt.current(), GeomAPI_Shape::FACE);
137       if (!anOldShapesCompound->isNull())
138         aNewShapes.push_back(anIt.current());
139     }
140
141     if (!aNewShapes.empty())
142       theHistory = aNewShapes;
143   }
144 }