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