Salome HOME
added new Sewing feature
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Sewing.cpp
1 // Copyright (C) 2014-2022  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 GeomAlgoAPI_Sewing::GeomAlgoAPI_Sewing(const ListOfShape& theShapes, const bool theAllowNonManifold, const double theTolerance)
36 {
37   build(theShapes, theAllowNonManifold, theTolerance);
38 }
39
40 void GeomAlgoAPI_Sewing::build(const ListOfShape& theShapes, const bool theAllowNonManifold, const double theTolerance)
41 {
42   if(theShapes.empty()) {
43     return;
44   }
45
46   BRepBuilderAPI_Sewing* aSewingBuilder = new BRepBuilderAPI_Sewing();
47   this->setImpl(aSewingBuilder);
48
49   aSewingBuilder->SetTolerance(theTolerance);
50   aSewingBuilder->SetFaceMode(Standard_True);
51   aSewingBuilder->SetFloatingEdgesMode(Standard_False);
52   aSewingBuilder->SetNonManifoldMode(theAllowNonManifold);
53
54   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
55     const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
56     aSewingBuilder->Add(aShape);
57   }
58
59   aSewingBuilder->Perform();
60
61   TopoDS_Shape aResult = aSewingBuilder->SewedShape();
62
63   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
64   aShape->setImpl(new TopoDS_Shape(aResult));
65   this->setShape(aShape);
66   this->setDone(true);
67 }
68
69 //==================================================================================================
70 #include <GeomAPI_ShapeIterator.h>
71
72 void GeomAlgoAPI_Sewing::modified(const std::shared_ptr<GeomAPI_Shape> theShape,
73                                   ListOfShape& theHistory)
74 {
75   if(!theShape.get()) {
76     return;
77   }
78
79   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
80   const BRepBuilderAPI_Sewing& aSewingBuilder = this->impl<BRepBuilderAPI_Sewing>();
81
82   TopoDS_Shape aModifiedShape = aSewingBuilder.Modified(aShape);
83   if(aModifiedShape.IsEqual(aShape)) {
84     aModifiedShape = aSewingBuilder.ModifiedSubShape(aShape);
85   }
86
87   for(TopExp_Explorer anExp(aModifiedShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
88     GeomShapePtr aGeomShape(new GeomAPI_Shape());
89     aGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
90     theHistory.push_back(aGeomShape);
91   }
92
93   if (theShape->shapeType() < GeomAPI_Shape::FACE) {
94     ListOfShape aNewShapes;
95     // collect faces and parent shapes, if it is not done yet
96     if (!isNewShapesCollected(theShape, GeomAPI_Shape::FACE))
97       collectNewShapes(theShape, GeomAPI_Shape::FACE);
98
99     for (GeomAPI_ShapeIterator anIt(shape()); anIt.more(); anIt.next()) {
100       GeomShapePtr anOldShapesCompound =
101           oldShapesForNew(theShape, anIt.current(), GeomAPI_Shape::FACE);
102       if (!anOldShapesCompound->isNull())
103         aNewShapes.push_back(anIt.current());
104     }
105
106     if (!aNewShapes.empty())
107       theHistory = aNewShapes;
108   }
109 }